gitee.com/KyleChenSource/lib-robot@v1.0.2/robottest/common/interpreter/logic_test.go (about) 1 package interpreter 2 3 import ( 4 "fmt" 5 "math" 6 "testing" 7 ) 8 9 func init() { 10 LogDebug = func(s string, args ...any) { fmt.Printf(s+"\n", args...) } 11 } 12 13 type TestData struct { 14 s string 15 r any 16 } 17 18 type Ctx struct { 19 _ctx map[string]any 20 } 21 22 func (this *Ctx) Get(s string) (any, bool) { 23 ret, err := this._ctx[s] 24 return ret, err 25 } 26 27 func (this *Ctx) Set(s string, v any) { 28 this._ctx[s] = v 29 } 30 31 func TestLogic(t *testing.T) { 32 scripts := []struct { 33 s string 34 r any 35 }{ 36 {"1+2", float64(3)}, 37 {"1+2+3-3-2-1", float64(0)}, 38 {"2*3*2/2/3/2", float64(1)}, 39 {"1+2+3*4-5/5", float64(14)}, 40 {"1+2+3*4*4/2+2/2", float64(28)}, 41 {"3 > 2", true}, 42 {"3 >= 2", true}, 43 {"3 <= 2", false}, 44 {"3 <= 3", true}, 45 {"3 == 3", true}, 46 {"! 3 == 3", false}, 47 {"1+2+3*4/4/3-1 > 2 && 3 < 4", true}, 48 {"1+2+3*4-5 > 2 || 3 > 4", true}, 49 {"1+2+3*4-5 > 2 && 3 > 4", false}, 50 {"Rand > 0", true}, 51 {"Rand 1 3 < 4", true}, 52 {"Variable = 10", float64(10)}, 53 {"Variable == 10", true}, 54 {"Variable = Variable+3", float64(13)}, 55 {"Variable == 13", true}, 56 {"None", false}, 57 {"! None", true}, 58 {"Variable==Value", false}, 59 {"Variable=\"hello\"", "hello"}, 60 {"Variable==\"hello\"", true}, 61 {"Variable==\"hello\";Variable=Variable+\"world\"", "helloworld"}, 62 {"Variable = 10;Variable = Variable + Rand 10 12", 21}, 63 {"VariableX = 10.2;VariableY = 2.7;Variable = \"refresh \" + VariableX + \" \" + VariableY", "refresh 10.200000 2.700000"}, 64 // {"VariableX = 10.2;VariableY = 2.7;Variable = \"refresh \" + (VariableX + 3) + \" \" + (VariableY + 2)", "refresh 13.200000 2.900000"}, failed 65 // {"\"Variable\"+\"Ext\" = Variable + \"world\"", "helloworld"}, 66 // {"Get \"VariableExt\"", "helloworld"}, 67 } 68 69 ctx := Ctx{_ctx: make(map[string]any)} 70 for _, d := range scripts { 71 r, e := DoString([]byte(d.s), 3, 1, &ctx) 72 if e != nil { 73 t.Errorf("script:%s err:%s", d.s, e.Error()) 74 } else { 75 switch r.(type) { 76 case float64: 77 78 f := r.(float64) 79 want, err := d.r.(float64) 80 if !err || math.Abs(want-f) > 0.00001 { 81 t.Errorf("script:%s want:%v get:%v", d.s, d.r, r) 82 } else { 83 i := int64(f) 84 if f-float64(i) < 0.00001 { 85 fmt.Printf("script:%s int64:%d\n", d.s, i) 86 } else { 87 fmt.Printf("script:%s float64:%f\n", d.s, r.(float64)) 88 } 89 } 90 case bool: 91 b := r.(bool) 92 want, err := d.r.(bool) 93 if !err || want != b { 94 t.Errorf("script:%s want:%v get:%v", d.s, d.r, r) 95 } else { 96 fmt.Printf("script:%s bool:%t\n", d.s, b) 97 } 98 case string: 99 s := r.(string) 100 want, err := d.r.(string) 101 if !err || want != s { 102 t.Errorf("script:%s want:%v get:%v", d.s, d.r, r) 103 } else { 104 fmt.Printf("script:%s string:%s\n", d.s, s) 105 } 106 } 107 } 108 } 109 }