diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index f6f5788..4ff66c0 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -30,6 +30,7 @@ func ParseScript(scripts map[string]string, p := &plruntime.Script{ FuncCall: call, Name: name, + Content: content, Ast: stmts, } diff --git a/pkg/engine/op_test.go b/pkg/engine/op_test.go index 6dfe372..d826b03 100644 --- a/pkg/engine/op_test.go +++ b/pkg/engine/op_test.go @@ -5,100 +5,113 @@ package engine -// func TestOp(t *testing.T) { -// pl := ` -// b = 1 + 1 -// a = (b + 2) == 4 || False -// c = a * 3 + +100 + -10 + -// 3/1.1 -// d = 4 / 3 -// e = "dwdw" + "3" -// add_key(e) - -// map_a = {"a": 1, "b" :2 , "1.1": 2, "nil": [1,2.1,"3"], "1": nil} - -// f = map_a["a"] - -// aaaa = 1.0 == (b = 1) - -// a = v = a -// x7 = [1, 2.1, "3"] -// if b == 2 { -// x = 2 -// for i = 1; i < 4; i = i+1 { -// x1 = 1 + x -// e = e + "1" -// if i == 2 { -// break -// } -// continue -// e = e + "2" -// } -// } -// ddd = "" - -// # 无序遍历 key -// # for x in {'a': 1, "b":2, "c":3} { -// # ddd = ddd + x -// # } - -// # add_key(ddd) - -// abc = { -// "a": [1,2,3], -// "d": "a", -// "1": 2, -// "d": nil -// } -// add_key(abc) -// abc["a"][-1] = 5 -// add_key(abc) -// ` + ";`aa dw.` = abc;" + "add_key(`aa dw.`)" + ` -// for x in [1,2,3 ] { -// for y in [1,3,4] { -// if y == 3 { -// break -// } -// continue -// } -// break -// } - -// add_key(len1, len([12,2])) -// add_key(len2, len("123")) - -// for a = 0; a < 12; a = a + 1 { -// if a > 5 { -// add_key(ef, a) -// break -// } -// continue -// a = a - 1 -// } - -// ` - -// scripts, errs := ParseScript(map[string]string{ -// "abc.ppl": pl, -// }, funcs.FuncsMap, funcs.FuncsCheckMap) -// if len(errs) != 0 { -// t.Fatal(errs["abc.ppl"]) -// } - -// script := scripts["abc.ppl"] - -// m, tags, f, tn, drop, err := RunScriptWithRMapIn(script, "test", nil, nil, time.Now(), nil) - -// t.Log(m, tags, tn, drop) -// if err != nil { -// t.Error(err) -// } -// assert.Equal(t, map[string]any{ -// "aa dw.": `{"1":2,"a":[1,2,5],"d":null}`, -// "abc": `{"1":2,"a":[1,2,5],"d":null}`, -// "e": "dwdw3", -// "ef": int64(6), -// "len1": int64(2), -// "len2": int64(3), -// }, f) -// } +import ( + "testing" + "time" + + "github.com/GuanceCloud/ppl/pkg/engine/runtime" + "github.com/GuanceCloud/ppl/pkg/inimpl/guancecloud/funcs" + "github.com/GuanceCloud/ppl/pkg/inimpl/guancecloud/input" + "github.com/stretchr/testify/assert" +) + +func TestOp(t *testing.T) { + pl := ` + b = 1 + 1 + a = (b + 2) == 4 || False + c = a * 3 + +100 + -10 + + 3/1.1 + d = 4 / 3 + e = "dwdw" + "3" + add_key(e) + + map_a = {"a": 1, "b" :2 , "1.1": 2, "nil": [1,2.1,"3"], "1": nil} + + f = map_a["a"] + + aaaa = 1.0 == (b = 1) + + a = v = a + x7 = [1, 2.1, "3"] + if b == 2 { + x = 2 + for i = 1; i < 4; i = i+1 { + x1 = 1 + x + e = e + "1" + if i == 2 { + break + } + continue + e = e + "2" + } + } + ddd = "" + + # 无序遍历 key + # for x in {'a': 1, "b":2, "c":3} { + # ddd = ddd + x + # } + + # add_key(ddd) + + abc = { + "a": [1,2,3], + "d": "a", + "1": 2, + "d": nil + } + add_key(abc) + abc["a"][-1] = 5 + add_key(abc) +` + ";`aa dw.` = abc;" + "add_key(`aa dw.`)" + ` +for x in [1,2,3 ] { + for y in [1,3,4] { + if y == 3 { + break + } + continue + } + break +} + +add_key(len1, len([12,2])) +add_key(len2, len("123")) + +for a = 0; a < 12; a = a + 1 { + if a > 5 { + add_key(ef, a) + break + } + continue + a = a - 1 + } + +` + + scripts, errs := ParseScript(map[string]string{ + "abc.ppl": pl, + }, funcs.FuncsMap, funcs.FuncsCheckMap) + if len(errs) != 0 { + t.Fatal(errs["abc.ppl"]) + } + + script := scripts["abc.ppl"] + + pt := input.GetPoint() + pt = input.InitPt(pt, "test", nil, nil, time.Now()) + err := runtime.RunScriptWithRMapIn(script, pt, nil) + if err != nil { + t.Fatal(err.ChainError()) + } + + t.Log(pt) + + assert.Equal(t, map[string]any{ + "aa dw.": `{"1":2,"a":[1,2,5],"d":null}`, + "abc": `{"1":2,"a":[1,2,5],"d":null}`, + "e": "dwdw3", + "ef": int64(6), + "len1": int64(2), + "len2": int64(3), + }, pt.Fields) +}