Skip to content

Commit

Permalink
provide location information when reporting errors #15
Browse files Browse the repository at this point in the history
  • Loading branch information
vircoys committed Nov 23, 2022
1 parent 1663496 commit df9afd4
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 97 deletions.
1 change: 1 addition & 0 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func ParseScript(scripts map[string]string,
p := &plruntime.Script{
FuncCall: call,
Name: name,
Content: content,
Ast: stmts,
}

Expand Down
207 changes: 110 additions & 97 deletions pkg/engine/op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit df9afd4

Please sign in to comment.