跳去內容

Template:Test case/doc

出自維基百科,自由嘅百科全書

This template generates a test case for two or more templates. Each template is called with the same parameters, and the test case can be displayed in various different formats.

All parameters passed to this template are passed through to the test-case templates, with the exception of parameters starting with an underscore character ("_"), which are reserved for internal use.

If no templates are specified explicitly, the module uses the base page of the current page as the first template, and its /sandbox subpage as the second template. If only one template is specified explicitly, its /sandbox subpage is used as the second template.

Usage

[編輯]

Basic syntax

{{test case
| [param1] = [value1]
| [param2] = [value2]
| [param3] = [value3]
...
}}

Common syntax

The following options will work with all different test case formats.

{{test case
| _format      = 
| _collapsible = 
| _resetRefs   = 
| _code        = 
| _showcode    = 

| _template  = 
| _template1 = 
| _heading1  = 
| _template2 = 
| _heading2  = 
| _template3 = 
| _heading3  = 
...

| [param1] = [value1]
| [param2] = [value2]
| [param3] = [value3]
...
}}

Some test case formats accept additional parameters as well. See their sections for documentation.

Common parameters

[編輯]
Parameter Description
_format The format to display the test cases in. This can be "columns", "rows", "inline" or "default".
_template1, _template2, _template3... The names of the templates to be processed. The default of _template1 is the base page of the current page. If _template1 is specified, the default for _template2 is its /sandbox subpage; if not, the default for _template2 is the /sandbox subpage of the base page of the current page. Template names should be entered as you would use them in a normal template invocation, i.e. "3x", not "Template:3x". If you need to test a page in the main namespace you can use a colon prefix, i.e. ":article name".
_template An alias for _template1. If both _template and _template1 are specified, _template will take precedence.
_heading1, _heading2, _heading3... Custom headings for each of the templates.
_collapsible If set to "yes", the test case is made collapsible. The test case is collapsed and given a green heading if all the template outputs are the same. If any of the template outputs differ, the test case is expanded and given a yellow heading.
_resetRefs If set to "yes", any references generated by the templates tested are reset after each template is expanded.
_showcode Show the template code used to make the test case. If an invocation is specified with the _code parameter, then that is used. Otherwise, an approximation is generated from the arguments. This approximation is not perfect - whitespace in named arguments is lost, and duplicate arguments are ignored - but it should be fine for template code that is input all on one line without spaces between parameters.
_code Used to specify template code inside <nowiki>...</nowiki> tags. This is used with the _showcode option to display the template invocation while preserving whitespace. You must use the magic word "__TEMPLATENAME__" instead of the template name. For example: | _code = <nowiki>{{__TEMPLATENAME__|first|second|foo=bar}}</nowiki>. If you find yourself using this option a lot, you may want to consider using {{test case nowiki}}, which allows you to generate the entire test case from template code in nowiki tags, saving you from typing in all the arguments twice.

Note: due to the way nowiki tags work, the template treats the HTML entities &lt;, &gt; and &quot; as if they were the literal characters <, > and " when used with this parameter.

Test case formats

[編輯]

Default

[編輯]

The default test case format looks like this:


{{ombox}}

{{ombox/sandbox}}

Error: expandTemplate: template "ombox/sandbox" does not exist

It doesn't have any extra parameters.

Columns

[編輯]

The columns format arranges the test cases side by side in a table. This is useful for comparing long and thin templates such as infoboxes. It has the following extra parameters:

Parameter Description
_class HTML classes for the table. Use |_class=wikitable to format the table as a wikitable.
_style Custom inline CSS for the table.
_caption Custom caption for the table. The default is "Side by side comparison".
_rowheader An extra heading for the row containing the templates.
_heading0 If _rowheader is supplied, this is an optional heading above it in the template row.
_after This text is displayed after the output of each template.

Example


Side by side comparison
{{ombox}}{{ombox/sandbox}}
Error: expandTemplate: template "ombox/sandbox" does not exist

Rows

[編輯]

The rows format arranges the test cases in rows in a table. It has the following extra parameters:

Parameter Description
_class HTML classes for the table. Use |_class=wikitable to format the table as a wikitable.
_style Custom inline CSS for the table.
_caption Custom caption for the table.

Example


{{ombox}}
{{ombox/sandbox}}
Error: expandTemplate: template "ombox/sandbox" does not exist

Inline

[編輯]

The inline format is for test cases that can be displayed entirely on one line. If used with templates that display on multiple lines it may produce unexpected results. It doesn't have any extra parameters.

Examples (using Template:3x)

Default:


  • Lorem ipsum Lorem ipsum Lorem ipsum
  • Error: expandTemplate: template "3x/sandbox" does not exist

With _showcode:


  • {{3x|Lorem ipsum }} → Lorem ipsum Lorem ipsum Lorem ipsum
  • {{3x/sandbox|Lorem ipsum }}Error: expandTemplate: template "3x/sandbox" does not exist

Note: As this format starts with a bullet, a newline will always be inserted at the start of the template output due to bug 14974.

Collapsible test cases

[編輯]

You can use |_collapsible=yes with any format to turn it into a collapsible test case. If all the template outputs are the same the test case is collapsed and given a green heading. If any of the template outputs differ, the test case is expanded and given a yellow heading. You can set a custom title for collapsible test cases with the |_title= parameter.

Examples


{{ombox}}

{{ombox}}

{{ombox/sandbox}}

Error: expandTemplate: template "ombox/sandbox" does not exist

Using the columns format
Side by side comparison
{{ombox}}{{ombox/sandbox}}
Error: expandTemplate: template "ombox/sandbox" does not exist
Using the rows format
{{ombox}}
{{ombox/sandbox}}
Error: expandTemplate: template "ombox/sandbox" does not exist
Example of differing output

{{ombox}}

{{tmbox}}

Examples

[編輯]

With _showcode

[編輯]
Usage
{{test case|_template1=ombox|_showcode=yes|text=Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}}
Output
{{ombox|text=Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}}

{{ombox}}

{{ombox/sandbox}}

Error: expandTemplate: template "ombox/sandbox" does not exist

With _showcode, using a nowiki invocation via _code

[編輯]
Usage
{{test case|_template1=ombox|_showcode=yes
|_code = 
<nowiki>{{__TEMPLATENAME__
| text = Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}}</nowiki>
|text=Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}}
Output
{{ombox
| text = Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
}}

{{ombox}}

{{ombox/sandbox}}

Error: expandTemplate: template "ombox/sandbox" does not exist

With _showcode and _collapsible

[編輯]
Usage
{{test case|_template1=ombox|_showcode=yes|_collapsible=yes|text=Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}}
Output
{{ombox}}
{{ombox|text=Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}}

{{ombox}}

{{ombox/sandbox}}

Error: expandTemplate: template "ombox/sandbox" does not exist

Technical limitations

[編輯]

The template has the following known technical limitations:

  • An error will be generated if processing the entire page takes more than 10 seconds. This is an intentional limit in the Scribunto extension that runs Module:Template test case, on which this template is based. If a test cases page takes more than 10 seconds to load, you may need to split it up into different pages.
  • When generating the template code used with |_showcode= whitespace in named parameters is ignored, numbered parameters like |1= may be incorrectly displayed as positional parameters, and duplicate parameters are ignored. This is due to how template parameters are processed before they are passed to Module:Template test case.
  • When using a template invocation inside nowiki tags with the |_code= parameter, the HTML entities &lt;, &gt; and &quot; are converted to the literal characters <, > and ". This is due to the way nowiki tags work.
  • For collapsible test cases, all text inside extension tags (e.g. <ref>, <references>, <gallery> and <nowiki>) is treated as equal. These tags are converted to strip markers before they are passed to the template, and all strip markers include a random string as they have to be unique. Even if strip markers were unstripped before being compared, the same wikitext would not be guaranteed to result in exactly the same unstripped code. So this module ignores the content of strip markers for the purposes of testing equality. This has the unwanted side effect that, e.g. Some text<ref>foo</ref> is treated as equal to Some text<gallery>bar</gallery>, but should be adequate for most uses.

See also

[編輯]

Templates invoking Module:Template test case: