Module:Documentation/Documentation

This module is used to generate documentation for modules (e.g. Module:UtilsMarkup) and module-based templates (e.g. Template:Letter). The documentation is generated from a specification in the form of a Lua table. This specification can be re-used to parse and validate input, as well as generate. It can also be used to generate usage examples. For module functions, one can can specify what the expected output should be for a given example, turning it into an automated unit test.

The advantages of this design are as follows:
 * Documentation is standardized.
 * The documentation for a module/template is located on the same page as the module code, allowing for both to be edited together at the same time.
 * Template documentation is less likely to be incorrect or out of date. For example, there is no chance that the documentation says a parameter is optional when in fact it is required—the code that documents which parameters are required is the same code used to actually verify that required args are present when the template is used.

For documenting simple templates that are not module-based, see Template:Usage and Template:Examples.

Templates
To generate documentation for a module-based documentation, add this to the template page:

And add this to the module page:

Examples

 * Template:Franchise/Store Game
 * Template:Game Rating
 * Template:Letter
 * Template:Trading Quest

Usage
To create module documentation, create the page  with the following contents

Then on the actual module page, at the end of the script just before the final return, add a  field to the export table.

Tests
The  property transforms documentation examples into actual test cases, using a deep equality assertion between the expected output and actual output. If the assertion fails, the page is added to Category:Modules with failing tests.

This module therefore rolls documentation and testing into one. This approach was chosen over ScribuntoUnit and Docbunto for the following reasons:
 * Module functionality that is worth testing is worth documenting too, and vice-versa. The specification should be written only once, in one place.
 * For modules like Module:UtilsLayout that render a ton of markup, it is not practical to test with coded assertions. In these cases it suffices for module developers to manually check that the examples are outputting correct visuals.
 * A module's code, tests, and documentation are tightly coupled. It's important that they all be on the same page so that they can be edited simultaneously.

Schemas
This module integrates with Module:Schema to generate type documentation for function parameters. Note for example the parameter documentation for Module:UtilsLayout:


 * 
 * 
 * 


 * 

The above indicates that:
 * is expected to be a table with two keys:  and.
 * is required and of type string.
 * is optional and of type string.
 * is an optional number whose default value is 1.

Hovering over the key name indicates its expected data type. Types may be marked with symbols:

A given schema can have alternative structures. These are indicated with tabs.

Data
Category:Module Data can be documented and validated by adding the following:

Examples

 * Module:UtilsLayout/Tabs/Documentation
 * Module:UtilsTable/Documentation
 * Module:UtilsLanguage/Data/Documentation