-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,200 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package diff | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/pmezard/go-difflib/difflib" | ||
) | ||
|
||
var spewConfig = spew.ConfigState{ | ||
Indent: " ", | ||
DisablePointerAddresses: true, | ||
DisableCapacities: true, | ||
SortKeys: true, | ||
DisableMethods: true, | ||
MaxDepth: 10, | ||
} | ||
|
||
var spewConfigStringerEnabled = spew.ConfigState{ | ||
Indent: " ", | ||
DisablePointerAddresses: true, | ||
DisableCapacities: true, | ||
SortKeys: true, | ||
MaxDepth: 10, | ||
} | ||
|
||
// Diff method returns diff text of 2 testing objects | ||
func Diff(expectv any, gotv any) string { | ||
e, g := pre(expectv, gotv) | ||
if e == "" && g == "" { | ||
return "" | ||
} | ||
|
||
return getDiff(e, g, "Expected", "Actually got") | ||
} | ||
|
||
// DiffSimple method returns diff text of 2 objects as "a and b" | ||
func DiffSimple(av any, bv any) string { | ||
a, b := pre(av, bv) | ||
if a == "" && b == "" { | ||
return "" | ||
} | ||
|
||
return getDiff(a, b, "a", "b") | ||
} | ||
|
||
func pre(expectv any, gotv any) (string, string) { | ||
if expectv == nil || gotv == nil { | ||
return "", "" | ||
} | ||
|
||
et, ek := typeAndKind(expectv) | ||
gt, _ := typeAndKind(gotv) | ||
|
||
if et != gt { | ||
return "", "" | ||
} | ||
|
||
if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String { | ||
return "", "" | ||
} | ||
|
||
var e, g string | ||
|
||
switch et { | ||
case reflect.TypeOf(""): | ||
e = reflect.ValueOf(expectv).String() | ||
g = reflect.ValueOf(gotv).String() | ||
case reflect.TypeOf(time.Time{}): | ||
e = spewConfigStringerEnabled.Sdump(expectv) | ||
g = spewConfigStringerEnabled.Sdump(gotv) | ||
default: | ||
e = spewConfig.Sdump(expectv) | ||
g = spewConfig.Sdump(gotv) | ||
} | ||
|
||
return e, g | ||
} | ||
|
||
func typeAndKind(v any) (reflect.Type, reflect.Kind) { | ||
t := reflect.TypeOf(v) | ||
k := t.Kind() | ||
|
||
if k == reflect.Ptr { | ||
t = t.Elem() | ||
k = t.Kind() | ||
} | ||
return t, k | ||
} | ||
|
||
func getDiff(e string, g string, fromFile string, toFile string) string { | ||
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ | ||
A: difflib.SplitLines(e), | ||
B: difflib.SplitLines(g), | ||
FromFile: fromFile, | ||
FromDate: "", | ||
ToFile: toFile, | ||
ToDate: "", | ||
Context: 1, | ||
}) | ||
|
||
return diff | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package diff | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDiffStruct(t *testing.T) { | ||
type S struct { | ||
id int | ||
name string | ||
} | ||
|
||
a := &S{ | ||
id: 123, | ||
name: "aiko", | ||
} | ||
b := &S{ | ||
id: 124, | ||
name: "eiko", | ||
} | ||
|
||
got := "\n" + Diff(a, b) // "\n" is for comparing to heredoc. | ||
expect := ` | ||
--- Expected | ||
+++ Actually got | ||
@@ -1,4 +1,4 @@ | ||
(*diff.S)({ | ||
- id: (int) 123, | ||
- name: (string) (len=4) "aiko" | ||
+ id: (int) 124, | ||
+ name: (string) (len=4) "eiko" | ||
}) | ||
` | ||
if got != expect { | ||
t.Errorf("Diff was wrong.\n[Got]\n%s\n\n[Expect]\n%s\n", got, expect) | ||
} | ||
} | ||
|
||
func TestDiffString(t *testing.T) { | ||
a := "beer" | ||
b := "deer" | ||
|
||
got := "\n" + Diff(a, b) | ||
expect := ` | ||
--- Expected | ||
+++ Actually got | ||
@@ -1 +1 @@ | ||
-beer | ||
+deer | ||
` | ||
if got != expect { | ||
t.Errorf("Diff was wrong.\n[Got]\n%s\n\n[Expect]\n%s\n", got, expect) | ||
} | ||
} | ||
|
||
func TestDiffTime(t *testing.T) { | ||
a := time.Date(2023, 4, 23, 0, 0, 0, 0, time.UTC) | ||
b := time.Date(2023, 4, 24, 0, 0, 0, 0, time.UTC) | ||
|
||
got := "\n" + Diff(a, b) | ||
expect := ` | ||
--- Expected | ||
+++ Actually got | ||
@@ -1,2 +1,2 @@ | ||
-(time.Time) 2023-04-23 00:00:00 +0000 UTC | ||
+(time.Time) 2023-04-24 00:00:00 +0000 UTC | ||
` // What is last space? It would be created by difflib.GetUnifiedDiffString?? Anyway, I ignore :-) | ||
if got != expect { | ||
t.Errorf("Diff was wrong.\n[Got]\n%#v\n\n[Expect]\n%#v\n", got, expect) | ||
} | ||
} | ||
|
||
func TestDiffNil(t *testing.T) { | ||
if Diff(nil, nil) != "" { | ||
t.Error("<nil> input should be blank string") | ||
} | ||
|
||
if Diff(1, "string") != "" { | ||
t.Error("Input values should be same type") | ||
} | ||
|
||
if Diff(1, 2) != "" { | ||
t.Error("NOT supported int type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module github.com/bayashi/witness | ||
|
||
go 1.18 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 | ||
github.com/pmezard/go-difflib v1.0.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= |
Oops, something went wrong.