Skip to content

Commit

Permalink
Merge pull request #22 from bayashi/tiny-fix
Browse files Browse the repository at this point in the history
Tiny fixes
  • Loading branch information
bayashi committed Jan 8, 2024
2 parents a47abe8 + 8685f37 commit 56e6c5c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 59 deletions.
33 changes: 14 additions & 19 deletions obj/obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package obj

import (
"fmt"
"regexp"
"testing"

tu "github.com/bayashi/witness/testutil"
)

type Example struct {
Expand Down Expand Up @@ -69,59 +70,53 @@ func TestBooleanPointer(t *testing.T) {
f := false
o := NewObject(&f)

expectRe := regexp.MustCompile(`\(\*bool\)\([0-9a-fx]+\)\(false\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
if ok, msg := tu.Match(`\(\*bool\)\([0-9a-fx]+\)\(false\)`, o.AsString()); !ok {
t.Error(msg)
}
}

func TestError(t *testing.T) {
e := fmt.Errorf("foo error")
o := NewObject(e)

expectRe := regexp.MustCompile(`\(\*errors\.errorString\)\([0-9a-fx]+\)\(foo error\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
if ok, msg := tu.Match(`\(\*errors\.errorString\)\([0-9a-fx]+\)\(foo error\)`, o.AsString()); !ok {
t.Error(msg)
}
}

func TestPointerValueInt(t *testing.T) {
i := 123
o := NewObject(&i)

expectRe := regexp.MustCompile(`\(\*int\)\([0-9a-fx]+\)\(123\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
if ok, msg := tu.Match(`\(\*int\)\([0-9a-fx]+\)\(123\)`, o.AsString()); !ok {
t.Error(msg)
}
}

func TestPointerValueFloat(t *testing.T) {
var i float64 = 0.123
o := NewObject(&i)

expectRe := regexp.MustCompile(`\(*float64\)\([0-9a-fx]+\)\(0\.123\)`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
if ok, msg := tu.Match(`\(*float64\)\([0-9a-fx]+\)\(0\.123\)`, o.AsString()); !ok {
t.Error(msg)
}
}

func TestStructValue(t *testing.T) {
i := struct{ ID int }{ ID: 123 }
o := NewObject(i)

expectRe := regexp.MustCompile(`struct { ID int }{ID:123}`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
if ok, msg := tu.Match(`struct { ID int }{ID:123}`, o.AsString()); !ok {
t.Error(msg)
}
}

func TestStructPointerValue(t *testing.T) {
i := struct{ ID int }{ ID: 123 }
o := NewObject(&i)

expectRe := regexp.MustCompile(`[0-9a-fx]+, &struct { ID int }{ID:123}`)
if expectRe.FindStringSubmatch(o.AsString()) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), o.AsString())
if ok, msg := tu.Match(`[0-9a-fx]+, &struct { ID int }{ID:123}`, o.AsString()); !ok {
t.Error(msg)
}
}

Expand Down
4 changes: 2 additions & 2 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (f *Failure) Put() string {
lines += fmt.Sprintf(
"\t%s%s%s\t%s\n",
c.Label,
r.Separator(),
r.separator(),
c.indentSpaces(longestLen),
indentMessage(c.Body, longestLen),
)
Expand Down Expand Up @@ -167,7 +167,7 @@ type Report struct {
LabelSeparator *string
}

func (r *Report) Separator() string {
func (r *Report) separator() string {
if r.LabelSeparator != nil {
return *r.LabelSeparator
}
Expand Down
15 changes: 15 additions & 0 deletions testutil/regexp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testutil

import (
"fmt"
"regexp"
)

func Match(re string, target string) (bool, string) {
reg := regexp.MustCompile(re)
if !reg.MatchString(target) {
return false, fmt.Sprintf("Not matched the regexp `%s` for %q", reg.String(), target)
}

return true, ""
}
9 changes: 5 additions & 4 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package trace

import (
"regexp"
"testing"

tu "github.com/bayashi/witness/testutil"
)

func TestInfo(t *testing.T) {
trace := Info()
if len(trace) != 1 {
t.Error("trace length should be 1.")
}
var traceRegexp = regexp.MustCompile(`/witness/trace/trace_test\.go:\d+$`)
if !traceRegexp.MatchString(trace[0]) {
t.Errorf("trace was not match Regexp:`%s`, Got:`%s`", traceRegexp.String(), trace[0])

if ok, msg := tu.Match(`/witness/trace/trace_test\.go:\d+$`, trace[0]); !ok {
t.Error(msg)
}
}

Expand Down
41 changes: 35 additions & 6 deletions witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,32 @@ type Witness struct {
got *obj.Object
expect *obj.Object
name string
messages []map[string]string // additional info
messages []map[string]string // additional info as {"label": "message"}
showDiff bool // If true, show a diff string for "got" and "expect"
showRaw bool // If true, show raw values as string or dumped data for "got" and "expect"
showRaw bool // If true, show raw values as string(raw string or dumped string) for "got" and "expect"
}

// You can write "witness.New(witness.ShowDiff, witness.NotShowRaw)" instead of raw boolean
const ShowDiff, ShowRaw, NotShowDiff, NotShowRaw = true, true, false, false

// you don't need to call `New`. You can call `Got` or `Expect` directly without calling `New`.
// You don't need to call `New`. You can call `Got` or `Expect` directly without calling `New` like below.
//
// witness.Got("abc").Fail(t, "somehow")
//
// Above works. You should call `New` when you need to set options for several reports
// You should call `New` when you need to set options for several reports
// in order to avoid calling `ShowDiff` or `ShowRaw` for each report.
func New(showDiff bool, showRaw bool) *Witness {
//
// w := witness.New(witness.ShowDiff, witness.ShowRaw)
// w.Got(123).Fail(t, "Not expected")
// w.Got("c").Fail(t, "Expected d")
func New(showFlag ...bool) *Witness {
var showDiff, showRaw = false, false
if len(showFlag) >= 2 {
showDiff = showFlag[0]
showRaw = showFlag[1]
} else if len(showFlag) == 1 {
showDiff = showFlag[0]
}
return &Witness{
showDiff: showDiff,
showRaw: showRaw,
Expand Down Expand Up @@ -60,6 +71,8 @@ func Name(n string) *Witness {
name: n,
}
}

// Set test name
func (w *Witness) Name(n string) *Witness {
w.name = n

Expand All @@ -72,6 +85,8 @@ func Namef(format string, a ...any) *Witness {
name: fmt.Sprintf(format, a...),
}
}

// Set test name by format
func (w *Witness) Namef(format string, a ...any) *Witness {
w.name = fmt.Sprintf(format, a...)

Expand All @@ -84,6 +99,8 @@ func Got(v any) *Witness {
got: obj.NewObject(v),
}
}

// Set Got value
func (w *Witness) Got(v any) *Witness {
if w.got != nil && w.got.Touch() {
panic("Already set Got()")
Expand All @@ -100,6 +117,8 @@ func Expect(v any) *Witness {
expect: obj.NewObject(v),
}
}

// Set Expect value
func (w *Witness) Expect(v any) *Witness {
if w.expect != nil && w.expect.Touch() {
panic("Already set Expect()")
Expand Down Expand Up @@ -212,6 +231,15 @@ func Fail(t *testing.T, reason string, got any, expect ...any) {
}
}

// FailNow is shortcut method. There are same expression.
//
// witness.Got(got).FailNow(t, reason)
// witness.FailNow(t, reason, got)
//
// FailNow with 2 values cases are below
//
// witness.Got(got).Expect(expect).FailNow(t, reason)
// witness.FailNow(t, reason, got, expect)
func FailNow(t *testing.T, reason string, got any, expect ...any) {
if len(expect) == 0 {
Got(got).FailNow(t, reason)
Expand All @@ -221,11 +249,12 @@ func FailNow(t *testing.T, reason string, got any, expect ...any) {
}

// Diff is to get a diff string of 2 objects for debugging in test
// Two args should be same type. Otherwise, diff string will be a blank string.
func Diff(a any, b any) string {
return diff.DiffSimple(a, b)
}

// Dump is to get a dumped string for debugging in test
// Dump is to get a dumped string by `spew.Sdump` for debugging in test
func Dump(v any) string {
return obj.NewObject(v).AsDumpString()
}
70 changes: 42 additions & 28 deletions witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package witness

import (
"fmt"
"regexp"
"strings"
"testing"

"github.com/bayashi/witness/report"
tu "github.com/bayashi/witness/testutil"
)

// Global variables to check result
Expand All @@ -25,6 +25,29 @@ func stub() {
}
}

func TestNil(t *testing.T) {
stub()

Got(nil).Expect(nil).Fail(t, "oops")

// Fail reason: oops
// Type: Expect:<nil>, Got:<nil>
// Expected: <nil>
// Actually got: <nil>

if ok, msg := tu.Match(`Type:\s*\tExpect:<nil>, Got:<nil>`, res); !ok {
t.Error(msg)
}

if ok, msg := tu.Match(`Expected:\s*\t<nil>`, res); !ok {
t.Error(msg)
}

if ok, msg := tu.Match(`Actually got:\s*\t<nil>`, res); !ok {
t.Error(msg)
}
}

func TestError(t *testing.T) {
stub()

Expand All @@ -35,14 +58,12 @@ func TestError(t *testing.T) {
// Type: Got:*errors.errorString
// Actually got: error example 123

gotTypeRe := regexp.MustCompile(`Type:\s*\tGot:\*errors\.errorString`)
if gotTypeRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", gotTypeRe.String(), res)
if ok, msg := tu.Match(`Type:\s*\tGot:\*errors\.errorString`, res); !ok {
t.Error(msg)
}

gotRe := regexp.MustCompile(`Actually got:\s*\t\(\*errors\.errorString\)\([0-9a-fx]+\)\(error example 123\)`)
if gotRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res)
if ok, msg := tu.Match(`Actually got:\s*\t\(\*errors\.errorString\)\([0-9a-fx]+\)\(error example 123\)`, res); !ok {
t.Error(msg)
}
}

Expand Down Expand Up @@ -80,9 +101,8 @@ func TestFailGot(t *testing.T) {
t.Errorf("Expected to be contained type, but not: %q", res)
}

gotRe := regexp.MustCompile(fmt.Sprintf("Actually got:\\s*\\t%q", got))
if gotRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res)
if ok, msg := tu.Match(fmt.Sprintf("Actually got:\\s*\\t%q", got), res); !ok {
t.Error(msg)
}
}

Expand Down Expand Up @@ -118,14 +138,12 @@ func TestFailGotExpect(t *testing.T) {
t.Errorf("Expected to be contained types, but not: %q", res)
}

gotRe := regexp.MustCompile(fmt.Sprintf("Actually got:\\s*\\t%q", got))
if gotRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", gotRe.String(), res)
if ok, msg := tu.Match(fmt.Sprintf("Actually got:\\s*\\t%q", got), res); !ok {
t.Error(msg)
}

expectRe := regexp.MustCompile(fmt.Sprintf("Expected:\\s*\\t%q", expect))
if expectRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", expectRe.String(), res)
if ok, msg := tu.Match(fmt.Sprintf("Expected:\\s*\\t%q", expect), res); !ok {
t.Error(msg)
}
}

Expand Down Expand Up @@ -155,9 +173,8 @@ func TestFailWithDiff(t *testing.T) {
t.Errorf("Expected to be contained the string `Diff details:`, but not: %q", res)
}

diffRe := regexp.MustCompile("\\s*a\n\\s*-d\n\\s*\\+b\n\\s*c")
if diffRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", diffRe.String(), res)
if ok, msg := tu.Match("\\s*a\n\\s*-d\n\\s*\\+b\n\\s*c", res); !ok {
t.Error(msg)
}
}

Expand Down Expand Up @@ -186,14 +203,12 @@ func TestFailWithRawData(t *testing.T) {
// c
// ---

rawExpectRe := regexp.MustCompile("Raw Expect:\\s*\t---\n\\s*a\n\\s*d")
if rawExpectRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", rawExpectRe.String(), res)
if ok, msg := tu.Match("Raw Expect:\\s*\t---\n\\s*a\n\\s*d", res); !ok {
t.Error(msg)
}

rawGotRe := regexp.MustCompile("Raw Got:\\s*\t---\n\\s*a\n\\s*b")
if rawGotRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", rawGotRe.String(), res)
if ok, msg := tu.Match("Raw Got:\\s*\t---\n\\s*a\n\\s*b", res); !ok {
t.Error(msg)
}
}

Expand All @@ -211,8 +226,7 @@ func TestFailWithAdditionalMessage(t *testing.T) {
// Actually got: "a\nb\nc"
// Example Label: Some info

infoRe := regexp.MustCompile("Example Label:\\s*\tSome info\n")
if infoRe.FindStringSubmatch(res) == nil {
t.Errorf("Not matched the regexp `%s` for %q", infoRe.String(), res)
if ok, msg := tu.Match("Example Label:\\s*\tSome info\n", res); !ok {
t.Error(msg)
}
}

0 comments on commit 56e6c5c

Please sign in to comment.