From 97879af5fd51f6519479df13bb2606697b36b9ff Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 13 Nov 2023 10:11:01 +0100 Subject: [PATCH] more complex eval tests --- eval.go | 13 +++++++++++++ eval_test.go | 16 +++++++++------- gorageTable.go | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/eval.go b/eval.go index 8f7123d..5899255 100644 --- a/eval.go +++ b/eval.go @@ -329,3 +329,16 @@ func parse(f string) []*token { } return nodes } + +func runEval(f string) string { + p := parse(f) + t := toTree(p) + if len(t) == 0 { + panic("Error while runEval") + } + e := eval(t[0]) + if e == nil { + panic("Eval returned nil") + } + return string(e.value) +} diff --git a/eval_test.go b/eval_test.go index eed41c7..a0a25a0 100644 --- a/eval_test.go +++ b/eval_test.go @@ -19,13 +19,15 @@ import ( func TestEval(t *testing.T) { //println(float64(int(rune('a')))) //f := fmt.Sprintf("'%s' = 'hallo welt' & %d = 3 & %d = 4", "hallo welt", 3, 4) - - p := parse("( 'William' == 'William' && 2 == 2 ) || 85.5 >= 90.0") - tr := toTree(p) - //traverseTree(tr[0]) - //println(len(tr)) - //println(len(tr)) - println(string(eval(tr[0]).value)) + if runEval("( 'William' == 'William' && 2 == 2 ) || 85.5 >= 90.0") != "t" { + t.Fatalf("Should return true") + } + if runEval("1 != 1") != "f" { + t.Fatalf("Should be false") + } + if runEval("( 'Hi' == 'hi' ) || ( 1 == 1 && ( 5 != 5 !& ( t == f ) ) ) && 1.0 < 1.1") != "t" { + t.Fatalf("Should be true") + } //traverseTree(tr[0]) //_printTree(&tr[0]) diff --git a/gorageTable.go b/gorageTable.go index 09e12a9..31b2271 100644 --- a/gorageTable.go +++ b/gorageTable.go @@ -164,7 +164,7 @@ func (g *GorageTable) Where(f string) *GorageTable { tmp = append(tmp, k) } q := strings.Join(tmp, " ") - if e := eval(toTree(parse(q))[0]); e != nil && string(e.value) == "t" { + if e := runEval(q); e == "t" { res.Rows = append(res.Rows, v) } }