Writing Lua modules

From semantic-mediawiki.org
Jump to: navigation, search
Table of Contents

This documentation is about supporting mws:Extension:Scribunto by providing Lua modules that can be used to {{#invoke:}} internal methods and are accessible via a module.

Available libraries and functions

smw.property.lua

smw.property.getPropertyType( property )
returns the type of a property

Example

{{#invoke: Module_name | function_name | arg1 | arg2 | arg3 ... }}

-- Module:SMW/Property
local p = {}

-- Return type
function p.type(frame)
		if not smw.property then
			return "smw.property module not found"
		end

		if frame.args[1] == nil then
			return "no parameter found"
		else
			type = smw.property.getPropertyType( frame.args[1] )
		end

		if type == nil then
			return "(no values)"
		end

		return type
end
return p

Invoked functions in Module:SMW/Property will return

* {{#invoke:SMW/Property|type|Modification date}} --> _dat
* {{#invoke:SMW/Property|type}} --> no parameter found

Unit tests

For details on how to write Lua unit tests see here or the tests/phpunit/includes/lua/PropertyLibraryTests.lua example.

local testframework = require 'Module:TestFramework'

-- Tests
local tests = {
	{ name = 'getPropertyType (empty property)', func = smw.property.getPropertyType,
		args = { '' },
		expect = { nil }
	},
	{ name = 'getPropertyType (known property)', func = smw.property.getPropertyType,
		args = { 'Modification date' },
		expect = { '_dat' }
	},
	{ name = 'getPropertyType (unknown property)', func = smw.property.getPropertyType,
		args = { 'Foo' },
		expect = { '_wpg' }
	}
}

return testframework.getTestProvider( tests )

In order to keep regressions to a minimum, please keep in mind the no test, no deployment policy.

See also