From d7ebaec79774e25afd583b5ced6ca3a32d780c7d Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Sun, 26 Mar 2023 21:29:22 +0200 Subject: [PATCH] * Refactor tests for bidi rules W2, W3, W4 --- bidi.lua | 273 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 169 insertions(+), 104 deletions(-) diff --git a/bidi.lua b/bidi.lua index d98eb9b..c2a694f 100644 --- a/bidi.lua +++ b/bidi.lua @@ -135,76 +135,133 @@ local W2 = function(run, sos) return run 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 +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" }, + }, + { + { ["bidi_class"] = "AL" }, + { ["bidi_class"] = "AN" }, + }, + "L" ) --- 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_rule( + "Test W2: AL NI EN → AL NI AN", + W2, + { + { ["bidi_class"] = "AL" }, + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "EN" }, + }, + { + { ["bidi_class"] = "AL" }, + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "AN" }, + }, + "L" ) --- 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_rule( + "Test W2: sos NI EN → sos NI EN (sos = L)", + W2, + { + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "EN" }, + }, + { + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "EN" }, + }, + "L" ) --- 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_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 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_rule( + "Test W2: L NI EN → L NI EN", + W2, + { + { ["bidi_class"] = "L" }, + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "EN" }, + }, + { + { ["bidi_class"] = "L" }, + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "EN" }, + }, + "L" ) --- 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 +test_rule( + "Test W2: R NI EN → R NI EN", + W2, + { + { ["bidi_class"] = "R" }, + { ["bidi_class"] = "NI" }, + { ["bidi_class"] = "EN" }, + }, + { + { ["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 = { - { ["bidi_class"] = "AL" }, - { ["bidi_class"] = "AL" }, - { ["bidi_class"] = "AL" }, -} -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 +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 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 = { - { ["bidi_class"] = "EN" }, - { ["bidi_class"] = "ES" }, - { ["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_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" }, + } ) --- Test W4: EN CS EN → EN EN EN -local test_input = { - { ["bidi_class"] = "EN" }, - { ["bidi_class"] = "CS" }, - { ["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_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" }, + } ) --- Test W4: AN CS AN → AN AN AN -local test_input = { - { ["bidi_class"] = "AN" }, - { ["bidi_class"] = "CS" }, - { ["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 +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" }, + } ) bidi.W5 = function(run)