Modul:Wikidata/lib/testcases
< Modul:Wikidata | lib
local myModule = require 'Modul:Wikidata/lib'
local builder = require 'Modul:Wikidata/build'
local ScribuntoUnit = require 'Modul:ScribuntoUnit'
local suite = ScribuntoUnit:new()
function suite:testAddWdClass()
self:assertStringContains( '>string<', myModule.addWdClass( 'string' ), true )
self:assertStringContains( 'class="wd"', myModule.addWdClass( 'string' ), true )
end
function suite:testAugmentArgs()
local out
out = myModule.augmentArgs({ precision = 11, ['qualifiers precision'] = 10 }, { precision = 9 }, 'qualifiers ')
self:assertEquals(10, out.precision)
out = myModule.augmentArgs({ precision = 11 }, { precision = 9 }, 'qualifiers ')
self:assertEquals(9, out.precision)
local args = { precision = 11 }
local defaults = { precision = 9 }
out = myModule.augmentArgs(args, defaults)
self:assertEquals(11, out.precision)
out.precision = 10
self:assertEquals(10, out.precision)
self:assertEquals(11, args.precision)
self:assertEquals(9, defaults.precision)
end
function suite:testGetInterwikiPrefix()
local sites = {
en = 'en:',
enwiki = 'w:en:',
commons = 'commons:',
commonswiki = 'w:commons:',
cswiki = 'w:cs:',
cswikiversity = 'v:cs:',
dewikiquote = 'q:de:',
q = 'q:',
wikidata = 'd:',
wikidatawiki = 'd:',
wikiquote = 'q:',
}
for site, prefix in pairs(sites) do
self:assertEquals(prefix, myModule.getInterwikiPrefix(site))
end
end
function suite:testCategory()
self:assertTrue(false)
end
function suite:testFormatDateRange()
local Time = require 'Modul:Time'
local Formatters = require 'Modul:Wikidata/Formatters'
local formatRawValue = function (value, options)
local _value = mw.clone(value)
_value.precision = math.min(options.precision, _value.precision)
local era = ''
if options.showera then
if _value.year < 1 then
era = ' BCE'
else
era = ' CE'
end
end
return tostring(_value) .. era
end
Formatters.Formatters.time = {
formatValue = function (value, options)
return formatRawValue(Time.newFromWikidataValue(value), options)
end,
getRawValue = Time.newFromWikidataValue,
formatRawValue = formatRawValue,
}
local provider = {
{ { begin = '1890-01-09', ending = '1938-12-25' }, 11, '1890-01-09 – 1938-12-25' },
{ { begin = '1890-01-09', ending = '1938-12-25' }, 10, '1890-01 – 1938-12' },
{ { begin = '1890-01-09', ending = '1938-12-25' }, 9, '1890–1938' },
{ { begin = '1890-01', ending = '1938-12' }, 10, '1890-01 – 1938-12' },
{ { begin = '1890-01-09', ending = nil }, 11, '1890-01-09' },
{ { begin = '1890-01-09', ending = nil }, 10, '1890-01' },
{ { begin = nil, ending = '1938-12-25' }, 11, '1938-12-25' },
{ { begin = nil, ending = '1938-12-25' }, 10, '1938-12' },
{ { begin = '1890', ending = '1890' }, 9, '1890' },
{ { begin = '1890', ending = '1890' }, 10, '1890' },
{ { begin = '1890-01', ending = '1890-12' }, nil, '1890-01 – 1890-12' },
{ { begin = '-100-07', ending = '-0044-03-15' }, 11, '-0100-07 – -0044-03-15 BCE' },
{ { begin = '-100-07', ending = '-0044-03-15' }, 10, '-0100-07 – -0044-03 BCE' },
{ { begin = '-100-07', ending = '-0044-03-15' }, nil, '-0100–-0044 BCE' },
{ { begin = '-100-07', ending = '0044-03-15' }, 11, '-0100-07 BCE – 0044-03-15 CE' },
{ { begin = '-100-07', ending = '0044-03-15' }, nil, '-0100 BCE – 0044 CE' },
}
for _, data in ipairs(provider) do
for key, value in pairs(data[1]) do
data[1][key] = builder.buildSnak{ datatype = 'time', value = value, type = 'value' }
end
local range = myModule.formatDateRange(data[1], { precision = data[2] })
if not data[1].ending then
local begin = Formatters.getFormattedValue(data[1].begin, { precision = data[2] })
self:assertStringContains(begin, range, true)
self:assertNotStringContains('–', range, true)
elseif not data[1].begin then
local ending = Formatters.getFormattedValue(data[1].ending, { precision = data[2] })
self:assertStringContains(ending, range, true)
self:assertNotStringContains('–', range, true)
else
self:assertEquals(data[3], range)
end
end
Formatters.Formatters.time = nil
end
function suite:testFormatDateRange_dateFormat()
self:assertEquals(
'* 1890',
myModule.formatDateRange(
{ begin = builder.buildSnak{ datatype = 'time', value = '1890-01-09', type = 'value' } },
{ ['begin-format'] = '* %s', ['end-format'] = '† %s', nolink = true }
)
)
self:assertEquals(
'† 1938',
myModule.formatDateRange(
{ ending = builder.buildSnak{ datatype = 'time', value = '1938-12-25', type = 'value' } },
{ ['begin-format'] = '* %s', ['end-format'] = '† %s', nolink = true }
)
)
end
function suite:testFormatFromPattern()
self:assertEquals(
'http://www.wikidata.org/wiki/abcdefg',
myModule.formatFromPattern( 'abcdefg', 'http://www.wikidata.org/wiki/$1' )
)
self:assertEquals(
'http://www.wikidata.org/wiki/abcdefg/abcdefg',
myModule.formatFromPattern( 'abcdefg', 'http://www.wikidata.org/wiki/$1/$1' )
)
self:assertEquals(
'http://www.wikidata.org/wiki/',
myModule.formatFromPattern( 'abcdefg', 'http://www.wikidata.org/wiki/' )
)
end
function suite:testGetEntityIdFromValue()
self:assertEquals(
'Q1',
myModule.getEntityIdFromValue( builder.buildValueFromDatatype{ datatype = "wikibase-item", value = 1 } )
)
self:assertEquals(
'P5',
myModule.getEntityIdFromValue( builder.buildValueFromDatatype{ datatype = "wikibase-property", value = 5 } )
)
end
function suite:testGetItemIdFromURI()
self:assertEquals( 'Q1985727', myModule.getItemIdFromURI('http://www.wikidata.org/entity/Q1985727') )
self:assertFalse( myModule.getItemIdFromURI('http://www.wikidata.org/entity/Foo') )
self:assertFalse( myModule.getItemIdFromURI('http://www.wikidata.org/entity/Q') )
self:assertFalse( myModule.getItemIdFromURI('http://www.wikidata.org/entity/') )
end
function suite:testGetLabelInLanguage()
self:assertTrue(false)
end
function suite:testIsOptionTrue()
local options = {
addcat = true,
addclass = 'true',
addlink = 'yes',
property = nil,
showmore = '1',
showsource = 1,
}
self:assertTrue( myModule.IsOptionTrue( options, 'addcat' ) )
self:assertTrue( myModule.IsOptionTrue( options, 'addclass' ) )
self:assertTrue( myModule.IsOptionTrue( options, 'addlink' ) )
self:assertTrue( myModule.IsOptionTrue( options, 'showmore' ) )
self:assertTrue( myModule.IsOptionTrue( options, 'showsource' ) )
self:assertFalse( myModule.IsOptionTrue( options, 'property' ) )
end
function suite:testIsPropertyId()
for _, val in ipairs{ 'P1', 'P999', 'P99999' } do
self:assertTrue( myModule.isPropertyId( val ) )
end
for _, val in ipairs{ '', 'xxx', 'P', 'P0', 'P00' } do
self:assertFalse( myModule.isPropertyId( val ) )
end
end
function suite:testIsSnakValue()
self:assertTrue( myModule.IsSnakValue( builder.buildSnak{
snaktype = 'value', datatype = 'wikibase-item', value = 1 } ) )
self:assertFalse( myModule.IsSnakValue( builder.buildSnak{ snaktype = 'somevalue' } ) )
self:assertFalse( myModule.IsSnakValue( builder.buildSnak{ snaktype = 'novalue' } ) )
end
function suite:testSimpleCompare()
local provider = {
{
first = 1,
second = 2,
expected = -1
},
{
first = 2,
second = 1,
expected = 1
},
{
first = 2,
second = 2,
expected = 0
},
{
first = 'x',
second = 'y',
expected = -1
},
}
for _, data in ipairs( provider ) do
self:assertEquals( data.expected, myModule.simpleCompare( data.first, data.second ) )
end
end
function suite:testTextToTable()
self:assertDeepEquals(
{ 'a', 'b', 'c' },
myModule.textToTable( 'a,b,c' )
)
self:assertDeepEquals(
{ 'a' },
myModule.textToTable( 'a' )
)
self:assertDeepEquals(
{ 'a', 'b', 'c' },
myModule.textToTable( 'a-b-c', { split_pattern = '-' } )
)
self:assertDeepEquals(
{ 'a', 'b', 'c' },
myModule.textToTable( { 'a', 'b', 'c' } )
)
end
return suite