test.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. const assert = require ('assert')
  3. const printableCharacters = require (process.env.PRINTABLE_CHARACTERS_TEST_FILE)
  4. // cannot use spread operator in tests due to Node v4 compatibility requirements...
  5. const strlen = printableCharacters.strlen
  6. , isBlank = printableCharacters.isBlank
  7. , blank = printableCharacters.blank
  8. , ansiEscapeCodes = printableCharacters.ansiEscapeCodes
  9. , zeroWidthCharacters = printableCharacters.zeroWidthCharacters
  10. , partition = printableCharacters.partition
  11. , first = printableCharacters.first
  12. describe ('printable-characters', () => {
  13. it ('determines visible length', () => {
  14. assert.equal (strlen ('💩'), 1)
  15. //assert.equal (strlen ('👩‍❤️‍💋‍👩'), 1) // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution
  16. assert.equal (strlen ('❤️'), 1)
  17. assert.equal (strlen ('foo bar'), 7)
  18. assert.equal (strlen ('\u001b[106mfoo bar\u001b[49m'), 7)
  19. })
  20. it ('detects blank text', () => {
  21. assert (!isBlank ('💩'))
  22. assert (!isBlank ('foobar'))
  23. assert ( isBlank ('\u001b[106m \t \t \n \u001b[49m'))
  24. })
  25. it ('matches zero-width characters and ANSI escape codes', () => {
  26. let s = '\u001b[106m' + 'foo' + '\n\n' + 'bar' + '\u001b[49m'
  27. assert (s = s.replace (ansiEscapeCodes, ''), 'foo\n\nbar')
  28. assert ( s.replace (zeroWidthCharacters, ''), 'foobar')
  29. })
  30. it ('obtains blank string of the same width', () => {
  31. assert.equal (blank ('💩'), ' ')
  32. //assert.equal (blank ('👩‍❤️‍💋‍👩'), ' ') // FAILING, see http://blog.jonnew.com/posts/poo-dot-length-equals-two for possible solution
  33. assert.equal (blank ('❤️'), ' ')
  34. assert.equal (blank ('foo'), ' ')
  35. assert.equal (blank ('\n'), '\n')
  36. assert.equal (blank ('\t'), '\t')
  37. assert.equal (blank ('\tfoo \nfoo'), '\t \n ')
  38. assert.equal (blank ('\u001b[22m\u001b[1mfoo \t\u001b[39m\u001b[22m'), ' \t')
  39. })
  40. it ('extracts invisible parts followed by visible ones', () => {
  41. assert.deepEqual (partition (''), [ ])
  42. assert.deepEqual (partition ('foo'), [['', 'foo'] ])
  43. assert.deepEqual (partition ('\u001b[1mfoo'), [['\u001b[1m', 'foo'] ])
  44. assert.deepEqual (partition ('\u001b[1mfoo\u0000bar'), [['\u001b[1m', 'foo'], ['\u0000', 'bar'] ])
  45. assert.deepEqual (partition ('\u001b[1mfoo\u0000bar\n'), [['\u001b[1m', 'foo'], ['\u0000', 'bar'], ['\n', '']])
  46. })
  47. it ('gets first N visible symbols (preserving invisible parts)', () => {
  48. assert.equal (first ('💩23456789', 0), '')
  49. assert.equal (first ('💩23456789', 3), '💩23')
  50. assert.equal (first ('💩23456789', 100), '💩23456789')
  51. const s = '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m'
  52. assert.equal (first (s, 0), '\u001b[22m\u001b[1m' + '' + '\u0000' + '' + '\u001b[39m' + '' + '\n' + '' + '\u001b[39m\u001b[22m')
  53. assert.equal (first (s, 3), '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '' + '\u001b[39m' + '' + '\n' + '' + '\u001b[39m\u001b[22m')
  54. assert.equal (first (s, 4), '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '4' + '\u001b[39m' + '' + '\n' + '' + '\u001b[39m\u001b[22m')
  55. assert.equal (first (s, 6), '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '6' + '\n' + '' + '\u001b[39m\u001b[22m')
  56. assert.equal (first (s, 9), '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m')
  57. assert.equal (first (s, 100), '\u001b[22m\u001b[1m' + '💩23' + '\u0000' + '45' + '\u001b[39m' + '67' + '\n' + '89' + '\u001b[39m\u001b[22m')
  58. })
  59. })