@@ -216,6 +216,10 @@ kama (10 > 1) {
216
216
`"Habari" - "Habari"` ,
217
217
"Operesheni Haielweki: NENO - NENO" ,
218
218
},
219
+ {
220
+ `{"jina": "Avi"}[fn(x) {x}];` ,
221
+ "Samahani, FUNCTION haitumiki kama key" ,
222
+ },
219
223
}
220
224
221
225
for _ , tt := range tests {
@@ -337,11 +341,11 @@ func TestBuiltinFunctions(t *testing.T) {
337
341
input string
338
342
expected interface {}
339
343
}{
340
- {`len ("")` , 0 },
341
- {`len ("four")` , 4 },
342
- {`len ("hello world")` , 11 },
343
- {`len (1)` , "Samahani, hii function haitumiki na NAMBARI" },
344
- {`len ("one", "two")` , "Hoja hazilingani, tunahitaji=1, tumepewa=2" },
344
+ {`idadi ("")` , 0 },
345
+ {`idadi ("four")` , 4 },
346
+ {`idadi ("hello world")` , 11 },
347
+ {`idadi (1)` , "Samahani, hii function haitumiki na NAMBARI" },
348
+ {`idadi ("one", "two")` , "Hoja hazilingani, tunahitaji=1, tumepewa=2" },
345
349
}
346
350
347
351
for _ , tt := range tests {
@@ -426,3 +430,89 @@ func TestArrayIndexExpressions(t *testing.T) {
426
430
}
427
431
}
428
432
}
433
+
434
+ func TestDictLiterals (t * testing.T ) {
435
+ input := `acha two = "two";
436
+ {
437
+ "one": 10 - 9,
438
+ two: 1 +1,
439
+ "thr" + "ee": 6 / 2,
440
+ 4: 4,
441
+ kweli: 5,
442
+ sikweli: 6
443
+ }`
444
+
445
+ evaluated := testEval (input )
446
+ result , ok := evaluated .(* object.Dict )
447
+ if ! ok {
448
+ t .Fatalf ("Eval didn't return a dict, got=%T(%+v)" , evaluated , evaluated )
449
+ }
450
+
451
+ expected := map [object.HashKey ]int64 {
452
+ (& object.String {Value : "one" }).HashKey (): 1 ,
453
+ (& object.String {Value : "two" }).HashKey (): 2 ,
454
+ (& object.String {Value : "three" }).HashKey (): 3 ,
455
+ (& object.Integer {Value : 4 }).HashKey (): 4 ,
456
+ TRUE .HashKey (): 5 ,
457
+ FALSE .HashKey (): 6 ,
458
+ }
459
+
460
+ if len (result .Pairs ) != len (expected ) {
461
+ t .Fatalf ("Dict has wrong number of pairs, got=%d" , len (result .Pairs ))
462
+ }
463
+
464
+ for expectedKey , expectedValue := range expected {
465
+ pair , ok := result .Pairs [expectedKey ]
466
+ if ! ok {
467
+ t .Errorf ("No pair for give key" )
468
+ }
469
+
470
+ testIntegerObject (t , pair .Value , expectedValue )
471
+ }
472
+ }
473
+
474
+ func TestDictIndexExpression (t * testing.T ) {
475
+ tests := []struct {
476
+ input string
477
+ expected interface {}
478
+ }{
479
+ {
480
+ `{"foo": 5}["foo"]` ,
481
+ 5 ,
482
+ },
483
+ {
484
+ `{"foo": 5}["bar"]` ,
485
+ nil ,
486
+ },
487
+ {
488
+ `acha key = "foo"; {"foo": 5}[key]` ,
489
+ 5 ,
490
+ },
491
+ {
492
+ `{}["foo"]` ,
493
+ nil ,
494
+ },
495
+ {
496
+ `{5: 5}[5]` ,
497
+ 5 ,
498
+ },
499
+ {
500
+ `{kweli: 5}[kweli]` ,
501
+ 5 ,
502
+ },
503
+ {
504
+ `{sikweli: 5}[sikweli]` ,
505
+ 5 ,
506
+ },
507
+ }
508
+
509
+ for _ , tt := range tests {
510
+ evaluated := testEval (tt .input )
511
+ integer , ok := tt .expected .(int )
512
+ if ok {
513
+ testIntegerObject (t , evaluated , int64 (integer ))
514
+ } else {
515
+ testNullObject (t , evaluated )
516
+ }
517
+ }
518
+ }
0 commit comments