@@ -21,6 +21,9 @@ describe('#constructor', () => {
21
21
} ) ;
22
22
23
23
describe ( '#lexer' , ( ) => {
24
+ it ( 'throws on an unknown function name.' , ( ) => {
25
+ expect ( ( ) => new CalcKu ( 'BOGUS(1,2)' ) . lexer ( ) ) . toThrow ( / u n k n o w n / i) ;
26
+ } ) ;
24
27
it ( 'extracts grouping tokens' , ( ) => {
25
28
let results = new CalcKu ( '((10 + 10) / 4)' ) . lexer ( ) ;
26
29
expect ( Array . isArray ( results ) ) . toBe ( true ) ;
@@ -369,47 +372,55 @@ describe('#value', () => {
369
372
}
370
373
} ;
371
374
let tests = [
372
- [ '10 + 5 - 1' , 14 ] ,
373
- //grouping
374
- [ '(10 + (5 * 2))' , 20 ] ,
375
- //numeric/boolean mix
376
- [ 'true + 3' , 4 ] ,
377
- [ 'false + 3' , 3 ] ,
378
- //order of operations
379
- [ '10 + 5 - 12 / 3 * 2' , 7 ] ,
380
- [ '(15 - 2 * 4) + (1 + 1 / 4)' , 8.25 ] ,
381
- [ '10 + 5 - 12 / 3 * 2 + true' , 8 ] ,
382
- [ 'false + (15 - 2 * 4) + (1 + 1 / 4) - true' , 7.25 ] ,
383
- //functions
384
- [ 'HELLOWORLD()' , 'Hello world.' ] ,
385
- [ 'SUM(1, 2, 3)' , 6 ] ,
386
- [ '6 / SUM(1, 2, 3) + 3' , 4 ] ,
387
- //property references
388
- [ '{num} + 3' , 334458 ] ,
389
- [ '{detail.more} + -4 / {detail.less}' , 8 ] ,
390
- [ '{detail.others:0}' , 1 ] ,
391
- //comments
392
- [ `(15 - 2 * 4)
393
- //test comment
394
- //and again
395
- + (1 + 1 / 4)` , 8.25 ] ,
396
- //consolidate
397
- [ '"hi" & " there x" & 3 & true' , 'hi there x3true' ] ,
398
- //logical
399
- [ '1 and true' , true ] ,
400
- [ '0 and true' , false ] ,
401
- [ '1 and false' , false ] ,
402
- [ '0 and false' , false ] ,
403
- [ '1 or true' , true ] ,
404
- [ '0 or true' , true ] ,
405
- [ '1 or false' , true ] ,
406
- [ '0 or false' , false ] ,
407
- [ 'false AND true OR (true AND false)' , false ]
375
+ // ['10 + 5 - 1', 14],
376
+ // //grouping
377
+ // ['(10 + (5 * 2))', 20],
378
+ // //numeric/boolean mix
379
+ // ['true + 3', 4],
380
+ // ['false + 3', 3],
381
+ // //order of operations
382
+ // ['10 + 5 - 12 / 3 * 2', 7],
383
+ // ['(15 - 2 * 4) + (1 + 1 / 4)', 8.25],
384
+ // ['10 + 5 - 12 / 3 * 2 + true', 8],
385
+ // ['false + (15 - 2 * 4) + (1 + 1 / 4) - true', 7.25],
386
+ // //functions
387
+ // ['HELLOWORLD()', 'Hello world.'],
388
+ // ['SUM(1, 2, 3)', 6],
389
+ // ['6 / SUM(1, 2, 3) + 3', 4],
390
+ [ 'SUM(SUM(1, 3), 4, 8, 5)' , 21 ] ,
391
+ // //property references
392
+ // ['{num} + 3', 334458],
393
+ // ['{detail.more} + -4 / {detail.less}', 8],
394
+ // ['{detail.others:0}', 1],
395
+ // //comments
396
+ // [`(15 - 2 * 4)
397
+ // //test comment
398
+ // //and again
399
+ // + (1 + 1 / 4)`, 8.25],
400
+ // //consolidate
401
+ // ['"hi" & " there x" & 3 & true', 'hi there x3true'],
402
+ // //logical
403
+ // ['1 and true', true],
404
+ // ['0 and true', false],
405
+ // ['1 and false', false],
406
+ // ['0 and false', false],
407
+ // ['1 or true', true],
408
+ // ['0 or true', true],
409
+ // ['1 or false', true],
410
+ // ['0 or false', false],
411
+ // ['false AND true OR (true AND false)', false]
408
412
] ;
409
413
for ( let t of tests ) {
410
- it ( `expression "${ t [ 0 ] } " should evaluate to ${ typeof t [ 1 ] === 'string' ? `"${ t [ 1 ] } "` : t [ 1 ] } on sample.` , ( ) => {
414
+ it . only ( `expression "${ t [ 0 ] } " should evaluate to ${ typeof t [ 1 ] === 'string' ? `"${ t [ 1 ] } "` : t [ 1 ] } on sample.` , ( ) => {
411
415
// console.log(JSON.stringify(new CalcKu(t[0]).lexer(), null, 4));
412
416
expect ( new CalcKu ( t [ 0 ] ) . value ( sample ) ) . toBe ( t [ 1 ] ) ;
413
417
} ) ;
414
418
}
419
+ it ( 'handles recursively referenced objects.' , ( ) => {
420
+ let sample = {
421
+ abc : 123
422
+ } ;
423
+ sample . zed = sample ;
424
+ expect ( new CalcKu ( '{zed.zed.zed.zed.zed.zed.zed.abc} * 2' ) . value ( sample ) ) . toBe ( 246 ) ;
425
+ } ) ;
415
426
} ) ;
0 commit comments