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