Exemplo n.º 1
0
func TestMath(t *testing.T) {
	df, err := gopp.NewDecoderFactory(mathgopp, "Eqn")
	if err != nil {
		t.Error(err)
		return
	}
	df.RegisterType(MathExprFactor{})
	df.RegisterType(MathNumberFactor{})
	df.RegisterType(MathSum{})
	df.RegisterType(MathProduct{})
	dec := df.NewDecoder(strings.NewReader("5+1=6\n"))
	var eqn MathEqn
	err = dec.Decode(&eqn)
	if err != nil {
		t.Error(err)
		return
	}

	expectedEqn := MathEqn{
		Left: MathSum{
			First:  MathNumberFactor{5},
			Second: MathNumberFactor{1},
		},
		Right: MathNumberFactor{6},
	}

	if eqn != expectedEqn {
		t.Errorf("Expected %q, got %q.", expectedEqn, eqn)
	}
}
Exemplo n.º 2
0
func TestDecodePtrSlice(t *testing.T) {
	grammar := `
ignore: /^\s+/

Start => {field=Kids} <<Node>>*
Node => {field=Val} <dig>

dig = /(\d+)/
`
	data := []string{"1", "4", "9", "42"}
	in := strings.Join(data, " ")

	df, err := gopp.NewDecoderFactory(grammar, "Start")
	if err != nil {
		t.Error(err)
	}
	dec := df.NewDecoder(strings.NewReader(in))
	out := &Node{}
	err = dec.Decode(out)
	if err != nil {
		t.Error(err)
	}
	if len(data) != len(out.Kids) {
		t.Fatalf("Expected %d nodes, got %d", len(data), len(out.Kids))
	}
	for i, s := range data {
		if out.Kids[i].Val != s {
			t.Errorf("Expected node %s, got %s", s, out.Kids[i])
		}
	}
}
Exemplo n.º 3
0
func TestLiteral(t *testing.T) {
	df, err := gopp.NewDecoderFactory(literalgopp, "Start")
	if err != nil {
		t.Error(err)
		return
	}
	for _, test := range LiteralTestTable {
		dec := df.NewDecoder(strings.NewReader(test.src))
		var lit LiteralTester
		err = dec.Decode(&lit)
		if err != nil {
			t.Error(err)
			return
		}
		if lit != test.expected {
			t.Errorf("Expected %+v, got %+v.", test.expected, lit)
		}
	}
}
Exemplo n.º 4
0
func TestErrors(t *testing.T) {
subject:
	for _, s := range ErrorSubjects {
		df, err := gopp.NewDecoderFactory(s.Gopp, "Start")
		if err != nil {
			t.Error(err)
			continue subject
		}
	scase:
		for _, c := range s.Cases {
			dec := df.NewDecoder(strings.NewReader(c.Document))
			err = dec.Decode(&XYZ{})
			if !(err == nil && c.ExpectedError == "") && (err != nil && err.Error() != c.ExpectedError) {
				t.Error(err)
				continue scase
			}
		}
	}
}
Exemplo n.º 5
0
func TestSubjects(t *testing.T) {
subject:
	for _, s := range Subjects {
		df, err := gopp.NewDecoderFactory(s.Gopp, "Start")
		if err != nil {
			t.Error(err)
			continue subject
		}
	scase:
		for _, c := range s.Cases {
			dec := df.NewDecoder(strings.NewReader(c.Document))
			err = dec.Decode(c.Object)
			if err != nil {
				t.Error(err)
				continue scase
			}
			if !reflect.DeepEqual(c.Object, c.Expected) {
				t.Errorf("(%s) With %q, got %+v, expected %+v.", s.Name, c.Document, c.Object, c.Expected)
			}
		}
	}
}