// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). // All rights reserved. // t_expr_test.go package expr import ( "testing" ) func TestExpr(t *testing.T) { section := "Expr" inputs := []inputType{ /* 1 */ {`0?{}`, nil, nil}, /* 2 */ {`fact=func(n){(n)?{1}::{n*fact(n-1)}}; fact(5)`, int64(120), nil}, /* 3 */ {`builtin "os.file"; f=fileOpen("test-file.txt"); line=fileReadText(f); fileClose(f); line`, "uno", nil}, /* 4 */ {`mynot=func(v){int(v)?{true}::{false}}; mynot(0)`, true, nil}, /* 5 */ {`1 ? {1} : [1+0] {3*(1+1)}`, int64(6), nil}, /* 6 */ {`a=3; a+=1; a`, int64(4), nil}, /* 7 */ {`a=3; a-=1; a`, int64(2), nil}, /* 8 */ {`a=3; a*=2; a`, int64(6), nil}, /* 9 */ {`v=[10,20,30]; v[0]+=1; v[0]`, int64(11), nil}, /* 10 */ {`r={"a":10}; r["a"]+=1; r["a"]`, int64(11), nil}, /* 11 */ {`a=3; a/=2`, int64(1), nil}, /* 12 */ {`*=2`, nil, `[1:2] infix operator "*=" requires two non-nil operands, got 1`}, /* 13 */ {`a=3; a*=2+1; a`, int64(9), nil}, /* 14 */ {`a=3; (a*=2)+1; a`, int64(6), nil}, /* 15 */ {`a=3; a*=2)+1; a`, nil, `[1:11] unexpected token ")"`}, /* 16 */ {`v=[2]; a=1; v[a-=1]=5; v[0]`, int64(5), nil}, /* 17 */ {`true ? {"a"} :: {"b"}`, "a", nil}, /* 18 */ {`$$`, NewDict(map[any]any{"variables": NewDict(nil), "functions": NewDict(nil)}), nil}, ///* 19 */ {`$$global`, NewDict(map[any]any{"variables": NewDict(nil), "functions": NewDict(nil)}), nil}, /* 19 */ {` ds={ "init":func(@end){@current=0 but true}, //"current":func(){current}, "next":func(){ ((next=current+1) <= end) ? [true] {@current=next but current} :: {nil} } }; it=$(ds,3); it++; it++ `, int64(1), nil}, } // t.Setenv("EXPR_PATH", ".") // runTestSuiteSpec(t, section, inputs, 18) runTestSuite(t, section, inputs) }