* Refactor tests for bidi rules W2, W3, W4

This commit is contained in:
Nils Dagsson Moskopp 2023-03-26 21:29:22 +02:00
parent 732b063dcd
commit d7ebaec797
1 changed files with 169 additions and 104 deletions

215
bidi.lua
View File

@ -135,76 +135,133 @@ local W2 = function(run, sos)
return run
end
-- Test W2: AL EN → AL AN
local test_input = {
dump_table = function(table_, indentation)
assert( "table" == type(table_) )
local indentation = indentation or 0
assert( "number" == type(indentation) )
local result = {}
for key, value in pairs(table_) do
local prefix = "\n" .. (" "):rep(indentation) .. key .. ": "
if "table" == type(value) then
result[#result + 1] = prefix .. dump_table(value, indentation + 1)
else
result[#result + 1] = prefix .. tostring(value)
end
end
return table.concat(result)
end
local test_rule = function(
description,
rule,
test_input,
expected_output,
...
)
assert( "string" == type(description) )
assert( "function" == type(rule) )
assert( "table" == type(test_input) )
assert( "table" == type(expected_output) )
local test_output = rule(test_input, ...)
for i = 1, #test_input do
assert(
test_output[i].bidi_class == expected_output[i].bidi_class,
description ..
dump_table(test_output)
)
end
end
test_rule(
"Test W2: AL EN → AL AN",
W2,
{
{ ["bidi_class"] = "AL" },
{ ["bidi_class"] = "EN" },
}
local test_output = W2(test_input, "L")
assert(
"AL" == test_output[1].bidi_class and
"AN" == test_output[2].bidi_class
},
{
{ ["bidi_class"] = "AL" },
{ ["bidi_class"] = "AN" },
},
"L"
)
-- Test W2: AL NI EN → AL NI AN
local test_input = {
test_rule(
"Test W2: AL NI EN → AL NI AN",
W2,
{
{ ["bidi_class"] = "AL" },
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
}
local test_output = W2(test_input, "L")
assert(
"AL" == test_output[1].bidi_class and
"NI" == test_output[2].bidi_class and
"AN" == test_output[3].bidi_class
},
{
{ ["bidi_class"] = "AL" },
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "AN" },
},
"L"
)
-- Test W2: sos NI EN → sos NI EN (sos = L)
local test_input = {
test_rule(
"Test W2: sos NI EN → sos NI EN (sos = L)",
W2,
{
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
}
local test_output = W2(test_input, "L")
assert(
"NI" == test_output[1].bidi_class and
"EN" == test_output[2].bidi_class
)
-- Test W2: sos NI EN → sos NI EN (sos = R)
local test_input = {
},
{
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
}
local test_output = W2(test_input, "R")
assert(
"NI" == test_output[1].bidi_class and
"EN" == test_output[2].bidi_class
},
"L"
)
-- Test W2: L NI EN → L NI AN
local test_input = {
test_rule(
"Test W2: sos NI EN → sos NI EN (sos = R)",
W2,
{
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
},
{
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
},
"R"
)
test_rule(
"Test W2: L NI EN → L NI EN",
W2,
{
{ ["bidi_class"] = "L" },
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
}
local test_output = W2(test_input, "L")
assert(
"L" == test_output[1].bidi_class and
"NI" == test_output[2].bidi_class and
"EN" == test_output[3].bidi_class
},
{
{ ["bidi_class"] = "L" },
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
},
"L"
)
-- Test W2: R NI EN → R NI AN
local test_input = {
test_rule(
"Test W2: R NI EN → R NI EN",
W2,
{
{ ["bidi_class"] = "R" },
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
}
local test_output = W2(test_input, "L")
assert(
"R" == test_output[1].bidi_class and
"NI" == test_output[2].bidi_class and
"EN" == test_output[3].bidi_class
},
{
{ ["bidi_class"] = "R" },
{ ["bidi_class"] = "NI" },
{ ["bidi_class"] = "EN" },
},
"L"
)
local W3 = function(run)
@ -217,17 +274,19 @@ local W3 = function(run)
return run
end
-- Test W3: AL AL AL → R R R
local test_input = {
test_rule(
"Test W3: AL AL AL → R R R",
W3,
{
{ ["bidi_class"] = "AL" },
{ ["bidi_class"] = "AL" },
{ ["bidi_class"] = "AL" },
},
{
{ ["bidi_class"] = "R" },
{ ["bidi_class"] = "R" },
{ ["bidi_class"] = "R" },
}
local test_output = W3(test_input)
assert(
"R" == test_output[1].bidi_class and
"R" == test_output[2].bidi_class and
"R" == test_output[3].bidi_class
)
local W4 = function(run)
@ -268,43 +327,49 @@ local W4 = function(run)
return run
end
-- Test W4: EN ES EN → EN EN EN
local test_input = {
test_rule(
"Test·W4:·EN·ES·EN·→·EN·EN·EN",
W4,
{
{ ["bidi_class"] = "EN" },
{ ["bidi_class"] = "ES" },
{ ["bidi_class"] = "EN" },
},
{
{ ["bidi_class"] = "EN" },
{ ["bidi_class"] = "EN" },
{ ["bidi_class"] = "EN" },
}
local test_output = W4(test_input)
assert(
"EN" == test_output[1].bidi_class and
"EN" == test_output[2].bidi_class and
"EN" == test_output[3].bidi_class
)
-- Test W4: EN CS EN → EN EN EN
local test_input = {
test_rule(
"Test·W4:·EN·CS·EN·→·EN·EN·EN",
W4,
{
{ ["bidi_class"] = "EN" },
{ ["bidi_class"] = "CS" },
{ ["bidi_class"] = "EN" },
},
{
{ ["bidi_class"] = "EN" },
{ ["bidi_class"] = "EN" },
{ ["bidi_class"] = "EN" },
}
local test_output = W4(test_input)
assert(
"EN" == test_output[1].bidi_class and
"EN" == test_output[2].bidi_class and
"EN" == test_output[3].bidi_class
)
-- Test W4: AN CS AN → AN AN AN
local test_input = {
test_rule(
"Test·W4:·AN·CS·AN·→·AN·AN·AN",
W4,
{
{ ["bidi_class"] = "AN" },
{ ["bidi_class"] = "CS" },
{ ["bidi_class"] = "AN" },
},
{
{ ["bidi_class"] = "AN" },
{ ["bidi_class"] = "AN" },
{ ["bidi_class"] = "AN" },
}
local test_output = W4(test_input)
assert(
"AN" == test_output[1].bidi_class and
"AN" == test_output[2].bidi_class and
"AN" == test_output[3].bidi_class
)
bidi.W5 = function(run)