Template:unit-test-equals

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
X mark.svg Expected: {{{expected}}}
Actual: {{{actual}}}



Usage[edit]

This template is for writing "unit-tests"; see [[Template:he-adj/helper/unit-tests]] (the "unit-test" page for {{he-adj/helper}}) for an example. It just compares {{{expected}}} to {{{actual}}} and makes sure that they're equal. If so, you get something like this:

Yes check.svg foo

and if not, you get something like this:

X mark.svg Expected: foo
Actual: bar

and the page is added to Category:Unit test failures, unless {{{nocat}}} is set to something non-blank.

Some notes:

  • Unfortunately, it won't work if the parameters evaluate to wiki-text containing <nowiki>…</nowiki> or <nowiki/>, since MediaWiki implements those via an intermediate step that replaces the whole thing with something like ~UNIQ3c52e5057596f478-nowiki-00000002-QINU~. If desired, this can generally be worked around by manually escaping whatever needed to be escaped.
  • To maximize testability, all instances of {{PAGENAME}} can be replaced with {{{PAGENAME|{{PAGENAME}}}}} (and likewise for other such magic words), allowing a unit-test page to "pretend" that it's on a different page by passing a PAGENAME= parameter.
    • If the page-name contains an apostrophe, then {{PAGENAME}} will contain &#39;, so templates that do sophisticated things with {{PAGENAME}} should be tested with PAGENAME={{PAGENAME:foo'bar}} (or whatnot) to make sure they behave as expected in that case.
  • This is a very strict equality test. Even something as minor as one internal space vs. two internal spaces will trigger a "unit-test" "failure", even though that's a non-semantic difference that HTMLtidy will remove anyway. If a "unit-test" "fails" and you can't figure out why, it may help to try something like {{unit-test-equals|expected={{urlencode:foo}}|actual={{urlencode:bar}}}} so that every single byte becomes explicitly visible and you can see where the difference is.
  • If a page of unit-tests is transcluded onto other pages (e.g. using {{unit-test-navbox}}), then you probably only want the unit-test page itself to appear in Category:Unit test failures. To that end, you can pass in nocat=<includeonly>1</includeonly>.

See also[edit]