Modul:Wikidata/Formatters/testcases

Z Wikipedie, otevřené encyklopedie
Skočit na navigaci Skočit na vyhledávání
local builder = require 'Modul:Wikidata/build'
local ScribuntoUnit = require 'Modul:ScribuntoUnit'
local suite = ScribuntoUnit:new()

function suite:testDefaultFormatter()
	local myModule = require 'Modul:Wikidata/Formatters'
	local expected_options = {
		foo = 'bar'
	}
	local snak = builder.buildSnak{ datatype = 'string', value = 'foo' }
	local i = 0
	myModule.Formatters.string = {
		getRawValue = function(value, options)
			i = i + 1
			self:assertEquals('foo', value)
			self:assertDeepEquals(expected_options, options)
			return 'raw'
		end,
		formatRawValue = function(value, options)
			i = i + 1
			self:assertEquals('foo', value)
			self:assertDeepEquals(expected_options, options)
			return 'rawFormatted'
		end,
		formatValue = function(value, options)
			i = i + 1
			self:assertEquals('foo', value)
			self:assertDeepEquals(expected_options, options)
			return 'formatted'
		end,
	}
	self:assertEquals('raw', myModule.getRawValue(snak, expected_options))
	self:assertEquals('rawFormatted', myModule.formatRawValue('foo', 'string', expected_options))
	self:assertEquals('formatted', myModule.getFormattedValue(snak, expected_options))
	self:assertEquals(3, i)
end

function suite:testCustomFormatter()
	local myModule = require 'Modul:Wikidata/Formatters'
	local expected_options = {
		['value-formatter'] = 'xxx'
	}
	local snak = builder.buildSnak{ datatype = 'string', value = 'foo' }
	local i = 0
	myModule.Formatters.xxx = {
		getRawValue = function(value, options)
			self:assertTrue(false)
		end,
		formatRawValue = function(value, options)
			i = i + 1
			self:assertEquals('foo', value)
			self:assertDeepEquals(expected_options, options)
			return 'rawFormatted'
		end,
		formatValue = function(value, options)
			i = i + 1
			self:assertEquals('foo', value)
			self:assertDeepEquals(expected_options, options)
			return 'formatted'
		end,
	}
	myModule.getRawValue(snak, expected_options)
	self:assertEquals('rawFormatted', myModule.formatRawValue('foo', 'xxx', expected_options))
	self:assertEquals('formatted', myModule.getFormattedValue(snak, expected_options))
	self:assertEquals(2, i)
end

function suite:testRawSpecialValue()
	local myModule = require 'Modul:Wikidata/Formatters'
	for _, snaktype in ipairs{ 'somevalue', 'novalue' } do
		local snak = builder.buildSnak{ snaktype = snaktype }
		self:assertEquals(snaktype, myModule.getRawValue(snak))
	end
end

function suite:testFormattedSpecialValue()
	local myModule = require 'Modul:Wikidata/Formatters'
	for _, snaktype in ipairs{ 'somevalue', 'novalue' } do
		local snak = builder.buildSnak{ snaktype = snaktype }
		self:assertEquals(snaktype, myModule.getFormattedValue(snak, {}))
		self:assertEquals('xxx', myModule.getFormattedValue(snak, { [snaktype] = 'xxx' }))
	end
end

return suite