Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Allow templated error message in assertEquals #327

Closed
jakub-g opened this Issue Jan 11, 2013 · 2 comments

Comments

Projects
None yet
1 participant
Collaborator

jakub-g commented Jan 11, 2013

Today there's plenty of code like this in our test cases

this.assertEquals(input.value, "foo", "Input should be 'foo' got '" + input.value + "'");

It would be much more elegant if we could write

this.assertEquals(input.value, "foo", "Input should be %1, got %2");

By the way the code of assertEquals already displays expected and real value but only when there is no message passed -- however it's just too generic so it's not very helpful to quickly find the source of wrong assertion (especially when there are assertions in a loop etc.):

        assertEquals : function (value1, value2, optMsg) {
            this.assertTrue((value1 === value2), optMsg || ("Expected : " + value1 + ". Got : " + value2));
        },
Collaborator

jakub-g commented Jan 11, 2013

Actually I also think about sth like this:
this.assertEquals(input.value, "foo", "Input's value: %0");
in which %0 would generated the expected: ... got: ... value automatically.

That way we can keep the backward compat for those invocations which use custom error msg, while simplifying and making more readable the test cases code.

It could work as a simple string replacement, looking for %0, %1, %2.

Collaborator

jakub-g commented Jan 14, 2013

The other thing that can be improved by the way is adding optional "assert identifier" in assertEquals (the same way we have in $assert) which can be derived e.g. from line number at the time of writing the assertion. However since it must done in a backward-compatible way, it will have to be the last param instead of the first (unless we want to reinvent new function names which surely we don't want to do).

jakub-g added a commit to jakub-g/ariatemplates that referenced this issue Jan 14, 2013

feat #327 improve error messages in assertEquals/NotEquals/JsonEquals
1. Allow usage of %1 %2 in assertEquals messages

%1 and %2 are shortcuts for value1 and value2 params passed to
%assertEquals and can be used as placeholders in the error message.

This commit implements the feature (Assert.js) and changes some of the
unit tests to use the new capability.

2. Fix the obvious issue with the error message of
'assertNotEquals'.

3. Write serialized versions of JSON entities in assertJsonEquals
in case there's no default error message passed.

Close #327. Close #328.

@jakub-g jakub-g closed this in 48b49da Jan 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment