idna/processing_wbtest.mbt1///|2/// The labels of `domain`, found the way ToASCII and ToUnicode find them.3fn split_with_label_end(domain : String) -> Array[String] {4 let labels : Array[String] = []5 let mut start = 06 for ;; {7 let end = label_end(domain, start)8 labels.push(domain[start:end].to_owned())9 if end == domain.length() {10 break11 }12 start = end + 113 }14 labels15}1617///|18test "label_end: every separator" {19 debug_inspect(20 split_with_label_end("a.b\u{3002}c\u{FF0E}d\u{FF61}e"),21 content=(22 #|["a", "b", "c", "d", "e"]23 ),24 )25 debug_inspect(split_with_label_end(""), content="[\"\"]")26 debug_inspect(split_with_label_end("."), content="[\"\", \"\"]")27 debug_inspect(28 split_with_label_end("a..b."),29 content="[\"a\", \"\", \"b\", \"\"]",30 )31}3233///|34test "label_end: supplementary characters next to separators" {35 let labels = split_with_label_end("\u{1F600}.a\u{1F600}\u{3002}\u{1F600}")36 inspect(labels.length(), content="3")37 inspect(labels[0] == "\u{1F600}", content="true")38 inspect(labels[1] == "a\u{1F600}", content="true")39 inspect(labels[2] == "\u{1F600}", content="true")40}4142///|43test "map_domain: unchanged input is returned as is" {44 let domain = "b\u{00FC}cher.example"45 inspect(46 physical_equal(map_domain(domain, use_std3_ascii_rules=true), domain),47 content="true",48 )49}5051///|52test "map_domain: the first change copies the prefix before it" {53 // The prefix holds a supplementary character (two UTF-16 code units).54 inspect(55 map_domain("a\u{1F600}\u{00C4}b", use_std3_ascii_rules=true) ==56 "a\u{1F600}\u{00E4}b",57 content="true",58 )59 // Ignored (soft hyphen), mapped to several characters (U+00BD), deviation kept.60 inspect(61 map_domain("a\u{00AD}b\u{00BD}\u{00DF}", use_std3_ascii_rules=true) ==62 "ab1\u{2044}2\u{00DF}",63 content="true",64 )65 inspect(map_domain("\u{00AD}", use_std3_ascii_rules=true), content="")66}6768///|69test "validate_hyphens: positions count characters, not code units" {70 // The third and fourth characters are hyphens after a supplementary one.71 try validate_hyphens("\u{1F600}a--b") catch {72 error => inspect(error, content="InvalidHyphen")73 } noraise {74 _ => fail("expected InvalidHyphen")75 }76 // Hyphens in the second and third characters are allowed.77 validate_hyphens("\u{1F600}--a")78 validate_hyphens("a-b-c")79 try validate_hyphens("ab-") catch {80 error => inspect(error, content="InvalidHyphen")81 } noraise {82 _ => fail("expected InvalidHyphen")83 }84}8586///|87test "has_ace_prefix ignores ASCII case" {88 inspect(has_ace_prefix("xn--a"), content="true")89 inspect(has_ace_prefix("XN--a"), content="true")90 inspect(has_ace_prefix("xN--"), content="true")91 inspect(has_ace_prefix("xn-"), content="false")92 inspect(has_ace_prefix("xn-a-"), content="false")93 inspect(has_ace_prefix("axn--"), content="false")94}9596///|97test "ASCII shortcut agrees with the mapping table" {98 // `map_text` and `validate_char_status` skip the table for ASCII, taking99 // every character as valid but A-Z, which maps to a-z.100 for code in 0..<0x80 {101 let c = code.unsafe_to_char()102 let entry = @idna_data.lookup_idna_entry(c)103 if code is ('A'..<='Z') {104 assert_true(entry.status() is Mapped)105 let mapping = StringBuilder()106 entry.write_mapping(mapping)107 assert_eq(mapping.to_string(), (code + 32).unsafe_to_char().to_string())108 } else {109 assert_true(entry.status() is Valid)110 }111 }112}113114///|115test "map_domain: ASCII uppercase is mapped without a lookup" {116 inspect(117 map_domain("Ab.C\u{00C4}", use_std3_ascii_rules=true),118 content="ab.c\u{00E4}",119 )120 let domain = "ab-c_d.e"121 inspect(122 physical_equal(map_domain(domain, use_std3_ascii_rules=false), domain),123 content="true",124 )125}126
///|
/// The labels of `domain`, found the way ToASCII and ToUnicode find them.
fn split_with_label_end(domain : String) -> Array[String] {
let labels : Array[String] = []
let mut start = 0
for ;; {
let end = label_end(domain, start)
labels.push(domain[start:end].to_owned())
if end == domain.length() {
break
}
start = end + 1
}
labels
}
///|
test "label_end: every separator" {
debug_inspect(
split_with_label_end("a.b\u{3002}c\u{FF0E}d\u{FF61}e"),
content=(
#|["a", "b", "c", "d", "e"]
),
)
debug_inspect(split_with_label_end(""), content="[\"\"]")
debug_inspect(split_with_label_end("."), content="[\"\", \"\"]")
debug_inspect(
split_with_label_end("a..b."),
content="[\"a\", \"\", \"b\", \"\"]",
)
}
///|
test "label_end: supplementary characters next to separators" {
let labels = split_with_label_end("\u{1F600}.a\u{1F600}\u{3002}\u{1F600}")
inspect(labels.length(), content="3")
inspect(labels[0] == "\u{1F600}", content="true")
inspect(labels[1] == "a\u{1F600}", content="true")
inspect(labels[2] == "\u{1F600}", content="true")
}
///|
test "map_domain: unchanged input is returned as is" {
let domain = "b\u{00FC}cher.example"
inspect(
physical_equal(map_domain(domain, use_std3_ascii_rules=true), domain),
content="true",
)
}
///|
test "map_domain: the first change copies the prefix before it" {
// The prefix holds a supplementary character (two UTF-16 code units).
inspect(
map_domain("a\u{1F600}\u{00C4}b", use_std3_ascii_rules=true) ==
"a\u{1F600}\u{00E4}b",
content="true",
)
// Ignored (soft hyphen), mapped to several characters (U+00BD), deviation kept.
inspect(
map_domain("a\u{00AD}b\u{00BD}\u{00DF}", use_std3_ascii_rules=true) ==
"ab1\u{2044}2\u{00DF}",
content="true",
)
inspect(map_domain("\u{00AD}", use_std3_ascii_rules=true), content="")
}
///|
test "validate_hyphens: positions count characters, not code units" {
// The third and fourth characters are hyphens after a supplementary one.
try validate_hyphens("\u{1F600}a--b") catch {
error => inspect(error, content="InvalidHyphen")
} noraise {
_ => fail("expected InvalidHyphen")
}
// Hyphens in the second and third characters are allowed.
validate_hyphens("\u{1F600}--a")
validate_hyphens("a-b-c")
try validate_hyphens("ab-") catch {
error => inspect(error, content="InvalidHyphen")
} noraise {
_ => fail("expected InvalidHyphen")
}
}
///|
test "has_ace_prefix ignores ASCII case" {
inspect(has_ace_prefix("xn--a"), content="true")
inspect(has_ace_prefix("XN--a"), content="true")
inspect(has_ace_prefix("xN--"), content="true")
inspect(has_ace_prefix("xn-"), content="false")
inspect(has_ace_prefix("xn-a-"), content="false")
inspect(has_ace_prefix("axn--"), content="false")
}
///|
test "ASCII shortcut agrees with the mapping table" {
// `map_text` and `validate_char_status` skip the table for ASCII, taking
// every character as valid but A-Z, which maps to a-z.
for code in 0..<0x80 {
let c = code.unsafe_to_char()
let entry = @idna_data.lookup_idna_entry(c)
if code is ('A'..<='Z') {
assert_true(entry.status() is Mapped)
let mapping = StringBuilder()
entry.write_mapping(mapping)
assert_eq(mapping.to_string(), (code + 32).unsafe_to_char().to_string())
} else {
assert_true(entry.status() is Valid)
}
}
}
///|
test "map_domain: ASCII uppercase is mapped without a lookup" {
inspect(
map_domain("Ab.C\u{00C4}", use_std3_ascii_rules=true),
content="ab.c\u{00E4}",
)
let domain = "ab-c_d.e"
inspect(
physical_equal(map_domain(domain, use_std3_ascii_rules=false), domain),
content="true",
)
}