kana.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { test } from 'uvu';
  2. import * as assert from 'uvu/assert';
  3. import { normalizeInput, KanaInputState } from '../src/kana';
  4. import { TransitionResult } from '../src/state';
  5. function testInput(input: string, line: string) {
  6. const inputState = new KanaInputState(line);
  7. let kanaCount = 0;
  8. inputState.map((_, m) => {
  9. m.addObserver((result, boundary) => {
  10. if (boundary) {
  11. kanaCount += 1;
  12. }
  13. assert.is(
  14. result,
  15. TransitionResult.SUCCESS,
  16. `Expected ${input} to match ${line}`
  17. );
  18. });
  19. });
  20. for (const c of input.split('')) {
  21. inputState.handleInput(c);
  22. }
  23. assert.ok(inputState.isFinished(), `Expected inputState to be finished`);
  24. assert.is(
  25. kanaCount,
  26. line.length,
  27. `Expected ${line.length} boundaries, got ${kanaCount}`
  28. );
  29. }
  30. function testFail(input: string, line: string) {
  31. const inputState = new KanaInputState(line);
  32. let fail = false;
  33. inputState.map((_, m) => {
  34. m.addObserver((result, _boundary) => {
  35. fail =
  36. fail ||
  37. result === TransitionResult.FAILED ||
  38. result === TransitionResult.SKIPPED;
  39. });
  40. });
  41. for (const c of input.split('')) {
  42. inputState.handleInput(c);
  43. }
  44. fail = fail || !inputState.isFinished();
  45. assert.ok(fail, `Expected ${input} to fail on ${line}`);
  46. }
  47. test('normalizeInput', () => {
  48. assert.is(normalizeInput('ABCdef'), 'abcdef');
  49. assert.is(normalizeInput('フェスティバル'), 'ふぇすてぃばる');
  50. assert.is(normalizeInput('  '), ' ');
  51. });
  52. test('multiple romanization single kana', () => {
  53. testInput('si', 'し');
  54. testInput('shi', 'し');
  55. testInput('ji', 'じ');
  56. testInput('zi', 'じ');
  57. testInput('ti', 'ち');
  58. testInput('chi', 'ち');
  59. testInput('tu', 'つ');
  60. testInput('tsu', 'つ');
  61. testInput('fu', 'ふ');
  62. testInput('hu', 'ふ');
  63. });
  64. test('multiple romanization double kana', () => {
  65. testInput('sha', 'しゃ');
  66. testInput('sya', 'しゃ');
  67. // testInput('shilya', 'しゃ');
  68. // testInput('silya', 'しゃ');
  69. testInput('cha', 'ちゃ');
  70. testInput('tya', 'ちゃ');
  71. // testInput('chilya', 'ちゃ');
  72. // testInput('tilya', 'ちゃ');
  73. testInput('ja', 'じゃ');
  74. testInput('jya', 'じゃ');
  75. testInput('zya', 'じゃ');
  76. // testInput('jilya', 'じゃ');
  77. // testInput('zilya', 'じゃ');
  78. testInput('fe', 'ふぇ');
  79. // testInput('fule', 'ふぇ');
  80. });
  81. test('small tsu', () => {
  82. testInput('katto', 'カット');
  83. // testInput('kaltsuto', 'かっと');
  84. // testInput('kaltuto', 'かっと');
  85. testInput('ejji', 'エッジ');
  86. testInput('ezzi', 'エッジ');
  87. // testInput('extuji', 'エッジ');
  88. // testInput('extsuzi', 'エッジ');
  89. testInput('hassha', 'はっしゃ');
  90. testInput('hassya', 'はっしゃ');
  91. // testInput('haltusha', 'はっしゃ');
  92. // testInput('haltusya', 'はっしゃ');
  93. });
  94. test('nn', () => {
  95. testInput('nn', 'ん');
  96. testInput('nna', 'んあ');
  97. testFail('na', 'んあ');
  98. testInput('nda', 'んだ');
  99. testInput('nnda', 'んだ');
  100. testFail('nnnda', 'んだ');
  101. testInput('nnnda', 'んんだ');
  102. testInput('nnnnda', 'んんだ');
  103. testFail('nya', 'んにゃ');
  104. testFail('nnya', 'んにゃ');
  105. testInput('nnnya', 'んにゃ');
  106. });
  107. test.run();