diff options
| author | Shulhan <ms@kilabit.info> | 2026-04-12 21:20:40 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-04-12 22:33:31 +0700 |
| commit | c1a33bead8068e02c34fafb09c5519d5e5c18706 (patch) | |
| tree | 5ed751850bf933812febc380e356f1d1369788cd | |
| parent | 05849aa15abc0bcfc6a76fc86af2ef6590023ce2 (diff) | |
| download | pakakeh.go-c1a33bead8068e02c34fafb09c5519d5e5c18706.tar.xz | |
lib/test: rewrite Assert to use [diff.Unified]
Using diff Unified improve the test output readability when two large
strings are not match.
| -rw-r--r-- | lib/test/example_test.go | 54 | ||||
| -rw-r--r-- | lib/test/test.go | 134 | ||||
| -rw-r--r-- | lib/test/writer.go | 5 |
3 files changed, 61 insertions, 132 deletions
diff --git a/lib/test/example_test.go b/lib/test/example_test.go index 039515e3..78d56be9 100644 --- a/lib/test/example_test.go +++ b/lib/test/example_test.go @@ -75,15 +75,10 @@ func ExampleAssert_struct() { } func ExampleAssert_string() { - var ( - tw = test.BufferWriter{} + tw := test.BufferWriter{} - exp string - got string - ) - - exp = `a string` - got = `b string` + exp := `a string` + got := `b string` test.Assert(&tw, ``, exp, got) fmt.Println(tw.String()) @@ -94,44 +89,43 @@ func ExampleAssert_string() { fmt.Println(tw.String()) // Output: // !!! Assert: expecting string(a string), got string(b string) - // !!! : - // --++ - // 0 - func (tw *BufferWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) } - // 0 + func (tw *BufferWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) } + // !!! Assert: : not matched, see the diff below + // --- want + // +++ got + // @@ -1 +1 @@ + // -func (tw *BufferWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) } + // +func (tw *BufferWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) } } func ExampleAssert_string2() { - var ( - tw = test.BufferWriter{} - - exp string - got string - ) - - exp = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. + exp := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce cursus libero in velit dapibus tincidunt. Vestibulum vulputate ipsum ac nisl viverra pharetra. Sed at mi in urna lobortis bibendum. Vivamus tempus enim in urna fermentum, non volutpat nisi lacinia.` - got = `Fusce cursus libero in velit dapibus tincidunt. + got := `Fusce cursus libero in velit dapibus tincidunt. Vestibulum vulputate ipsum ac nisl viverra pharetra. Sed at mi in urna lobortis bibendum. Sed pretium nisl ut dolor ullamcorper blandit. Sed faucibus felis iaculis, sagittis erat quis, tempor nisi.` - test.Assert(&tw, `Assert string`, exp, got) + tw := test.BufferWriter{} + test.Assert(&tw, `string`, exp, got) fmt.Println(tw.String()) // Output: - // !!! Assert string: - // ---- EXPECTED - // 0 - Lorem ipsum dolor sit amet, consectetur adipiscing elit. - // ++++ GOT - // 4 + Sed faucibus felis iaculis, sagittis erat quis, tempor nisi. - // --++ - // 4 - Vivamus tempus enim in urna fermentum, non volutpat nisi lacinia. - // 3 + Sed pretium nisl ut dolor ullamcorper blandit. + // !!! Assert: string: not matched, see the diff below + // --- want + // +++ got + // @@ -1,5 +1,5 @@ + // -Lorem ipsum dolor sit amet, consectetur adipiscing elit. + // Fusce cursus libero in velit dapibus tincidunt. + // Vestibulum vulputate ipsum ac nisl viverra pharetra. + // Sed at mi in urna lobortis bibendum. + // -Vivamus tempus enim in urna fermentum, non volutpat nisi lacinia. + // +Sed pretium nisl ut dolor ullamcorper blandit. + // +Sed faucibus felis iaculis, sagittis erat quis, tempor nisi. } func ExampleLoadDataDir() { diff --git a/lib/test/test.go b/lib/test/test.go index 66419a5b..7f3364b1 100644 --- a/lib/test/test.go +++ b/lib/test/test.go @@ -1,142 +1,76 @@ -// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> -// // SPDX-License-Identifier: BSD-3-Clause +// SPDX-FileCopyrightText: 2018 M. Shulhan <ms@kilabit.info> // Package test provide library for helping with testing. package test import ( "bytes" - "fmt" "git.sr.ht/~shulhan/pakakeh.go/lib/reflect" - "git.sr.ht/~shulhan/pakakeh.go/lib/text" "git.sr.ht/~shulhan/pakakeh.go/lib/text/diff" ) -// Assert compare two interfaces: exp and got for equality. -// If both parameters are not equal, the function will call print and try to -// describe the position (type and value) where value are not matched and call -// Fatalf. +// Assert compares two interfaces values, exp and got for equality. +// If both parameters are not equal, the function will try to describe the +// type and value for both of them and print it with [testing.T.Fatalf]. // // If exp implement the extended [reflect.Equaler], then it will use the -// method [Equal] with got as parameter. +// method [Equal] and pass got as parameter. // -// If exp and got is a struct, it will print the first non-matched field in +// If exp and got is a struct, it will print the first non-matching field in // the following format, // -// !!! Assert: [<name>: ] T.<Field>: expecting <type>(<value>), got <type>(<value>) +// !!! Assert: <name>: T.<Field>: expecting <type>(<value>), got <type>(<value>) // // If both exp and got types are string and its longer than 50 chars, it -// will use the [diff.Text] to show the difference between them. -// The diff output is as follow, -// -// !!! <name>: -// ---- EXPECTED -// <LINE_NUM> - "<STRING>" -// ... -// ++++ GOT -// <LINE_NUM> + "<STRING>" -// ... -// --++ -// <LINE_NUM> - "<LINE_EXP>" -// <LINE_NUM> + "<LINE_GOT>" -// -// Any lines after "----" indicate the lines that test expected, from `exp` -// parameter. +// will use the [diff.Unified] to show the difference between them. // -// Any lines after "++++" indicate the lines that test got, from `got` -// parameter. -// -// Any lines after "--++" indicate that the same line between expected and got -// but different content. -// -// - The "<LINE_NUM> - " print the expected line. -// - The "<LINE_NUM> + " print the got line. +// !!! Assert: <name>: +// <UnifiedDiff> // // LIMITATION: this method does not support recursive pointer, for example a // node that point to parent and parent that point back to node again. func Assert(w Writer, name string, exp, got any) { w.Helper() - var logp = `Assert` - var err error - - err = reflect.DoEqual(exp, got) + err := reflect.DoEqual(exp, got) if err == nil { return } - if printStringDiff(w, name, exp, got) { - return - } - - if len(name) == 0 { - w.Fatalf(`!!! %s: %s`, logp, err) - } else { - w.Fatalf(`!!! %s: %s: %s`, logp, name, err) - } -} - -func printStringDiff(w Writer, name string, exp, got any) bool { - var ( - diffData diff.Data - expStr string - gotStr string - ok bool - ) + logp := `Assert` + var gotStr string + var diffd *diff.Data + var bb bytes.Buffer - expStr, ok = exp.(string) + expStr, ok := exp.(string) if !ok { - return false + goto out } - gotStr, ok = got.(string) if !ok { - return false - } - - if len(expStr) < 50 { - return false - } - - diffData = diff.Text([]byte(expStr), []byte(gotStr), diff.LevelWords) - if diffData.IsMatched { - return true - } - - 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) - } + goto out } - - 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 max(len(expStr), len(gotStr)) < 50 { + goto out } - 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) - } + diffd = diff.Unified([]byte(expStr), []byte(gotStr)) + if diffd.IsMatched { + return } - + diffd.OldName = `want` + diffd.NewName = `got` + w.Errorf("!!! %s: %s: not matched, see the diff below\n", logp, name) + diffd.WriteUnified(&bb, 3) w.Fatal(bb.String()) + return - return true +out: + if len(name) == 0 { + w.Fatalf(`!!! %s: %s`, logp, err) + } else { + w.Fatalf(`!!! %s: %s: %s`, logp, name, err) + } } diff --git a/lib/test/writer.go b/lib/test/writer.go index 63b0fcc0..ea9f7edb 100644 --- a/lib/test/writer.go +++ b/lib/test/writer.go @@ -3,8 +3,9 @@ package test -// Writer contains common methods between testing.T and testing.B, a subset -// of testing.TB that cannot be used due to private methods. +// Writer contains common methods between [testing.T], [testing.B] and +// [testing.F], a subset of [testing.TB] that cannot be used due to private +// methods. type Writer interface { Error(args ...any) Errorf(format string, args ...any) |
