diff --git a/t_builtin-iterator_test.go b/t_builtin-iterator_test.go index 676d431..c769a4e 100644 --- a/t_builtin-iterator_test.go +++ b/t_builtin-iterator_test.go @@ -6,8 +6,11 @@ package expr import ( "fmt" + "io" "os" "testing" + + "git.portale-stac.it/go-pkg/expr/types/list" ) const ( @@ -18,7 +21,7 @@ func TestFuncRun(t *testing.T) { section := "Builtin-Iterator" inputs := []inputType{ - /* 1 */ {`builtin "iterator"; it=$(1,2,3); run(it)`, nil, nil}, + /* 1 */ {`builtin "iterator"; it=$(1,2,3); run(it)`, int64(3), nil}, /* 2 */ {`builtin "iterator"; run($(1,2,3), func(index,item){item+10})`, nil, nil}, /* 3 */ {`builtin "iterator"; run($(..4), func(index,item){status=status+item; true}, {"status":0})`, int64(6), nil}, /* 4 */ {`builtin ["iterator", "fmt"]; run($(1,2,3), func(index,item){println(item+10)})`, nil, nil}, @@ -27,18 +30,59 @@ func TestFuncRun(t *testing.T) { /* 7 */ {`builtin "iterator"; run($(1,2,3), func(){1}, "prrr")`, nil, `type of "vars" must be dict, passed prrr [string]`}, /* 8 */ {`builtin "iterator"; run($(1,2,3), operator=nil)`, int64(3), nil}, /* 9 */ {`builtin "iterator"; run($(1,2,3), operatorx=nil)`, nil, `run(): unknown param "operatorx"`}, - /* 10 */ {fmt.Sprintf(`builtin ["os.file", "iterator"]; it = fileLineIterator(%q); run(it)`, testFilePath), int64(2), nil}, - /* 11 */ {`builtin "iterator"; op = func(index, item){ (index > 2) ? {abort("aborted")} :: {status="ok"} }; run($(..5), op)`, "aborted", nil}, - /* 12 */ {`builtin "iterator"; op = func(index, item){ (index > 2) ? {abort("aborted")} :: {status="ok"} }; run($(..1), op)`, "ok", nil}, + /* 10 */ {`builtin "iterator"; op = func(index, item){ (index > 2) ? {abort("aborted")} :: {status="ok"} }; run($(..5), op)`, "aborted", nil}, + /* 11 */ {`builtin "iterator"; op = func(index, item){ (index > 2) ? {abort("aborted")} :: {status="ok"} }; run($(..1), op)`, "ok", nil}, + } + //t.Setenv("EXPR_PATH", ".") + + // RunTestSuiteSpec(t, section, inputs, 10) + RunTestSuite(t, section, inputs) + + cleanupBuiltinIterator() +} + +func TestFuncOsLineIter(t *testing.T) { + section := "Builtin-OS-File-Line-Iter" + inputs := []inputType{ + /* 1 */ {fmt.Sprintf(`builtin ["os.file", "iterator"]; it = fileLineIterator(%q); run(it)`, testFilePath), int64(2), nil}, + /* 2 */ {fmt.Sprintf(`builtin "os.file"; it=fileLineIterator(%q); it++`, testFilePath), "uno", nil}, + /* 3 */ {fmt.Sprintf(`builtin "os.file"; it=fileLineIterator(%q); it++;it++;it++`, testFilePath), nil, io.EOF.Error()}, + /* 4 */ {fmt.Sprintf(`builtin "os.file"; it=fileLineIterator(%q); it.clean`, testFilePath), nil, nil}, + /* 5 */ {fmt.Sprintf(`builtin "os.file"; it=fileLineIterator(%q); string(it)`, testFilePath), + fmt.Sprintf(`$(fileLineIterator@%q)`, testFilePath), nil}, + /* 6 */ {fmt.Sprintf(`builtin "os.file"; #$$(fileLineIterator(%q) filter (#${_} == 2))`, testFilePath), int64(0), nil}, + /* 7 */ {fmt.Sprintf(`builtin "os.file"; #$$(fileLineIterator(%q) filter (#${_} == 3))`, testFilePath), int64(2), nil}, + /* 8 */ {fmt.Sprintf(`builtin "os.file"; $$(fileLineIterator(%q) map ${__})`, testFilePath), list.NewLinkedListA(0, 1), nil}, } if err := setupBuiltinIterator(); err != nil { t.Error(err) return } - //t.Setenv("EXPR_PATH", ".") - // RunTestSuiteSpec(t, section, inputs, 10) + // RunTestSuiteSpec(t, section, inputs, 1) + RunTestSuite(t, section, inputs) + + cleanupBuiltinIterator() +} + +func TestFuncOsByteIter(t *testing.T) { + section := "Builtin-OS-File-Byte-Iter" + inputs := []inputType{ + /* 1 */ {fmt.Sprintf(`builtin "os.file"; it=fileByteIterator(%q); string(it)`, testFilePath), + fmt.Sprintf(`$(fileByteIterator@%q)`, testFilePath), nil}, + /* 2 */ {fmt.Sprintf(`builtin ["os.file", "string"]; it=fileByteIterator(%q); char(it++)`, testFilePath), `u`, nil}, + /* 3 */ {fmt.Sprintf(`builtin ["os.file", "string"]; it=fileByteIterator(%q); it++; it.reset; char(it++)`, testFilePath), `u`, nil}, + } + + if err := setupBuiltinIterator(); err != nil { + t.Error(err) + return + } + + // t.Setenv("EXPR_PATH", ".") + + // RunTestSuiteSpec(t, section, inputs, 24) RunTestSuite(t, section, inputs) cleanupBuiltinIterator() diff --git a/t_builtin-os-file_test.go b/t_builtin-os-file_test.go index ee474c6..cbbe561 100644 --- a/t_builtin-os-file_test.go +++ b/t_builtin-os-file_test.go @@ -5,10 +5,13 @@ package expr import ( - "io" "testing" ) +// const ( +// builtinOsFile_dummyFile = "/tmp/.go-test_dummy.txt" +// ) + func TestFuncOs(t *testing.T) { section := "Builtin-OS-File" inputs := []inputType{ @@ -27,30 +30,29 @@ func TestFuncOs(t *testing.T) { /* 13 */ {`builtin "os.file"; handle=fileClose(123)`, nil, `fileClose(): invalid file handle`}, /* 14 */ {`builtin "os.file"; handle=fileOpen("/tmp/dummy"); c=fileReadTextAll(handle); fileClose(handle); c`, "bye-bye", nil}, /* 15 */ {`builtin "os.file"; c=fileReadTextAll(123)`, nil, `fileReadTextAll(): invalid file handle 123 [int64]`}, - /* 16 */ {`builtin "os.file"; it=fileLineIterator("test-file.txt"); it++`, "uno", nil}, - /* 17 */ {`builtin "os.file"; it=fileLineIterator("test-file.txt"); it++;it++;it++`, nil, io.EOF.Error()}, - /* 18 */ {`builtin "os.file"; it=fileLineIterator("test-file.txt"); it.clean`, nil, nil}, - /* 19 */ {`builtin "os.file"; it=fileLineIterator("test-file.txt"); string(it)`, `$(fileLineIterator@"test-file.txt")`, nil}, - /* 20 */ {`builtin "os.file"; handle=fileOpen("/etc/hosts"); fileClose(handle); string(handle)`, `reader`, nil}, - /* 21 */ {`builtin "os.file"; handle=fileCreate("/tmp/dummy"); fileClose(handle); string(handle)`, `writer`, nil}, + /* 16 */ {`builtin "os.file"; handle=fileOpen("/etc/hosts"); fileClose(handle); string(handle)`, `reader`, nil}, + /* 17 */ {`builtin "os.file"; handle=fileCreate("/tmp/dummy"); fileClose(handle); string(handle)`, `writer`, nil}, } // t.Setenv("EXPR_PATH", ".") - // runTestSuiteSpec(t, section, inputs, 24) + // RunTestSuiteSpec(t, section, inputs, 17) RunTestSuite(t, section, inputs) } -func TestFuncOsByteIter(t *testing.T) { - section := "Builtin-OS-File-Byte-Iter" - inputs := []inputType{ - /* 1 */ {`builtin "os.file"; it=fileByteIterator("test-file.txt"); string(it)`, `$(fileByteIterator@"test-file.txt")`, nil}, - /* 2 */ {`builtin ["os.file", "string"]; it=fileByteIterator("test-file.txt"); char(it++)`, `u`, nil}, - /* 3 */ {`builtin ["os.file", "string"]; it=fileByteIterator("test-file.txt"); it++; it.reset; char(it++)`, `u`, nil}, - } +// func setupBuiltinOsFile() (err error) { +// var fh *os.File +// // Create test file +// if fh, err = os.Create(builtinOsFile_dummyFile); err == nil { +// defer fh.Close() +// fmt.Fprintln(fh, "uno") +// fmt.Fprintln(fh, "due") +// } +// return +// } - // t.Setenv("EXPR_PATH", ".") - - // runTestSuiteSpec(t, section, inputs, 24) - RunTestSuite(t, section, inputs) -} +// func cleanupBuiltinOsFile() { +// if fileExists(builtinOsFile_dummyFile) { +// os.Remove(builtinOsFile_dummyFile) +// } +// } diff --git a/t_expr_test.go b/t_expr_test.go index 2250914..f42c5c7 100644 --- a/t_expr_test.go +++ b/t_expr_test.go @@ -16,27 +16,26 @@ func TestExpr(t *testing.T) { 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 */ {`2 ? {"a"}`, nil, `[1:2] no case catches the value (2) of the selection expression`}, - /* 19 */ {`2 ? 5`, nil, "[1:6] expected `{`, got `5`"}, - /* 20 */ {`a=2; ${a}`, int64(2), nil}, - /* 21 */ {`$_=2; $_`, int64(2), nil}, - /* 22 */ {`$$`, dict.NewDict(map[any]any{"vars": dict.NewDict(nil), "funcs": dict.NewDict(nil)}), nil}, - /* 23 */ {` + /* 3 */ {`mynot=func(v){int(v)?{true}::{false}}; mynot(0)`, true, nil}, + /* 4 */ {`1 ? {1} : [1+0] {3*(1+1)}`, int64(6), nil}, + /* 5 */ {`a=3; a+=1; a`, int64(4), nil}, + /* 6 */ {`a=3; a-=1; a`, int64(2), nil}, + /* 7 */ {`a=3; a*=2; a`, int64(6), nil}, + /* 8 */ {`v=[10,20,30]; v[0]+=1; v[0]`, int64(11), nil}, + /* 9 */ {`r={"a":10}; r["a"]+=1; r["a"]`, int64(11), nil}, + /* 10 */ {`a=3; a/=2`, int64(1), nil}, + /* 11 */ {`*=2`, nil, `[1:2] infix operator "*=" requires two non-nil operands, got 1`}, + /* 12 */ {`a=3; a*=2+1; a`, int64(9), nil}, + /* 13 */ {`a=3; (a*=2)+1; a`, int64(6), nil}, + /* 14 */ {`a=3; a*=2)+1; a`, nil, `[1:11] unexpected token ")"`}, + /* 15 */ {`v=[2]; a=1; v[a-=1]=5; v[0]`, int64(5), nil}, + /* 16 */ {`true ? {"a"} :: {"b"}`, "a", nil}, + /* 17 */ {`2 ? {"a"}`, nil, `[1:2] no case catches the value (2) of the selection expression`}, + /* 18 */ {`2 ? 5`, nil, "[1:6] expected `{`, got `5`"}, + /* 19 */ {`a=2; ${a}`, int64(2), nil}, + /* 20 */ {`$_=2; $_`, int64(2), nil}, + /* 21 */ {`$$`, dict.NewDict(map[any]any{"vars": dict.NewDict(nil), "funcs": dict.NewDict(nil)}), nil}, + /* 22 */ {` ds={ "init":func(@end){@current=0 but true}, //"current":func(){current}, diff --git a/t_iterator_test.go b/t_iterator_test.go index da4ac92..c54b56c 100644 --- a/t_iterator_test.go +++ b/t_iterator_test.go @@ -130,8 +130,6 @@ func TestFilterIterator(t *testing.T) { inputs := []inputType{ /* 1 */ {`$$([1,2,3] filter $_%2==0)`, list.NewLinkedListA(2), nil}, /* 2 */ {`it = [1,2,3] filter $_%2!=1; $$(it)`, list.NewLinkedListA(2), nil}, - /* 3 */ {`builtin "os.file"; #$$(fileLineIterator("test-file.txt") filter (#${_} == 2))`, int64(0), nil}, - /* 4 */ {`builtin "os.file"; #$$(fileLineIterator("test-file.txt") filter (#${_} == 3))`, int64(2), nil}, } // runTestSuiteSpec(t, section, inputs, 2) @@ -171,8 +169,7 @@ func TestMapIterator(t *testing.T) { /* 1 */ {`$$([3,4,5] map ${_#})`, list.NewLinkedListA(1, 2, 3), nil}, /* 2 */ {`#$$($(..10) map ${_})`, int64(10), nil}, /* 3 */ {`#$$($(10..0) map ${_})`, int64(10), nil}, - /* 4 */ {`builtin "os.file"; $$(fileLineIterator("test-file.txt") map ${__})`, list.NewLinkedListA(0, 1), nil}, - /* 5 */ {`$$(["1", "2", "3"] map int())`, nil, `int(): too few params -- expected 1, got 0`}, + /* 4 */ {`$$(["1", "2", "3"] map int())`, nil, `int(): too few params -- expected 1, got 0`}, } // runTestSuiteSpec(t, section, inputs, 2) diff --git a/test-file.txt b/test-file.txt deleted file mode 100644 index 319940d..0000000 --- a/test-file.txt +++ /dev/null @@ -1,2 +0,0 @@ -uno -due