模板:Test case

維基百科,自由的百科全書
文件圖示 模板文件[檢視] [編輯] [歷史] [清除快取]


這個模板生成兩個或更多模板的測試用例。每個模板都使用相同的參數調用,測試用例可以以各種不同的格式顯示。

傳遞給這個模板的所有參數都會傳遞到測試用例模板,除了以下劃線字符("_")開頭的參數,這些參數被保留供內部使用。

如果沒有顯式指定模板,模塊將使用當前頁面的基本頁面作為第一個模板,以及其/sandbox子頁面作為第二個模板。如果只顯式指定了一個模板,則使用其/sandbox子頁面作為第二個模板。

Usage[編輯]

Basic syntax

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

Common syntax

以下選項適用於所有不同的測試用例格式。

{{test case
| _format      = 
| _output      = 
| _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 used to display the test cases. This can be "columns", "rows", "tablerows", "inline" or "default".
_output Allows changes in how the template's output is displayed. The default output is displayed when the _output parameter is not specified or when it is set to any unsupported value. A nowiki version of the template's output is displayed when |_output=nowiki; this is helpful when there are differences in the underlying wikitext which are not visible in the default output. Both the default output and nowiki version are displayed when |_output=nowiki+.
_collapsible If set to "y" or "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. See #Collapsible test cases for other parameters which only work when _collapsible is enabled.
_template1, _template2, _template3... 要處理的模板的名稱。默認情況下,_template1 是當前頁面的基本頁面。如果指定了 _template1,則 _template2 的默認值是其 /sandbox 子頁面;如果沒有指定 _template1,則 _template2 的默認值是當前頁面的基本頁面的 /sandbox 子頁面。模板名稱應該按照在正常模板調用中使用的方式輸入,即"3x",而不是"Template:3x"。如果需要測試主名稱空間中的頁面,可以使用冒號前綴,即":文章名稱"。
_template 作為 _template1 的別名。如果同時指定了 _template 和 _template1,則 _template 會優先使用。
_showtemplate1, _showtemplate2, _showtemplate3... If this is set to "no", then no output is shown for that template. This is useful if you only want to show the main template, or if you only want to show the sandbox template.
_heading1, _heading2, _heading3... Custom headings for each of the templates.
_showheader If this is set to "n" or "no", then no headers are shown.
_before, _after Optional text to display before and after the output of each template.
_resetRefs If set to "y" or "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 - white space 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 用於在 <nowiki>...</nowiki> 標籤內指定模板代碼。這與 _showcode 選項一起使用,以顯示模板調用並保留空格。您必須使用魔術詞 "__TEMPLATENAME__" 而不是模板名稱。例如:| _code = <nowiki>{{__TEMPLATENAME__|first|second|foo=bar}}</nowiki>。如果您發現自己經常使用此選項,可以考慮使用 {{test case nowiki}},它允許您從 nowiki 標籤中的模板代碼生成整個測試用例,避免您兩次鍵入所有參數。注意:由於 nowiki 標籤的工作方式,當與此參數一起使用時,模板將把 HTML 實體 &lt;、&gt; 和 &quot; 視為文字字符 <、> 和 "。

Test case formats
[編輯]

Default format
[編輯]

當未指定 _format 或指定 |_format=default 時使用此格式。它不包含任何額外的參數。

Example

{{ombox}}

{{ombox/sandbox}}

Columns format
[編輯]

使用 |_format=columns 將測試用例以表格形式並排放置。這對於比較長而窄的模板,如信息框,非常有用。它具有以下額外的參數:

參數 描述
_class 表格的HTML類。使用 |_class=wikitable 將表格格式化為 wikitable。
_style 表格的自定義內聯 CSS。
_caption 表格的自定義標題。默認為 "Side by side comparison"。
_rowheader 包含模板的行的額外標題。
_heading0 如果提供了 _rowheader,則是模板行中它上方的可選標題。

示例

左右對比
{{ombox}}{{ombox/sandbox}}

Rows and tablerows formats
[編輯]

使用 |_format=rows|_format=tablerows 將測試用例以表格形式一個在另一個上方排列。這兩種格式在標題的放置上有所不同:rows 將它們放在上方,而tablerows 將它們放在模板輸出的左側。它具有以下額外的參數:

參數 描述
_class 表格的HTML類。使用 |_class=wikitable 以將表格格式化為wikitable。
_style 表格的自定義內聯CSS。
_caption 表格的自定義標題。

Example – rows

{{ombox}}
{{ombox/sandbox}}

Example – tablerows

{{ombox}}
{{ombox/sandbox}}

Inline format
[編輯]

使用 |_format=inline 可以將測試用例完全顯示在一行上。如果與顯示多行的模板一起使用,可能會產生意外的結果。該格式通過在每行輸出前放置 * 來創建一個項目符號列表。它具有以下額外的參數:

參數 描述
_addline 在列表中,模板輸出後要添加的文本。
_prefix 替換每行輸出前的 * 的字符。使用 |_prefix=# 將顯示一個編號列表

注意:由於 bug 14974 的原因,由於這個格式以一個項目符號開始,模板輸出的開頭將始終插入一個換行符。

默認:

  • Lorem ipsum Lorem ipsum Lorem ipsum
  • Lorem ipsum Lorem ipsum Lorem ipsum

使用|_showcode=yes

  • {{3x|Lorem ipsum }} → Lorem ipsum Lorem ipsum Lorem ipsum
  • {{3x/sandbox|Lorem ipsum }} → Lorem ipsum Lorem ipsum Lorem ipsum

使用|_prefix=#|_addline=

  1. {{Height|m=1.51}} → 1.51公尺(4英尺1112英寸)
  2. {{Height/sandbox|m=1.51}} → 1.51公尺(4英尺1112英寸)
  3. {{Cvt|1.51|m|frac=2}} → 1.51米(4英尺1112英寸)

輸出[編輯]

_output 參數允許更改模板輸出的顯示方式。它應該適用於任何測試用例格式;以下示例使用|format=tablerows

默認輸出[編輯]

默認輸出在未指定_output參數或將其設置為任何不受支持的值時顯示;此示例使用|_output=default

{{URL}} example.com
{{Official website}} example.com

Nowiki 輸出[編輯]

當使用|_output=nowiki時,顯示模板輸出的nowiki版本。如果在默認輸出中看不到的底層wikitext存在差異,這將非常有幫助。

{{URL}} <span class="url">[https://www.example.com example.com]</span>
{{Official website}} <span class="official-website"><span class="url">[https://www.example.com example.com]</span></span>

Nowiki+ 輸出[編輯]

當使用|_output=nowiki+時,同時顯示默認輸出和nowiki版本。

{{URL}} example.com
<span class="url">[https://www.example.com example.com]</span>
{{Official website}} example.com
<span class="official-website"><span class="url">[https://www.example.com example.com]</span></span>

Collapsible test cases
[編輯]

如果 _collapsible 設置為 "y" 或 "yes",則測試用例將變為可摺疊。如果所有模板輸出相同,測試用例會摺疊並顯示一個綠色標題。如果任何模板輸出不同,測試用例將展開並顯示一個黃色標題。它具有以下額外的參數:

參數 描述
_title 使用 |_title= 顯示可摺疊測試用例的自定義標題。
_titlecode 使用 |_titlecode=yes 將模板代碼顯示為可摺疊測試用例的標題。
_notcollapsed 如果設置為 yyes,在加載頁面時測試用例將始終展開。如果設置為 nno,在加載頁面時測試用例將始終摺疊。此設置覆蓋默認功能,即除非兩個或更多輸出不匹配,否則測試用例將被摺疊。

Examples

Default title[編輯]

{{ombox}}

{{ombox}}

{{ombox/sandbox}}

With _title[編輯]

Using the columns format
左右對比
{{ombox}}{{ombox/sandbox}}
Using the rows format
{{ombox}}
{{ombox/sandbox}}
Example of differing output

{{ombox}}

{{tmbox}}

With _titlecode=yes[編輯]

This is useful for templates with a small number of parameters. It displays the template code in the title, eliminating the need for both |_title= and |_showcode=yes. For actual tests implemented with |_titlecode=yes, see Template:URL/testcases.

{{URL|www.example.com}}
{{URL}} www.example.com
<span class="url">[http://www.example.com www<wbr/>.example<wbr/>.com]</span>
{{URL/sandbox}} www.example.com
<span class="url">[http://www.example.com www<wbr/>.example<wbr/>.com]</span>

With _notcollapsed=yes[編輯]

If |_notcollapsed=yes, the test case will always be expanded upon loading the page. This setting overrides the default functionality in which the test case is collapsed unless two or more outputs do not match. The following example would normally be collapsed, as indicated by green background of title:

Example of same output

{{ombox}}

{{ombox}}

With _notcollapsed=no[編輯]

If |_notcollapsed=no, the test case will always be collapsed upon loading the page. This setting overrides the default functionality in which the test case is collapsed unless two or more outputs do not match. The following would normally be expanded, as indicated by yellow background of title:

Example of differing output

{{ombox}}

{{tmbox}}

附加示例[編輯]

使用 _showcode[編輯]

用法
{{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.}}
輸出
{{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}}

使用 _showcode,通過 _code 使用 nowiki 調用[編輯]

用法
{{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.
}}
輸出
{{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}}

使用 _showcode 和 _collapsible[編輯]

用法
{{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.}}
輸出
{{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}}

Technical limitations[編輯]

該模板具有以下已知的技術限制:

  • 如果處理整個頁面的時間超過10秒,將生成錯誤。這是基於 Module:Template test caseScribunto擴展中的一個故意設置的限制。如果一個測試用例頁面加載時間超過10秒,可能需要將其拆分為不同的頁面。
  • 在生成與 |_showcode=一起使用的模板代碼時,命名參數中的空格被忽略,而像|1=這樣的編號參數可能會被錯誤地顯示為位置參數,重複的參數也會被忽略。這是因為模板參數在傳遞給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 each extension tag (e.g. <ref>, <references>, <gallery> or <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 unique hexadecimal string. 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. all <ref>...</ref> tags are treated as though they are equal, but should be adequate for most uses.

參見[編輯]

調用Module:Template test case的模板: