forked from muan/emojilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
135 lines (114 loc) · 3.12 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var fs = require('fs')
var rawData = fs.read('emojis.json').toString()
var buildFailed = false
var passed = function() { console.log("\x1B[92mPASSED\x1B[0m\n") }
var failed = function() {
console.log("\x1B[91mFAILED\x1B[0m\n")
buildFailed = true
}
var reveal = function() {
if(buildFailed) {
console.log(":cry: \x1B[91mNo good, something failed.\x1B[0m :boom:\n")
phantom.exit(buildFailed)
} else {
console.log(":sparkles: \x1B[96mWho's awesome? You're awesome!\x1B[0m :+1:\n")
phantom.exit()
}
}
//
console.log("\nTEST: Correct JSON format")
try {
var data = JSON.parse(fs.read('emojis.json'))
var keys = Object.keys(data)
keys.splice(keys.indexOf('keys'), 1)
passed()
} catch(e) {
console.log('Invalid JSON format. See the CONTRIBUTING doc for reference.')
failed()
reveal()
}
//
console.log("TEST: Correct number of emojis")
if(keys.length !== 862) {
console.log("There are 862 emojis, but emojis.json has " + keys.length + " entries.")
failed()
} else {
passed()
}
//
console.log("TEST: Ordered keys are up to date")
if(data.keys.length !== 862) {
console.log("There are 862 emojis, but keys contains " + data.keys.length + " emojis.")
failed()
} else {
passed()
}
//
console.log("TEST: No duplicated entries")
var arr = []
var dups = []
keys.forEach(function(key) {
if(arr.indexOf(key) < 0) {
arr.push(key)
} else {
dups.push(key)
}
})
if(dups.length > 0) {
dups.forEach(function(key) {
console.log('There is more than one "' + key + '" in emojis.json.')
})
failed()
} else {
passed()
}
//
console.log("TEST: No unnecessary keywords")
var unnecessities = []
var unnecessitiesInKeywords = []
keys.forEach(function(key) {
data[key]["keywords"].forEach(function(keyword) {
if(key.match(keyword)) {
unnecessities.push([key, keyword])
}
var otherKeywords = data[key]["keywords"].slice()
otherKeywords.splice(data[key]["keywords"].indexOf(keyword), 1)
otherKeywords.forEach(function(otherKeyword) {
if(otherKeyword.match(keyword)) {
unnecessitiesInKeywords.push([otherKeyword, keyword, key])
}
})
})
})
if(unnecessities.length > 0 || unnecessitiesInKeywords.length > 0) {
unnecessities.forEach(function(arr) {
console.log('"' + arr[1] + '" is unnecessary as it is already part of "' + arr[0] + '" and will be matched.')
})
unnecessitiesInKeywords.forEach(function(arr) {
console.log('"' + arr[1] + '" is unnecessary as a "' + arr[2] + '" already has a keyword "' + arr[0] + '".')
})
failed()
} else {
passed()
}
//
console.log("TEST: Line format")
var offenses = []
var lines = rawData.replace(/^{\n([\s\S]*)\n}\n$/, '$1').split("\n")
var baseRegex = '^ "keywords": \\["[\\w- ]+"(, "[\\w- ]+")*\\]'
var contentRegex = new RegExp(baseRegex + ',$')
var lastLineRegex = new RegExp(baseRegex + '$')
lines.forEach(function(line, index) {
if(line.match(/keywords/) && !line.match(contentRegex)) {
offenses.push(index + 2)
}
})
if(offenses.length > 0) {
offenses.forEach(function(lineNo) {
console.log('Line ' + lineNo + ' has the wrong format.')
})
failed()
} else {
passed()
}
reveal()