diff --git a/fmt_pretty.go b/fmt_pretty.go index 58bcf3b..6f1ae67 100644 --- a/fmt_pretty.go +++ b/fmt_pretty.go @@ -58,7 +58,7 @@ func (f *pretty) Feature(ft *gherkin.Feature, p string, c []byte) { fmt.Fprintln(f.out, "") } f.features = append(f.features, &feature{Path: p, Feature: ft}) - fmt.Fprintln(f.out, whiteb(ft.Keyword+":")+" "+ft.Name) + fmt.Fprintln(f.out, keywordAndName(ft.Keyword, ft.Name)) if strings.TrimSpace(ft.Description) != "" { for _, line := range strings.Split(ft.Description, "\n") { fmt.Fprintln(f.out, s(f.indent)+strings.TrimSpace(line)) @@ -106,12 +106,21 @@ func (f *pretty) Node(node interface{}) { } } +func keywordAndName(keyword, name string) string { + title := whiteb(keyword + ":") + if len(name) > 0 { + title += " " + name + } + return title +} + func (f *pretty) printUndefinedScenario(sc interface{}) { if f.bgSteps > 0 { - f.commentPos = f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background)) - fmt.Fprintln(f.out, "\n"+s(f.indent)+whiteb(f.feature.Background.Keyword+": "+f.feature.Background.Name)) + bg := f.feature.Background + f.commentPos = f.longestStep(bg.Steps, f.length(bg)) + fmt.Fprintln(f.out, "\n"+s(f.indent)+keywordAndName(bg.Keyword, bg.Name)) - for _, step := range f.feature.Background.Steps { + for _, step := range bg.Steps { f.bgSteps-- f.printStep(step, nil, colors.Cyan) } @@ -120,12 +129,12 @@ func (f *pretty) printUndefinedScenario(sc interface{}) { switch t := sc.(type) { case *gherkin.Scenario: f.commentPos = f.longestStep(t.Steps, f.length(sc)) - text := s(f.indent) + whiteb(t.Keyword+":") + " " + t.Name + text := s(f.indent) + keywordAndName(t.Keyword, t.Name) text += s(f.commentPos-f.length(t)+1) + f.line(t.Location) fmt.Fprintln(f.out, "\n"+text) case *gherkin.ScenarioOutline: f.commentPos = f.longestStep(t.Steps, f.length(sc)) - text := s(f.indent) + whiteb(t.Keyword+":") + " " + t.Name + text := s(f.indent) + keywordAndName(t.Keyword, t.Name) text += s(f.commentPos-f.length(t)+1) + f.line(t.Location) fmt.Fprintln(f.out, "\n"+text) @@ -254,7 +263,7 @@ func (f *pretty) printExampleHeader(example *gherkin.Examples, max []int) { cells := make([]string, len(example.TableHeader.Cells)) // an example table header fmt.Fprintln(f.out, "") - fmt.Fprintln(f.out, s(f.indent*2)+whiteb(example.Keyword+":")+" "+example.Name) + fmt.Fprintln(f.out, s(f.indent*2)+keywordAndName(example.Keyword, example.Name)) for i, cell := range example.TableHeader.Cells { val := cyan(cell.Value) @@ -313,13 +322,14 @@ func (f *pretty) printStepKind(res *stepResult) { f.outlineSteps = append(f.outlineSteps, res) } var bgStep bool + bg := f.feature.Background // if has not printed background yet switch { // first background step - case f.bgSteps > 0 && f.bgSteps == len(f.feature.Background.Steps): - f.commentPos = f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background)) - fmt.Fprintln(f.out, "\n"+s(f.indent)+whiteb(f.feature.Background.Keyword+": "+f.feature.Background.Name)) + case f.bgSteps > 0 && f.bgSteps == len(bg.Steps): + f.commentPos = f.longestStep(bg.Steps, f.length(bg)) + fmt.Fprintln(f.out, "\n"+s(f.indent)+keywordAndName(bg.Keyword, bg.Name)) f.bgSteps-- bgStep = true // subsequent background steps @@ -331,12 +341,12 @@ func (f *pretty) printStepKind(res *stepResult) { // print scenario keyword and value if first example if !f.scenarioKeyword { f.commentPos = f.longestStep(f.scenario.Steps, f.length(f.scenario)) - if f.feature.Background != nil { - if bgLen := f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background)); bgLen > f.commentPos { + if bg != nil { + if bgLen := f.longestStep(bg.Steps, f.length(bg)); bgLen > f.commentPos { f.commentPos = bgLen } } - text := s(f.indent) + whiteb(f.scenario.Keyword+":") + " " + f.scenario.Name + text := s(f.indent) + keywordAndName(f.scenario.Keyword, f.scenario.Name) text += s(f.commentPos-f.length(f.scenario)+1) + f.line(f.scenario.Location) fmt.Fprintln(f.out, "\n"+text) f.scenarioKeyword = true @@ -346,12 +356,12 @@ func (f *pretty) printStepKind(res *stepResult) { // print scenario keyword and value if first example if !f.scenarioKeyword { f.commentPos = f.longestStep(f.outline.Steps, f.length(f.outline)) - if f.feature.Background != nil { - if bgLen := f.longestStep(f.feature.Background.Steps, f.length(f.feature.Background)); bgLen > f.commentPos { + if bg != nil { + if bgLen := f.longestStep(bg.Steps, f.length(bg)); bgLen > f.commentPos { f.commentPos = bgLen } } - text := s(f.indent) + whiteb(f.outline.Keyword+": ") + f.outline.Name + text := s(f.indent) + keywordAndName(f.outline.Keyword, f.outline.Name) text += s(f.commentPos-f.length(f.outline)+1) + f.line(f.outline.Location) fmt.Fprintln(f.out, "\n"+text) f.scenarioKeyword = true diff --git a/formatter-tests/features/scenario_with_background.feature b/formatter-tests/features/scenario_with_background.feature new file mode 100644 index 0000000..5c04899 --- /dev/null +++ b/formatter-tests/features/scenario_with_background.feature @@ -0,0 +1,9 @@ +Feature: single scenario with background + + Background: named + Given passing step + And passing step + + Scenario: scenario + When passing step + Then passing step diff --git a/formatter-tests/features/scenario_without_steps_with_background.feature b/formatter-tests/features/scenario_without_steps_with_background.feature new file mode 100644 index 0000000..98cd6be --- /dev/null +++ b/formatter-tests/features/scenario_without_steps_with_background.feature @@ -0,0 +1,6 @@ +Feature: empty feature + + Background: + Given passing step + + Scenario: without steps diff --git a/formatter-tests/features/two_scenarios_with_background_fail.feature b/formatter-tests/features/two_scenarios_with_background_fail.feature new file mode 100644 index 0000000..8ce8e3d --- /dev/null +++ b/formatter-tests/features/two_scenarios_with_background_fail.feature @@ -0,0 +1,12 @@ +Feature: two scenarios with background fail + + Background: + Given passing step + And failing step + + Scenario: one + When passing step + Then passing step + + Scenario: two + Then passing step diff --git a/formatter-tests/pretty/scenario_with_background b/formatter-tests/pretty/scenario_with_background new file mode 100644 index 0000000..d9f05c9 --- /dev/null +++ b/formatter-tests/pretty/scenario_with_background @@ -0,0 +1,13 @@ +Feature: single scenario with background + + Background: named + Given passing step # formatters_print_test.go:64 -> passingStepDef + And passing step # formatters_print_test.go:64 -> passingStepDef + + Scenario: scenario # formatter-tests/features/scenario_with_background.feature:7 + When passing step # formatters_print_test.go:64 -> passingStepDef + Then passing step # formatters_print_test.go:64 -> passingStepDef + +1 scenarios (1 passed) +4 steps (4 passed) +0s diff --git a/formatter-tests/pretty/scenario_without_steps_with_background b/formatter-tests/pretty/scenario_without_steps_with_background new file mode 100644 index 0000000..2ab684f --- /dev/null +++ b/formatter-tests/pretty/scenario_without_steps_with_background @@ -0,0 +1,10 @@ +Feature: empty feature + + Background: + Given passing step + + Scenario: without steps # formatter-tests/features/scenario_without_steps_with_background.feature:6 + +1 scenarios (1 undefined) +No steps +0s diff --git a/formatter-tests/pretty/two_scenarios_with_background_fail b/formatter-tests/pretty/two_scenarios_with_background_fail new file mode 100644 index 0000000..3a4f89f --- /dev/null +++ b/formatter-tests/pretty/two_scenarios_with_background_fail @@ -0,0 +1,29 @@ +Feature: two scenarios with background fail + + Background: + Given passing step # formatters_print_test.go:64 -> passingStepDef + And failing step # formatters_print_test.go:80 -> failingStepDef + step failed + + Scenario: one # formatter-tests/features/two_scenarios_with_background_fail.feature:7 + When passing step # formatters_print_test.go:64 -> passingStepDef + Then passing step # formatters_print_test.go:64 -> passingStepDef + + Scenario: two # formatter-tests/features/two_scenarios_with_background_fail.feature:11 + step failed + Then passing step # formatters_print_test.go:64 -> passingStepDef + +--- Failed steps: + + Scenario: one # formatter-tests/features/two_scenarios_with_background_fail.feature:7 + And failing step # formatter-tests/features/two_scenarios_with_background_fail.feature:5 + Error: step failed + + Scenario: two # formatter-tests/features/two_scenarios_with_background_fail.feature:11 + And failing step # formatter-tests/features/two_scenarios_with_background_fail.feature:5 + Error: step failed + + +2 scenarios (2 failed) +7 steps (2 passed, 2 failed, 3 skipped) +0s