Extension:MWUnit/Extending MWUnit

This page has information about MWUnit's code. It is intended for developers that want to modify it or write code that ties into it. It is not intended for site administrators or end-users.

Defining new assertions
MWUnit provides a mechanism to define new assertions. To define a new assertion, an extension must register the MWUnitGetAssertionClasses hook:

Any assertion class must implement the  interface. This interface has the following methods:


 * getName: Returns the name of this assertion as used in the parser function magic word, without the `assert_` prefix.
 * shouldRegister: Returns true if and only if this assertion should be registered with the Parser. Allows extensions to define conditional assertions.
 * getRequiredArgumentCount: Returns the number of arguments required by the assertion.

These methods define metadata used by the assertion factory to register the assertion class. Additionally, the  method should be implemented. This method has variable arguments and is therefore not included in the interface.

The first argument passed to the  method is the reference to the error message, shown when the assertion fails. This parameter should be set in the function. The next parameters are the parameters passed to the parser call, with the last argument being the optional user-supplied error message. The function should return  if the assertion succeeded,   if the assertion failed or   when the assertion is deemed invalid/risky (i.e. invalid parameters or an exception occurred).

We will be defining the assertion, that only succeeds when the first argument is   and the second argument is. An example implementation of this looks like this:

Preventing parser functions from being mocked
Sometimes it is useful to disallow the mocking of certain parser functions, such as additional assertions you defined.

An extension can register the MWUnitGetReservedFunctions hook to add additional or remove reserved parser functions.