Esempio n. 1
0
func TestLex(t *testing.T) {
	l := New([]lex.Lex{
		trie.New("tag1", "foo", "foots"),
		trie.New("tag2", "foo", "fool"),
	})
	tests := []struct {
		input rune
		match bool
		tag   tag.Tag
		err   bool
	}{
		{'f', false, nil, false},
		{'o', false, nil, false},
		{'o', true, tag.New("tag1"), false},
		{'l', true, tag.New("tag2"), false},
		{'s', false, nil, true},
	}
	for i, test := range tests {
		l.Next(test.input)
		if l.Match() != test.match {
			t.Errorf("match not equal on input %d: expected %v, got %v", i, test.match, l.Match())
		}
		if !tag.Equal(l.Tag(), test.tag) {
			t.Errorf("tag not equal on input %d: expected %v, got %v", i, test.tag, l.Tag())
		}
		if l.Error() != test.err {
			t.Errorf("error not equal on input %d: expected %v, got %v", i, test.err, l.Error())
		}
	}
}
Esempio n. 2
0
func TestBufferParens(t *testing.T) {
	taker := SimpleTaker([]Match{})
	b := Buffer{
		Taker: &taker,
		Lex:   trie.New("tag", "{", "}"),
	}
	expected := SimpleTaker{
		Match{0, "{", tag.New("tag")},
		Match{1, "}", tag.New("tag")},
		Match{2, "{", tag.New("tag")},
		Match{3, "}", tag.New("tag")},
	}
	n, err := b.Read([]byte("{}{}"))
	if err == nil {
		err = b.Close()
	}
	if err != nil {
		t.Errorf("expected err == nil, got %v", err)
	}
	if n != 4 {
		t.Errorf("expected n == 4, got %d", n)
	}
	if len(taker) != 4 {
		t.Errorf("expected 4 matches, got %d", len(taker))
	}
	for i, m := range taker {
		if m.Index != expected[i].Index || m.Str != expected[i].Str || !tag.Equal(m.Tag, expected[i].Tag) {
			t.Errorf("taker[%d] != %v, got %v", i, expected[i], m)
		}
	}
}
Esempio n. 3
0
func TestParens(t *testing.T) {
	tests := []struct {
		str string
		ok  bool
	}{
		{"{}", true},
		{"{}{}", true},
		{"{{}}", true},
		{"{}}{", false},
		{"{{}}{}", true},
		{"}{", false},
	}
	for _, test := range tests {
		parser := parse.New()
		parens := &Parens{parser}
		parser.States["parens"] = parens
		parser.Push("parens")
		b := lex.Buffer{
			Taker: parser,
			Lex:   trie.New("trie", "{", "}"),
		}
		n, err := b.Read([]byte(test.str))
		if (n == len(test.str) && err == nil) != test.ok {
			if test.ok {
				t.Errorf("Expected no error on %s: %d read, error %s", test.str, n, err)
			} else {
				t.Errorf("Expected error on %s", test.str)
			}
		}
		fmt.Println("Done test!")
	}
}
Esempio n. 4
0
func TestBuffer(t *testing.T) {
	taker := SimpleTaker([]Match{})
	b := Buffer{
		Taker: &taker,
		Lex:   trie.New("tag", "abc", "abcdef", "abcdefgh", "ggg"),
	}
	expected := SimpleTaker{
		Match{0, "abcdef", tag.New("tag")},
		Match{6, "ggg", tag.New("tag")},
	}
	n, err := b.Read([]byte("abcde"))
	if n != 5 {
		t.Errorf("expected n == 5, got %d", n)
	}
	if err != nil {
		t.Errorf("expected err == nil, got %v", err)
	}
	n, err = b.Read([]byte("fggg"))
	if err == nil {
		err = b.Close()
	}
	if n != 4 {
		t.Errorf("expected n == 4, got %d", n)
	}
	if err != nil {
		t.Errorf("expected err == nil, got %v", err)
	}
	if len(taker) != len(expected) {
		t.Errorf("expected len(taker) == %d, got %d", len(expected), len(taker))
	}
	for i, m := range taker {
		if m.Index != expected[i].Index || m.Str != expected[i].Str || !tag.Equal(m.Tag, expected[i].Tag) {
			t.Errorf("taker[%d] != %v, got %v", i, expected[i], m)
		}
	}
	n, err = b.Read([]byte("xyz"))
	if n != 3 {
		t.Errorf("expected n == 3, got %d", n)
	}
	if err == nil {
		t.Error("expected err != nil, got nil")
	}
}
Esempio n. 5
0
func TestStack(t *testing.T) {
	tests := []struct {
		str string
		ok  bool
	}{
		{"()", true},
		{"()()", true},
		{"(())", true},
		{"())(", false},
		{"(())()", true},
		{")(", false},
	}
	for _, test := range tests {
		t.Logf("Running test on %s", test.str)
		p := &parens{}
		s := New(p)
		b := lex.Buffer{
			Taker: s,
			Lex:   trie.New("trie", "(", ")"),
		}
		n, err := b.Read([]byte(test.str))
		if err == nil {
			err = b.Close()
		}
		if test.ok {
			if n != len(test.str) || err != nil {
				t.Fatalf("expected no error on %s: %d read, error %s", test.str, n, err)
			}
			if len(s.Stack) != 1 {
				t.Fatalf("expected stack size one on %s, got %d", test.str, len(s.Stack))
			} else if s.Stack[0].(*parens).String() != test.str {
				t.Fatalf("expected parse as %s, got %s", test.str, s.Stack[0].(*parens).String())
			}
		} else if n == len(test.str) && err == nil {
			t.Fatalf("expected error on input %s", test.str)
		}
	}
}