@@ -234,6 +234,172 @@ extension BuiltinTests.AggregatesTests {
234234 ) ,
235235 ]
236236
237+ static let sumTests : [ BuiltinTests . TestCase ] = [
238+ BuiltinTests . TestCase (
239+ description: " empty array " ,
240+ name: " sum " ,
241+ args: [ [ ] ] ,
242+ expected: . success( 0 )
243+ ) ,
244+ BuiltinTests . TestCase (
245+ description: " array " ,
246+ name: " sum " ,
247+ args: [ [ 1 , 2 , 3.14 , 4 ] ] ,
248+ expected: . success( . number( NSDecimalNumber ( decimal: 1 + 2 + 3.14 + 4 ) ) )
249+ ) ,
250+ BuiltinTests . TestCase (
251+ description: " array of various objects " ,
252+ name: " sum " ,
253+ args: [ [ 1 , " a " , " b " ] ] ,
254+ expected: . failure(
255+ BuiltinError . argumentTypeMismatch (
256+ arg: " collection " , got: " array[any<number, string>] " , want: " any<array[number], set[number]> " ) )
257+ ) ,
258+ BuiltinTests . TestCase (
259+ description: " empty set " ,
260+ name: " sum " ,
261+ args: [ . set( [ ] ) ] ,
262+ expected: . success( 0 )
263+ ) ,
264+ BuiltinTests . TestCase (
265+ description: " set " ,
266+ name: " sum " ,
267+ args: [ . set( [ 1 , 5 , 8.65 ] ) ] ,
268+ expected: . success( . number( NSDecimalNumber ( decimal: 1 + 5 + 8.65 ) ) )
269+ ) ,
270+ BuiltinTests . TestCase (
271+ description: " set of various objects " ,
272+ name: " sum " ,
273+ args: [ . set( [ 1 , " a " , " b " ] ) ] ,
274+ expected: . failure(
275+ BuiltinError . argumentTypeMismatch (
276+ arg: " collection " , got: " set[any<number, string>] " , want: " any<array[number], set[number]> " ) )
277+ ) ,
278+ ]
279+
280+ static let productTests : [ BuiltinTests . TestCase ] = [
281+ BuiltinTests . TestCase (
282+ description: " empty array " ,
283+ name: " product " ,
284+ args: [ [ ] ] ,
285+ expected: . success( 1 )
286+ ) ,
287+ BuiltinTests . TestCase (
288+ description: " array " ,
289+ name: " product " ,
290+ args: [ [ 1 , 2 , 3.14 , 4 ] ] ,
291+ expected: . success( . number( NSDecimalNumber ( decimal: 1 * 2 * 3.14 * 4 ) ) )
292+ ) ,
293+ BuiltinTests . TestCase (
294+ description: " array of various objects " ,
295+ name: " product " ,
296+ args: [ [ 1 , " a " ] ] ,
297+ expected: . failure(
298+ BuiltinError . argumentTypeMismatch (
299+ arg: " collection " , got: " array<number, string> " , want: " any<array[number], set[number]> " ) )
300+ ) ,
301+ BuiltinTests . TestCase (
302+ description: " empty set " ,
303+ name: " product " ,
304+ args: [ . set( [ ] ) ] ,
305+ expected: . success( 1 )
306+ ) ,
307+ BuiltinTests . TestCase (
308+ description: " set " ,
309+ name: " product " ,
310+ args: [ . set( [ 1 , 5 , 8.65 ] ) ] ,
311+ expected: . success( . number( NSDecimalNumber ( decimal: 1 * 5 * 8.65 ) ) )
312+ ) ,
313+ BuiltinTests . TestCase (
314+ description: " set of various objects " ,
315+ name: " product " ,
316+ args: [ . set( [ 1 , " a " ] ) ] ,
317+ expected: . failure(
318+ BuiltinError . argumentTypeMismatch (
319+ arg: " collection " , got: " set<number, string> " , want: " any<array[number], set[number]> " ) )
320+ ) ,
321+ ]
322+
323+ static let sortTests : [ BuiltinTests . TestCase ] = [
324+ BuiltinTests . TestCase (
325+ description: " array " ,
326+ name: " sort " ,
327+ args: [ [ 1 , 100 , 2 ] ] ,
328+ expected: . success( [ 1 , 2 , 100 ] )
329+ ) ,
330+ BuiltinTests . TestCase (
331+ description: " string array " ,
332+ name: " sort " ,
333+ args: [ [ " b " , " a " ] ] ,
334+ expected: . success( [ " a " , " b " ] )
335+ ) ,
336+ BuiltinTests . TestCase (
337+ description: " empty array " ,
338+ name: " sort " ,
339+ args: [ [ ] ] ,
340+ expected: . success( [ ] )
341+ ) ,
342+ BuiltinTests . TestCase (
343+ description: " array of objects " ,
344+ name: " sort " ,
345+ args: [
346+ [ [ " a " : 1 ] , [ " a " : 100 ] , [ " a " : 3 ] ]
347+ ] ,
348+ expected: . success( [ [ " a " : 1 ] , [ " a " : 3 ] , [ " a " : 100 ] ] )
349+ ) ,
350+ BuiltinTests . TestCase (
351+ description: " array of objects with different keys " ,
352+ name: " sort " ,
353+ args: [
354+ [ [ " a " : 100 ] , [ " c " : 3 , " d " : 4 ] , [ " b " : 101 ] ]
355+ ] ,
356+ expected: . success( [ [ " a " : 100 ] , [ " b " : 101 ] , [ " c " : 3 , " d " : 4 ] ] )
357+ ) ,
358+ BuiltinTests . TestCase (
359+ description: " set " ,
360+ name: " sort " ,
361+ args: [ . set( [ 1 , 100 , 2 ] ) ] ,
362+ expected: . success( [ 1 , 2 , 100 ] )
363+ ) ,
364+ BuiltinTests . TestCase (
365+ description: " string set " ,
366+ name: " sort " ,
367+ args: [ . set( [ " b " , " a " ] ) ] ,
368+ expected: . success( [ " a " , " b " ] )
369+ ) ,
370+ BuiltinTests . TestCase (
371+ description: " empty set " ,
372+ name: " sort " ,
373+ args: [ . set( [ ] ) ] ,
374+ expected: . success( [ ] )
375+ ) ,
376+ BuiltinTests . TestCase (
377+ description: " set of objects " ,
378+ name: " sort " ,
379+ args: [
380+ . set( [ [ " a " : 1 ] , [ " a " : 100 ] , [ " a " : 3 ] ] )
381+ ] ,
382+ expected: . success( [ [ " a " : 1 ] , [ " a " : 3 ] , [ " a " : 100 ] ] )
383+ ) ,
384+ BuiltinTests . TestCase (
385+ description: " set of objects with different keys " ,
386+ name: " sort " ,
387+ args: [
388+ . set( [ [ " a " : 100 ] , [ " c " : 3 , " d " : 4 ] , [ " b " : 101 ] ] )
389+ ] ,
390+ // 2nd element has largest key
391+ expected: . success( [ [ " a " : 100 ] , [ " b " : 101 ] , [ " c " : 3 , " d " : 4 ] ] )
392+ ) ,
393+ BuiltinTests . TestCase (
394+ description: " array of different types " ,
395+ name: " sort " ,
396+ args: [
397+ [ [ 1 , 100 , 0 ] , . object( [ " z " : 999 ] ) , . set( [ 0 ] ) , [ 999 ] , " 10000 " ]
398+ ] ,
399+ expected: . success( [ " 10000 " , [ 1 , 100 , 0 ] , [ 999 ] , . object( [ " z " : 999 ] ) , . set( [ 0 ] ) ] )
400+ ) ,
401+ ]
402+
237403 static var allTests : [ BuiltinTests . TestCase ] {
238404 [
239405 BuiltinTests . generateFailureTests (
@@ -256,6 +422,28 @@ extension BuiltinTests.AggregatesTests {
256422 allowedArgTypes: [ " array " , " set " ] ,
257423 generateNumberOfArgsTest: true ) ,
258424 minTests,
425+
426+ BuiltinTests . generateFailureTests (
427+ builtinName: " sum " , sampleArgs: [ [ ] ] ,
428+ argIndex: 0 , argName: " collection " ,
429+ allowedArgTypes: [ " array " , " set " ] ,
430+ generateNumberOfArgsTest: true ) ,
431+ sumTests,
432+
433+ BuiltinTests . generateFailureTests (
434+ builtinName: " product " , sampleArgs: [ [ ] ] ,
435+ argIndex: 0 , argName: " collection " ,
436+ allowedArgTypes: [ " array " , " set " ] ,
437+ generateNumberOfArgsTest: true ) ,
438+ productTests,
439+
440+ BuiltinTests . generateFailureTests (
441+ builtinName: " sort " , sampleArgs: [ [ ] ] ,
442+ argIndex: 0 , argName: " collection " ,
443+ allowedArgTypes: [ " array " , " set " ] ,
444+ generateNumberOfArgsTest: true ) ,
445+ sortTests,
446+
259447 ] . flatMap { $0 }
260448 }
261449
0 commit comments