Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Dec 5, 2023
1 parent 5b134d1 commit 97fda45
Show file tree
Hide file tree
Showing 16 changed files with 1,200 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-staticcheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
go: ["1.19"]
go: ["1.21"]

runs-on: ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: false
matrix:
os: ["windows-latest", "ubuntu-latest", "macOS-latest"]
go: ["1.18", "1.19", "1.20"]
go: ["1.21"]

runs-on: ${{ matrix.os }}

Expand Down
70 changes: 67 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,78 @@
<a href="https://goreportcard.com/report/github.com/bayashi/witness" title="witness report card" target="_blank"><img src="https://goreportcard.com/badge/github.com/bayashi/witness" alt="witness report card"></a>
<a href="https://pkg.go.dev/github.com/bayashi/witness" title="Go witness package reference" target="_blank"><img src="https://pkg.go.dev/badge/github.com/bayashi/witness.svg" alt="Go Reference: witness"></a>

`witness` provides
`witness` is a test helper to make an evident report on a fail of your test.

## Usage

Simple case.

```go
package main

import (
"fmt"
"testing"

w "github.com/bayashi/witness"
)

func main() {
func TestExample(t *testing.T) {
g := "a\nb\nc"
e := "a\nd\nc"

if g != e {
w.Got(g).Expect(e).Fail(t, "Not same")
}
}
```

below result will be shown:

```go
Trace: /home/usr/go/src/github.com/bayashi/witness/witness_test.go:33
Fail reason: Not same
Type: Expect:string, Got:string
Expected: "a\nd\nc"
Actually got: "a\nb\nc"
```

It's able to show more additional info:

```go
w.Got(g).Expect(e).ShowMore().Fail(t, "Not same")
```

And then,

```go
Trace: /home/usr/go/src/github.com/bayashi/witness/witness_test.go:41
Fail reason: Not same
Type: Expect:string, Got:string
Expected: "a\nd\nc"
Actually got: "a\nb\nc"
Diff details: --- Expected
+++ Actually got
@@ -1,3 +1,3 @@
a
-d
+b
c
Raw Expect: ---
a
d
c
---
Raw Got: ---
a
b
c
---
```

So easy and fair, isn't this?

You shouldn't need to spend your time anymore to show fail report.

## Installation

```cmd
Expand All @@ -34,3 +90,11 @@ MIT License
## Author

Dai Okabayashi: https://github.com/bayashi

## See Also

https://github.com/bayashi/actually

## Special Thanks To

https://github.com/stretchr/testify
104 changes: 104 additions & 0 deletions diff/diff.go
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
}
87 changes: 87 additions & 0 deletions diff/diff_test.go
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")
}
}
7 changes: 6 additions & 1 deletion go.mod
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
)
4 changes: 4 additions & 0 deletions go.sum
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=
9 changes: 0 additions & 9 deletions main.go

This file was deleted.

Loading

0 comments on commit 97fda45

Please sign in to comment.