Edge Rewrite
Jump to content

Module:Goban/testcases

From Wikipedia, the free encyclopedia
-- Unit tests for [[Module:Goban]]. Click talk page to run tests.
local p = require('Module:UnitTests')

local goban = require('Module:Goban')

local DEFAULT19 = {
	rows = '19', cols = '19', u = 'u', d = 'd', l = 'l', r = 'r',
	hoshi = '4,4;4,10;4,16;10,4;10,10;10,16;16,4;16,10;16,16',
}

local function render(cfg, args)
	local parent = mw.getCurrentFrame():newChild{ title = 'Template:Goban', args = args }
	local child = parent:newChild{ title = 'Module:Goban', args = cfg }
	return goban.board(child)
end

-- Number of times a class attribute appears in the output.
local function count(out, class)
	return select(2, out:gsub('class="' .. class .. '"', ''))
end

function p:test_bare_19x19()
	local out = render(DEFAULT19, {})
	self:equals('wrapper', out:find('<div class="goban" style="font-size:24px">', 1, true) ~= nil, true)
	self:equals('19 rows', count(out, 'goban%-row'), 19)
	self:equals('361 points', select(2, out:gsub('class="goban%-pt', '')), 361)
	self:equals('9 hoshi', count(out, 'goban%-pt goban%-hoshi'), 9)
	self:equals('corners', count(out, 'goban%-pt goban%-ul') + count(out, 'goban%-pt goban%-ur')
		+ count(out, 'goban%-pt goban%-dl') + count(out, 'goban%-pt goban%-dr'), 4)
	self:equals('no images', out:find('[[File:', 1, true), nil)
end

function p:test_size_and_borders()
	local out = render({ rows = '5', cols = '5' }, { [26] = '20', u = 'u', r = 'r' })
	self:equals('size', out:find('font-size:20px', 1, true) ~= nil, true)
	self:equals('top-right corner', count(out, 'goban%-pt goban%-ur'), 1)
	self:equals('no left edge', count(out, 'goban%-pt goban%-l'), 0)
	local bad = render({ rows = '5', cols = '5' }, { [26] = 'big' })
	self:equals('invalid size falls back', bad:find('font-size:24px', 1, true) ~= nil, true)
end

function p:test_tiles()
	local out = render({ rows = '2', cols = '5' },
		{ 'b', 'wT', 'bS', '07', '10', 'A', 'X', '-c', 'uc', '3' })
	local three = render({ rows = '1', cols = '2' }, { '101', '250' })
	self:equals('black stone', out:find('<span class="goban-stone goban-b"></span>', 1, true) ~= nil, true)
	self:equals('triangle', out:find('<span class="goban-stone goban-w goban-T">\226\150\179</span>', 1, true) ~= nil, true)
	self:equals('square', out:find('<span class="goban-stone goban-b goban-S"></span>', 1, true) ~= nil, true)
	self:equals('odd move is black', out:find('<span class="goban-stone goban-b">07</span>', 1, true) ~= nil, true)
	self:equals('even move is white', out:find('<span class="goban-stone goban-w">10</span>', 1, true) ~= nil, true)
	self:equals('three-digit black', three:find('<span class="goban-stone goban-b goban-3">101</span>', 1, true) ~= nil, true)
	self:equals('three-digit white', three:find('<span class="goban-stone goban-w goban-3">250</span>', 1, true) ~= nil, true)
	self:equals('letter', out:find('<span class="goban-mark">A</span>', 1, true) ~= nil, true)
	self:equals('X', out:find('<span class="goban-mark goban-X">X</span>', 1, true) ~= nil, true)
	self:equals('ringed hoshi', out:find('<span class="goban-pt goban-hoshi goban-c"></span>', 1, true) ~= nil, true)
	self:equals('ringed edge', out:find('<span class="goban-pt goban-u goban-c"></span>', 1, true) ~= nil, true)
	self:equals('digit label', out:find('<span class="goban-mark goban-label">3</span>', 1, true) ~= nil, true)
end

function p:test_unknown_code_is_escaped()
	local out = render({ rows = '1', cols = '1' }, { 'b<x>' })
	self:equals('escaped', out:find('goban-unknown">b&#60;x&#62;</span>', 1, true) ~= nil, true)
end

return p