From fe2a967670da2eb07601a7fe54df7c6d3f4bea23 Mon Sep 17 00:00:00 2001 From: tfreville Date: Mon, 9 Mar 2020 14:55:30 +0100 Subject: [PATCH] Clean change on pretty. Make test on new TC meaningfull again. --- features/formatter/pretty.feature | 21 +++++++++--- fixtures/cucumber_output.json | 22 +++---------- fixtures/progress_output.txt | 4 +-- fmt_pretty.go | 20 +++++------- suite_context.go | 53 +++++++++++++++---------------- utils.go | 2 +- 6 files changed, 58 insertions(+), 64 deletions(-) diff --git a/features/formatter/pretty.feature b/features/formatter/pretty.feature index 8492c2d..1de2977 100644 --- a/features/formatter/pretty.feature +++ b/features/formatter/pretty.feature @@ -300,21 +300,32 @@ Feature: pretty formatter Scenario: test scenario Given Ignore I save some value X under key Y + And I allow variable injection When Ignore I use value {{Y}} Then Ignore Godog rendering should not break + And Ignore test + | key | val | + | 1 | 2 | + | 3 | 4 | + And I disable variable injection """ - And I allow variable injection When I run feature suite with formatter "pretty" Then the rendered output will be as follows: """ Feature: inject long value - Scenario: test scenario # features/inject.feature:3 - Given Ignore I save some value X under key Y # suite_context.go:0 -> SuiteContext.func7 + Scenario: test scenario # features/inject.feature:3 + Given Ignore I save some value X under key Y # suite_context.go:0 -> SuiteContext.func7 + And I allow variable injection # suite_context.go:0 -> *suiteContext When Ignore I use value someverylonginjectionsoweacanbesureitsurpasstheinitiallongeststeplenghtanditwillhelptestsmethodsafety # suite_context.go:0 -> SuiteContext.func7 - Then Ignore Godog rendering should not break # suite_context.go:0 -> SuiteContext.func7 + Then Ignore Godog rendering should not break # suite_context.go:0 -> SuiteContext.func7 + And Ignore test # suite_context.go:0 -> SuiteContext.func7 + | key | val | + | 1 | 2 | + | 3 | 4 | + And I disable variable injection # suite_context.go:0 -> *suiteContext 1 scenarios (1 passed) - 3 steps (3 passed) + 6 steps (6 passed) 0s """ \ No newline at end of file diff --git a/fixtures/cucumber_output.json b/fixtures/cucumber_output.json index 959ed18..16ca3fb 100644 --- a/fixtures/cucumber_output.json +++ b/fixtures/cucumber_output.json @@ -2627,7 +2627,7 @@ "name": "a feature \"features/inject.feature\" file:", "line": 297, "doc_string": { - "value": " Feature: inject long value\n\n Scenario: test scenario\n Given Ignore I save some value X under key Y\n When Ignore I use value someverylonginjectionsoweacanbesureitsurpasstheinitiallongeststeplenghtanditwillhelptestsmethodsafety\n Then Ignore Godog rendering should not break", + "value": " Feature: inject long value\n\n Scenario: test scenario\n Given Ignore I save some value X under key Y\n And I allow variable injection\n When Ignore I use value {{Y}}\n Then Ignore Godog rendering should not break\n And Ignore test\n | key | val |\n | 1 | 2 |\n | 3 | 4 |\n And I disable variable injection", "content_type": "", "line": 298 }, @@ -2639,22 +2639,10 @@ "duration": 0 } }, - { - "keyword": "And ", - "name": "I allow variable injection", - "line": 306, - "match": { - "location": "suite_context.go:0" - }, - "result": { - "status": "passed", - "duration": 0 - } - }, { "keyword": "When ", "name": "I run feature suite with formatter \"pretty\"", - "line": 307, + "line": 312, "match": { "location": "suite_context.go:0" }, @@ -2666,11 +2654,11 @@ { "keyword": "Then ", "name": "the rendered output will be as follows:", - "line": 308, + "line": 313, "doc_string": { - "value": " Feature: inject long value\n\n Scenario: test scenario # features/inject.feature:3\n Given Ignore I save some value X under key Y # suite_context.go:0 -\u003e SuiteContext.func7\n When Ignore I use value someverylonginjectionsoweacanbesureitsurpasstheinitiallongeststeplenghtanditwillhelptestsmethodsafety # suite_context.go:0 -\u003e SuiteContext.func7\n Then Ignore Godog rendering should not break # suite_context.go:0 -\u003e SuiteContext.func7\n\n 1 scenarios (1 passed)\n 3 steps (3 passed)\n 0s", + "value": " Feature: inject long value\n\n Scenario: test scenario # features/inject.feature:3\n Given Ignore I save some value X under key Y # suite_context.go:0 -\u003e SuiteContext.func7\n And I allow variable injection # suite_context.go:0 -\u003e *suiteContext\n When Ignore I use value someverylonginjectionsoweacanbesureitsurpasstheinitiallongeststeplenghtanditwillhelptestsmethodsafety # suite_context.go:0 -\u003e SuiteContext.func7\n Then Ignore Godog rendering should not break # suite_context.go:0 -\u003e SuiteContext.func7\n And Ignore test # suite_context.go:0 -\u003e SuiteContext.func7\n | key | val |\n | 1 | 2 |\n | 3 | 4 |\n And I disable variable injection # suite_context.go:0 -\u003e *suiteContext\n\n 1 scenarios (1 passed)\n 6 steps (6 passed)\n 0s", "content_type": "", - "line": 309 + "line": 314 }, "match": { "location": "suite_context.go:0" diff --git a/fixtures/progress_output.txt b/fixtures/progress_output.txt index 4a98d75..1c45625 100644 --- a/fixtures/progress_output.txt +++ b/fixtures/progress_output.txt @@ -2,9 +2,9 @@ ...................................................................... 140 ...................................................................... 210 ...................................................................... 280 -........................... 307 +.......................... 306 79 scenarios (79 passed) -307 steps (307 passed) +306 steps (306 passed) 0s \ No newline at end of file diff --git a/fmt_pretty.go b/fmt_pretty.go index 8be257c..bef7db0 100644 --- a/fmt_pretty.go +++ b/fmt_pretty.go @@ -124,13 +124,12 @@ func (f *pretty) printUndefinedScenario(sc interface{}) { case *gherkin.Scenario: f.commentPos = f.longestStep(t.Steps, f.length(sc)) text := s(f.indent) + keywordAndName(t.Keyword, t.Name) - text += s(f.commentPos-f.length(t)) + f.line(t.Location) + 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) + keywordAndName(t.Keyword, t.Name) - text += s(f.commentPos-f.length(t)) + f.line(t.Location) - + text += s(f.commentPos-f.length(t)+1) + f.line(t.Location) fmt.Fprintln(f.out, "\n"+text) for _, example := range t.Examples { @@ -249,9 +248,9 @@ func (f *pretty) printExampleRow(row *gherkin.TableRow, max []int, clr colors.Co for i, cell := range row.Cells { val := clr(cell.Value) ln := utf8.RuneCountInString(val) - cells[i] = val + s(max[i]-ln+1) + cells[i] = val + s(max[i]-ln) } - fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cells, "| ")+"|") + fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cells, " | ")+" |") } func (f *pretty) printExampleHeader(example *gherkin.Examples, max []int) { @@ -263,9 +262,9 @@ func (f *pretty) printExampleHeader(example *gherkin.Examples, max []int) { for i, cell := range example.TableHeader.Cells { val := cyan(cell.Value) ln := utf8.RuneCountInString(val) - cells[i] = val + s(max[i]-ln+1) + cells[i] = val + s(max[i]-ln) } - fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cells, "| ")+"|") + fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cells, " | ")+" |") } func (f *pretty) printStep(step *gherkin.Step, def *StepDef, c colors.ColorFunc) { @@ -290,7 +289,6 @@ func (f *pretty) printStep(step *gherkin.Step, def *StepDef, c colors.ColorFunc) text += c(step.Text) } text += s(f.commentPos-f.length(step)+1) + blackb(fmt.Sprintf("# %s", def.definitionID())) - default: text += c(step.Text) } @@ -344,7 +342,6 @@ func (f *pretty) printStepKind(res *stepResult) { } 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 } @@ -360,7 +357,6 @@ func (f *pretty) printStepKind(res *stepResult) { } 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 } @@ -404,9 +400,9 @@ func (f *pretty) printTable(t *gherkin.DataTable, c colors.ColorFunc) { for i, cell := range row.Cells { val := c(cell.Value) ln := utf8.RuneCountInString(val) - cols[i] = val + s(l[i]-ln+1) + cols[i] = val + s(l[i]-ln) } - fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cols, "| ")+"|") + fmt.Fprintln(f.out, s(f.indent*3)+"| "+strings.Join(cols, " | ")+" |") } } diff --git a/suite_context.go b/suite_context.go index c0c1e60..5f2115f 100644 --- a/suite_context.go +++ b/suite_context.go @@ -16,10 +16,6 @@ import ( "github.com/cucumber/godog/gherkin" ) -var ( - allowInjection = true -) - // SuiteContext provides steps for godog suite execution and // can be used for meta-testing of godog features/steps themselves. // @@ -107,25 +103,27 @@ func SuiteContext(s *Suite, additionalContextInitializers ...func(suite *Suite)) return nil }) - s.BeforeStep(func(step *gherkin.Step) { - if !allowInjection { - return - } - step.Text = injectAll(step.Text) - args := step.Argument - if args != nil { - switch arg := args.(type) { - case *gherkin.DataTable: - for i := 0; i < len(arg.Rows); i++ { - for n, cell := range arg.Rows[i].Cells { - arg.Rows[i].Cells[n].Value = injectAll(cell.Value) - } + s.BeforeStep(c.inject) +} +func (s *suiteContext) inject(step *gherkin.Step) { + if !s.allowInjection { + return + } + + step.Text = injectAll(step.Text) + args := step.Argument + if args != nil { + switch arg := args.(type) { + case *gherkin.DataTable: + for i := 0; i < len(arg.Rows); i++ { + for n, cell := range arg.Rows[i].Cells { + arg.Rows[i].Cells[n].Value = injectAll(cell.Value) } - case *gherkin.DocString: - arg.Content = injectAll(arg.Content) } + case *gherkin.DocString: + arg.Content = injectAll(arg.Content) } - }) + } } func injectAll(inTo string) string { @@ -148,11 +146,12 @@ type firedEvent struct { } type suiteContext struct { - paths []string - testedSuite *Suite - extraCIs []func(suite *Suite) - events []*firedEvent - out bytes.Buffer + paths []string + testedSuite *Suite + extraCIs []func(suite *Suite) + events []*firedEvent + out bytes.Buffer + allowInjection bool } func (s *suiteContext) ResetBeforeEachScenario(interface{}) { @@ -164,10 +163,11 @@ func (s *suiteContext) ResetBeforeEachScenario(interface{}) { SuiteContext(s.testedSuite, s.extraCIs...) // reset all fired events s.events = []*firedEvent{} + s.allowInjection = false } func (s *suiteContext) iSetVariableInjectionTo(to string) error { - allowInjection = to == "allow" + s.allowInjection = to == "allow" return nil } @@ -189,7 +189,6 @@ func (s *suiteContext) iRunFeatureSuiteWithFormatter(name string) error { if f == nil { return fmt.Errorf(`formatter "%s" is not available`, name) } - s.testedSuite.fmt = f("godog", colors.Uncolored(&s.out)) if err := s.parseFeatures(); err != nil { return err diff --git a/utils.go b/utils.go index 11d082e..ac63a55 100644 --- a/utils.go +++ b/utils.go @@ -20,7 +20,7 @@ var ( // repeats a space n times func s(n int) string { - if n <= 0 { + if n < 0 { n = 1 } return strings.Repeat(" ", n)