Hello World

The Hello World is a customary small snippet of code for familiarizing yourself with a new system. Such a module could be something like the following

local lib = {}

function lib.say()
  return "Hello"
end

return lib

A similar but slightly more complex Lua module is available as HelloWorld.lua in the example section, and with a test module HelloWorld.pickle.lua.

A few slightly different test modules (“pickles”) can be made. The first one use an implicit subject, while the second use an explicit subject.

Implicit subject

The simplest form will use implicit transfer of test subjects, that is the parent module above this one with the correct content model will be used as the test subject. If the pickle page is named as Module:Hello World/testcase then the module at Module:Hello World will be used.

This gives the absolute simplest form as the following, given that the configuration is set to allow the common require to be stripped off

return describe (function()
  it 'say' (function()
    expect :toBe( "Hello" )
  end)
end)

Code 1: Implicit subject.

This form is what we use most of the time, as it nicely avoids a few lines of template code.

If we write out the code, with all extra statements, then it will be (TODO)

local lib = require 'Module:Hello World'
describe (lib) (function()
  it 'say' (function()
    expect :toBe( "Hello" )
  end)
end)
return result()

Code 2: Implicit subject written out.

Explicit subject

Sometimes we can't use the implicit form, usually because the subject isn't properly identified. This could happen if the subject must be built before it can be tested. In this case we must do an explicit transfer of test subjects, and that is usually done by a call inside a subject function. This function should return an instance of something that has or can be given a metatable. The subject is tucked away on a stack, and the top value reused whenever a test case (the description is a test case) lacks a subject.

This gives a form as the following

subject (function()
  return require 'Module:Hello World'
end)
describe (function()
  it 'say' (function()
    expect :toBe( "Hello" )
  end)
end)

Code 3: Explicit subject.

This form can be simplified into the following

describe :subject(require 'Module:Hello World') (function()
  it 'say' (function()
    expect :toBe( "Hello" )
  end)
end)

Code 4: Explicit subject simplified.

And even further simplified, given that a table is recognized as a subject, as the following

describe (require 'Module:Hello World') (function()
  it 'say' (function()
    expect :toBe( "Hello" )
  end)
end)

Code 5: Explicit subject simplified and rewritten.

Which is virtually the same as the form in code 2.

Notes

  • call style – how to make the MediaWiki linter accept the chained style
generated by LDoc 1.4.6