Example #1
0
func TestTrieLex(t *testing.T) {
	trie := New("tag", "{", "}")
	tests := []struct {
		input rune
		match bool
		tag   tag.Tag
		err   bool
	}{
		{'{', true, tag.New("tag"), false},
		{'}', true, tag.New("tag"), false},
	}
	for i, test := range tests {
		trie.Reset()
		trie.Next(test.input)
		if trie.Match() != test.match {
			t.Errorf("match not equal on input %d: expected %v, got %v", i, test.match, trie.Match())
		}
		if !tag.Equal(trie.Tag(), test.tag) {
			t.Errorf("tag not equal on input %d: expected %v, got %v", i, test.tag, trie.Tag())
		}
		if trie.Error() != test.err {
			t.Errorf("error not equal on input %d: expected %v, got %v", i, test.err, trie.Error())
		}
	}
}
Example #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)
		}
	}
}
Example #3
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())
		}
	}
}
Example #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")
	}
}