Skip to content
This repository has been archived by the owner on Sep 19, 2018. It is now read-only.

Make assert_object_equals more accurate and give better error messages #144

Closed
wants to merge 1 commit into from

Conversation

johnmellor
Copy link

As an alternative to #143, this pull request proposes to fix assert_object_equals.

Consider the following tests:

test(function() {
    assert_object_equals(false, true);
}, "1");
test(function() {
    assert_object_equals(1, true);
}, "2");
test(function() {
    assert_object_equals(false, {});
}, "3");
test(function() {
    assert_object_equals({}, false);
}, "4");
test(function() {
    assert_object_equals("abc", ["a", "b", "c"]);
}, "5");
test(function() {
    assert_object_equals(["a", "b", "c"], "abc");
}, "6");
test(function() {
    assert_object_equals({a:{}}, {a:false});
}, "7");
test(function() {
    assert_object_equals({b:{}}, {b:null});
}, "8");
test(function() {
    assert_object_equals({c:null}, {c:{}});
}, "9");
test(function() {
    assert_object_equals({d:false}, {d:{}});
}, "10");
test(function() {
    assert_object_equals({x:{y:{z:false}}}, {x:{y:{z:true}}});
}, "11");

Previously, the first 8 would incorrectly pass, and the last 3 would
give confusing error messages:

FAIL 9 assert_object_equals: property "c" expected object "[object Object]" got object "[object Object]"
FAIL 10 assert_object_equals: property "d" expected object "[object Object]" got object "[object Object]"
FAIL 11 assert_object_equals: property "z" expected object "[object Object]" got object "[object Object]"

After this patch, all the cases fail correctly and have clear error messages:

FAIL 1 assert_object_equals: expected true but got false
FAIL 2 assert_object_equals: expected (boolean) true but got (number) 1
FAIL 3 assert_object_equals: expected (object) object "[object Object]" but got (boolean) false
FAIL 4 assert_object_equals: expected (boolean) false but got (object) object "[object Object]"
FAIL 5 assert_object_equals: expected (object) ["a", "b", "c"] but got (string) "abc"
FAIL 6 assert_object_equals: expected (string) "abc" but got (object) ["a", "b", "c"]
FAIL 7 assert_object_equals: property ["a"]: expected (boolean) false but got (object) object "[object Object]"
FAIL 8 assert_object_equals: property ["b"]: expected null but got object "[object Object]"
FAIL 9 assert_object_equals: property ["c"]: expected object "[object Object]" but got null
FAIL 10 assert_object_equals: property ["d"]: expected (object) object "[object Object]" but got (boolean) false
FAIL 11 assert_object_equals: property ["x"]["y"]["z"]: expected true but got false

Consider the following tests:

test(function() {
    assert_object_equals(false, true);
}, "1");
test(function() {
    assert_object_equals(1, true);
}, "2");
test(function() {
    assert_object_equals(false, {});
}, "3");
test(function() {
    assert_object_equals({}, false);
}, "4");
test(function() {
    assert_object_equals("abc", ["a", "b", "c"]);
}, "5");
test(function() {
    assert_object_equals(["a", "b", "c"], "abc");
}, "6");
test(function() {
    assert_object_equals({a:{}}, {a:false});
}, "7");
test(function() {
    assert_object_equals({b:{}}, {b:null});
}, "8");
test(function() {
    assert_object_equals({c:null}, {c:{}});
}, "9");
test(function() {
    assert_object_equals({d:false}, {d:{}});
}, "10");
test(function() {
    assert_object_equals({x:{y:{z:false}}}, {x:{y:{z:true}}});
}, "11");

Previously, the first 8 would incorrectly pass, and the last 3 would
give confusing error messages:

FAIL 9 assert_object_equals: property "c" expected object "[object Object]" got object "[object Object]"

FAIL 10 assert_object_equals: property "d" expected object "[object Object]" got object "[object Object]"

FAIL 11 assert_object_equals: property "z" expected object "[object Object]" got object "[object Object]"

After this patch, all the cases fail correctly and have clear error messages:

FAIL 1 assert_object_equals: expected true but got false

FAIL 2 assert_object_equals: expected (boolean) true but got (number) 1

FAIL 3 assert_object_equals: expected (object) object "[object Object]" but got (boolean) false

FAIL 4 assert_object_equals: expected (boolean) false but got (object) object "[object Object]"

FAIL 5 assert_object_equals: expected (object) ["a", "b", "c"] but got (string) "abc"

FAIL 6 assert_object_equals: expected (string) "abc" but got (object) ["a", "b", "c"]

FAIL 7 assert_object_equals: property ["a"]: expected (boolean) false but got (object) object "[object Object]"

FAIL 8 assert_object_equals: property ["b"]: expected null but got object "[object Object]"

FAIL 9 assert_object_equals: property ["c"]: expected object "[object Object]" but got null

FAIL 10 assert_object_equals: property ["d"]: expected (object) object "[object Object]" but got (boolean) false

FAIL 11 assert_object_equals: property ["x"]["y"]["z"]: expected true but got false
@hoppipolla-critic-bot
Copy link

Critic review: https://critic.hoppipolla.co.uk/r/5759

This is an external review system which you may optionally use for the code review of your pull request.

In order to help critic track your changes, please do not make in-place history rewrites (e.g. via git rebase -i or git commit --amend) when updating this pull request.

@jgraham
Copy link
Member

jgraham commented Aug 27, 2015

Thank you for the patch, but I really really just want this to die. It's too complex to be good for testing.

@jgraham jgraham closed this Aug 27, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants