+ Add tests for bidi rule W2

This commit is contained in:
Nils Dagsson Moskopp 2023-03-26 20:38:08 +02:00
parent 4d8bea08fd
commit 58bc44bc7c
1 changed files with 85 additions and 7 deletions

View File

@ -97,7 +97,13 @@ bidi.W1 = function(run, sos)
return run return run
end end
bidi.W2 = function(run, sos) local W2 = function(run, sos)
-- sos is the text ordering type assigned to the virtual position
-- before an isolating run sequence
assert(
"L" == sos or
"R" == sos
)
for i = 1, #run do for i = 1, #run do
if "EN" == run[i].bidi_class then if "EN" == run[i].bidi_class then
-- Search backward from each instance of a European number -- Search backward from each instance of a European number
@ -105,30 +111,102 @@ bidi.W2 = function(run, sos)
local previous_strong_bidi_class = nil local previous_strong_bidi_class = nil
local j = i local j = i
repeat repeat
j = j - 1
local previous_bidi_class = run[j].bidi_class local previous_bidi_class = run[j].bidi_class
if ( if (
"L" == previous_bidi_class or -- left-to-right "L" == previous_bidi_class or -- left-to-right
"R" == previous_bidi_class or -- right-to-left "R" == previous_bidi_class or -- right-to-left
"AL" == previous_bidi_class or -- right-to-left (arabic) "AL" == previous_bidi_class or -- right-to-left (arabic)
sos == previvous_bidi_class sos == previous_bidi_class
) then ) then
previous_strong_bidi_class = previous_bidi_class previous_strong_bidi_class = previous_bidi_class
end end
j = j - 1
until( until(
nil ~= previous_strong_bidi_class or nil ~= previous_strong_bidi_class or
0 == j 1 == j
) )
-- If an AL is found, change the type of the European number -- If an AL is found, change the type of the European number
-- to Arabic number. -- to Arabic number.
if "AL" == run[i].bidi_class then if "AL" == previous_strong_bidi_class then
run[i].bidi_class = "R" run[i].bidi_class = "AN"
end end
end end
end end
return run return run
end end
-- Test W2: AL EN → AL AN
local test_input = {
{ ["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
)
-- Test W2: AL NI EN → AL NI AN
local test_input = {
{ ["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
)
-- Test W2: sos NI EN → sos NI EN (sos = L)
local test_input = {
{ ["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
)
-- Test W2: L NI EN → L NI AN
local test_input = {
{ ["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
)
-- Test W2: R NI EN → R NI AN
local test_input = {
{ ["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.W3 = function(run) bidi.W3 = function(run)
-- Change al ALs to R. -- Change al ALs to R.
for i = 1, #run do for i = 1, #run do
@ -244,7 +322,7 @@ end
bidi.resolve_weak_types = function(run, sos) bidi.resolve_weak_types = function(run, sos)
run = bidi.W1(run, sos) run = bidi.W1(run, sos)
run = bidi.W2(run, sos) run = W2(run, sos)
run = bidi.W3(run) run = bidi.W3(run)
run = bidi.W4(run) run = bidi.W4(run)
run = bidi.W5(run) run = bidi.W5(run)