unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

tools/emitter/emitter_wbtest.mbt
1///|2test "Emitter writes source fragments and literals" {3  let out = Emitter::new()4  out.write("let answer = ")5  out.write_line(hex_literal(42, min_width=4))6  out.write_line("let text = \{string_literal([0x41, 0x1F600])}")7  inspect(8    out.finish(),9    content=(10      #|let answer = 0x002A11      #|let text = "\u{0041}\u{1F600}"12      #|13    ),14  )15  inspect(hex_upper(0), content="0")16}1718///|19test "value trie shares blocks and packs the higher planes" {20  let values = FixedArray::make(MaxCodePoint + 1, 0)21  values[0x41] = 122  values[0xE0001] = 223  let out = Emitter::new()24  out.write_value_trie(25    property="Test",26    prefix="test",27    lookup="fn lookup_test",28    lookup_doc=["Look up the test value", "of a character"],29    value_bits=3,30    values,31  )32  let source = out.finish()33  for34    expected in [35      // One leaf block holds `A`; every other block is the shared zero block.36      "let test_trie_leaf : Bytes = [\n" +37      "  0,\n".repeat(16) +38      "  0,\n  1,\n" +39      "  0,\n".repeat(14) +40      "]",41      // Planes 4 and up: 0 from U+40000, 2 at U+E0001, 0 again from U+E0002.42      "let test_high_ranges : ReadOnlyArray[Int] = [\n  0x200000,\n  0x70000A,\n  0x700010,\n]",43      "/// Look up the test value\n/// of a character\nfn lookup_test(c : Char) -> Int {",44      "let key = (cp << 3) | 7",45    ] {46    guard source.contains(expected) else {47      fail("Generated source is missing: \{expected}")48    }49  }50}5152///|53test "value trie rejects values wider than value_bits" {54  let values = FixedArray::make(MaxCodePoint + 1, 0)55  values[0x41] = 856  try57    Emitter::new().write_value_trie(58      property="Test",59      prefix="test",60      lookup="fn lookup_test",61      lookup_doc=[],62      value_bits=3,63      values,64    )65  catch {66    error =>67      inspect(68        error.to_string().contains("Test value does not fit in 3 bits"),69        content="true",70      )71  } noraise {72    _ => fail("expected an oversized value to be rejected")73  }74}7576///|77test "value trie stores wide values in two bytes and narrow high ranges" {78  let values = FixedArray::make(MaxCodePoint + 1, 0)79  values[0x41] = 0x12380  values[0xE0001] = 281  let out = Emitter::new()82  out.write_value_trie(83    property="Test",84    prefix="test",85    lookup="fn lookup_test",86    lookup_doc=[],87    value_bits=12,88    range_value_bits=3,89    values,90  )91  let source = out.finish()92  for93    expected in [94      // `A` is the second value of its block, the high half of the first95      // pair: 0x123 << 16.96      "let test_trie_leaf : ReadOnlyArray[Int] = [\n  0,\n".repeat(1) +97      "  0,\n".repeat(7) +98      "  19070976,\n",99      "let test_high_ranges : ReadOnlyArray[Int] = [\n  0x200000,\n  0x70000A,\n  0x700010,\n]",100      // One leaf block index per entry: U+0040 starts leaf block 1.101      "let test_trie_mid : ReadOnlyArray[Int] = [\n" +102      "  0,\n".repeat(4) +103      "  1,\n",104      "  let leaf = test_trie_mid[mid]\n",105      "(test_trie_leaf[offset >> 1] >> ((offset & 1) << 4)) & 0xFFFF",106    ] {107    guard source.contains(expected) else {108      fail("Generated source is missing: \{expected}")109    }110  }111  values[0xE0001] = 8112  try113    Emitter::new().write_value_trie(114      property="Test",115      prefix="test",116      lookup="fn lookup_test",117      lookup_doc=[],118      value_bits=12,119      range_value_bits=3,120      values,121    )122  catch {123    error =>124      inspect(125        error126        .to_string()127        .contains("Test value at 0xE0001 does not fit in 3 bits"),128        content="true",129      )130  } noraise {131    _ => fail("expected a wide value above the trie to be rejected")132  }133}134135///|136test "enum converts ordinals with identity" {137  let values = [138    { name: "Open", comment: "opening", },139    { name: "Close", comment: "closing", },140    { name: "None", comment: "neither", },141  ]142  let out = Emitter::new()143  out.write_enum(144    type_name="Kind",145    doc=["Kinds"],146    converter="kind_from_ordinal",147    values~,148  )149  let source = out.finish()150  for151    expected in [152      "/// Kinds\n#derive(Debug, Eq)\npub(all) enum Kind {\n  Open // opening\n",153      "      Close => \"Close\"\n", "/// Convert a Kind ordinal, below 3, to the enum value.\n",154      "fn kind_from_ordinal(ordinal : Int) -> Kind = \"%identity\"\n",155    ] {156    guard source.contains(expected) else {157      fail("Generated source is missing: \{expected}")158    }159  }160  try161    Emitter::new().write_enum(162      type_name="Kind",163      doc=[],164      converter="kind_from_ordinal",165      values=[],166    )167  catch {168    error =>169      inspect(170        error.to_string().contains("Kind has no constructors"),171        content="true",172      )173  } noraise {174    _ => fail("expected an enum without constructors to be rejected")175  }176}177