2021-12-26 23:23:41 +00:00
|
|
|
package gg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
"github.com/mediocregopher/ginger/graph"
|
2021-12-26 23:23:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecoder(t *testing.T) {
|
|
|
|
|
2021-12-30 16:56:20 +00:00
|
|
|
zeroGraph := new(Graph)
|
2021-12-29 19:32:53 +00:00
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
i := func(i int64) Value {
|
|
|
|
return Value{Number: &i}
|
|
|
|
}
|
|
|
|
|
|
|
|
n := func(n string) Value {
|
|
|
|
return Value{Name: &n}
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:38:36 +00:00
|
|
|
vOut := func(edgeVal, val Value) *OpenEdge {
|
|
|
|
return graph.ValueOut(edgeVal, val)
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-30 18:38:36 +00:00
|
|
|
tOut := func(edgeVal Value, ins ...*OpenEdge) *OpenEdge {
|
|
|
|
return graph.TupleOut(edgeVal, ins...)
|
2021-12-29 19:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
tests := []struct {
|
|
|
|
in string
|
2021-12-30 16:56:20 +00:00
|
|
|
exp *Graph
|
2021-12-26 23:23:41 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: "",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph,
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = 1;",
|
2021-12-30 18:38:36 +00:00
|
|
|
exp: zeroGraph.AddValueIn(n("out"), vOut(ZeroValue, i(1))),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = incr < 1;",
|
2021-12-30 18:38:36 +00:00
|
|
|
exp: zeroGraph.AddValueIn(n("out"), vOut(n("incr"), i(1))),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < b < 1;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
2021-12-30 18:38:36 +00:00
|
|
|
n("out"),
|
2021-12-29 19:32:53 +00:00
|
|
|
tOut(
|
2021-12-26 23:23:41 +00:00
|
|
|
n("a"),
|
2021-12-30 18:38:36 +00:00
|
|
|
vOut(n("b"),
|
|
|
|
i(1)),
|
2021-12-26 23:23:41 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < b < (1; c < 2; d < e < 3;);",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
2021-12-30 18:38:36 +00:00
|
|
|
n("out"),
|
2021-12-29 19:32:53 +00:00
|
|
|
tOut(
|
2021-12-26 23:23:41 +00:00
|
|
|
n("a"),
|
2021-12-30 18:38:36 +00:00
|
|
|
tOut(
|
|
|
|
n("b"),
|
|
|
|
vOut(ZeroValue, i(1)),
|
|
|
|
vOut(n("c"), i(2)),
|
|
|
|
tOut(
|
|
|
|
n("d"),
|
|
|
|
vOut(n("e"), i(3)),
|
|
|
|
),
|
|
|
|
),
|
2021-12-26 23:23:41 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < b < (1; c < (d < 2; 3;); );",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
2021-12-30 18:38:36 +00:00
|
|
|
n("out"),
|
2021-12-29 19:32:53 +00:00
|
|
|
tOut(
|
2021-12-26 23:23:41 +00:00
|
|
|
n("a"),
|
2021-12-30 18:38:36 +00:00
|
|
|
tOut(
|
|
|
|
n("b"),
|
|
|
|
vOut(ZeroValue, i(1)),
|
|
|
|
tOut(
|
|
|
|
n("c"),
|
|
|
|
vOut(n("d"), i(2)),
|
|
|
|
vOut(ZeroValue, i(3)),
|
|
|
|
),
|
|
|
|
),
|
2021-12-26 23:23:41 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = { a = 1; b = c < d < 2; };",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
2021-12-30 18:38:36 +00:00
|
|
|
n("out"),
|
2021-12-29 19:32:53 +00:00
|
|
|
vOut(
|
2021-12-30 18:38:36 +00:00
|
|
|
ZeroValue,
|
2021-12-29 19:32:53 +00:00
|
|
|
Value{Graph: zeroGraph.
|
2021-12-30 18:38:36 +00:00
|
|
|
AddValueIn(n("a"), vOut(ZeroValue, i(1))).
|
2021-12-26 23:23:41 +00:00
|
|
|
AddValueIn(
|
2021-12-30 18:38:36 +00:00
|
|
|
n("b"),
|
2021-12-29 19:32:53 +00:00
|
|
|
tOut(
|
2021-12-26 23:23:41 +00:00
|
|
|
n("c"),
|
2021-12-30 18:38:36 +00:00
|
|
|
vOut(n("d"), i(2)),
|
2021-12-26 23:23:41 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < { b = 1; } < 2;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
2021-12-30 18:38:36 +00:00
|
|
|
n("out"),
|
2021-12-29 19:32:53 +00:00
|
|
|
tOut(
|
2021-12-26 23:23:41 +00:00
|
|
|
n("a"),
|
2021-12-30 18:38:36 +00:00
|
|
|
vOut(
|
|
|
|
Value{Graph: zeroGraph.
|
|
|
|
AddValueIn(n("b"), vOut(ZeroValue, i(1))),
|
|
|
|
},
|
|
|
|
i(2),
|
|
|
|
),
|
2021-12-26 23:23:41 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "a = 1; b = 2;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.
|
2021-12-30 18:38:36 +00:00
|
|
|
AddValueIn(n("a"), vOut(ZeroValue, i(1))).
|
|
|
|
AddValueIn(n("b"), vOut(ZeroValue, i(2))),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
|
|
|
|
|
|
|
r := &mockReader{body: []byte(test.in)}
|
|
|
|
lexer := NewLexer(r)
|
|
|
|
|
|
|
|
got, err := DecodeLexer(lexer)
|
|
|
|
assert.NoError(t, err)
|
2021-12-29 19:32:53 +00:00
|
|
|
assert.True(t, got.Equal(test.exp), "\nexp:%v\ngot:%v", test.exp, got)
|
2021-12-26 23:23:41 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|