示例#1
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!")
	}
}
示例#2
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)
		}
	}
}