Module:ConvertTestcase

-- Used to compare the current version of the convert template with the sandboxlua version

local function bag -- Return a table to hold lines of text. return { n = 0, add = function (self, s)           self.n = self.n + 1 self[self.n] = s       end, join = function (self, sep) return table.concat(self, sep or '\n') end, } end

local function clean(s) -- 2013-04-08: Template:Convert now appends text like -- "" to its output, -- so remove that also. s = s:gsub(" ", " ") s = s:gsub("&#32;", " ") s = s:gsub("_", " ") s = s:gsub("&mdash;", "—") s = s:gsub("–", "–") s = s:gsub("²", "2") s = s:gsub("³", "3") s = s:lower s = s:gsub("%[%[category:.-%]%]", "")

-- convert links with descriptions the same as the link to just a link -- for example acre to acre -- and acres to acres s = s:gsub("%[%[([^%[%]]-)|%1]]", "%1") s = s:gsub("%[%[([^%[%]]-)|%1s]]", "%1s") return s end

local function nolinks(s) -- Remove wikilink target to test if difference is due to a link disagreement. -- Change, for example, "acre" to "acre", then "ha" to "ha". s = s:gsub("%[%[([^%[%]|]-)%]%]", "%1") return s:gsub("%[%^%[%-|(.-)%]%]", "%1") end

local function nospans(s) -- Remove any html spans to test if difference is due to a span disagreement. return s:gsub("", "") end

local function statusbox(label, color, align) return "| style=\"text-align:" .. align .. ";color:white;background:" .. color .. ";\" | " .. label end

local function statusboxlinkspan(label) return "| style=\"text-align:center;color:white;background-image: -moz-linear-gradient(left, olive, teal); background-image: -ms-linear-gradient(left, olive, teal); background-image: -webkit-linear-gradient(left, olive, teal); background-image: linear-gradient(left, olive, teal);\" | " .. label end

local function onerow(frame, voutput, vunit, vunit2, vbaseunit) local insert = table.insert local args = {} insert(args, "1") if vbaseunit then insert(args, vbaseunit) end insert(args, vunit) if vunit2 then insert(args, "5") insert(args, vunit2) end local template = "&#123;{convert" .. "&#124;" .. table.concat(args, "&#124;") .. "}}" -- 123="{", 124="|"    args.lk = "on" local v1 = frame:expandTemplate{ title = "convert", args = args } local v2 = frame:expandTemplate{ title = "convert/sandboxlua", args = args } voutput:add("| " .. vunit .. "") voutput:add("| " .. template) voutput:add("| " .. v1) voutput:add("| " .. v2) v1 = clean(v1) v2 = clean(v2) if v1 == v2 then voutput:add(statusbox("Exact Match", "green", "center")) elseif nolinks(v1) == nolinks(v2) then voutput:add(statusbox("Link", "olive", "left")) elseif nospans(v1) == nospans(v2) then voutput:add(statusbox("Span", "teal", "right")) elseif nospans(nolinks(v1)) == nospans(nolinks(v2)) then voutput:add(statusboxlinkspan("Link & Span")) else voutput:add(statusbox("Different", "red", "center")) end voutput:add("|-") end

local i = {}

function i.ConvertTestcaseHeader(frame) local voutput = bag voutput:add("{| class=\"wikitable\"") voutput:add("! style=\"text-align:left;\" | Unit") voutput:add("! style=\"text-align:left;\" | Convert Code") voutput:add("! style=\"text-align:left;\" | Convert (Live)") voutput:add("! style=\"text-align:left;\" | Convert (Sandboxlua)") voutput:add("! style=\"text-align:left;\" | Result") voutput:add("|-") return voutput:join end

function i.ConvertTestcaseFooter(frame) return "|}\n" end

function i.ConvertTestcase(frame) local vtype = frame.args["type"] local vtoonly = frame.args["toonly"] local vfromonly = frame.args["fromonly"] local vunit = frame.args["unit"] local vunit2 = frame.args["unit2"] local voutput = bag local vunits = { ["acceleration"] = "m/s2", ["area"] = "m2", ["density"] = "kg/m3", ["energy"] = "kJ", ["energypermass"] = "kJ/kg", ["energypervolume"] = "kJ/l", ["energyperlength"] = "kJ/km", ["exhaustemission"] = "kg/km", ["flow"] = "m3/s", ["force"] = "N", ["fuelconsumption"] = "l/km", ["gradient"] = "m/km", ["length"] = "m", ["lineardensity"] = "kg/m", ["mass"] = "g", ["power"] = "W", ["pressure"] = "Pa", ["population-density"] = "PD/km2", ["radioactivity"] = "Ci", ["speed"] = "m/s", ["temperature"] = "C", ["temperature-change"] = "C-change", ["time"] = "s", ["torque"] = "Nm", ["volume"] = "m3" }   if vtoonly ~= "yes" then onerow(frame, voutput, vunit, vunit2) end if vfromonly ~= "yes" then local vbaseunit = vunits[vtype] if vbaseunit ~= nil and vbaseunit ~= vunit then onerow(frame, voutput, vunit, vunit2, vbaseunit) end end return voutput:join end

return i