test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* eslint-env mocha */
  2. 'use strict'
  3. const assert = require('assert')
  4. const encodeUtf8 = require('./')
  5. const testCases = [
  6. '゚・✿ヾ╲(。◕‿◕。)╱✿・゚',
  7. '𝌆',
  8. '🐵 🙈 🙉 🙊',
  9. '💩',
  10. 'åß∂ƒ©˙∆˚¬…æ',
  11. 'Hello, World!',
  12. 'Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗',
  13. '𝕿𝖍𝖊 𝖖𝖚𝖎𝖈𝖐 𝖇𝖗𝖔𝖜𝖓 𝖋𝖔𝖝 𝖏𝖚𝖒𝖕𝖘 𝖔𝖛𝖊𝖗 𝖙𝖍𝖊 𝖑𝖆𝖟𝖞 𝖉𝖔𝖌',
  14. '사회과학원 어학연구소'
  15. ]
  16. const badStrings = [
  17. {
  18. input: 'abc123',
  19. expected: [0x61, 0x62, 0x63, 0x31, 0x32, 0x33],
  20. name: 'Sanity check'
  21. },
  22. {
  23. input: '\uD800',
  24. expected: [0xef, 0xbf, 0xbd],
  25. name: 'Surrogate half (low)'
  26. },
  27. {
  28. input: '\uDC00',
  29. expected: [0xef, 0xbf, 0xbd],
  30. name: 'Surrogate half (high)'
  31. },
  32. {
  33. input: 'abc\uD800123',
  34. expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33],
  35. name: 'Surrogate half (low), in a string'
  36. },
  37. {
  38. input: 'abc\uDC00123',
  39. expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33],
  40. name: 'Surrogate half (high), in a string'
  41. },
  42. {
  43. input: '\uDC00\uD800',
  44. expected: [0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd],
  45. name: 'Wrong order'
  46. }
  47. ]
  48. describe('encode-utf8', () => {
  49. describe('test strings', () => {
  50. for (const input of testCases) {
  51. it(`should encode "${input}"`, () => {
  52. const actual = Buffer.from(encodeUtf8(input))
  53. const expected = Buffer.from(input, 'utf8')
  54. assert.ok(actual.equals(expected))
  55. })
  56. }
  57. })
  58. describe('web platform test', () => {
  59. for (const testCase of badStrings) {
  60. it(testCase.name, () => {
  61. const actual = Array.from(new Uint8Array(encodeUtf8(testCase.input)))
  62. assert.deepStrictEqual(actual, testCase.expected)
  63. })
  64. }
  65. })
  66. })