forked from muan/emojilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
211 lines (176 loc) · 4.97 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* global phantom */
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()
}
}
var categories = {
people: 287,
animals_and_nature: 161,
food_and_drink: 85,
activity: 83,
travel_and_places: 118,
objects: 180,
symbols: 271,
flags: 247,
_custom: 15
}
// Calculate numbeer of emojis from categories
var numberOfEmojis = Object.keys(categories).map(function (key) { return categories[key] }).reduce(function (a, b) { return a + b })
//
console.log('\nTEST: Correct JSON format')
try {
var data = JSON.parse(fs.read('emojis.json'))
var orderd_keys = JSON.parse(fs.read('ordered.json'))
var keys = Object.keys(data)
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 !== numberOfEmojis) {
console.log('There are ' + numberOfEmojis + ' emojis, but emojis.json has ' + keys.length + ' entries.')
failed()
} else {
passed()
}
//
console.log('TEST: Ordered keys are up to date')
if (orderd_keys.length !== numberOfEmojis) {
console.log('There are ' + numberOfEmojis + ' emojis, but keys contains ' + orderd_keys.length + ' emojis.')
failed()
} else {
passed()
}
//
console.log('TEST: Correct number of emojis in each category')
var counter = []
Object.keys(categories).forEach(function (category) {
var count = keys.map(function (key) {
return data[key]['category']
}).filter(function (mojicategory) {
return mojicategory === category
}).length
if (count !== categories[category]) {
counter.push([category, count])
}
})
if (counter.length > 0) {
counter.forEach(function (result) {
console.log('There are ' + result[1] + ' emojis in \'' + result[0] + '\', but the expected number is ' + categories[result[0]])
})
failed()
} else {
passed()
}
//
console.log('TEST: No duplicated entries')
var arr = []
var dups = []
var keysFromRawData = rawData.match(/\".+\": {/g)
keysFromRawData.forEach(function (key) {
key = key.replace(/\"|\:|\s|\{/g, '')
if (arr.indexOf(key) < 0) {
arr.push(key)
} else {
dups.push(key)
}
})
var charsFromRawData = rawData.match(/"char": ".+"/g)
charsFromRawData.forEach(function (character) {
character = character.replace(/:|"|(char)|\s/g, '')
if (arr.indexOf(character) < 0) {
arr.push(character)
} else {
dups.push(character)
}
})
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 === 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 === keyword) {
unnecessitiesInKeywords.push([otherKeyword, keyword, key])
}
})
})
})
if (unnecessities.length > 0 || unnecessitiesInKeywords.length > 0) {
unnecessities.forEach(function (arr) {
console.log('"' + arr[1] + '" is unnecessary as the key is already "' + arr[0] + '".')
})
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": \\["[^\"]+"(, "[^\"]+")*\\]'
var contentRegex = 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()
}
//
console.log('TEST: Properties')
var properties = ["keywords", "char", "fitzpatrick_scale", "category"]
var emojiWithWrongKeys = []
keys.forEach(function (key) {
if (Object.keys(data[key]).join() !== properties.join()) {
emojiWithWrongKeys.push(key)
}
})
if (emojiWithWrongKeys.length > 0) {
emojiWithWrongKeys.forEach(function (emoji) {
console.log(emoji + ' has keys "' + Object.keys(data[emoji]).join(', ') + '", expect: "' + properties.join(', ') + '"')
})
failed()
} else {
passed()
}
reveal()