From fe5c8e9619109ae5f4deee1f52fa3aed310f90d0 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Wed, 26 Jun 2024 04:29:40 +0200 Subject: [PATCH] tests improved --- t_bool_test.go | 66 +++++++++++++++++++++++++++++++++++++++ t_builtin-fmt_test.go | 7 ++--- t_builtin-os-file_test.go | 6 ++++ t_builtin-string_test.go | 2 +- t_common_test.go | 14 --------- t_funcs_test.go | 3 +- t_template_test.go | 2 +- t_utils_test.go | 4 +-- 8 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 t_bool_test.go diff --git a/t_bool_test.go b/t_bool_test.go new file mode 100644 index 0000000..a8099f1 --- /dev/null +++ b/t_bool_test.go @@ -0,0 +1,66 @@ +// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com). +// All rights reserved. + +// t_bool_test.go +package expr + +import ( + "errors" + "testing" +) + +func TestBool(t *testing.T) { + section := "Bool" + inputs := []inputType{ + /* 1 */ {`true`, true, nil}, + /* 2 */ {`false`, false, nil}, + /* 3 */ {`not false`, true, nil}, + /* 4 */ {`not 1`, false, nil}, + /* 5 */ {`not "true"`, false, nil}, + /* 6 */ {`not "false"`, false, nil}, + /* 7 */ {`not ""`, true, nil}, + /* 8 */ {`not []`, nil, errors.New(`[1:4] prefix/postfix operator "NOT" do not support operand '[]' [list]`)}, + /* 9 */ {`true and false`, false, nil}, + /* 10 */ {`true and []`, nil, errors.New(`[1:9] left operand 'true' [bool] and right operand '[]' [list] are not compatible with operator "AND"`)}, + /* 11 */ {`[] and false`, nil, errors.New(`got list as left operand type of 'AND' operator, it must be bool`)}, + /* 12 */ {`true or false`, true, nil}, + /* 13 */ {`true or []`, true, nil}, + /* 14 */ {`[] or false`, nil, errors.New(`got list as left operand type of 'OR' operator, it must be bool`)}, + /* 13 */ //{`true or []`, nil, errors.New(`[1:8] left operand 'true' [bool] and right operand '[]' [list] are not compatible with operator "OR"`)}, + } + + // t.Setenv("EXPR_PATH", ".") + + // runTestSuiteSpec(t, section, inputs, 1) + runTestSuite(t, section, inputs) +} + +func TestBoolNoShortcut(t *testing.T) { + section := "Bool-NoShortcut" + inputs := []inputType{ + /* 1 */ {`true`, true, nil}, + /* 2 */ {`false`, false, nil}, + /* 3 */ {`not false`, true, nil}, + /* 4 */ {`not 1`, false, nil}, + /* 5 */ {`not "true"`, false, nil}, + /* 6 */ {`not "false"`, false, nil}, + /* 7 */ {`not ""`, true, nil}, + /* 8 */ {`not []`, nil, errors.New(`[1:4] prefix/postfix operator "NOT" do not support operand '[]' [list]`)}, + /* 9 */ {`true and false`, false, nil}, + /* 10 */ {`true and []`, nil, errors.New(`[1:9] left operand 'true' [bool] and right operand '[]' [list] are not compatible with operator "AND"`)}, + /* 11 */ {`[] and false`, nil, errors.New(`[1:7] left operand '[]' [list] and right operand 'false' [bool] are not compatible with operator "AND"`)}, + /* 12 */ {`true or false`, true, nil}, + /* 13 */ {`true or []`, nil, errors.New(`[1:8] left operand 'true' [bool] and right operand '[]' [list] are not compatible with operator "OR"`)}, + /* 14 */ {`[] or false`, nil, errors.New(`[1:6] left operand '[]' [list] and right operand 'false' [bool] are not compatible with operator "OR"`)}, + } + + // t.Setenv("EXPR_PATH", ".") + + ctx := NewSimpleStore() + current := SetCtrl(ctx, ControlBoolShortcut, false) + + // runCtxTestSuiteSpec(t, ctx, section, inputs, 1) + runCtxTestSuite(t, ctx, section, inputs) + + SetCtrl(ctx, ControlBoolShortcut, current) +} diff --git a/t_builtin-fmt_test.go b/t_builtin-fmt_test.go index 125c2d3..79de2ee 100644 --- a/t_builtin-fmt_test.go +++ b/t_builtin-fmt_test.go @@ -21,14 +21,13 @@ func TestFuncFmt(t *testing.T) { //t.Setenv("EXPR_PATH", ".") - // parserTestSpec(t, section, inputs, 19) + // runTestSuiteSpec(t, section, inputs, 19) runTestSuite(t, section, inputs) } - func TestFmt(t *testing.T) { section := "Builtin-Fmt" - + text := "ciao mondo" inputs := []inputType{ /* 1 */ {fmt.Sprintf(`println("%s")`, text), int64(11), nil}, @@ -43,7 +42,7 @@ func TestFmt(t *testing.T) { runCtxTestSuite(t, ctx, section, inputs) SetCtrl(ctx, ControlStdout, currentStdout) - if b.String() != text + "\n" { + if b.String() != text+"\n" { t.Errorf("println(): Got: %q, Want: %q", b.String(), text+"\n") } } diff --git a/t_builtin-os-file_test.go b/t_builtin-os-file_test.go index f70d094..236b7e8 100644 --- a/t_builtin-os-file_test.go +++ b/t_builtin-os-file_test.go @@ -21,6 +21,12 @@ func TestFuncOs(t *testing.T) { /* 7 */ {`builtin "os.file"; word=fileReadText(nil, "-")`, nil, errors.New(`fileReadText(): invalid file handle`)}, /* 8 */ {`builtin "os.file"; fileWriteText(nil, "bye")`, nil, errors.New(`fileWriteText(): invalid file handle`)}, /* 9 */ {`builtin "os.file"; handle=fileOpen()`, nil, errors.New(`fileOpen(): too few params -- expected 1, got 0`)}, + /* 10 */ {`builtin "os.file"; handle=fileOpen(123)`, nil, errors.New(`fileOpen(): missing or invalid file path`)}, + /* 11 */ {`builtin "os.file"; handle=fileCreate(123)`, nil, errors.New(`fileCreate(): missing or invalid file path`)}, + /* 12 */ {`builtin "os.file"; handle=fileAppend(123)`, nil, errors.New(`fileAppend(): missing or invalid file path`)}, + /* 13 */ {`builtin "os.file"; handle=fileClose(123)`, nil, errors.New(`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, errors.New(`fileReadTextAll(): invalid file handle 123 [int64]`)}, } // t.Setenv("EXPR_PATH", ".") diff --git a/t_builtin-string_test.go b/t_builtin-string_test.go index fe32e04..00cb757 100644 --- a/t_builtin-string_test.go +++ b/t_builtin-string_test.go @@ -64,6 +64,6 @@ func TestFuncString(t *testing.T) { //t.Setenv("EXPR_PATH", ".") - // parserTestSpec(t, section, inputs, 19) + // runTestSuiteSpec(t, section, inputs, 19) runTestSuite(t, section, inputs) } diff --git a/t_common_test.go b/t_common_test.go index 0824f27..f24b24e 100644 --- a/t_common_test.go +++ b/t_common_test.go @@ -33,20 +33,6 @@ func runCtxTestSuiteSpec(t *testing.T, ctx ExprContext, section string, inputs [ func runTestSuiteSpec(t *testing.T, section string, inputs []inputType, spec ...int) { runCtxTestSuiteSpec(t, nil, section, inputs, spec...) -/* - succeeded := 0 - failed := 0 - for _, count := range spec { - good := doTest(t, nil, section, &inputs[count-1], count) - - if good { - succeeded++ - } else { - failed++ - } - } - t.Logf("%s -- test count: %d, succeeded: %d, failed: %d", section, len(spec), succeeded, failed) -*/ } func runCtxTestSuite(t *testing.T, ctx ExprContext, section string, inputs []inputType) { diff --git a/t_funcs_test.go b/t_funcs_test.go index da2f4f8..97f376c 100644 --- a/t_funcs_test.go +++ b/t_funcs_test.go @@ -30,12 +30,13 @@ func TestFuncs(t *testing.T) { /* 16 */ {`f=func(x,n=2,y){x+n}`, nil, errors.New(`[1:16] can't mix default and non-default parameters`)}, /* 17 */ {`f=func(x,n){1}; f(3,4,)`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)}, /* 18 */ {`factory=func(base){func(){@base=base+1}}; inc10=factory(10); inc5=factory(5); inc10(); inc5(); inc10()`, int64(12), nil}, + /* 19 */ {`f=func(a,y=1,z="sos"){}; string(f)`, `f(a, y=1, z="sos"):any{}`, nil}, // /* 18 */ {`f=func(a){a*2}`, nil, errors.New(`[1:24] expected "function-param-value", got ")"`)}, } // t.Setenv("EXPR_PATH", ".") - // parserTestSpec(t, section, inputs, 17) + // runTestSuiteSpec(t, section, inputs, 17) runTestSuite(t, section, inputs) } diff --git a/t_template_test.go b/t_template_test.go index f0688ee..c9a675b 100644 --- a/t_template_test.go +++ b/t_template_test.go @@ -16,6 +16,6 @@ func TestSomething(t *testing.T) { // t.Setenv("EXPR_PATH", ".") - // parserTestSpec(t, section, inputs, 1) + // runTestSuiteSpec(t, section, inputs, 1) runTestSuite(t, section, inputs) } diff --git a/t_utils_test.go b/t_utils_test.go index 63028ac..372c875 100644 --- a/t_utils_test.go +++ b/t_utils_test.go @@ -98,7 +98,7 @@ func TestToIntOk(t *testing.T) { wantValue := int(64) wantErr := error(nil) - gotValue, gotErr := ToInt(source, "test") + gotValue, gotErr := ToGoInt(source, "test") if gotErr != nil || gotValue != wantValue { t.Errorf("toInt(%v, \"test\") gotValue=%v, gotErr=%v -> wantValue=%v, wantErr=%v", @@ -111,7 +111,7 @@ func TestToIntErr(t *testing.T) { wantValue := 0 wantErr := errors.New(`test expected integer, got uint64 (64)`) - gotValue, gotErr := ToInt(source, "test") + gotValue, gotErr := ToGoInt(source, "test") if gotErr.Error() != wantErr.Error() || gotValue != wantValue { t.Errorf("toInt(%v, \"test\") gotValue=%v, gotErr=%v -> wantValue=%v, wantErr=%v",