From 726a451b7a345d5bab0cda498e8b2e47d6f1b5e9 Mon Sep 17 00:00:00 2001 From: gedi Date: Thu, 2 Jun 2016 08:36:27 +0300 Subject: [PATCH] fix utf8 character counting for printers --- fmt_pretty.go | 9 +++++---- suite.go | 12 +++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/fmt_pretty.go b/fmt_pretty.go index 1357b06..ca6b94d 100644 --- a/fmt_pretty.go +++ b/fmt_pretty.go @@ -6,6 +6,7 @@ import ( "regexp" "strings" "time" + "unicode/utf8" "github.com/DATA-DOG/godog/gherkin" ) @@ -377,13 +378,13 @@ func (f *pretty) line(loc *gherkin.Location) string { func (f *pretty) length(node interface{}) int { switch t := node.(type) { case *gherkin.Background: - return f.indent + len(strings.TrimSpace(t.Keyword)+": "+t.Name) + return f.indent + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+": "+t.Name) case *gherkin.Step: - return f.indent*2 + len(strings.TrimSpace(t.Keyword)+" "+t.Text) + return f.indent*2 + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+" "+t.Text) case *gherkin.Scenario: - return f.indent + len(strings.TrimSpace(t.Keyword)+": "+t.Name) + return f.indent + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+": "+t.Name) case *gherkin.ScenarioOutline: - return f.indent + len(strings.TrimSpace(t.Keyword)+": "+t.Name) + return f.indent + utf8.RuneCountInString(strings.TrimSpace(t.Keyword)+": "+t.Name) } panic(fmt.Sprintf("unexpected node %T to determine length", node)) } diff --git a/suite.go b/suite.go index 8aec11e..865031a 100644 --- a/suite.go +++ b/suite.go @@ -6,9 +6,9 @@ import ( "path/filepath" "reflect" "regexp" - "runtime" "strconv" "strings" + "unicode/utf8" "github.com/DATA-DOG/godog/gherkin" ) @@ -372,13 +372,15 @@ func (s *Suite) runScenario(scenario *gherkin.Scenario, b *gherkin.Background) ( func (s *Suite) printStepDefinitions() { var longest int for _, def := range s.steps { - if longest < len(def.Expr.String()) { - longest = len(def.Expr.String()) + n := utf8.RuneCountInString(def.Expr.String()) + if longest < n { + longest = n } } for _, def := range s.steps { - location := runtime.FuncForPC(def.hv.Pointer()).Name() - spaces := strings.Repeat(" ", longest-len(def.Expr.String())) + n := utf8.RuneCountInString(def.Expr.String()) + location := def.funcName() + spaces := strings.Repeat(" ", longest-n) fmt.Println(cl(def.Expr.String(), yellow)+spaces, cl("# "+location, black)) } if len(s.steps) == 0 {