Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-environment

Environment files for Pudu: load .env variables safely with layered files, discovery, expansion, typed reads, and secrets kept out of every message

0.1.0Apache-2.01

InstallClose

EncodingTest.pudu

Pudu63 lines5.4 KB

GitHub ↗
1/** @Test.Domain.Encoding.Suite — bytes to text in each encoding */2module PuduLangEnvironment.Domain.EncodingTest34import Std.Bytes as Bytes5import Std.Io as Io6import Std.Result as Result7import Std.Test as Test8import PuduLangEnvironment.Domain.Encoding as Encoding910/// The text hexadecimal bytes spell in the declared encoding.11fn decoded(hex: Str, declared: Encoding.Encoding) -> Option[Str] {12  Encoding.decode(&Result.unwrapOr(Bytes.decodeHex(hex), Bytes.empty()), declared)13}1415/// Runs the suite.16fn main() -> Int {17  let checks = Test.suite("Domain.Encoding", &[18      Test.equals("UTF-8 is read", &decoded("4b3dc3a9", Encoding.Utf8), &Some("K=é")),19      Test.equals("invalid UTF-8 is refused", &decoded("4bff", Encoding.Utf8), &None),20      Test.equals("a UTF-8 mark is dropped", &decoded("efbbbf4b", Encoding.Utf8), &Some("K")),21      Test.equals("a UTF-8 mark overrides the declared encoding", &decoded("efbbbfc3a9", Encoding.Latin1), &Some("é")),22      Test.equals("Latin-1 maps each byte to its character", &decoded("4b3de9ff", Encoding.Latin1), &Some("K=éÿ")),23      Test.equals("Latin-1 reads byte zero", &decoded("00", Encoding.Latin1), &Some("\u{0}")),24      Test.equals("UTF-16LE is read", &decoded("4b003d00e900", Encoding.Utf16Le), &Some("K=é")),25      Test.equals("UTF-16BE is read", &decoded("004b003d00e9", Encoding.Utf16Be), &Some("K=é")),26      Test.equals("a high unit is placed by its order", &decoded("0001", Encoding.Utf16Le), &Some("Ā")),27      Test.equals("a UTF-16LE mark is honoured and dropped", &decoded("fffe4b00", Encoding.Utf8), &Some("K")),28      Test.equals("a UTF-16BE mark is honoured and dropped", &decoded("feff004b", Encoding.Utf8), &Some("K")),29      Test.equals("a surrogate pair is joined", &decoded("3dd800de", Encoding.Utf16Le), &Some("😀")),30      Test.equals("a surrogate pair is joined big-endian", &decoded("d83dde00", Encoding.Utf16Be), &Some("😀")),31      Test.equals("the highest pair is joined", &decoded("dbffdfff", Encoding.Utf16Be), &Some("\u{10FFFF}")),32      Test.equals("the lowest pair is joined", &decoded("d800dc00", Encoding.Utf16Be), &Some("\u{10000}")),33      Test.equals("units beside the surrogates are characters", &decoded("d7ffe000", Encoding.Utf16Be), &Some("\u{D7FF}\u{E000}")),34      Test.equals("an odd length is refused", &decoded("4b00ff", Encoding.Utf16Le), &None),35      Test.equals("a high surrogate at the end is refused", &decoded("4b0000d8", Encoding.Utf16Le), &None),36      Test.equals("a high surrogate before a non-low unit is refused", &decoded("d8000041", Encoding.Utf16Be), &None),37      Test.equals("a high surrogate before a high surrogate is refused", &decoded("dbffdbff", Encoding.Utf16Be), &None),38      Test.equals("a high surrogate before a unit above the lows is refused", &decoded("d800e000", Encoding.Utf16Be), &None),39      Test.equals("a lone low surrogate is refused", &decoded("dc00", Encoding.Utf16Be), &None),40      Test.equals("the last low surrogate alone is refused", &decoded("dfff", Encoding.Utf16Be), &None),41      Test.equals("ASCII is read", &decoded("4b3d7e7f", Encoding.Ascii), &Some("K=~\u{7F}")),42      Test.equals("a byte above ASCII is refused", &decoded("4b3d80", Encoding.Ascii), &None),43      Test.equals("UTF-32LE is read", &decoded("4b0000003d000000e9000000", Encoding.Utf32Le), &Some("K=é")),44      Test.equals("UTF-32BE is read", &decoded("0000004b0000003d000000e9", Encoding.Utf32Be), &Some("K=é")),45      Test.equals("every octet of a UTF-32 unit counts", &[decoded("00f60100", Encoding.Utf32Le), decoded("0001f600", Encoding.Utf32Be)], &[Some("😀"), Some("😀")]),46      Test.equals("a UTF-32 length off a multiple of four is refused", &[decoded("4b000000ff", Encoding.Utf32Le), decoded("000000", Encoding.Utf32Be)], &[None, None]),47      Test.equals("a UTF-32 unit that is no character is refused", &[decoded("00d80000", Encoding.Utf32Le), decoded("00110000", Encoding.Utf32Be)], &[None, None]),48      Test.equals("a UTF-32LE mark wins over the UTF-16LE mark it starts with", &decoded("fffe00004b000000", Encoding.Utf8), &Some("K")),49      Test.equals("a UTF-32BE mark is honoured and dropped", &decoded("0000feff0000004b", Encoding.Latin1), &Some("K")),50      Test.equals("a UTF-32 mark alone is empty text", &[decoded("fffe0000", Encoding.Utf8), decoded("0000feff", Encoding.Utf8)], &[Some(""), Some("")]),51      Test.equals("empty bytes are empty text", &[decoded("", Encoding.Utf8), decoded("", Encoding.Ascii), decoded("", Encoding.Latin1), decoded("", Encoding.Utf16Le), decoded("", Encoding.Utf16Be), decoded("", Encoding.Utf32Le), decoded("", Encoding.Utf32Be)], &[Some(""), Some(""), Some(""), Some(""), Some(""), Some(""), Some("")]),52      Test.equals("a UTF-8 mark alone is empty text", &decoded("efbbbf", Encoding.Latin1), &Some("")),53      Test.equals("a UTF-16 mark alone is empty text", &[decoded("fffe", Encoding.Utf8), decoded("feff", Encoding.Utf8)], &[Some(""), Some("")]),54      Test.equals("a lone mark byte is not a mark", &decoded("ff", Encoding.Latin1), &Some("ÿ")),55      Test.equals("every encoding has its name", &[Encoding.name(&Encoding.Utf8), Encoding.name(&Encoding.Ascii), Encoding.name(&Encoding.Latin1), Encoding.name(&Encoding.Utf16Le), Encoding.name(&Encoding.Utf16Be), Encoding.name(&Encoding.Utf32Le), Encoding.name(&Encoding.Utf32Be)], &["UTF-8", "ASCII", "Latin-1", "UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE"])56    ])57  let ran = Test.run(&checks)58  for failure in Test.failuresOf(&ran) {59    let _reported = Io.writeErrorLine(failure)60  }61  Test.report(&ran)62}63