local t = require("Modul:UnitTests")
local lk = require("Modul:link")
local mut = require("Modul:grammar-link")
local nowiki = function(s)
return s:gsub('[\r\n][!]', {["\n!"] = "\n !", ["\r!"] = "\r !"})
end
function t:test01a_basic()
self:equals('mut.link("form1", "infl")', mut.link("form1", "infl"), '<span data-grammar-link="infl">' .. lk.link("form1") .. '</span>', {nowiki=1})
self:equals('mut.link(" form1 ", "infl")', mut.link(" form1 ", "infl"), '<span data-grammar-link="infl">' .. lk.link("form1") .. '</span>', {nowiki=1})
self:equals('mut.link("form1", "infl", "sv")', mut.link("form1", "infl", "sv"), '<span data-grammar-link="infl">' .. lk.link("form1", "sv") .. '</span>', {nowiki=1})
self:equals('mut.link("form1", "infl", "sv", "verb)', mut.link("form1", "infl", "sv", "verb"), '<span class="grammar-inline" data-lang="sv" data-h3="verb"><span data-grammar-link="infl">' .. lk.link("form1", "sv") .. '</span></span>', {nowiki=1})
self:equals('mut.link("form1", "adv")', mut.link("form1", "adv"), '<span data-grammar-link="adv">' .. lk.link("form1") .. '</span>', {nowiki=1})
self:equals('mut.link("form1", "adv", "de")', mut.link("form1", "adv", "de"), '<span data-grammar-link="adv">' .. lk.link("form1", "de") .. '</span>', {nowiki=1})
self:equals('mut.link("form1", "adv", "de", "verb)', mut.link("form1", "adv", "de", "verb"), '<span class="grammar-inline" data-lang="de" data-h3="verb"><span data-grammar-link="adv">' .. lk.link("form1", "de") .. '</span></span>', {nowiki=1})
self:equals('mut.link("form1", "perfpart")', mut.link("form1", "perfpart"), '<span data-grammar-link="perfpart">' .. lk.link("form1") .. '</span>', {nowiki=1})
self:equals('mut.link("form1", "perfpart", "en")', mut.link("form1", "perfpart", "en"), '<span data-grammar-link="perfpart">' .. lk.link("form1", "en") .. '</span>', {nowiki=1})
self:equals('mut.link("form1", "perfpart", "en", "verb)', mut.link("form1", "perfpart", "en", "verb"), '<span class="grammar-inline" data-lang="en" data-h3="verb"><span data-grammar-link="perfpart">' .. lk.link("form1", "en") .. '</span></span>', {nowiki=1})
end
function t:test01b_basic_export()
self:equals_deep('mut.linkExport("form1", "infl")', mut.linkExport("form1", "infl"), {"form1"}, {nowiki=1})
self:equals_deep('mut.linkExport("form1", "adv")', mut.linkExport("form1", "adv"), {"form1"}, {nowiki=1})
self:equals_deep('mut.linkExport("form1", "perfpart")', mut.linkExport("form1", "perfpart"), {"form1"}, {nowiki=1})
self:equals_deep('mut.linkExport(" form1 ", "infl")', mut.linkExport(" form1 ", "infl"), {"form1"}, {nowiki=1})
end
function t:test02a_multiple()
self:equals('mut.link("form1, form2", "infl")', mut.link("form1, form2", "infl"), '<span data-grammar-link="infl">' .. lk.link("form1") .. '</span>, <span data-grammar-link="infl">' .. lk.link("form2") .. '</span>', {nowiki=1})
self:equals('mut.link("form1, form2", "infl", "sv")', mut.link("form1, form2", "infl", "sv"), '<span data-grammar-link="infl">' .. lk.link("form1", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("form2", "sv") .. '</span>', {nowiki=1})
self:equals('mut.link("form1, form2", "infl", "sv", "verb)', mut.link("form1, form2", "infl", "sv", "verb"), '<span class="grammar-inline" data-lang="sv" data-h3="verb"><span data-grammar-link="infl">' .. lk.link("form1", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("form2", "sv") .. '</span></span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], [[form2]]", "infl"</nowiki>)', mut.link("[[form1]], [[form2]]", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]") .. '</span>, <span data-grammar-link="infl">' .. lk.link("[[form2]]") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], [[form2]]", "infl", "sv"</nowiki>)', mut.link("[[form1]], [[form2]]", "infl", "sv"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("[[form2]]", "sv") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], [[form2]]", "infl", "sv", "verb"</nowiki>)', mut.link("[[form1]], [[form2]]", "infl", "sv", "verb"), '<span class="grammar-inline" data-lang="sv" data-h3="verb"><span data-grammar-link="infl">' .. lk.link("[[form1]]", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("[[form2]]", "sv") .. '</span></span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], form2", "infl"</nowiki>)', mut.link("[[form1]], form2", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]") .. '</span>, <span data-grammar-link="infl">' .. lk.link("form2") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], form2", "infl", "sv"</nowiki>)', mut.link("[[form1]], form2", "infl", "sv"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("form2", "sv") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], form2", "infl", "sv", "verb"</nowiki>)', mut.link("[[form1]], form2", "infl", "sv", "verb"), '<span class="grammar-inline" data-lang="sv" data-h3="verb"><span data-grammar-link="infl">' .. lk.link("[[form1]]", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("form2", "sv") .. '</span></span>', {nowiki=1})
self:equals('<nowiki>mut.link("form1, [[form2]]", "infl"</nowiki>)', mut.link("form1, [[form2]]", "infl"), '<span data-grammar-link="infl">' .. lk.link("form1") .. '</span>, <span data-grammar-link="infl">' .. lk.link("[[form2]]") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("form1, [[form2]]", "infl", "sv"</nowiki>)', mut.link("form1, [[form2]]", "infl", "sv"), '<span data-grammar-link="infl">' .. lk.link("form1", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("[[form2]]", "sv") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("form1, [[form2]]", "infl", "sv", "verb"</nowiki>)', mut.link("form1, [[form2]]", "infl", "sv", "verb"), '<span class="grammar-inline" data-lang="sv" data-h3="verb"><span data-grammar-link="infl">' .. lk.link("form1", "sv") .. '</span>, <span data-grammar-link="infl">' .. lk.link("[[form2]]", "sv") .. '</span></span>', {nowiki=1})
end
function t:test02b_multiple_export()
self:equals_deep('mut.linkExport("form1, form2", "infl")', mut.linkExport("form1, form2", "infl"), {"form1", "form2"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("[[form1]], [[form2]]", "infl"</nowiki>)', mut.linkExport("[[form1]], [[form2]]", "infl"), {"form1", "form2"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("[[form1]], form2", "infl"</nowiki>)', mut.linkExport("[[form1]], form2", "infl"), {"form1", "form2"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("form1, [[form2]]", "infl"</nowiki>)', mut.linkExport("form1, [[form2]]", "infl"), {"form1", "form2"}, {nowiki=1})
end
function t:test03a_misc()
self:equals('<nowiki>mut.link("[[form1]]<sup>1</sup>")</nowiki>', mut.link("[[form1]]<sup>1</sup>", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]") .. '</span><sup>1</sup>', {nowiki=1})
self:equals('<nowiki>mut.link("[[form1]], vard. [[form2]]")</nowiki>', mut.link("[[form1]], vard. [[form2]]", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]") .. '</span>, vard. <span data-grammar-link="infl">' .. lk.link("[[form2]]") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("([[form1]])")</nowiki>', mut.link("([[form1]])", "infl"), [=['''('''<span data-grammar-link="infl">]=] .. lk.link("[[form1]]") .. [=[</span>''')''']=], {nowiki=1})
self:equals('<nowiki>mut.link("[[form1|fōrm1]]")</nowiki>', mut.link("[[form1|fōrm1]]", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[form1|fōrm1]]") .. '</span>', {nowiki=1})
self:equals('<nowiki>mut.link("[[Övriga_uppslagsord#EU:s|EU:s]]")</nowiki>', mut.link("[[Övriga_uppslagsord#EU:s|EU:s]]", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[Övriga_uppslagsord#EU:s|EU:s]]") .. '</span>', {nowiki=1})
--self:equals('<nowiki>mut.link("[[form1]]<ref>[[Källa]]</ref>")</nowiki>', mut.link("[[form1]]<ref>[[Källa]]</ref>", "infl"), '<span data-grammar-link="infl">' .. lk.link("[[form1]]") .. '</span><ref>[[Källa]]</ref>', {nowiki=1}) -- inaktiverad tills vi vet hur vi vill ha det
self:equals([[<nowiki>mut.link("<nowiki/>form1, form2")</nowiki><br>''Med nowiki i modul'']], mut.link("<nowiki/>form1, form2", "infl"), '<nowiki/>form1, <span data-grammar-link="infl">' .. lk.link("[[form2]]") .. '</span>', {nowiki=1})
local uniq_nowiki = "\127\"`UNIQ--nowiki-00000001-QINU`\"'\127"
self:equals([[<nowiki>mut.link("<nowiki/>form1, form2")</nowiki><br>''Med nowiki i mall'']], mut.link(uniq_nowiki .. "form1, form2", "infl"), uniq_nowiki .. 'form1, <span data-grammar-link="infl">' .. lk.link("[[form2]]") .. '</span>', {nowiki=1})
end
function t:test03b_misc_export()
self:equals_deep('<nowiki>mut.linkExport("[[form1]]<sup>1</sup>")</nowiki>', mut.linkExport("[[form1]]<sup>1</sup>", "infl"), {"form1"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("[[form1]], vard. [[form2]]")</nowiki>', mut.linkExport("[[form1]], vard. [[form2]]", "infl"), {"form1", "form2"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("([[form1]])")</nowiki>', mut.linkExport("([[form1]])", "infl"), {"(form1)"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("[[form1|fōrm1]]")</nowiki>', mut.linkExport("[[form1|fōrm1]]", "infl"), {"fōrm1"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("[[Övriga_uppslagsord#EU:s|EU:s]]")</nowiki>', mut.linkExport("[[Övriga_uppslagsord#EU:s|EU:s]]", "infl"), {"EU:s"}, {nowiki=1})
self:equals_deep([[<nowiki>mut.linkExport("<nowiki/>form1, form2")</nowiki><br>''Med nowiki i modul'']], mut.linkExport("<nowiki/>form1, form2", "infl"), {"form2"}, {nowiki=1})
local uniq_nowiki = "\127\"`UNIQ--nowiki-00000001-QINU`\"'\127"
self:equals_deep([[<nowiki>mut.linkExport("<nowiki/>form1, form2")</nowiki><br>''Med nowiki i mall'']], mut.linkExport(uniq_nowiki .. "form1, form2", "infl"), {"form2"}, {nowiki=1})
end
function t:test04a_do_not_link()
self:equals('mut.link("ord1 ord2", "infl")', mut.link("ord1 ord2", "infl"), "ord1 ord2", {nowiki=1})
self:equals('mut.link("noform", "no-link")', mut.link("noform", "no-link"), "noform", {nowiki=1})
end
function t:test04b_do_not_link_export()
self:equals_deep('mut.linkExport("ord1 ord2", "infl")', mut.linkExport("ord1 ord2", "infl"), {"ord1 ord2"}, {nowiki=1})
self:equals_deep('mut.linkExport("noform", "no-link")', mut.linkExport("noform", "no-link"), {}, {nowiki=1})
end
function t:test05a_link_but_no_tag()
self:equals('<nowiki>mut.link("noform1", "link-only")</nowiki>', mut.link("noform1", "link-only"), lk.link("noform1"), {nowiki=1})
self:equals('<nowiki>mut.link("noform1, noform2", "link-only")</nowiki>', mut.link("noform1, noform2", "link-only"), lk.link("noform1, noform2"), {nowiki=1})
self:equals('<nowiki>mut.link("[[noform1]], noform2", "link-only")</nowiki>', mut.link("[[noform1]], noform2", "link-only"), lk.link("[[noform1]], noform2"), {nowiki=1})
end
function t:test05b_link_but_no_tag_export()
self:equals_deep('<nowiki>mut.linkExport("noform1", "link-only")</nowiki>', mut.linkExport("noform1", "link-only"), {}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("noform1, noform2", "link-only")</nowiki>', mut.linkExport("noform1, noform2", "link-only"), {}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("[[noform1]], noform2", "link-only")</nowiki>', mut.linkExport("[[noform1]], noform2", "link-only"), {}, {nowiki=1})
end
function t:test06a_special_cases()
self:equals('mut.link("", "infl")', mut.link("", "infl"), " ", {nowiki=1})
self:equals('mut.link("", "no-link")', mut.link("", "no-link"), " ", {nowiki=1})
self:equals('mut.link("", "link-only")', mut.link("", "link-only"), " ", {nowiki=1})
self:equals('mut.link("-", "infl")', mut.link("-", "infl"), "–", {nowiki=1})
self:equals('mut.link("-", "no-link")', mut.link("-", "no-link"), "–", {nowiki=1})
self:equals('mut.link("-", "link-only")', mut.link("-", "link-only"), "–", {nowiki=1})
self:equals('mut.link("?", "infl")', mut.link("?", "infl"), "?", {nowiki=1})
self:equals('mut.link("?", "no-link")', mut.link("?", "no-link"), "?", {nowiki=1})
self:equals('mut.link("?", "link-only")', mut.link("?", "link-only"), "?", {nowiki=1})
end
function t:test06b_special_cases_export()
self:equals_deep('mut.linkExport("", "infl")', mut.linkExport("", "infl"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("", "no-link")', mut.linkExport("", "no-link"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("", "link-only")', mut.linkExport("", "link-only"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("-", "infl")', mut.linkExport("-", "infl"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("-", "no-link")', mut.linkExport("-", "no-link"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("-", "link-only")', mut.linkExport("-", "link-only"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("?", "infl")', mut.linkExport("?", "infl"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("?", "no-link")', mut.linkExport("?", "no-link"), {}, {nowiki=1})
self:equals_deep('mut.linkExport("?", "link-only")', mut.linkExport("?", "link-only"), {}, {nowiki=1})
end
function t:test_07a_maybe_slightly()
self:equals('mut.link("(form1)", "infl")', mut.link("(form1)", "infl"), "'''('''" .. mut.link("form1", "infl") .. "''')'''", {nowiki=1})
self:equals('mut.link("form1?", "infl")', mut.link("form1?", "infl"), "'''('''form1''')?'''", {nowiki=1})
self:equals('mut.link(" form1? ", "infl")', mut.link(" form1? ", "infl"), "'''('''form1''')?'''", {nowiki=1})
self:equals('mut.link("(form1), form2", "infl")', mut.link("(form1), form2", "infl"), "'''('''" .. mut.link("form1", "infl") .. "''')''', " .. mut.link("form2", "infl"), {nowiki=1})
self:equals('mut.link("form1?, form2", "infl")', mut.link("form1?, form2", "infl"), "'''('''form1''')?''', " .. mut.link("form2", "infl"), {nowiki=1})
self:equals('<nowiki>mut.link("(form1), [[form2]]", "infl")</nowiki>', mut.link("(form1), [[form2]]", "infl"), "'''('''" .. mut.link("form1", "infl") .. "''')''', " .. mut.link("form2", "infl"), {nowiki=1})
self:equals('<nowiki>mut.link("form1?, [[form2]]", "infl")</nowiki>', mut.link("form1?, [[form2]]", "infl"), "'''('''form1''')?''', " .. mut.link("form2", "infl"), {nowiki=1})
end
function t:test_07b_maybe_slightly_export()
self:equals_deep('mut.linkExport("(form1)", "infl")', mut.linkExport("(form1)", "infl"), {"(form1)"}, {nowiki=1})
self:equals_deep('mut.linkExport("form1?", "infl")', mut.linkExport("form1?", "infl"), {"form1?"}, {nowiki=1})
self:equals_deep('mut.linkExport(" form1? ", "infl")', mut.linkExport(" form1? ", "infl"), {"form1?"}, {nowiki=1})
self:equals_deep('mut.linkExport("(form1), form2", "infl")', mut.linkExport("(form1), form2", "infl"), {"(form1)", "form2"}, {nowiki=1})
self:equals_deep('mut.linkExport("form1?, form2", "infl")', mut.linkExport("form1?, form2", "infl"), {"form1?", "form2"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("(form1), [[form2]]", "infl")</nowiki>', mut.linkExport("(form1), [[form2]]", "infl"), {"(form1)", "form2"}, {nowiki=1})
self:equals_deep('<nowiki>mut.linkExport("form1?, [[form2]]", "infl")</nowiki>', mut.linkExport("form1?, [[form2]]", "infl"), {"form1?", "form2"}, {nowiki=1})
end
function t:test_08_do_not_add_anchor_if_pagename_equals_inflection()
self:equals('mut.link("<nowiki>' .. mw.title.getCurrentTitle().text .. '", "infl", "en", "verb</nowiki>")', mut.link(mw.title.getCurrentTitle().text, "infl", "en", "verb"), '<span class="grammar-inline" data-lang="en" data-h3="verb"><span data-grammar-link="infl">[[' .. mw.title.getCurrentTitle().text .. ']]</span></span>', {nowiki=1})
self:equals('mut.link("<nowiki>[[' .. mw.title.getCurrentTitle().text .. ']]", "infl", "en", "verb</nowiki>")', mut.link(mw.title.getCurrentTitle().text, "infl", "en", "verb"), '<span class="grammar-inline" data-lang="en" data-h3="verb"><span data-grammar-link="infl">[[' .. mw.title.getCurrentTitle().text .. ']]</span></span>', {nowiki=1})
self:equals('mut.link("<nowiki>[[' .. mw.title.getCurrentTitle().text .. ']], [[' .. mw.title.getCurrentTitle().text .. ']]", "infl", "en", "verb</nowiki>")', mut.link(mw.title.getCurrentTitle().text, "infl", "en", "verb"), '<span class="grammar-inline" data-lang="en" data-h3="verb"><span data-grammar-link="infl">[[' .. mw.title.getCurrentTitle().text .. ']]</span></span>', {nowiki=1})
end
return t