@@ -109,6 +109,137 @@ local function createTestLib(blocks: { TestBlock })
109
109
return true
110
110
end
111
111
112
+ local randomTuple = require ("./test-lib/randomTuple" )
113
+ local possibleTypes = { "number" , "string" , "boolean" }
114
+ local alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
115
+
116
+ local function createRandomTuple (types : { string }): { any }
117
+ local t : { any } = {}
118
+ for i , type in types do
119
+ if type == "number" then
120
+ t [i ] = math.random (0 , 999 )
121
+ elseif type == "string" then
122
+ local s = ""
123
+ for _ = 1 , math.random (5 , 10 ) do
124
+ local randomChar = math.random (# alphabet )
125
+ s ..= alphabet :sub (randomChar , randomChar )
126
+ end
127
+ t [i ] = s
128
+ elseif type == "boolean" then
129
+ t [i ] = math.random () > 0.5
130
+ else
131
+ error (`Unknown type: {type }` , 2 )
132
+ end
133
+ end
134
+ return t
135
+ end
136
+
137
+ local function createAssertEqTuple (name : string , tuple : { any }): (...any ) -> ()
138
+ local s = ""
139
+
140
+ s ..= "local "
141
+ for i = 1 , # tuple do
142
+ s ..= `v{i }`
143
+ if i < # tuple then
144
+ s ..= ", "
145
+ end
146
+ end
147
+ s ..= " = ...\n "
148
+
149
+ s ..= "if\n "
150
+ s ..= ` select("#", ...) ~= {# tuple } or\n `
151
+
152
+ local tupleStr = ""
153
+ for i , v in tuple do
154
+ local valueStr = if typeof (v ) == "string" then `"{v }"` else `{v }`
155
+ s ..= ` v{i } ~= {valueStr }`
156
+ tupleStr ..= valueStr
157
+
158
+ if i < # tuple then
159
+ s ..= " or\n "
160
+ tupleStr ..= "\t "
161
+ end
162
+ end
163
+ s ..= "\n then\n "
164
+ s ..= ' local s = ""\n '
165
+ s ..= ' for i = 1, select("#", ...) do'
166
+ s ..= " local v = select(i, ...)\n "
167
+ s ..= ' s ..= if typeof(v) == "string" then `"{v}"` else `{v}`\n '
168
+ s ..= ' if i < select("#", ...) then\n '
169
+ s ..= ' s ..= "\\ t"\n '
170
+ s ..= " end\n "
171
+ s ..= " end\n "
172
+ s ..= ` error(\` Equality Failed\\ n Expected: \\ t{tupleStr }\\ n Received: \\ t\{ s}\` , 2)\n `
173
+ s ..= "end"
174
+
175
+ return luau .load (s , { debugName = name })
176
+ end
177
+
178
+ local function createMapTuple (name : string , types : { string }): (...any ) -> ...any
179
+ local s = ""
180
+
181
+ s ..= "local "
182
+ for i = 1 , # types do
183
+ s ..= `v{i }`
184
+ if i < # types then
185
+ s ..= ", "
186
+ end
187
+ end
188
+ s ..= " = ...\n "
189
+
190
+ s ..= "return "
191
+ for i , type in types do
192
+ if type == "string" then
193
+ if math.random () > 0.5 then
194
+ s ..= `v{i }:upper()`
195
+ else
196
+ s ..= `v{i }:reverse()`
197
+ end
198
+ elseif type == "number" then
199
+ if math.random () > 0.5 then
200
+ s ..= `v{i } + {math.random (5 )}`
201
+ else
202
+ s ..= `v{i } - {math.random (5 )}`
203
+ end
204
+ elseif type == "boolean" then
205
+ s ..= `not v{i }`
206
+ else
207
+ error (`Unknown type: {type }` , 2 )
208
+ end
209
+
210
+ if i < # types then
211
+ s ..= ", "
212
+ end
213
+ end
214
+
215
+ return luau .load (s , { debugName = name })
216
+ end
217
+
218
+ function testLib .randomTuple (f : (randomTuple .TupleFunctions , ...any ) -> ())
219
+ local types = {}
220
+ for i = 1 , math.random (10 ) do
221
+ types [i ] = possibleTypes [math.random (# possibleTypes )]
222
+ end
223
+
224
+ local originalTuple = createRandomTuple (types )
225
+ local otherTuple = createRandomTuple (types )
226
+
227
+ local map = createMapTuple ("map" , types )
228
+
229
+ local tupleFunctions = {
230
+ map = map ,
231
+ other = function ()
232
+ return unpack (otherTuple )
233
+ end ,
234
+ assertEq = createAssertEqTuple ("assertEq" , originalTuple ),
235
+ assertEqMapped = createAssertEqTuple ("assertEqMapped" , { map (unpack (originalTuple )) }),
236
+ assertEqOther = createAssertEqTuple ("assertEqOther" , otherTuple ),
237
+ assertEqOtherMapped = createAssertEqTuple ("assertEqOtherMapped" , { map (unpack (otherTuple )) }),
238
+ }
239
+
240
+ f (tupleFunctions , unpack (originalTuple ))
241
+ end
242
+
112
243
return testLib
113
244
end
114
245
0 commit comments