Modul:Wikidata/Formatters/wikibase-entityid/testcases

Z Wikipedie, otevřené encyklopedie
local myModule = require 'Modul:Wikidata/Formatters/wikibase-entityid'
local lib = require 'Modul:Wikidata/lib'
local builder = require 'Modul:Wikidata/build'
local ScribuntoUnit = require 'Modul:ScribuntoUnit'
local suite = ScribuntoUnit:new()

function suite:testGetRawValue()
	local value = builder.buildValueFromDatatype{ datatype = 'wikibase-item', value = 1085 }
	self:assertEquals('Q1085', myModule.getRawValue(value, {}))
	self:assertEquals(1085, myModule.getRawValue(value, { numeric = true }))
	local value = builder.buildValueFromDatatype{ datatype = 'wikibase-property', value = 1 }
	self:assertEquals('P1', myModule.getRawValue(value, {}))
	self:assertEquals(1, myModule.getRawValue(value, { numeric = true }))
end

function suite:testFormatValue()
	local options = {}
	local value = builder.buildValueFromDatatype{ datatype = 'wikibase-item', value = 1085 }
	for _, numeric in ipairs{ false, true } do
		options.numeric = numeric
		self:assertEquals(
			myModule.formatEntityId('Q1085', options),
			myModule.formatValue(value, options)
		)
	end
end

function suite:testFormatEntityId()
	self:assertEquals( '[[Praha|Praha]]', myModule.formatEntityId( 'Q1085' ) )
	self:assertEquals( mw.wikibase.label( 'Q4115189' ), myModule.formatEntityId( 'Q4115189' ) )
	self:assertEquals(
		lib.getLinkWhenNonexistingLabel( 'Q6' ), -- neexistující položka
		myModule.formatEntityId( 'Q6' )
	)
	self:assertEquals(
		'[[voy:en:Prague|Praha]]',
		myModule.formatEntityId( 'Q1085', { link = 'enwikivoyage' })
	)
end

function suite:testFormatEntityGender()
	local options = { label = 'gender' }
	local value = 'Q36180'
	options.id = 'Q157970' -- Božena Němcová (žena)
	self:assertStringContains( 'spisovatelka', myModule.formatEntityId( value, options ), true )
	self:assertStringContains( '[[Spisovatel|', myModule.formatEntityId( value, options ), true )
	options.id = 'Q155855' -- Karel Čapek (muž)
	self:assertNotStringContains( 'spisovatelka', myModule.formatEntityId( value, options ), true )
	self:assertStringContains( 'spisovatel', myModule.formatEntityId( value, options ), true )
	self:assertStringContains( '[[Spisovatel|', myModule.formatEntityId( value, options ), true )
	options.id = 'Q1085' -- Praha (eh?)
	self:assertNotStringContains( 'spisovatelka', myModule.formatEntityId( value, options ), true )
	self:assertStringContains( 'spisovatel', myModule.formatEntityId( value, options ), true )
	self:assertStringContains( '[[Spisovatel|', myModule.formatEntityId( value, options ), true )
end

function suite:testGetLinkWhenNonexistingLabel()
	self:assertStringContains( 'Q6', lib.getLinkWhenNonexistingLabel( 'Q6' ), true )
end

function suite:testFormatUnit()
	local options = { label = 'unitsymbol' }
	self:assertEquals('[[Metr|m]]', myModule.formatEntityId('Q11573', options))
	self:assertEquals(myModule.formatEntityId('Q1085', {}), myModule.formatEntityId('Q1085', options))
end

return suite