Jump to content

Module:Convert/tester/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 03:39, 11 October 2013 (new features). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This module runs unit tests to compare template output with expected text.

While intended for testing Module:Convert, the tester should be useful with other templates that require many tests using a simple format for the test input.

Example

It is not necessary to save the testcases page before viewing test results. For example, Module:Convert/sandbox/testcases could be edited to change the tests. While still editing that page, paste "Module talk:Convert/sandbox/testcases" (without quotes) into the page title box under "Preview page with this template", then click "Show preview".

The testcases talk page (for example, Module talk:Convert/sandbox/testcases) contains:

{{#invoke:convert/sandbox/testcases|run_tests}}

The testcases module page (for example, Module:Convert/sandbox/testcases) may contain:

local tests = [==[

A template to be tested must be at the start of a line.
Lines which do not start with a template are ignored.
{{convert/sandbox|1|acre|lk=on}}            1 [[acre]] (0.40 [[hectare|ha]])
{{convert/sandbox|1|m2|acres|lk=on}}        1 [[square metre]] (0.00025 [[acre]]s)
{{convert/sandbox|0.16|/l|2|disp=table}}    align="right"|0.16\n|align="right"|0.61

]==]

local p = require('Module:Convert/tester')
p.tests = tests
return p

Format

Tests are extracted from a multiline string. Any line that does not start with a template is ignored. Processed lines start with a template, followed by whitespace, then the wikitext that should result from expanding the template.

The expected output must be entered in a single line. If the template outputs multiple lines, those lines must be joined with "\n" (two characters—backslash n).

The templates do not have to be the same, for example, the following tests would work:

local tests = [==[

{{convert|12|m}}                            12 metres (39 ft)
{{convert/sandbox|12|m}}                    12 metres (39 ft)
{{age|1989|7|23|2003|7|14}}                 13
{{age in days|2007|5|24|2008|4|23}}         335

]==]

In the results, the status column shows "Pass" if the output from the template exactly matches the expected text. If there is no expected text, the template output is shown in the Actual column with a blank status. If the given expected text differs from the template output, the template output is shown in the Actual column with status "Fail", and the number of fails is shown at the top of the page. Searching the page for "Fail" will find each problem. Any "Fail" result is followed by a row showing the nowiki actual and expected wikitext.

Specifying tests

If using a testcases module (as in the above example), the test text is assigned to p.tests before executing run_tests.

Alternatively, the test text can be read from any page, or from any section on any page. For example, the following wikitext could be entered in a sandbox:

== Mixed tests ==
<pre>
{{convert|12|m}}                            12 metres (39&nbsp;ft)
{{convert/sandbox|0.16|/l|2|disp=table}}    align="right"|0.16\n|align="right"|0.61
{{age in days|2007|5|24|2008|4|23}}         335
The following line is incorrect to demonstrate a "fail".
{{convert|12|m|lk=on}}                      12 [[meter]]s (39&nbsp;[[Foot|ft]])
The following line demonstrates the result when no expected text is provided.
{{convert/sandbox|1|-|5|in|mm|lk=on}}
</pre>

Given the above, the tests can be run as shown in the following section.

Running tests from any page

Entering either of the following lines of wikitext in a sandbox or talk page would run the tests found at the specified location. The first line would show all tests on page "Template talk:Example", while the second would show only those tests on that page that are in the "Mixed tests" section.

{{#invoke:convert/tester|run_tests|page=Template talk:Example}}
{{#invoke:convert/tester|run_tests|page=Template talk:Example|section=Mixed tests}}

As a demonstration, the following line is used to produce the table shown below.

{{#invoke:convert/tester|run_tests|page=Module:Convert/tester/doc|section=Specifying tests}}

3 tests failed, 1 test ignored because expected text is blank.

Template Expected Actual, if different Status
{{convert|12|m}} 12 metres (39&nbsp;ft) 12 metres (39 ft) Fail
(above, nowiki) 12 metres (39&amp;nbsp;ft) 12 metres (39&nbsp;ft)
{{convert/sandbox|0.16|/l|2|disp=table}} align="right"|0.16
|align="right"|0.61
style="text-align:right;"|0.16
|style="text-align:right;"|0.61
Fail
(above, nowiki) align="right"|0.16
|align="right"|0.61
style="text-align:right;"|0.16
|style="text-align:right;"|0.61
{{age in days|2007|5|24|2008|4|23}} 335 Pass
{{convert|12|m|lk=on}} 12 meters (39&nbsp;ft) 12 metres (39 ft) Fail
(above, nowiki) 12 [[meter]]s (39&amp;nbsp;[[Foot|ft]]) 12 [[metre]]s (39&nbsp;[[Foot (unit)|ft]])
{{convert/sandbox|1|-|5|in|mm|lk=on}} 1–5 inches (25–127 mm)