Skip to content

Commit

Permalink
Merge pull request #17 from kinbiko/fix-line-numbers
Browse files Browse the repository at this point in the history
Ignore stackframes from this package in assertions
  • Loading branch information
kinbiko authored Jun 3, 2019
2 parents 2c312ee + 7d50302 commit c775cc2
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 0 deletions.
3 changes: 3 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func (a *Asserter) checkArray(path string, act, exp []interface{}) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
if len(act) != len(exp) {
a.Printer.Errorf("length of arrays at '%s' were different. Expected array to be of length %d, but contained %d element(s)", path, len(exp), len(act))
serializedAct, serializedExp := serialize(act), serialize(exp)
Expand Down
3 changes: 3 additions & 0 deletions boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func extractBoolean(b string) (bool, error) {
}

func (a *Asserter) checkBoolean(path string, act, exp bool) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
if act != exp {
a.Printer.Errorf("expected boolean at '%s' to be %v but was %v", path, exp, act)
}
Expand Down
12 changes: 12 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

func (a *Asserter) pathassertf(path, act, exp string) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
if act == exp {
return
}
Expand Down Expand Up @@ -102,3 +105,12 @@ func findType(j string) (jsonType, error) {
}
return jsonTypeUnknown, fmt.Errorf(`unable to identify JSON type of "%s"`, j)
}

// *testing.T has a Helper() func that allow testing tools like this package to
// ignore their own frames when calling Errorf on *testing.T instances.
// This interface is here to avoid breaking backwards compatibility in terms of
// the interface we expect in New.
type tt interface {
Printer
Helper()
}
3 changes: 3 additions & 0 deletions exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ func New(p Printer) *Asserter {
// You may use "<<PRESENCE>>" against any type of value. The only exception is null, which
// will result in an assertion failure.
func (a *Asserter) Assertf(actualJSON, expectedJSON string, fmtArgs ...interface{}) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
a.pathassertf("$", actualJSON, fmt.Sprintf(expectedJSON, fmtArgs...))
}
78 changes: 78 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,84 @@ but expected JSON was:
}
}

type testTT struct {
testPrinter
invokedCount int
}

func (tt *testTT) Helper() {
tt.invokedCount++
}

func TestHelperGetsCalled(t *testing.T) {
testTable := []struct {
name string
in string
exp string
expCount int
}{
{
name: "1 at the top level, 1 for core",
in: `null`,
exp: `""`,
expCount: 2,
},
{
name: "1 at the top level, 1 for core, 1 for boolean",
in: `true`,
exp: `false`,
expCount: 3,
},
{
name: "1 at the top level, 1 for core, 1 for string",
in: `"hello"`,
exp: `"henlo"`,
expCount: 3,
},
{
name: "1 at the top level, 1 for core, 1 for number",
in: `1234`,
exp: `4321`,
expCount: 3,
},
{
name: "1 at the top level, 2 for core, 1 for object, 1 for string",
in: `{"hello": "world"}`,
exp: `{"hello": "世界"}`,
expCount: 5,
},
{
name: "1 at the top level, 2 for core, 2 for object, 2 for string",
in: `{"hello": {"name": "kinbiko"}}`,
exp: `{"hello": {"name": "他の人"}}`,
expCount: 7,
},
{
name: "1 at the top level, 2 for core, 1 for array, 2 for string",
in: `["hello", "world"]`,
exp: `["hello", "世界"]`,
expCount: 6,
},
{
name: "1 at the top level, 4 for core, 3 for array, 4 for string",
in: `[["hello", "world"], ["bye", "moon"]]`,
exp: `[["hello", "世界"], ["bye", "月"]]`,
expCount: 13,
},
}

for _, tc := range testTable {
t.Run(tc.name, func(st *testing.T) {
tt := &testTT{}
ja := jsonassert.New(tt)
ja.Assertf(tc.in, tc.exp) // assertions should never fail
if got := tt.invokedCount; got != tc.expCount {
st.Errorf("expected %d calls to Helper but got %d", tc.expCount, got)
}
})
}
}

func setup() (*testPrinter, *jsonassert.Asserter) {
tp := &testPrinter{}
return tp, jsonassert.New(tp)
Expand Down
3 changes: 3 additions & 0 deletions number.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
const minDiff = 0.000001

func (a *Asserter) checkNumber(path string, act, exp float64) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
diff := math.Abs(act - exp)
if diff > minDiff {
a.Printer.Errorf("expected number at '%s' to be '%.7f' but was '%.7f'", path, exp, act)
Expand Down
3 changes: 3 additions & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func (a *Asserter) checkObject(path string, act, exp map[string]interface{}) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
if len(act) != len(exp) {
a.Printer.Errorf("expected %d keys at '%s' but got %d keys", len(exp), path, len(act))
}
Expand Down
3 changes: 3 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func (a *Asserter) checkString(path, act, exp string) {
if t, ok := a.Printer.(tt); ok {
t.Helper()
}
if act != exp {
if len(exp+act) < 50 {
a.Printer.Errorf("expected string at '%s' to be '%s' but was '%s'", path, exp, act)
Expand Down

0 comments on commit c775cc2

Please sign in to comment.