func TestExpr(t *testing.T) { expect := gspec.Expect(t.FailNow) for _, tc := range []struct { sg fmt.Stringer s string }{ {c(`a`), `[a]`}, {c(`abc`), `[a-c]`}, {c(`[`), `[\[]`}, {c(`]`), `[\]]`}, {c(`-`), `[\-]`}, {b('a', 'c'), `[a-c]`}, {s(`xy`), `xy`}, {c(`b`).Negate(), `[\x00-ac-\U0010ffff]`}, {c(`b`).Negate().Negate(), `[b]`}, {c(`bf`).Negate(), `[\x00-ac-eg-\U0010ffff]`}, {c(`bf`).Negate().Negate(), `[bf]`}, {c("\x00").Negate(), `[\x01-\U0010ffff]`}, {c("\x00").Negate().Negate(), `[\x00]`}, {c("\U0010ffff").Negate(), `[\x00-\U0010fffe]`}, {c("\U0010ffff").Negate().Negate(), `[\U0010ffff]`}, {c(`abcde`).Exclude(c(`bd`)), `[ace]`}, } { expect(tc.sg.String()).Equal(tc.s) } }
func testParse(s gspec.S, P *R, tokens TT, expected string) { expect := gspec.Expect(s.FailNow, 1) scanner := newTestScanner(append(tokens, tok("", EOF))) parser := New(P) for scanner.Scan() { parser.Parse(scanner.Token()) } results := parser.Results() expect(len(results)).Equal(1) expect("\n" + results[0].String()).Equal(gspec.Unindent(expected)) }
func TestCompatible(t *testing.T) { expect := gspec.Expect(t.FailNow) srcList := []string{ "compatible_test.go", } for _, srcFile := range srcList { fset := token.NewFileSet() stdAst, stdErr := std.ParseFile(fset, srcFile, nil, std.ParseComments) fset = token.NewFileSet() gomAst, gomErr := gom.ParseFile(fset, srcFile, nil, gom.ParseComments) expect("parse error", gomErr).Equal(stdErr) //expect("decl count", len(gomAst.Decls)).Equal(len(stdAst.Decls)) //fmt.Println(gomAst.Decls[1].(*ast.FuncDecl).Type.Results) //fmt.Println(stdAst.Decls[1].(*ast.FuncDecl).Type.Results) for i := range stdAst.Decls { expect(astStr(gomAst.Decls[i], fset)).Equal(astStr(stdAst.Decls[i], fset)) } expect(astStr(gomAst, fset)).Equal(astStr(stdAst, fset)) } }
func testMatch(t *testing.T, m Matcher, input string, expectedSize int, expectedMatched bool) { expect := gspec.Expect(t.FailNow, 1) size, matched := m.Match([]byte(input)) expect("matched", matched).Equal(expectedMatched) expect("size of the match", size).Equal(expectedSize) }
package scan import ( "fmt" "strings" "testing" "github.com/hailiang/gspec" // ogdl "github.com/ogdl/flow" ) const EOF = 0 var _ = gspec.Add(func(s gspec.S) { describe, given, it, they, and := gspec.Alias5("describe", "given", "it", "they", "and", s) expect := gspec.Expect(s.FailNow) describe("patterns", func() { given("a pattern", func() { a := Pat("ab") it("can be repeated zero or one time", func() { expect(a.ZeroOrOne().String()).Equal("(?:ab)?") }) it("can be repeated zero or more times", func() { expect(a.ZeroOrMore().String()).Equal("(?:ab)*") }) it("can be repeated one or more times", func() { expect(a.OneOrMore().String()).Equal("(?:ab)+") }) }) given("multiple patterns", func() { a, b, c := Pat("aa"), Pat("bb"), Pat("cc")