forked from wilson1yan/lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_generator_test.lua
More file actions
278 lines (238 loc) · 8.65 KB
/
language_generator_test.lua
File metadata and controls
278 lines (238 loc) · 8.65 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
--[[ Copyright (C) 2017-2019 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]
local asserts = require 'testing.asserts'
local test_runner = require 'testing.test_runner'
local texter = require 'language.texter'
local EQ = asserts.EQ
local NE = asserts.NE
local tests = {}
local constraints = require 'language.constraints'
local generator = require 'language.generator'
local make = require 'language.make'
local set = require 'common.set'
-- Given a list, return a table mapping list element -> number of occurrences.
local function listToHistogram(list)
local count = {}
for _, value in ipairs(list) do
count[value] = (count[value] or 0) + 1
end
return count
end
function tests.applyConstraints_checkNumericConstant()
local NUMERIC_VALUE = 12
local context = generator.createContext()
EQ(context:applyConstraints(NUMERIC_VALUE, {}, 'pet', 1), NUMERIC_VALUE)
end
function tests.applyConstraints_checkStringConstant()
local STRING_VALUE = 'pig'
local context = generator.createContext()
EQ(context:applyConstraints(STRING_VALUE, {}, 'pet', 1), STRING_VALUE)
end
function tests.applyConstraints_checkBoolConstantTrue()
local BOOL_VALUE = true
local context = generator.createContext()
EQ(context:applyConstraints(BOOL_VALUE, {}, 'pet', 1), BOOL_VALUE)
end
function tests.applyConstraints_checkBoolConstantFalse()
local BOOL_VALUE = false
local context = generator.createContext()
EQ(context:applyConstraints(BOOL_VALUE, {}, 'pet', 1), BOOL_VALUE)
end
function tests.applyConstraints_checkFunction()
local func = function(candidates, context, attribute, group)
return {
candidates = candidates,
context = context,
attribute = attribute,
group = group
}
end
local context = generator.createContext()
local CANDIDATES = {'Hello', 'Mum'}
local ATTRIBUTE = 'greeting'
local GROUP = 1
local EXPECTATION = {
candidates = CANDIDATES,
context = context,
attribute = ATTRIBUTE,
group = GROUP
}
local result = context:applyConstraints(func, CANDIDATES, ATTRIBUTE, GROUP)
asserts.tablesEQ(result, EXPECTATION)
end
function tests.applyConstraints_checkSequenceOfFunctions()
-- Reverses the input.
local reverse = function(candidates, context, attribute, group)
local output = {}
for i = #candidates, 1, -1 do
table.insert(output, candidates[i])
end
return output
end
-- Returns the first two elements only.
local pick2 = function(candidates, context, attribute, group)
return {candidates[1], candidates[2]}
end
local ATTRIBUTE = 'unused'
local GROUP = 1
local CANDIDATES = {'one', 'two', 'three', 'four'}
-- Sanity check the helper functions first.
asserts.tablesEQ(reverse(CANDIDATES), {'four', 'three', 'two', 'one'})
asserts.tablesEQ(pick2(CANDIDATES), {'one', 'two'})
local context = generator.createContext()
-- The output of each step should be passed to the next as candidates
asserts.tablesEQ(
context:applyConstraints({reverse, pick2}, CANDIDATES, ATTRIBUTE, GROUP),
{'four', 'three'})
asserts.tablesEQ(
context:applyConstraints({pick2, reverse}, CANDIDATES, ATTRIBUTE, GROUP),
{'two', 'one'})
end
function tests.getChoicesFor_shouldReturnPossibleAttributeValues()
local CANDIDATES = {'one', 'two', 'three', 'four'}
local context = generator.createContext{
attributes = {numbers = CANDIDATES},
}
local CHANGE_CANDIDATES = {'six', 'seven', 'eight'}
context:setChoicesFor('numbers', CHANGE_CANDIDATES)
asserts.tablesEQ(context:getChoicesFor('numbers'), CHANGE_CANDIDATES)
end
function tests.processSpec_shouldGetCandidatesFromContext()
local function identity(candidates, context, attribute, group)
return candidates
end
local CANDIDATES = {'one', 'two', 'three', 'four'}
-- Sanity check the helper function first.
asserts.tablesEQ(identity(CANDIDATES), CANDIDATES)
local context = generator.createContext()
context:setChoicesFor('numbers', CANDIDATES)
asserts.tablesEQ(context:processSpec(identity, 'numbers', 1), CANDIDATES)
local CHANGE_CANDIDATES = {'six', 'seven', 'eight'}
context:setChoicesFor('numbers', CHANGE_CANDIDATES)
asserts.tablesEQ(context:processSpec(identity, 'numbers', 1),
CHANGE_CANDIDATES)
end
function tests.processSpec_shouldUseDefaultIfNoExplicitSpec()
local DEFAULT_NUMBER = 31
local DEFAULT_STRING = 'default_numberwang'
local DEFAULT_FALSE = false
local context = generator.createContext{
defaults = {
numbers = DEFAULT_NUMBER,
strings = DEFAULT_STRING,
liars = DEFAULT_FALSE
},
}
local nilSpec = nil
EQ(context:processSpec(nilSpec, 'numbers', 1), DEFAULT_NUMBER)
EQ(context:processSpec(nilSpec, 'strings', 1), DEFAULT_STRING)
EQ(context:processSpec(nilSpec, 'liars', 1), DEFAULT_FALSE)
end
function tests.processSpecs_shouldReturnConstrainedItemsPerGroup()
local FRUIT = {'apple', 'orange'}
local FRUIT_SET = set.Set(FRUIT)
local COLOR = {'blue', 'green'}
local COLOR_SET = set.Set(COLOR)
local context = generator.createContext{
attributes = {
fruit = FRUIT,
color = COLOR,
},
}
local spec = {
{fruit = 'banana'},
{}, -- No explicit specs should default to random choice.
{color = constraints.first}
}
local itemsPerGroup = {10, 1, 7}
context:processSpecs(spec, itemsPerGroup)
EQ(context:groupCount(), #spec)
local group1 = context:getGroup(1)
EQ(#group1, itemsPerGroup[1])
for _, generatedTable in ipairs(group1) do
EQ(generatedTable.fruit, 'banana')
assert(COLOR_SET[generatedTable.color])
end
local group2 = context:getGroup(2)
EQ(#group2, itemsPerGroup[2])
for _, generatedTable in ipairs(group2) do
assert(FRUIT_SET[generatedTable.fruit])
assert(COLOR_SET[generatedTable.color])
end
local group3 = context:getGroup(3)
EQ(#group3, itemsPerGroup[3])
for _, generatedTable in ipairs(group3) do
assert(FRUIT_SET[generatedTable.fruit])
EQ(generatedTable.color, COLOR[1])
end
end
function tests.processSpecs_shouldOnlyPutAttributesMarkedProcessInOutput()
local function tableSize(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local TO_PROCESS = {'fruit'}
local context = generator.createContext{
attributes = {
fruit = {'apple', 'orange'},
color = {'blue', 'green'},
},
process = TO_PROCESS,
}
local spec = {
{fruit = 'banana'},
{ignored = 'true'},
{color = constraints.first}
}
local itemsPerGroup = {10, 1, 7}
context:processSpecs(spec, itemsPerGroup)
EQ(context:groupCount(), #spec)
for _, generatedTable in ipairs(context:getFlattenedOutput()) do
-- Generated tables should contain nothing but fruit.
NE(generatedTable.fruit, nil)
EQ(tableSize(generatedTable), #TO_PROCESS)
end
end
function tests.vocabulary_shouldReturnDeDuplicatedAttributeValues()
local context = generator.createContext{
attributes = {fruit = {'apple', 'orange'},
color = {'orange', 'red'}},
process = {'fruit', 'color'},
}
local vocab = context:vocabulary()
local EXPECTATION = listToHistogram({'apple', 'orange', 'red'})
asserts.tablesEQ(listToHistogram(vocab), EXPECTATION)
end
function tests.vocabulary_shouldIncludeUserDictEntriesOnceOnly()
local context = generator.createContext{
attributes = {fruit = {'apple', 'orange'},
color = {'orange', 'red'}},
process = {'fruit', 'color'},
}
local vocab = context:vocabulary{userDict = {'red', 'fox'}}
local EXPECTATION = listToHistogram({'apple', 'orange', 'red', 'fox'})
asserts.tablesEQ(listToHistogram(vocab), EXPECTATION)
end
function tests.vocabulary_shouldRestrictOutputIfExplicitAttributesGiven()
local context = generator.createContext{
attributes = {fruit = {'apple', 'orange'},
color = {'orange', 'red'}},
process = {'fruit', 'color'},
}
local vocab = context:vocabulary{attributes = {'fruit'}}
local EXPECTATION = listToHistogram({'apple', 'orange'})
asserts.tablesEQ(listToHistogram(vocab), EXPECTATION)
end
return test_runner.run(tests)