grapheme_splitter_tests.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. const fs = require('fs')
  2. const test = require('tape')
  3. const GraphemeSplitter = require('../index')
  4. function ucs2encode(array) {
  5. return array.map( value => {
  6. let output = '';
  7. if (value > 0xFFFF) {
  8. value -= 0x10000;
  9. output += String.fromCharCode(value >>> 10 & 0x3FF | 0xD800);
  10. value = 0xDC00 | value & 0x3FF;
  11. }
  12. output += String.fromCharCode(value);
  13. return output;
  14. }).join('');
  15. }
  16. function testDataFromLine(line) {
  17. const codePoints = line.split(/\s*[×÷]\s*/).map(c => parseInt(c, 16));
  18. const input = ucs2encode(codePoints);
  19. const expected = line.split(/\s*÷\s*/) .map(sequence => {
  20. const codePoints = sequence.split(/\s*×\s*/).map(c => parseInt(c, 16))
  21. return ucs2encode(codePoints)
  22. });
  23. return { input, expected };
  24. }
  25. const testData = fs.readFileSync('tests/GraphemeBreakTest.txt', 'utf-8')
  26. .split('\n')
  27. .filter(line =>
  28. line != null && line.length > 0 && !line.startsWith('#'))
  29. .map(line => line.split('#')[0])
  30. .map(testDataFromLine);
  31. // ---------------------------------------------------------------------------
  32. // Test cases
  33. // ---------------------------------------------------------------------------
  34. test('splitGraphemes returns properly split list from string', t => {
  35. const splitter = new GraphemeSplitter();
  36. t.plan(testData.length);
  37. testData.forEach( ({ input, expected }) => {
  38. const result = splitter.splitGraphemes(input);
  39. t.deepLooseEqual(result, expected);
  40. });
  41. t.end();
  42. });
  43. test('iterateGraphemes returns properly split iterator from string', t => {
  44. const splitter = new GraphemeSplitter();
  45. t.plan(testData.length);
  46. testData.forEach( ({ input, expected }) => {
  47. const result = splitter.iterateGraphemes(input);
  48. t.deepLooseEqual([...result], expected);
  49. });
  50. t.end();
  51. });
  52. test('countGraphemes returns the correct number of graphemes in string', t => {
  53. const splitter = new GraphemeSplitter();
  54. t.plan(testData.length);
  55. testData.forEach( ({ input, expected }) => {
  56. const result = splitter.countGraphemes(input);
  57. t.equal(result, expected.length);
  58. });
  59. t.end();
  60. });