aboutsummaryrefslogtreecommitdiff
path: root/lib/test/test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-09-12 23:47:27 +0700
committerShulhan <ms@kilabit.info>2022-09-15 01:16:02 +0700
commitad3dbcc2b6e9e2a23fcca17df89039a660bb23bc (patch)
tree8d8ed83c063480c71960c775a8c48504262748c8 /lib/test/test.go
parent60bcaa7d75c9a17dc5a9c6349bb7b16797d6aac3 (diff)
downloadpakakeh.go-ad3dbcc2b6e9e2a23fcca17df89039a660bb23bc.tar.xz
lib/test: use text/diff to compare strings on Assert
If both exp and got types are string and its longer than 50 chars, it will use the text/diff.Text to show the difference between them. The diff output is as follow, !!! string not matched: --++ <LINE_NUM> - "<LINE_EXP>" <LINE_NUM> + "<LINE_GOT>" ^<COL_NUM> - "<DELETED_STRING>" ^<COL_NUM> + "<INSERTED_STRING>" The "<LINE_NUM> - " print the line number in exp followed by line itself. The "<LINE_NUM> + " print the line number in got followed by line itself. The "^<COL_NUM> - " show the character number in exp line followed by deleted string (or string that not exist in got). The "^<COL_NUM> + " show the character number in got line followed by inserted string (or string that not exist in exp).
Diffstat (limited to 'lib/test/test.go')
-rw-r--r--lib/test/test.go78
1 files changed, 71 insertions, 7 deletions
diff --git a/lib/test/test.go b/lib/test/test.go
index bf4413d6..5435ca06 100644
--- a/lib/test/test.go
+++ b/lib/test/test.go
@@ -9,6 +9,7 @@ import (
"runtime"
"github.com/shuLhan/share/lib/reflect"
+ "github.com/shuLhan/share/lib/text/diff"
)
func printStackTrace(w Writer, trace []byte) {
@@ -40,17 +41,43 @@ func printStackTrace(w Writer, trace []byte) {
}
}
-// Assert compare two interfaces: `exp` and `got` for equality.
-// If both parameters are not equal, the function will call Fatalf that
-// describe the position (type and value) where value are not matched.
+// 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.
//
-// If `exp` implement the extended `reflect.Equaler`, then it will use the
-// method `IsEqual()` with `got` as parameter.
+// If exp implement the extended reflect.Equaler, then it will use the
+// method IsEqual with got as parameter.
+//
+// If exp and got is a struct, it will print the first non-matched field in
+// the following format,
+//
+// !!! 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 text/diff.Text to show the difference between them.
+// The diff output is as follow,
+//
+// !!! string not matched:
+// --++
+// <LINE_NUM> - "<LINE_EXP>"
+// <LINE_NUM> + "<LINE_GOT>"
+// ^<COL_NUM> - "<DELETED_STRING>"
+// ^<COL_NUM> + "<INSERTED_STRING>"
+//
+// The "<LINE_NUM> - " print the line number in exp followed by line itself.
+// The "<LINE_NUM> + " print the line number in got followed by line itself.
+// The "^<COL_NUM> - " show the character number in exp line followed by
+// deleted string (or string that not exist in got).
+// The "^<COL_NUM> + " show the character number in got line followed by
+// inserted string (or string that not exist in exp).
//
// WARNING: 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 interface{}) {
var (
+ logp = `Assert`
+
err error
trace []byte
)
@@ -64,9 +91,46 @@ func Assert(w Writer, name string, exp, got interface{}) {
runtime.Stack(trace, false)
printStackTrace(w, trace)
+ if printStringDiff(w, name, exp, got) {
+ return
+ }
+
if len(name) == 0 {
- w.Fatalf(`!!! %s`, err)
+ w.Fatalf(`!!! %s: %s`, logp, err)
} else {
- w.Fatalf(`!!! %s: %s`, name, err)
+ w.Fatalf(`!!! %s: %s: %s`, logp, name, err)
}
}
+
+func printStringDiff(w Writer, name string, exp, got interface{}) bool {
+ var (
+ diffData diff.Data
+ expStr string
+ gotStr string
+ ok bool
+ )
+
+ expStr, ok = exp.(string)
+ if !ok {
+ return false
+ }
+
+ gotStr, ok = got.(string)
+ if !ok {
+ return false
+ }
+
+ if len(expStr) < 50 {
+ return false
+ }
+
+ diffData = diff.Text([]byte(expStr), []byte(gotStr), diff.LevelWords)
+
+ if len(name) == 0 {
+ w.Log("!!! strings not matched:\n", diffData.String())
+ } else {
+ w.Logf("!!! %s:\n%s", name, diffData.String())
+ }
+
+ return true
+}