-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestRenderScript.lua
executable file
·330 lines (260 loc) · 10.4 KB
/
TestRenderScript.lua
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env lua
--- Tests on dumocks.renderScript.
-- @see dumocks.renderScript
-- set search path to include src directory
package.path = "src/?.lua;" .. package.path
-- set file locations
local INPUT_DIR = "test/renderScript/"
local OUTPUT_FILE = "test/results/TestRenderScript.html"
local lu = require("luaunit")
local sr = require("dumocks.RenderScript")
local msu = require("dumocks.ScreenUnit")
local SVG_WRAPPER_TEMPLATE = [[<li><p>%s<br>%s</p></li>]]
_G.TestRenderScript = {
allSvg = {
[[
<!DOCTYPE html>
<html>
<head>
<style>
ul.gallery {
list-style-type: none;
padding: 0;
margin: 5px;
display: grid;
grid-gap: 20px 5px;
grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
grid-template-rows: repeat(300px);
}
ul.gallery svg {
width: 100%;
height: 100%;
max-height: 450px;
}
</style>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fira+Mono&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Play&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Condensed&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap" rel="stylesheet">
</head>
<body>
<ul class="gallery">
]]
}
}
function _G.TestRenderScript:tearDown()
local closingTags = [[
</ul>
</body>
</html>
]]
-- save as file
local outputHandle, errorMsg = io.open(OUTPUT_FILE, "w")
if errorMsg then
error(errorMsg)
else
io.output(outputHandle):write(table.concat(self.allSvg, "\n"))
io.output(outputHandle):write(closingTags)
outputHandle:close()
end
end
function _G.TestRenderScript.testParameterValidation()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local oldEnv = _ENV
local _ENV = closure
---------------
-- copy from here to renderer
---------------
local layer = createLayer()
local font = loadFont("FiraMono", 50)
local success, result
success, result = pcall(getAvailableFontName, 0)
assert(not success)
assert(string.find(result, "out%-of%-bounds font index"), "Unexpected error message- " .. result)
success, result = pcall(loadFont, "invalid", 5)
assert(not success)
assert(string.find(result, "unknown font <invalid>"), "Unexpected error message- " .. result)
success, result = pcall(addText)
assert(not success)
assert(string.find(result, "expected number for parameter 5"), "Unexpected error message- " .. result)
success, result = pcall(addText, nil, nil, nil, nil, 2.3)
assert(not success)
assert(string.find(result, "expected number for parameter 4"), "Unexpected error message- " .. result)
success, result = pcall(addText, nil, nil, nil, 1.2, 2.3)
assert(not success)
assert(string.find(result, "expected string for parameter 3"), "Unexpected error message- " .. result)
success, result = pcall(addText, nil, nil, "text", 1.2, 2.3)
assert(not success)
assert(string.find(result, "expected integer for parameter 2"), "Unexpected error message- " .. result)
success, result = pcall(addText, nil, 2, "text", 1.2, 2.3)
assert(not success)
assert(string.find(result, "expected integer for parameter 1"), "Unexpected error message- " .. result)
success, result = pcall(addText, 5, 6, "text", 1.2, 2.3)
assert(not success)
assert(string.find(result, "invalid layer handle"), "Unexpected error message- " .. result)
success, result = pcall(addText, layer, 6, "text", 1.2, 2.3)
assert(not success)
assert(string.find(result, "invalid font handle"), "Unexpected error message- " .. result)
success, result = pcall(addText, layer, font, "Success", 50, 50)
assert(success)
-- auto-box number to string
success, result = pcall(addText, layer, font, 1, 50, 50)
assert(success)
---------------
-- copy to here to renderer
---------------
_ENV = oldEnv
end
function _G.TestRenderScript.testGetLocale()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local expected, actual
-- English
expected = "en-US"
screenRenderer.locale = expected
actual = closure.getLocale()
lu.assertEquals(actual, expected)
-- French
expected = "fr-FR"
screenRenderer.locale = expected
actual = closure.getLocale()
lu.assertEquals(actual, expected)
-- German
expected = "de-DE"
screenRenderer.locale = expected
actual = closure.getLocale()
lu.assertEquals(actual, expected)
end
function _G.TestRenderScript.testGetSetFontSize()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local expected, actual
-- set by loading font
expected = 10
local font = closure.loadFont(closure.getAvailableFontName(1), expected)
actual = closure.getFontSize(font)
lu.assertEquals(actual, expected)
-- set by calling set
expected = 20
closure.setFontSize(font, expected)
actual = closure.getFontSize(font)
lu.assertEquals(actual, expected)
end
function _G.TestRenderScript.testGetInput()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local expected, actual
-- default: empty string
expected = ""
actual = closure.getInput()
lu.assertEquals(actual, expected)
-- non-default
expected = "test"
screenRenderer.input = expected
actual = closure.getInput()
lu.assertEquals(actual, expected)
end
function _G.TestRenderScript.testSetOutput()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local expected = "test"
closure.setOutput(expected)
lu.assertEquals(screenRenderer.output, expected)
end
function _G.TestRenderScript.testRequire()
local screenRenderer = sr:new()
local environment = screenRenderer:mockGetEnvironment()
-- working case - loads module on path
local mlu = environment.require("dumocks.LightUnit")
lu.assertNotNil(mlu)
lu.assertEquals(type(mlu), "table")
local mock = mlu:new(nil, 1, "long light m")
lu.assertEquals(mock:mockGetClosure().getClass(), "LightUnit")
-- non-working case - target module not on path
lu.assertErrorMsgContains("no file", environment.require, "missing")
-- temporarily add to path for bad module that fails to load
local oldPath = package.path
package.path = "test/?.lua;" .. package.path
lu.assertErrorMsgContains("unexpected symbol near", environment.require, "renderScript.badRequire")
package.path = oldPath
end
function _G.TestRenderScript:testShapes()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local script = assert(loadfile(INPUT_DIR .. "shapes.lua", "t", closure))
script()
self.allSvg[#self.allSvg + 1] = string.format(SVG_WRAPPER_TEMPLATE, "Shapes", screenRenderer:mockGenerateSvg())
end
function _G.TestRenderScript:testStrokeWidthTextAlign()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local script = assert(loadfile(INPUT_DIR .. "strokeAlign.lua", "t", closure))
script()
self.allSvg[#self.allSvg + 1] = string.format(SVG_WRAPPER_TEMPLATE, "Stroke Width/Text Align", screenRenderer:mockGenerateSvg())
end
function _G.TestRenderScript:testFontSampler()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local script = assert(loadfile(INPUT_DIR .. "fontSampler.lua", "t", closure))
-- script advances screens on mouse click, cursorDown will advance every repaint
screenRenderer.cursorDown = true
local page = 1
local index, previousIndex
index = 0
repeat
previousIndex = index
screenRenderer:mockReset()
script()
self.allSvg[#self.allSvg + 1] = string.format(SVG_WRAPPER_TEMPLATE, "Font Sampler " .. page, screenRenderer:mockGenerateSvg())
page = page + 1
index = closure.persistent.index
until index < previousIndex
end
function _G.TestRenderScript:testLayerOperations()
local screenRenderer = sr:new()
local closure = screenRenderer:mockGetEnvironment()
local script = assert(loadfile(INPUT_DIR .. "layerOperations.lua", "t", closure))
script()
self.allSvg[#self.allSvg + 1] = string.format(SVG_WRAPPER_TEMPLATE, "Layer Operations", screenRenderer:mockGenerateSvg())
end
function _G.TestRenderScript:testResolutions()
local screens = {"screen m", "sign xs", "vertical sign xs", "sign s", "sign l", "vertical sign l"}
local mockScreen, renderScript, environment, script
for _, name in pairs(screens) do
mockScreen = msu:new(nil, 1, name)
renderScript = sr:new(nil, mockScreen.resolutionX, mockScreen.resolutionY)
renderScript.input = name
environment = renderScript:mockGetEnvironment()
script = assert(loadfile(INPUT_DIR .. "resolutionCheck.lua", "t", environment))
script()
self.allSvg[#self.allSvg + 1] = string.format(SVG_WRAPPER_TEMPLATE, "Resolution: " .. name, renderScript:mockGenerateSvg())
end
end
-- TODO: Tests for render cost characterization
--- Characterization test to determine in-game behavior, can run on mock and uses assert instead of luaunit to run
-- in-game.
--
-- Test setup:
-- 1. 1x Screen or Sign, paste relevent bit directly into render script
--
-- Exercises: see verifyEnvironment.lua
function _G.TestRenderScript:testGameBehavior()
local renderScript = sr:new()
renderScript.fontStrings = {
Play = {
["."] = {10.78125 / 30, 10.3125 / 30},
["%"] = {1, 27.1875 / 30},
["%%%"] = {81.328125 / 30, 27.1875 / 30},
}
}
local closure = renderScript:mockGetEnvironment()
local script = assert(loadfile(INPUT_DIR .. "verifyEnvironment.lua", "t", closure))
script()
self.allSvg[#self.allSvg + 1] = string.format(SVG_WRAPPER_TEMPLATE, "Verify Environment", renderScript:mockGenerateSvg())
lu.assertEquals(renderScript.output, "")
end
os.exit(lu.LuaUnit.run())