Module:CLI/testcases

From Commons Wiki
Jump to navigation Jump to search

Template:Testcases module


-- Set up the test suite.
local suite = require('Module:ScribuntoUnit'):new()
local cli = require('Module:CLI/testcases/title')
local getLog, clearLog = mw.getLogBuffer, mw.clearLogBuffer

local GENERIC_HELP = [[
Command-line interface for Scribunto title library (mw.title) in debug console.

General command line sypnosis:
    p "<command> [<a> | -a <string>] [<b> | -b <string>] [<c> | -c <string>] [<d> | -d <string>] [--output | -o]"

Commands available in Module:CLI/testcases:
    makeTitle               : Generates a namespace-specific Scribunto title object.
    compare                 : Computes sort order for Scribunto title objects.
    equals                  : Whether two title identifiers or strings are identical.
    getCurrentTitle         : Fetches a Scribunto title object for the current page.
    new                     : Fetches data from a new Scribunto title object.

Command line options available in Module:CLI/testcases:
    1 | a                   : Object field in "getCurrentTitle", namespace of title in "makeTitle", or target title/identfier.
    2 | b                   : Object field in "new", article title/identifier in "makeTitle", or comparison title/identfier.
    3 | c                   : Method subpage or URL query in "new", or fragment in "makeTitle".
    4 | d                   : Method protocol in "new", or interwiki prefix in "makeTitle".
    output | o              : Return output instead of sending to console.
]]

local COMMAND_HELP = [[
equals: Whether two title identifiers or strings are identical.

Command sypnosis:
    p "equals [<a> | -a <string>] [<b> | -b <string>] [--output | -o]"

Command line options available in "equals":
    1 | a                   : Object field in "getCurrentTitle", namespace of title in "makeTitle", or target title/identfier.
    2 | b                   : Object field in "new", article title/identifier in "makeTitle", or comparison title/identfier.
    output | o              : Return output instead of sending to console.
]]

function suite:testNoCommandLine()
    suite:assertThrows(cli)
end

function suite:testNoArgument()
    suite:assertThrows(function()
        cli ''
    end)
    clearLog()
end

function suite:testUnterminatedQuote()
    suite:assertThrows(function()
        cli 'help "'
    end)
    clearLog()
end

function suite:testHelp()
    clearLog()

    cli 'help equals'
    self:assertEquals(COMMAND_HELP, getLog())
    clearLog()

    cli 'equals --help'
    self:assertEquals(COMMAND_HELP, getLog())
    clearLog()

    cli '--help equals'
    self:assertEquals(COMMAND_HELP, getLog())
    clearLog()

    cli '-help equals'
    self:assertEquals(COMMAND_HELP, getLog())
    clearLog()

    cli 'help'
    self:assertEquals(GENERIC_HELP, getLog())
    clearLog()

    cli 'help undefined'
    self:assertEquals('"undefined" is not a recognised command. Printing help..\n\n' .. GENERIC_HELP, getLog())
    clearLog()

    cli '--help'
    self:assertEquals(GENERIC_HELP, getLog())
    clearLog()
end

function suite:testCommand()
    clearLog()
    cli 'equals "DEV:Policy" "Dev Wiki:Policy"'
    self:assertEquals('true\n', getLog())
    clearLog()
    local val = cli 'equals "DEV:Policy" "Dev Wiki:Policy" --output'
    self:assertEquals(true, val)
end

return suite