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-29 19:32:53 +00:00
|
|
|
zeroGraph := new(graph.Graph[Value])
|
|
|
|
|
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-29 19:32:53 +00:00
|
|
|
vOut := func(val, edgeVal Value) graph.OpenEdge[Value] {
|
|
|
|
return graph.ValueOut(val, edgeVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
tOut := func(ins []graph.OpenEdge[Value], edgeVal Value) graph.OpenEdge[Value] {
|
|
|
|
return graph.TupleOut(ins, edgeVal)
|
|
|
|
}
|
|
|
|
|
|
|
|
type openEdge = graph.OpenEdge[Value]
|
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
tests := []struct {
|
|
|
|
in string
|
2021-12-29 19:32:53 +00:00
|
|
|
exp *graph.Graph[Value]
|
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-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(vOut(i(1), ZeroValue), n("out")),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = incr < 1;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(vOut(i(1), n("incr")), n("out")),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < b < 1;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
|
|
|
tOut(
|
|
|
|
[]openEdge{vOut(i(1), n("b"))},
|
2021-12-26 23:23:41 +00:00
|
|
|
n("a"),
|
|
|
|
),
|
|
|
|
n("out"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < b < (1; c < 2; d < e < 3;);",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
|
|
|
tOut(
|
|
|
|
[]openEdge{tOut(
|
|
|
|
[]openEdge{
|
|
|
|
vOut(i(1), ZeroValue),
|
|
|
|
vOut(i(2), n("c")),
|
|
|
|
tOut(
|
|
|
|
[]openEdge{vOut(i(3), n("e"))},
|
2021-12-26 23:23:41 +00:00
|
|
|
n("d"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
n("b"),
|
|
|
|
)},
|
|
|
|
n("a"),
|
|
|
|
),
|
|
|
|
n("out"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < b < (1; c < (d < 2; 3;); );",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
|
|
|
tOut(
|
|
|
|
[]openEdge{tOut(
|
|
|
|
[]openEdge{
|
|
|
|
vOut(i(1), ZeroValue),
|
|
|
|
tOut(
|
|
|
|
[]openEdge{
|
|
|
|
vOut(i(2), n("d")),
|
|
|
|
vOut(i(3), ZeroValue),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
n("c"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
n("b"),
|
|
|
|
)},
|
|
|
|
n("a"),
|
|
|
|
),
|
|
|
|
n("out"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = { a = 1; b = c < d < 2; };",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
|
|
|
vOut(
|
|
|
|
Value{Graph: zeroGraph.
|
|
|
|
AddValueIn(vOut(i(1), ZeroValue), n("a")).
|
2021-12-26 23:23:41 +00:00
|
|
|
AddValueIn(
|
2021-12-29 19:32:53 +00:00
|
|
|
tOut(
|
|
|
|
[]openEdge{
|
|
|
|
vOut(i(2), n("d")),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
n("c"),
|
|
|
|
),
|
|
|
|
n("b"),
|
|
|
|
),
|
|
|
|
},
|
2021-12-28 16:49:02 +00:00
|
|
|
ZeroValue,
|
2021-12-26 23:23:41 +00:00
|
|
|
),
|
|
|
|
n("out"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "out = a < { b = 1; } < 2;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.AddValueIn(
|
|
|
|
tOut(
|
|
|
|
[]openEdge{
|
|
|
|
vOut(
|
2021-12-26 23:23:41 +00:00
|
|
|
i(2),
|
2021-12-29 19:32:53 +00:00
|
|
|
Value{Graph: zeroGraph.
|
|
|
|
AddValueIn(vOut(i(1), ZeroValue), n("b")),
|
2021-12-26 23:23:41 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
n("a"),
|
|
|
|
),
|
|
|
|
n("out"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "a = 1; b = 2;",
|
2021-12-29 19:32:53 +00:00
|
|
|
exp: zeroGraph.
|
|
|
|
AddValueIn(vOut(i(1), ZeroValue), n("a")).
|
|
|
|
AddValueIn(vOut(i(2), ZeroValue), n("b")),
|
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
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|