1ffe3c632Sopenharmony_ci/**
2ffe3c632Sopenharmony_ci * @fileoverview Installs our custom equality matchers in Jasmine.
3ffe3c632Sopenharmony_ci */
4ffe3c632Sopenharmony_cigoog.module('protobuf.testing.jasmineProtoBuf');
5ffe3c632Sopenharmony_ci
6ffe3c632Sopenharmony_ciconst BufferDecoder = goog.require('protobuf.binary.BufferDecoder');
7ffe3c632Sopenharmony_ciconst ByteString = goog.require('protobuf.ByteString');
8ffe3c632Sopenharmony_ciconst {arrayBufferEqual} = goog.require('protobuf.binary.typedArrays');
9ffe3c632Sopenharmony_ci
10ffe3c632Sopenharmony_ci/**
11ffe3c632Sopenharmony_ci * A function that ensures custom equality for ByteStrings.
12ffe3c632Sopenharmony_ci * Since Jasmine compare structure by default Bytestrings might be equal that
13ffe3c632Sopenharmony_ci * are not equal since ArrayBuffers still compare content in g3.
14ffe3c632Sopenharmony_ci * (Jasmine fix upstream: https://github.com/jasmine/jasmine/issues/1687)
15ffe3c632Sopenharmony_ci * Also ByteStrings that are equal might compare non equal in jasmine of the
16ffe3c632Sopenharmony_ci * base64 string has been initialized.
17ffe3c632Sopenharmony_ci * @param {*} first
18ffe3c632Sopenharmony_ci * @param {*} second
19ffe3c632Sopenharmony_ci * @return {boolean|undefined}
20ffe3c632Sopenharmony_ci */
21ffe3c632Sopenharmony_ciconst byteStringEquality = (first, second) => {
22ffe3c632Sopenharmony_ci  if (second instanceof ByteString) {
23ffe3c632Sopenharmony_ci    return second.equals(first);
24ffe3c632Sopenharmony_ci  }
25ffe3c632Sopenharmony_ci
26ffe3c632Sopenharmony_ci  // Intentionally not returning anything, this signals to jasmine that we
27ffe3c632Sopenharmony_ci  // did not perform any equality on the given objects.
28ffe3c632Sopenharmony_ci};
29ffe3c632Sopenharmony_ci
30ffe3c632Sopenharmony_ci/**
31ffe3c632Sopenharmony_ci * A function that ensures custom equality for ArrayBuffers.
32ffe3c632Sopenharmony_ci * By default Jasmine does not compare the content of an ArrayBuffer and thus
33ffe3c632Sopenharmony_ci * will return true for buffers with the same length but different content.
34ffe3c632Sopenharmony_ci * @param {*} first
35ffe3c632Sopenharmony_ci * @param {*} second
36ffe3c632Sopenharmony_ci * @return {boolean|undefined}
37ffe3c632Sopenharmony_ci */
38ffe3c632Sopenharmony_ciconst arrayBufferCustomEquality = (first, second) => {
39ffe3c632Sopenharmony_ci  if (first instanceof ArrayBuffer && second instanceof ArrayBuffer) {
40ffe3c632Sopenharmony_ci    return arrayBufferEqual(first, second);
41ffe3c632Sopenharmony_ci  }
42ffe3c632Sopenharmony_ci  // Intentionally not returning anything, this signals to jasmine that we
43ffe3c632Sopenharmony_ci  // did not perform any equality on the given objects.
44ffe3c632Sopenharmony_ci};
45ffe3c632Sopenharmony_ci
46ffe3c632Sopenharmony_ci/**
47ffe3c632Sopenharmony_ci * A function that ensures custom equality for ArrayBuffers.
48ffe3c632Sopenharmony_ci * By default Jasmine does not compare the content of an ArrayBuffer and thus
49ffe3c632Sopenharmony_ci * will return true for buffers with the same length but different content.
50ffe3c632Sopenharmony_ci * @param {*} first
51ffe3c632Sopenharmony_ci * @param {*} second
52ffe3c632Sopenharmony_ci * @return {boolean|undefined}
53ffe3c632Sopenharmony_ci */
54ffe3c632Sopenharmony_ciconst bufferDecoderCustomEquality = (first, second) => {
55ffe3c632Sopenharmony_ci  if (first instanceof BufferDecoder && second instanceof BufferDecoder) {
56ffe3c632Sopenharmony_ci    return first.asByteString().equals(second.asByteString());
57ffe3c632Sopenharmony_ci  }
58ffe3c632Sopenharmony_ci  // Intentionally not returning anything, this signals to jasmine that we
59ffe3c632Sopenharmony_ci  // did not perform any equality on the given objects.
60ffe3c632Sopenharmony_ci};
61ffe3c632Sopenharmony_ci
62ffe3c632Sopenharmony_ci/**
63ffe3c632Sopenharmony_ci * Overrides the default ArrayBuffer toString method ([object ArrayBuffer]) with
64ffe3c632Sopenharmony_ci * a more readable representation.
65ffe3c632Sopenharmony_ci */
66ffe3c632Sopenharmony_cifunction overrideArrayBufferToString() {
67ffe3c632Sopenharmony_ci  /**
68ffe3c632Sopenharmony_ci   * Returns the hex values of the underlying bytes of the ArrayBuffer.
69ffe3c632Sopenharmony_ci   *
70ffe3c632Sopenharmony_ci   * @override
71ffe3c632Sopenharmony_ci   * @return {string}
72ffe3c632Sopenharmony_ci   */
73ffe3c632Sopenharmony_ci  ArrayBuffer.prototype.toString = function() {
74ffe3c632Sopenharmony_ci    const arr = Array.from(new Uint8Array(this));
75ffe3c632Sopenharmony_ci    return 'ArrayBuffer[' +
76ffe3c632Sopenharmony_ci        arr.map((b) => '0x' + (b & 0xFF).toString(16).toUpperCase())
77ffe3c632Sopenharmony_ci            .join(', ') +
78ffe3c632Sopenharmony_ci        ']';
79ffe3c632Sopenharmony_ci  };
80ffe3c632Sopenharmony_ci}
81ffe3c632Sopenharmony_ci
82ffe3c632Sopenharmony_cibeforeEach(() => {
83ffe3c632Sopenharmony_ci  jasmine.addCustomEqualityTester(arrayBufferCustomEquality);
84ffe3c632Sopenharmony_ci  jasmine.addCustomEqualityTester(bufferDecoderCustomEquality);
85ffe3c632Sopenharmony_ci  jasmine.addCustomEqualityTester(byteStringEquality);
86ffe3c632Sopenharmony_ci
87ffe3c632Sopenharmony_ci  overrideArrayBufferToString();
88ffe3c632Sopenharmony_ci});
89