jasmine_protobuf.js 3.0 KB

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