util_spec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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('isArrayBuffer', function () {
  40. it('handles array buffer values', function () {
  41. expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
  42. expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
  43. });
  44. it('handles non-array buffer values', function () {
  45. expect((0, _util.isArrayBuffer)('true')).toEqual(false);
  46. expect((0, _util.isArrayBuffer)(1)).toEqual(false);
  47. expect((0, _util.isArrayBuffer)(null)).toEqual(false);
  48. expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
  49. });
  50. });
  51. describe('isBool', function () {
  52. it('handles boolean values', function () {
  53. expect((0, _util.isBool)(true)).toEqual(true);
  54. expect((0, _util.isBool)(false)).toEqual(true);
  55. });
  56. it('handles non-boolean values', function () {
  57. expect((0, _util.isBool)('true')).toEqual(false);
  58. expect((0, _util.isBool)('false')).toEqual(false);
  59. expect((0, _util.isBool)(1)).toEqual(false);
  60. expect((0, _util.isBool)(0)).toEqual(false);
  61. expect((0, _util.isBool)(null)).toEqual(false);
  62. expect((0, _util.isBool)(undefined)).toEqual(false);
  63. });
  64. });
  65. describe('isEmptyObj', function () {
  66. it('handles empty objects', function () {
  67. expect((0, _util.isEmptyObj)({})).toEqual(true);
  68. });
  69. it('handles non-empty objects', function () {
  70. expect((0, _util.isEmptyObj)({ foo: 'bar' })).toEqual(false);
  71. });
  72. });
  73. describe('isNum', function () {
  74. it('handles numeric values', function () {
  75. expect((0, _util.isNum)(1)).toEqual(true);
  76. expect((0, _util.isNum)(0)).toEqual(true);
  77. expect((0, _util.isNum)(-1)).toEqual(true);
  78. expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
  79. expect((0, _util.isNum)(12.34)).toEqual(true);
  80. });
  81. it('handles non-numeric values', function () {
  82. expect((0, _util.isNum)('true')).toEqual(false);
  83. expect((0, _util.isNum)(true)).toEqual(false);
  84. expect((0, _util.isNum)(null)).toEqual(false);
  85. expect((0, _util.isNum)(undefined)).toEqual(false);
  86. });
  87. });
  88. describe('isSpace', function () {
  89. it('handles space characters', function () {
  90. expect((0, _util.isSpace)(0x20)).toEqual(true);
  91. expect((0, _util.isSpace)(0x09)).toEqual(true);
  92. expect((0, _util.isSpace)(0x0D)).toEqual(true);
  93. expect((0, _util.isSpace)(0x0A)).toEqual(true);
  94. });
  95. it('handles non-space characters', function () {
  96. expect((0, _util.isSpace)(0x0B)).toEqual(false);
  97. expect((0, _util.isSpace)(null)).toEqual(false);
  98. expect((0, _util.isSpace)(undefined)).toEqual(false);
  99. });
  100. });
  101. describe('isString', function () {
  102. it('handles string values', function () {
  103. expect((0, _util.isString)('foo')).toEqual(true);
  104. expect((0, _util.isString)('')).toEqual(true);
  105. });
  106. it('handles non-string values', function () {
  107. expect((0, _util.isString)(true)).toEqual(false);
  108. expect((0, _util.isString)(1)).toEqual(false);
  109. expect((0, _util.isString)(null)).toEqual(false);
  110. expect((0, _util.isString)(undefined)).toEqual(false);
  111. });
  112. });
  113. describe('log2', function () {
  114. it('handles values smaller than/equal to zero', function () {
  115. expect((0, _util.log2)(0)).toEqual(0);
  116. expect((0, _util.log2)(-1)).toEqual(0);
  117. });
  118. it('handles values larger than zero', function () {
  119. expect((0, _util.log2)(1)).toEqual(0);
  120. expect((0, _util.log2)(2)).toEqual(1);
  121. expect((0, _util.log2)(3)).toEqual(2);
  122. expect((0, _util.log2)(3.14)).toEqual(2);
  123. });
  124. });
  125. describe('stringToBytes', function () {
  126. it('handles non-string arguments', function () {
  127. expect(function () {
  128. (0, _util.stringToBytes)(null);
  129. }).toThrow(new Error('Invalid argument for stringToBytes'));
  130. });
  131. it('handles string arguments', function () {
  132. expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
  133. expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
  134. });
  135. });
  136. describe('stringToPDFString', function () {
  137. it('handles ISO Latin 1 strings', function () {
  138. var str = '\x8Dstring\x8E';
  139. expect((0, _util.stringToPDFString)(str)).toEqual('\u201Cstring\u201D');
  140. });
  141. it('handles UTF-16BE strings', function () {
  142. var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
  143. expect((0, _util.stringToPDFString)(str)).toEqual('string');
  144. });
  145. it('handles empty strings', function () {
  146. var str1 = '';
  147. expect((0, _util.stringToPDFString)(str1)).toEqual('');
  148. var str2 = '\xFE\xFF';
  149. expect((0, _util.stringToPDFString)(str2)).toEqual('');
  150. });
  151. });
  152. describe('removeNullCharacters', function () {
  153. it('should not modify string without null characters', function () {
  154. var str = 'string without null chars';
  155. expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
  156. });
  157. it('should modify string with null characters', function () {
  158. var str = 'string\x00With\x00Null\x00Chars';
  159. expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
  160. });
  161. });
  162. describe('ReadableStream', function () {
  163. it('should return an Object', function () {
  164. var readable = new _util.ReadableStream();
  165. expect(typeof readable === 'undefined' ? 'undefined' : _typeof(readable)).toEqual('object');
  166. });
  167. it('should have property getReader', function () {
  168. var readable = new _util.ReadableStream();
  169. expect(_typeof(readable.getReader)).toEqual('function');
  170. });
  171. });
  172. });