util_spec.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  17. var _util = require('../../shared/util');
  18. describe('util', function () {
  19. describe('bytesToString', function () {
  20. it('handles non-array arguments', function () {
  21. expect(function () {
  22. (0, _util.bytesToString)(null);
  23. }).toThrow(new Error('Invalid argument for bytesToString'));
  24. });
  25. it('handles array arguments with a length not exceeding the maximum', function () {
  26. expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual('');
  27. expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual('foo');
  28. });
  29. it('handles array arguments with a length exceeding the maximum', function () {
  30. var length = 10000;
  31. var bytes = new Uint8Array(length);
  32. for (var i = 0; i < length; i++) {
  33. bytes[i] = 'a'.charCodeAt(0);
  34. }
  35. var string = Array(length + 1).join('a');
  36. expect((0, _util.bytesToString)(bytes)).toEqual(string);
  37. });
  38. });
  39. describe('stringToBytes', function () {
  40. it('handles non-string arguments', function () {
  41. expect(function () {
  42. (0, _util.stringToBytes)(null);
  43. }).toThrow(new Error('Invalid argument for stringToBytes'));
  44. });
  45. it('handles string arguments', function () {
  46. expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
  47. expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
  48. });
  49. });
  50. describe('stringToPDFString', function () {
  51. it('handles ISO Latin 1 strings', function () {
  52. var str = '\x8Dstring\x8E';
  53. expect((0, _util.stringToPDFString)(str)).toEqual('\u201Cstring\u201D');
  54. });
  55. it('handles UTF-16BE strings', function () {
  56. var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
  57. expect((0, _util.stringToPDFString)(str)).toEqual('string');
  58. });
  59. it('handles empty strings', function () {
  60. var str1 = '';
  61. expect((0, _util.stringToPDFString)(str1)).toEqual('');
  62. var str2 = '\xFE\xFF';
  63. expect((0, _util.stringToPDFString)(str2)).toEqual('');
  64. });
  65. });
  66. describe('removeNullCharacters', function () {
  67. it('should not modify string without null characters', function () {
  68. var str = 'string without null chars';
  69. expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
  70. });
  71. it('should modify string with null characters', function () {
  72. var str = 'string\x00With\x00Null\x00Chars';
  73. expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
  74. });
  75. });
  76. describe('ReadableStream', function () {
  77. it('should return an Object', function () {
  78. var readable = new _util.ReadableStream();
  79. expect(typeof readable === 'undefined' ? 'undefined' : _typeof(readable)).toEqual('object');
  80. });
  81. it('should have property getReader', function () {
  82. var readable = new _util.ReadableStream();
  83. expect(_typeof(readable.getReader)).toEqual('function');
  84. });
  85. });
  86. });