diff --git a/gherkin/parse.go b/gherkin/parse.go index 2b502ef..1fbb2a0 100644 --- a/gherkin/parse.go +++ b/gherkin/parse.go @@ -86,7 +86,7 @@ func Parse(path string) (*Feature, error) { // reads tokens into AST and skips comments or new lines func (p *parser) next() *lexer.Token { - if p.ast.tail.value.Type == lexer.EOF { + if p.ast.tail != nil && p.ast.tail.value.Type == lexer.EOF { return p.ast.tail.value // has reached EOF, do not record it more than once } tok := p.lx.Next() @@ -99,9 +99,10 @@ func (p *parser) next() *lexer.Token { // peaks into next token, skips comments or new lines func (p *parser) peek() *lexer.Token { - if tok := p.lx.Peek(); tok.OfType(lexer.COMMENT, lexer.NEW_LINE) { - p.next() + if tok := p.lx.Peek(); !tok.OfType(lexer.COMMENT, lexer.NEW_LINE) { + return tok } + p.next() return p.peek() } diff --git a/gherkin/parse_feature_test.go b/gherkin/parse_feature_test.go new file mode 100644 index 0000000..18d596e --- /dev/null +++ b/gherkin/parse_feature_test.go @@ -0,0 +1,31 @@ +package gherkin + +import ( + "strings" + "testing" + + "github.com/l3pp4rd/go-behat/gherkin/lexer" +) + +var testFeatureSamples = map[string]string{ + "full": `Feature: gherkin parser + in order to run features + as gherkin lexer + I need to be able to parse a feature`, + "only_title": `Feature: gherkin`, +} + +func Test_normal_feature_parsing(t *testing.T) { + p := &parser{ + lx: lexer.New(strings.NewReader(testFeatureSamples["full"])), + path: "some.feature", + ast: newAST(), + } + ft, err := p.parseFeature() + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + if ft.Title != "gherkin parser" { + t.Fatalf("the feature title '%s' was not expected", ft.Title) + } +}