diff options
| author | Shulhan <ms@kilabit.info> | 2023-03-02 22:47:29 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-03-02 23:06:28 +0700 |
| commit | cdd878b72c675a7f1c13e0eb005e610d7b7269dc (patch) | |
| tree | 2af6f666a3a615d352244c82812f69e7237ff269 /lib/test | |
| parent | 4efba34f57ebc90b5a216ad9b104e25e7e812274 (diff) | |
| download | pakakeh.go-cdd878b72c675a7f1c13e0eb005e610d7b7269dc.tar.xz | |
lib/test: simplify the string diff output from Assert
In the output, instead of using %q we replace it with %s, because printing
string with double quote cause escaping and hard to read
This change may cause difference in white spaces not showed in the
terminal.
In the diff changes, only print the Old and New, without printing each
chunk.
Diffstat (limited to 'lib/test')
| -rw-r--r-- | lib/test/example_test.go | 11 | ||||
| -rw-r--r-- | lib/test/test.go | 38 |
2 files changed, 37 insertions, 12 deletions
diff --git a/lib/test/example_test.go b/lib/test/example_test.go index 16870be3..54055f02 100644 --- a/lib/test/example_test.go +++ b/lib/test/example_test.go @@ -92,15 +92,10 @@ func ExampleAssert_string() { fmt.Println(tw.String()) // Output: // !!! Assert: expecting string(a string), got string(b string) - // !!! strings not matched: + // !!! : // --++ - // 0 - "func (tw *testWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) }" - // 0 + "func (tw *testWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }" - // ^27 - "(" - // ^41 - " " - // ^27 + "f(format string, " - // ^69 + "f" - // ^56 + ", format" + // 0 - func (tw *testWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) } + // 0 + func (tw *testWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) } } func ExampleLoadDataDir() { diff --git a/lib/test/test.go b/lib/test/test.go index a52c66ac..d82c246f 100644 --- a/lib/test/test.go +++ b/lib/test/test.go @@ -6,9 +6,12 @@ package test import ( + "bytes" + "fmt" "runtime" "github.com/shuLhan/share/lib/reflect" + "github.com/shuLhan/share/lib/text" "github.com/shuLhan/share/lib/text/diff" ) @@ -144,11 +147,38 @@ func printStringDiff(w Writer, name string, exp, got interface{}) bool { return true } - if len(name) == 0 { - w.Fatal("!!! strings not matched:\n", diffData.String()) - } else { - w.Fatalf("!!! %s:\n%s", name, diffData.String()) + var ( + bb bytes.Buffer + line text.Line + ) + + fmt.Fprintf(&bb, "!!! %s:\n", name) + + if len(diffData.Dels) > 0 { + bb.WriteString("---- EXPECTED\n") + for _, line = range diffData.Dels { + fmt.Fprintf(&bb, "%d - %s\n", line.N, line.V) + } } + if len(diffData.Adds) > 0 { + bb.WriteString("++++ GOT\n") + for _, line = range diffData.Adds { + fmt.Fprintf(&bb, "%d + %s\n", line.N, line.V) + } + } + + if len(diffData.Changes) > 0 { + bb.WriteString("--++\n") + + var change diff.LineChange + for _, change = range diffData.Changes { + fmt.Fprintf(&bb, "%d - %s\n", change.Old.N, change.Old.V) + fmt.Fprintf(&bb, "%d + %s\n", change.New.N, change.New.V) + } + } + + w.Fatal(bb.String()) + return true } |
