Skip to content

Commit

Permalink
Merge pull request #9 from kinbiko/level-2-path
Browse files Browse the repository at this point in the history
rename level to path
  • Loading branch information
kinbiko authored Feb 1, 2019
2 parents 7d11e94 + e29f446 commit 13003a0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"strings"
)

func (a *Asserter) checkArray(level string, act, exp []interface{}) {
func (a *Asserter) checkArray(path string, act, exp []interface{}) {
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)", level, len(exp), len(act))
a.Printer.Errorf("actual JSON at '%s' was: %+v, but expected JSON was: %+v", level, serialize(act), serialize(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))
a.Printer.Errorf("actual JSON at '%s' was: %+v, but expected JSON was: %+v", path, serialize(act), serialize(exp))
return
}
for i := range act {
a.pathassertf(level+fmt.Sprintf("[%d]", i), serialize(act[i]), serialize(exp[i]))
a.pathassertf(path+fmt.Sprintf("[%d]", i), serialize(act[i]), serialize(exp[i]))
}
}

Expand Down
4 changes: 2 additions & 2 deletions boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func extractBoolean(b string) (bool, error) {
return false, fmt.Errorf("could not parse '%s' as a boolean", b)
}

func (a *Asserter) checkBoolean(level string, act, exp bool) {
func (a *Asserter) checkBoolean(path string, act, exp bool) {
if act != exp {
a.Printer.Errorf("expected boolean at '%s' to be %v but was %v", level, exp, act)
a.Printer.Errorf("expected boolean at '%s' to be %v but was %v", path, exp, act)
}
}
16 changes: 8 additions & 8 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func (a *Asserter) pathassertf(level string, act, exp string) {
func (a *Asserter) pathassertf(path string, act, exp string) {
if act == exp {
return
}
Expand All @@ -25,36 +25,36 @@ func (a *Asserter) pathassertf(level string, act, exp string) {
// If we're only caring about the presence of the key, then don't bother checking any further
if expPresence, _ := extractString(exp); expPresence == "<<PRESENCE>>" {
if actType == jsonNull {
a.Printer.Errorf(`expected the presence of any value at '%s', but was absent`, level)
a.Printer.Errorf(`expected the presence of any value at '%s', but was absent`, path)
}
return
}

if actType != expType {
a.Printer.Errorf("actual JSON (%s) and expected JSON (%s) were of different types at '%s'", actType, expType, level)
a.Printer.Errorf("actual JSON (%s) and expected JSON (%s) were of different types at '%s'", actType, expType, path)
return
}
switch actType {
case jsonBoolean:
actBool, _ := extractBoolean(act)
expBool, _ := extractBoolean(exp)
a.checkBoolean(level, actBool, expBool)
a.checkBoolean(path, actBool, expBool)
case jsonNumber:
actNumber, _ := extractNumber(act)
expNumber, _ := extractNumber(exp)
a.checkNumber(level, actNumber, expNumber)
a.checkNumber(path, actNumber, expNumber)
case jsonString:
actString, _ := extractString(act)
expString, _ := extractString(exp)
a.checkString(level, actString, expString)
a.checkString(path, actString, expString)
case jsonObject:
actObject, _ := extractObject(act)
expObject, _ := extractObject(exp)
a.checkObject(level, actObject, expObject)
a.checkObject(path, actObject, expObject)
case jsonArray:
actArray, _ := extractArray(act)
expArray, _ := extractArray(exp)
a.checkArray(level, actArray, expArray)
a.checkArray(path, actArray, expArray)
}
}

Expand Down
4 changes: 2 additions & 2 deletions number.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
// This is *probably* good enough. Can change this to be even smaller if necessary
const minDiff = 0.000001

func (a *Asserter) checkNumber(level string, act, exp float64) {
func (a *Asserter) checkNumber(path string, act, exp float64) {
diff := math.Abs(act - exp)
if diff > minDiff {
a.Printer.Errorf("expected number at '%s' to be '%.7f' but was '%.7f'", level, exp, act)
a.Printer.Errorf("expected number at '%s' to be '%.7f' but was '%.7f'", path, exp, act)
}
}

Expand Down
10 changes: 5 additions & 5 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import (
"strings"
)

func (a *Asserter) checkObject(level string, act, exp map[string]interface{}) {
func (a *Asserter) checkObject(path string, act, exp map[string]interface{}) {
if len(act) != len(exp) {
a.Printer.Errorf("expected %d keys at '%s' but got %d keys", len(exp), level, len(act))
a.Printer.Errorf("expected %d keys at '%s' but got %d keys", len(exp), path, len(act))
}
if unique := difference(act, exp); len(unique) != 0 {
a.Printer.Errorf("unexpected object key(s) %+v found at '%s'", serialize(unique), level)
a.Printer.Errorf("unexpected object key(s) %+v found at '%s'", serialize(unique), path)
}
if unique := difference(exp, act); len(unique) != 0 {
a.Printer.Errorf("expected object key(s) %+v missing at '%s'", serialize(unique), level)
a.Printer.Errorf("expected object key(s) %+v missing at '%s'", serialize(unique), path)
}
for key := range act {
if contains(exp, key) {
a.pathassertf(level+"."+key, serialize(act[key]), serialize(exp[key]))
a.pathassertf(path+"."+key, serialize(act[key]), serialize(exp[key]))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"strings"
)

func (a *Asserter) checkString(level, act, exp string) {
func (a *Asserter) checkString(path, act, exp string) {
if act != exp {
a.Printer.Errorf("expected string at '%s' to be '%s' but was '%s'", level, exp, act)
a.Printer.Errorf("expected string at '%s' to be '%s' but was '%s'", path, exp, act)
}
}

Expand Down

0 comments on commit 13003a0

Please sign in to comment.