textencoding_test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @fileoverview Tests for textdecoder.js.
  3. */
  4. goog.module('protobuf.binary.TextDecoderTest');
  5. goog.setTestOnly();
  6. const {decode, encode} = goog.require('protobuf.binary.textencoding');
  7. describe('Decode does', () => {
  8. it('return empty string for empty array', () => {
  9. expect(decode(new DataView(new ArrayBuffer(0)))).toEqual('');
  10. });
  11. it('throw on null being passed', () => {
  12. expect(() => decode(/** @type {!DataView} */ (/** @type {*} */ (null))))
  13. .toThrow();
  14. });
  15. });
  16. describe('Encode does', () => {
  17. it('return empty array for empty string', () => {
  18. expect(encode('')).toEqual(new Uint8Array(0));
  19. });
  20. it('throw on null being passed', () => {
  21. expect(() => encode(/** @type {string} */ (/** @type {*} */ (null))))
  22. .toThrow();
  23. });
  24. });
  25. /** @const {!TextEncoder} */
  26. const textEncoder = new TextEncoder('utf-8');
  27. /**
  28. * A Pair of string and Uint8Array representing the same data.
  29. * Each pair has the string value and its utf-8 bytes.
  30. */
  31. class Pair {
  32. /**
  33. * Constructs a pair from a given string.
  34. * @param {string} s
  35. * @return {!Pair}
  36. */
  37. static fromString(s) {
  38. return new Pair(s, textEncoder.encode(s).buffer);
  39. }
  40. /**
  41. * Constructs a pair from a given charCode.
  42. * @param {number} charCode
  43. * @return {!Pair}
  44. */
  45. static fromCharCode(charCode) {
  46. return Pair.fromString(String.fromCharCode(charCode));
  47. }
  48. /**
  49. * @param {string} stringValue
  50. * @param {!ArrayBuffer} bytes
  51. * @private
  52. */
  53. constructor(stringValue, bytes) {
  54. /** @const @private {string} */
  55. this.stringValue_ = stringValue;
  56. /** @const @private {!ArrayBuffer} */
  57. this.bytes_ = bytes;
  58. }
  59. /** Ensures that a given pair encodes and decodes round trip*/
  60. expectPairToMatch() {
  61. expect(decode(new DataView(this.bytes_))).toEqual(this.stringValue_);
  62. expect(encode(this.stringValue_)).toEqual(new Uint8Array(this.bytes_));
  63. }
  64. }
  65. describe('textencoding does', () => {
  66. it('works for empty string', () => {
  67. Pair.fromString('').expectPairToMatch();
  68. });
  69. it('decode and encode random strings', () => {
  70. // 1 byte strings
  71. Pair.fromString('hello').expectPairToMatch();
  72. Pair.fromString('HELLO1!');
  73. // 2 byte String
  74. Pair.fromString('©').expectPairToMatch();
  75. // 3 byte string
  76. Pair.fromString('❄').expectPairToMatch();
  77. // 4 byte string
  78. Pair.fromString('😁').expectPairToMatch();
  79. });
  80. it('decode and encode 1 byte strings', () => {
  81. for (let i = 0; i < 0x80; i++) {
  82. Pair.fromCharCode(i).expectPairToMatch();
  83. }
  84. });
  85. it('decode and encode 2 byte strings', () => {
  86. for (let i = 0xC0; i < 0x7FF; i++) {
  87. Pair.fromCharCode(i).expectPairToMatch();
  88. }
  89. });
  90. it('decode and encode 3 byte strings', () => {
  91. for (let i = 0x7FF; i < 0x8FFF; i++) {
  92. Pair.fromCharCode(i).expectPairToMatch();
  93. }
  94. });
  95. });