From d5755df70c274d8a9b20cf3a20b5da4b44fb54ae Mon Sep 17 00:00:00 2001 From: gedi Date: Tue, 23 Jun 2015 15:27:22 +0300 Subject: [PATCH] ensure source formatting with golint, go vet and go fmt --- .travis.yml | 14 ++++++++++-- fmt_pretty.go | 6 ++--- fmt_progress.go | 2 +- gherkin/feature_test.go | 4 ++-- gherkin/gherkin.go | 10 ++++----- gherkin/lexer.go | 48 ++++++++++++++++++++-------------------- gherkin/lexer_test.go | 20 ++++++++--------- gherkin/parse_test.go | 24 ++++++++++---------- gherkin/scenario_test.go | 14 ++++++------ gherkin/steps_test.go | 8 +++---- gherkin/token.go | 8 ++++--- suite.go | 19 ++++++++-------- suite_test.go | 2 +- 13 files changed, 96 insertions(+), 83 deletions(-) diff --git a/.travis.yml b/.travis.yml index db1cad3..e0af103 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,13 +6,23 @@ go: - tip script: - # pull all dependencies manually to see how many we have + # linters and vet + - go get golang.org/x/tools/cmd/vet + - go get github.com/golang/lint/golint + + # pull all external dependencies # remove them at all if possible - go get golang.org/x/tools/imports + - go get github.com/shiena/ansicolor # run standard go tests - go test -v ./... - go test -race ./... # test me with myself - - go run cmd/godog/main.go + - go run cmd/godog/main.go -f progress + + # code correctness + - sh -c 'if [ ! -z "$(go fmt ./...)" ]; then exit 1; fi' + - golint ./... + - go vet ./... diff --git a/fmt_pretty.go b/fmt_pretty.go index cc5c847..59791b4 100644 --- a/fmt_pretty.go +++ b/fmt_pretty.go @@ -18,7 +18,7 @@ func init() { }) } -var outlinePlaceholderRegexp *regexp.Regexp = regexp.MustCompile("<[^>]+>") +var outlinePlaceholderRegexp = regexp.MustCompile("<[^>]+>") // a built in default pretty formatter type pretty struct { @@ -306,7 +306,7 @@ func (f *pretty) printStepKind(stepAction interface{}) { case step.Background != nil && f.backgroundSteps == 0: return case step.Background != nil && f.backgroundSteps > 0: - f.backgroundSteps -= 1 + f.backgroundSteps-- } if f.outlineExamples != 0 { @@ -314,7 +314,7 @@ func (f *pretty) printStepKind(stepAction interface{}) { if len(f.outlineSteps) == f.outlineNumSteps { // an outline example steps has went through f.printOutlineExample(step.Scenario) - f.outlineExamples -= 1 + f.outlineExamples-- } return // wait till example steps } diff --git a/fmt_progress.go b/fmt_progress.go index 8d503af..bdd51da 100644 --- a/fmt_progress.go +++ b/fmt_progress.go @@ -109,7 +109,7 @@ func (f *progress) step(step interface{}) { case *undefined: fmt.Print(cl("U", yellow)) } - f.steps += 1 + f.steps++ if math.Mod(float64(f.steps), float64(f.stepsPerRow)) == 0 { fmt.Printf(" %d\n", f.steps) } diff --git a/gherkin/feature_test.go b/gherkin/feature_test.go index 8e782d1..83d4586 100644 --- a/gherkin/feature_test.go +++ b/gherkin/feature_test.go @@ -117,8 +117,8 @@ func Test_parse_feature_with_newlines(t *testing.T) { } p.assertMatchesTypes([]TokenType{ - NEW_LINE, - NEW_LINE, + NEWLINE, + NEWLINE, FEATURE, }, t) } diff --git a/gherkin/gherkin.go b/gherkin/gherkin.go index 3ce4ff8..a92f626 100644 --- a/gherkin/gherkin.go +++ b/gherkin/gherkin.go @@ -227,7 +227,7 @@ func (p *parser) peek() *Token { return p.peeked } - for p.peeked = p.lx.read(); p.peeked.OfType(COMMENT, NEW_LINE); p.peeked = p.lx.read() { + for p.peeked = p.lx.read(); p.peeked.OfType(COMMENT, NEWLINE); p.peeked = p.lx.read() { p.ast = append(p.ast, p.peeked) // record comments and newlines } @@ -292,7 +292,7 @@ func (p *parser) parseFeature() (ft *Feature, err error) { } // there must be a scenario or scenario outline otherwise - if !tok.OfType(SCENARIO, SCENARIO_OUTLINE) { + if !tok.OfType(SCENARIO, OUTLINE) { if tok.Type == EOF { return ft, nil // there may not be a scenario defined after background } @@ -324,7 +324,7 @@ func (p *parser) parseScenario() (s *Scenario, err error) { if examples := p.peek(); examples.Type == EXAMPLES { p.next() // jump over the peeked token peek := p.peek() - if peek.Type != TABLE_ROW { + if peek.Type != TABLEROW { return s, p.err(strings.Join([]string{ "expected a table row,", "but got '" + peek.Type.String() + "' instead, for scenario outline examples", @@ -359,7 +359,7 @@ func (p *parser) parseSteps() (steps []*Step, err error) { return steps, err } step.PyString.Step = step - case TABLE_ROW: + case TABLEROW: if step.Table, err = p.parseTable(); err != nil { return steps, err } @@ -394,7 +394,7 @@ func (p *parser) parsePystring() (*PyString, error) { func (p *parser) parseTable() (*Table, error) { tbl := &Table{Token: p.peek()} - for row := p.peek(); row.Type == TABLE_ROW; row = p.peek() { + for row := p.peek(); row.Type == TABLEROW; row = p.peek() { var cols []string for _, r := range strings.Split(strings.Trim(row.Value, "|"), "|") { cols = append(cols, strings.TrimFunc(r, unicode.IsSpace)) diff --git a/gherkin/lexer.go b/gherkin/lexer.go index 26c0acf..d44f880 100644 --- a/gherkin/lexer.go +++ b/gherkin/lexer.go @@ -24,25 +24,25 @@ var matchers = map[string]*regexp.Regexp{ // for now only english language is supported var keywords = map[TokenType]string{ // special - ILLEGAL: "Illegal", - EOF: "End of file", - NEW_LINE: "New line", - TAGS: "Tags", - COMMENT: "Comment", - PYSTRING: "PyString", - TABLE_ROW: "Table row", - TEXT: "Text", + ILLEGAL: "Illegal", + EOF: "End of file", + NEWLINE: "New line", + TAGS: "Tags", + COMMENT: "Comment", + PYSTRING: "PyString", + TABLEROW: "Table row", + TEXT: "Text", // general - GIVEN: "Given", - WHEN: "When", - THEN: "Then", - AND: "And", - BUT: "But", - FEATURE: "Feature", - BACKGROUND: "Background", - SCENARIO: "Scenario", - SCENARIO_OUTLINE: "Scenario Outline", - EXAMPLES: "Examples", + GIVEN: "Given", + WHEN: "When", + THEN: "Then", + AND: "And", + BUT: "But", + FEATURE: "Feature", + BACKGROUND: "Background", + SCENARIO: "Scenario", + OUTLINE: "Scenario Outline", + EXAMPLES: "Examples", } type lexer struct { @@ -70,9 +70,9 @@ func (l *lexer) read() *Token { // newline if len(line) == 0 { return &Token{ - Type: NEW_LINE, + Type: NEWLINE, Line: l.lines, - Keyword: keywords[NEW_LINE], + Keyword: keywords[NEWLINE], } } // comment @@ -172,25 +172,25 @@ func (l *lexer) read() *Token { // table row if m := matchers["table_row"].FindStringSubmatch(line); len(m) > 0 { return &Token{ - Type: TABLE_ROW, + Type: TABLEROW, Indent: len(m[1]), Line: l.lines, Value: strings.TrimSpace(m[2]), Text: line, Comment: strings.Trim(m[3], " #"), - Keyword: keywords[TABLE_ROW], + Keyword: keywords[TABLEROW], } } // scenario outline if m := matchers["scenario_outline"].FindStringSubmatch(line); len(m) > 0 { return &Token{ - Type: SCENARIO_OUTLINE, + Type: OUTLINE, Indent: len(m[1]), Line: l.lines, Value: strings.TrimSpace(m[2]), Text: line, Comment: strings.Trim(m[3], " #"), - Keyword: keywords[SCENARIO_OUTLINE], + Keyword: keywords[OUTLINE], } } // examples diff --git a/gherkin/lexer_test.go b/gherkin/lexer_test.go index e207abb..31ab79d 100644 --- a/gherkin/lexer_test.go +++ b/gherkin/lexer_test.go @@ -133,11 +133,11 @@ func Test_minimal_feature(t *testing.T) { TEXT, TEXT, TEXT, - NEW_LINE, + NEWLINE, BACKGROUND, GIVEN, - NEW_LINE, + NEWLINE, COMMENT, SCENARIO, @@ -171,9 +171,9 @@ func Test_table_row_reading(t *testing.T) { expectedTypes := []TokenType{ BACKGROUND, GIVEN, - TABLE_ROW, - TABLE_ROW, - TABLE_ROW, + TABLEROW, + TABLEROW, + TABLEROW, GIVEN, } expectedIndents := []int{2, 4, 6, 6, 6, 4} @@ -200,18 +200,18 @@ func Test_lexing_of_scenario_outline(t *testing.T) { tokens = append(tokens, tok.Type) } expected := []TokenType{ - SCENARIO_OUTLINE, + OUTLINE, GIVEN, AND, AND, WHEN, THEN, - NEW_LINE, + NEWLINE, EXAMPLES, - TABLE_ROW, - TABLE_ROW, - TABLE_ROW, + TABLEROW, + TABLEROW, + TABLEROW, } for i := 0; i < len(expected); i++ { if expected[i] != tokens[i] { diff --git a/gherkin/parse_test.go b/gherkin/parse_test.go index b7e7781..9f72a42 100644 --- a/gherkin/parse_test.go +++ b/gherkin/parse_test.go @@ -8,7 +8,7 @@ import ( func (a *parser) assertMatchesTypes(expected []TokenType, t *testing.T) { key := -1 for _, tok := range a.ast { - key += 1 + key++ if len(expected) <= key { t.Fatalf("there are more tokens in AST then expected, next is '%s'", tok.Type) } @@ -72,19 +72,19 @@ func Test_parse_feature_file(t *testing.T) { TEXT, TEXT, TEXT, - NEW_LINE, + NEWLINE, BACKGROUND, GIVEN, - TABLE_ROW, - NEW_LINE, + TABLEROW, + NEWLINE, SCENARIO, GIVEN, AND, WHEN, THEN, - NEW_LINE, + NEWLINE, TAGS, SCENARIO, @@ -92,23 +92,23 @@ func Test_parse_feature_file(t *testing.T) { AND, WHEN, THEN, - NEW_LINE, + NEWLINE, TAGS, SCENARIO, - NEW_LINE, + NEWLINE, - SCENARIO_OUTLINE, + OUTLINE, GIVEN, AND, AND, WHEN, THEN, - NEW_LINE, + NEWLINE, EXAMPLES, - TABLE_ROW, - TABLE_ROW, - TABLE_ROW, + TABLEROW, + TABLEROW, + TABLEROW, }, t) ft.assertHasNumScenarios(4, t) diff --git a/gherkin/scenario_test.go b/gherkin/scenario_test.go index 70b2b34..3b6a119 100644 --- a/gherkin/scenario_test.go +++ b/gherkin/scenario_test.go @@ -17,7 +17,7 @@ func (s *Scenario) assertOutlineStep(text string, t *testing.T) *Step { return stp } } - t.Fatal("expected scenario '%s' to have step: '%s', but it did not", s.Title, text) + t.Fatalf("expected scenario '%s' to have step: '%s', but it did not", s.Title, text) return nil } @@ -27,7 +27,7 @@ func (s *Scenario) assertStep(text string, t *testing.T) *Step { return stp } } - t.Fatal("expected scenario '%s' to have step: '%s', but it did not", s.Title, text) + t.Fatalf("expected scenario '%s' to have step: '%s', but it did not", s.Title, text) return nil } @@ -61,17 +61,17 @@ func Test_parse_scenario_outline(t *testing.T) { s.assertTitle("ls supports kinds of options", t) p.assertMatchesTypes([]TokenType{ - SCENARIO_OUTLINE, + OUTLINE, GIVEN, AND, AND, WHEN, THEN, - NEW_LINE, + NEWLINE, EXAMPLES, - TABLE_ROW, - TABLE_ROW, - TABLE_ROW, + TABLEROW, + TABLEROW, + TABLEROW, }, t) s.assertOutlineStep(`I am in a directory "test"`, t) diff --git a/gherkin/steps_test.go b/gherkin/steps_test.go index 0d7e360..6bf4cbc 100644 --- a/gherkin/steps_test.go +++ b/gherkin/steps_test.go @@ -146,7 +146,7 @@ func Test_parse_hash_table_given_step(t *testing.T) { p.next() // step over to eof p.assertMatchesTypes([]TokenType{ GIVEN, - TABLE_ROW, + TABLEROW, EOF, }, t) } @@ -172,9 +172,9 @@ func Test_parse_table_given_step(t *testing.T) { p.next() // step over to eof p.assertMatchesTypes([]TokenType{ GIVEN, - TABLE_ROW, - TABLE_ROW, - TABLE_ROW, + TABLEROW, + TABLEROW, + TABLEROW, EOF, }, t) } diff --git a/gherkin/token.go b/gherkin/token.go index da21b22..d1be1d8 100644 --- a/gherkin/token.go +++ b/gherkin/token.go @@ -5,21 +5,23 @@ import ( "unicode" ) +// TokenType defines a gherkin token type type TokenType int +// TokenType constants const ( ILLEGAL TokenType = iota COMMENT - NEW_LINE + NEWLINE EOF TEXT TAGS - TABLE_ROW + TABLEROW PYSTRING FEATURE BACKGROUND SCENARIO - SCENARIO_OUTLINE + OUTLINE EXAMPLES GIVEN WHEN diff --git a/suite.go b/suite.go index ea55eb8..8477ffd 100644 --- a/suite.go +++ b/suite.go @@ -58,6 +58,7 @@ type StepDef struct { // executions are catching panic error since it may // be a context specific error. type Suite interface { + Run() Step(expr Regexp, h StepHandler) // suite events BeforeSuite(f func()) @@ -84,10 +85,9 @@ type suite struct { afterSuiteHandlers []func() } -// New initializes a suite which supports the Suite -// interface. The instance is passed around to all -// context initialization functions from *_test.go files -func New() *suite { +// New initializes a Suite. The instance is passed around +// to all context initialization functions from *_test.go files +func New() Suite { return &suite{} } @@ -378,18 +378,19 @@ func (s *suite) runScenario(scenario *gherkin.Scenario) (err error) { return } -func (st *suite) printStepDefinitions() { +func (s *suite) printStepDefinitions() { var longest int - for _, def := range st.stepHandlers { + for _, def := range s.stepHandlers { if longest < len(def.Expr.String()) { longest = len(def.Expr.String()) } } - for _, def := range st.stepHandlers { + for _, def := range s.stepHandlers { location := runtime.FuncForPC(reflect.ValueOf(def.Handler).Pointer()).Name() - fmt.Println(cl(def.Expr.String(), yellow)+s(longest-len(def.Expr.String())), cl("# "+location, black)) + spaces := strings.Repeat(" ", longest-len(def.Expr.String())) + fmt.Println(cl(def.Expr.String(), yellow)+spaces, cl("# "+location, black)) } - if len(st.stepHandlers) == 0 { + if len(s.stepHandlers) == 0 { fmt.Println("there were no contexts registered, could not find any step definition..") } } diff --git a/suite_test.go b/suite_test.go index 54e2e56..18e3194 100644 --- a/suite_test.go +++ b/suite_test.go @@ -54,7 +54,7 @@ func (s *suiteContext) HandleBeforeScenario(*gherkin.Scenario) { } func (s *suiteContext) followingStepsShouldHave(args ...*Arg) error { - var expected []string = args[1].PyString().Lines + var expected = args[1].PyString().Lines var actual, unmatched []string var matched []int