2
0

util_spec.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. 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; };
  24. var _util = require('../../shared/util');
  25. describe('util', function () {
  26. describe('bytesToString', function () {
  27. it('handles non-array arguments', function () {
  28. expect(function () {
  29. (0, _util.bytesToString)(null);
  30. }).toThrow(new Error('Invalid argument for bytesToString'));
  31. });
  32. it('handles array arguments with a length not exceeding the maximum', function () {
  33. expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual('');
  34. expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual('foo');
  35. });
  36. it('handles array arguments with a length exceeding the maximum', function () {
  37. var length = 10000;
  38. var bytes = new Uint8Array(length);
  39. for (var i = 0; i < length; i++) {
  40. bytes[i] = 'a'.charCodeAt(0);
  41. }
  42. var string = Array(length + 1).join('a');
  43. expect((0, _util.bytesToString)(bytes)).toEqual(string);
  44. });
  45. });
  46. describe('isArrayBuffer', function () {
  47. it('handles array buffer values', function () {
  48. expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
  49. expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
  50. });
  51. it('handles non-array buffer values', function () {
  52. expect((0, _util.isArrayBuffer)('true')).toEqual(false);
  53. expect((0, _util.isArrayBuffer)(1)).toEqual(false);
  54. expect((0, _util.isArrayBuffer)(null)).toEqual(false);
  55. expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
  56. });
  57. });
  58. describe('isBool', function () {
  59. it('handles boolean values', function () {
  60. expect((0, _util.isBool)(true)).toEqual(true);
  61. expect((0, _util.isBool)(false)).toEqual(true);
  62. });
  63. it('handles non-boolean values', function () {
  64. expect((0, _util.isBool)('true')).toEqual(false);
  65. expect((0, _util.isBool)('false')).toEqual(false);
  66. expect((0, _util.isBool)(1)).toEqual(false);
  67. expect((0, _util.isBool)(0)).toEqual(false);
  68. expect((0, _util.isBool)(null)).toEqual(false);
  69. expect((0, _util.isBool)(undefined)).toEqual(false);
  70. });
  71. });
  72. describe('isEmptyObj', function () {
  73. it('handles empty objects', function () {
  74. expect((0, _util.isEmptyObj)({})).toEqual(true);
  75. });
  76. it('handles non-empty objects', function () {
  77. expect((0, _util.isEmptyObj)({ foo: 'bar' })).toEqual(false);
  78. });
  79. });
  80. describe('isNum', function () {
  81. it('handles numeric values', function () {
  82. expect((0, _util.isNum)(1)).toEqual(true);
  83. expect((0, _util.isNum)(0)).toEqual(true);
  84. expect((0, _util.isNum)(-1)).toEqual(true);
  85. expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
  86. expect((0, _util.isNum)(12.34)).toEqual(true);
  87. });
  88. it('handles non-numeric values', function () {
  89. expect((0, _util.isNum)('true')).toEqual(false);
  90. expect((0, _util.isNum)(true)).toEqual(false);
  91. expect((0, _util.isNum)(null)).toEqual(false);
  92. expect((0, _util.isNum)(undefined)).toEqual(false);
  93. });
  94. });
  95. describe('isSpace', function () {
  96. it('handles space characters', function () {
  97. expect((0, _util.isSpace)(0x20)).toEqual(true);
  98. expect((0, _util.isSpace)(0x09)).toEqual(true);
  99. expect((0, _util.isSpace)(0x0D)).toEqual(true);
  100. expect((0, _util.isSpace)(0x0A)).toEqual(true);
  101. });
  102. it('handles non-space characters', function () {
  103. expect((0, _util.isSpace)(0x0B)).toEqual(false);
  104. expect((0, _util.isSpace)(null)).toEqual(false);
  105. expect((0, _util.isSpace)(undefined)).toEqual(false);
  106. });
  107. });
  108. describe('isString', function () {
  109. it('handles string values', function () {
  110. expect((0, _util.isString)('foo')).toEqual(true);
  111. expect((0, _util.isString)('')).toEqual(true);
  112. });
  113. it('handles non-string values', function () {
  114. expect((0, _util.isString)(true)).toEqual(false);
  115. expect((0, _util.isString)(1)).toEqual(false);
  116. expect((0, _util.isString)(null)).toEqual(false);
  117. expect((0, _util.isString)(undefined)).toEqual(false);
  118. });
  119. });
  120. describe('log2', function () {
  121. it('handles values smaller than/equal to zero', function () {
  122. expect((0, _util.log2)(0)).toEqual(0);
  123. expect((0, _util.log2)(-1)).toEqual(0);
  124. });
  125. it('handles values larger than zero', function () {
  126. expect((0, _util.log2)(1)).toEqual(0);
  127. expect((0, _util.log2)(2)).toEqual(1);
  128. expect((0, _util.log2)(3)).toEqual(2);
  129. expect((0, _util.log2)(3.14)).toEqual(2);
  130. });
  131. });
  132. describe('stringToBytes', function () {
  133. it('handles non-string arguments', function () {
  134. expect(function () {
  135. (0, _util.stringToBytes)(null);
  136. }).toThrow(new Error('Invalid argument for stringToBytes'));
  137. });
  138. it('handles string arguments', function () {
  139. expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
  140. expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
  141. });
  142. });
  143. describe('stringToPDFString', function () {
  144. it('handles ISO Latin 1 strings', function () {
  145. var str = '\x8Dstring\x8E';
  146. expect((0, _util.stringToPDFString)(str)).toEqual('\u201Cstring\u201D');
  147. });
  148. it('handles UTF-16BE strings', function () {
  149. var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
  150. expect((0, _util.stringToPDFString)(str)).toEqual('string');
  151. });
  152. it('handles empty strings', function () {
  153. var str1 = '';
  154. expect((0, _util.stringToPDFString)(str1)).toEqual('');
  155. var str2 = '\xFE\xFF';
  156. expect((0, _util.stringToPDFString)(str2)).toEqual('');
  157. });
  158. });
  159. describe('removeNullCharacters', function () {
  160. it('should not modify string without null characters', function () {
  161. var str = 'string without null chars';
  162. expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
  163. });
  164. it('should modify string with null characters', function () {
  165. var str = 'string\x00With\x00Null\x00Chars';
  166. expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
  167. });
  168. });
  169. describe('ReadableStream', function () {
  170. it('should return an Object', function () {
  171. var readable = new _util.ReadableStream();
  172. expect(typeof readable === 'undefined' ? 'undefined' : _typeof(readable)).toEqual('object');
  173. });
  174. it('should have property getReader', function () {
  175. var readable = new _util.ReadableStream();
  176. expect(_typeof(readable.getReader)).toEqual('function');
  177. });
  178. });
  179. });