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