Module:OsmPageTitleParser/testcases

From OpenStreetMap Wiki
Jump to navigation Jump to search
-- Unit tests for [[Module:Page]]. Click talk page to run tests.
local p = require('Module:UnitTests')
local parser = require('Module:OsmPageTitleParser')

function p:tst(titleStr, expected, expectedParseFail)
  local title = mw.title.new(titleStr)
  local obj = {}
  local result = parser.parseTitleToObj(obj, title)
  if obj.language then
    obj.language = obj.language:getCode()
  end
  
  if not result then obj._parseFailed = true end
  if expectedParseFail then expected._parseFailed = true end

  self:equals_deep(titleStr, obj, expected, {nowiki=1})
end

function p:test_keyprefixes_en()
  self:tst('Key:something:*', {key='something:', language='en'})
end

function p:test_keys_en()
  self:tst('Key:something', {key='something', language='en'})
  self:tst('Key:some thing', {key='some_thing', language='en'})
  self:tst('Key:some_thing', {key='some_thing', language='en'})
  self:tst('Key:some:thing', {key='some:thing', language='en'})
  self:tst('KEY::some:thing', {key=':some:thing', language='en'})
  self:tst('Key:some:thing:aa', {key='some:thing:aa', language='en'})
  self:tst('Key:some:thing:aa:', {key='some:thing:aa:', language='en'})
end

function p:test_keys_international()
  self:tst('ko:key:some:thing', {key='some:thing', language='ko'})
  self:tst('kO:key:some:thing:o', {key='some:thing:o', language='ko'})
  self:tst('kO:key:so me:thing:o:', {key='so_me:thing:o:', language='ko'})
  self:tst('No:key:abc', {key='abc', language='no'})
end

function p:test_keys_international_known_NS()
  self:tst('ru:Key:something', {key='something', language='ru'})
  self:tst('rU:KEY:some:thing:o', {key='some:thing:o', language='ru'})
  self:tst('rU:keY:so me:thing:o:', {key='so_me:thing:o:', language='ru'})
end

function p:test_tags_en()
  self:tst('Tag:something=abc', {key='something', value='abc', language='en'})
  self:tst('Tag:some:thing=abc:xyz', {key='some:thing', value='abc:xyz', language='en'})
  self:tst('TAG::some:thing=aa=bb=c', {key=':some:thing', value='aa=bb=c', language='en'})
  self:tst('taG:some:thing:aa bb=yy', {key='some:thing:aa_bb', value='yy', language='en'})
  self:tst('Tag:some:thing:aa:=a b', {key='some:thing:aa:', value='a_b', language='en'})
end

function p:test_tags_international_known_NS()
  self:tst('ru:Key:something=abc', {key='something', value='abc', language='ru'})
  self:tst('rU:KEY:some:thing:o=a=b:c', {key='some:thing:o', value='a=b:c', language='ru'})
  self:tst('rU:keY:so me:thing:o:===', {key='so_me:thing:o:', value='==', language='ru'})
  self:tst('RU:Moscow', {language='ru'}, true)
end

function p:test_tags_international()
  self:tst('ko:key:some:thing=abc x:yz', {key='some:thing', value='abc_x:yz', language='ko'})
  self:tst('kO:key:some:thing:o=:a:', {key='some:thing:o', value=':a:', language='ko'})
  self:tst('kO:key:so me:thing:o:=*', {key='so_me:thing:o:', value='*', language='ko'})
end

function p:test_unparsable_titles()
  self:tst('something', {language='en'}, true)
  self:tst('FR:something', {language='fr'}, true)
  self:tst('ko:something', {language='ko'}, true)
  self:tst('some:thing', {language='en'}, true)
  self:tst('FR:some:thing', {language='fr'}, true)
  self:tst('KO:some:thing', {language='ko'}, true)
  self:tst('', {}, true)
end

function p:test_language_pseudonamespaces()
  self:tst('bh:Main', {language='bh'}, true)
  self:tst('ca-valencia:Main', {language='ca-valencia'}, true)
  self:tst('gcf:Main', {language='gcf'}, true)
  self:tst('gsw:Main', {language='gsw'}, true)
  self:tst('kbp:Main', {language='kbp'}, true)
  self:tst('kfa:Main', {language='kfa'}, true)
  self:tst('mrw:Main', {language='mrw'}, true)
  self:tst('rcf:Main', {language='rcf'}, true)
  self:tst('sr-cyrl:Main', {language='sr-cyrl'}, true)
  self:tst('sr-latn:Main', {language='sr-latn'}, true)
  self:tst('sxu:Main', {language='sxu'}, true)
  self:tst('swg:Main', {language='swg'}, true)
  self:tst('sxu:Main', {language='sxu'}, true)
  self:tst('trp:Main', {language='trp'}, true)
  self:tst('tzm:Main', {language='tzm'}, true)
  self:tst('zgh:Main', {language='zgh'}, true)

  self:tst('Nds:Main', {language='nds'}, true)
  self:tst('Gcf:Test', {language='gcf'}, true)
  self:tst('Zh-hant:Relation', {language='zh-hant'}, true)
  self:tst('Pt-br:Main', {language='pt-br'}, true) -- not really sure if this is not supposed to be 'pt'
  self:tst('No:Main', {language='no'}, true)
end

function p:test_non_language_pseudonamespaces()
  self:tst('POI:Scotiabank', {language='en'}, true)
  self:tst('Switzerland:Berne', {language='en'}, true)
end

function p:test_talkpages()
  self:tst('Talk:Main', {language='en'}, true)
  self:tst('Talk:Pt:Creating an Account', {language='pt'}, true)
  self:tst('Talk:Tag:phone=tag', {language='en', key='phone', value='tag'}, true)
  self:tst('JA talk:Bus routes in Kanagawa', {language='ja'}, true)
  self:tst('Talk:POI:The Church of Jesus Christ of Latter-day Saints', {language='en'}, true)
end


function p:splt(keyvalue, expectedKey, expectedValue)
  local key, value = parser.splitKeyValue(keyvalue)
  self:equals_deep(keyvalue or '(nil)', {k=key, v=value}, {k=expectedKey, v=expectedValue}, {nowiki=1})
end

function p:test_splitKeyValue()
  self:splt('akey=avalue', 'akey', 'avalue')
  self:splt('akey', 'akey', nil)
  self:splt('akey=', 'akey', '')
  self:splt('akey=ava=lue', 'akey', 'ava=lue')
  self:splt('akey==ava=lue=', 'akey', '=ava=lue=')
  self:splt('', '', nil)
  self:splt(nil, nil, nil)
end

function p:langPrefix(value, expectedValue)
  self:equals(value or '(nil)', parser.langPrefix(value), expectedValue, {nowiki=1})
end

function p:test_langPrefix()
  self:langPrefix('en', '')
  self:langPrefix('EN', '')
  self:langPrefix('fr', 'FR:')
  self:langPrefix('Fr', 'FR:')
  self:langPrefix('PT', 'Pt:')
  self:langPrefix('', '')
  self:langPrefix(nil, '')
end

return p