Modul:UnitTests/doc

A Wikiszótárból, a nyitott szótárból

This module facilitates creating unit tests for Lua modules.

Put the following at Module:name/testcases:

local tests = require('Module:UnitTests')

function tests:test_example()
	--[[ here be the tests ]]
end

return tests

Then put the following on Module:name/testcases/documentation:

{{#invoke:name/testcases|run_tests}}

Tests should be written as Lua methods whose names start with test. The self object contains the following methods, which may be called from the method:

preprocess_equals(text, expected, options)
Will check whether expanding templates in text results in expected.
preprocess_equals_many(prefix, suffix, cases, options)
preprocess_equals_preprocess(text1, text2, options)
Will check whether expanding templates in text1 and text2 results in the same string.
preprocess_equals_preprocess_many(prefix1, suffix1, prefix2, suffix2, cases, options)
equals(name, actual, expected, options)
Will check whether two primitive values are equal. name will be used as the row header. When the value is a table, equals_deep should be used.
equals_deep(name, actual, expected, options)
Will check whether two values, which may be tables, are equal. name will be used as the row header.

options should be given in a table or omitted. Currently, these are the options supported:

  • nowiki: Causes both the expected and the actual values to be wrapped in <nowiki> tags when rendering the results table.
  • comment: A comment to be added to the table.
  • display: A function to yield the form actually displayed in the table. This is used in testcases for pronunciation modules to make the IPA transcriptions display with the correct fonts.
  • show_difference: If this is set to true (or any truthy value besides a function), failing tests will have the first offending character highlighted in red (that is, the first character in the "actual" string that is different from the corresponding character in the "expected string"). If this is a function, the character will be highlighted using the function. (Currently only available in the equals checking function. The highlighter will highlight combining characters together with the characters they are placed on.)