UNPKG

11.6 kBJavaScriptView Raw
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
24var _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; };
25
26var _util = require('../../shared/util');
27
28var _primitives = require('../../core/primitives');
29
30var _test_utils = require('./test_utils');
31
32describe('util', function () {
33 describe('bytesToString', function () {
34 it('handles non-array arguments', function () {
35 expect(function () {
36 (0, _util.bytesToString)(null);
37 }).toThrow(new Error('Invalid argument for bytesToString'));
38 });
39 it('handles array arguments with a length not exceeding the maximum', function () {
40 expect((0, _util.bytesToString)(new Uint8Array([]))).toEqual('');
41 expect((0, _util.bytesToString)(new Uint8Array([102, 111, 111]))).toEqual('foo');
42 });
43 it('handles array arguments with a length exceeding the maximum', function () {
44 var length = 10000;
45 var bytes = new Uint8Array(length);
46 for (var i = 0; i < length; i++) {
47 bytes[i] = 'a'.charCodeAt(0);
48 }
49 var string = Array(length + 1).join('a');
50 expect((0, _util.bytesToString)(bytes)).toEqual(string);
51 });
52 });
53 describe('getInheritableProperty', function () {
54 it('handles non-dictionary arguments', function () {
55 expect((0, _util.getInheritableProperty)({
56 dict: null,
57 key: 'foo'
58 })).toEqual(undefined);
59 expect((0, _util.getInheritableProperty)({
60 dict: undefined,
61 key: 'foo'
62 })).toEqual(undefined);
63 });
64 it('handles dictionaries that do not contain the property', function () {
65 var emptyDict = new _primitives.Dict();
66 expect((0, _util.getInheritableProperty)({
67 dict: emptyDict,
68 key: 'foo'
69 })).toEqual(undefined);
70 var filledDict = new _primitives.Dict();
71 filledDict.set('bar', 'baz');
72 expect((0, _util.getInheritableProperty)({
73 dict: filledDict,
74 key: 'foo'
75 })).toEqual(undefined);
76 });
77 it('fetches the property if it is not inherited', function () {
78 var ref = new _primitives.Ref(10, 0);
79 var xref = new _test_utils.XRefMock([{
80 ref: ref,
81 data: 'quux'
82 }]);
83 var dict = new _primitives.Dict(xref);
84 dict.set('foo', 'bar');
85 expect((0, _util.getInheritableProperty)({
86 dict: dict,
87 key: 'foo'
88 })).toEqual('bar');
89 dict.set('baz', ['qux', ref]);
90 expect((0, _util.getInheritableProperty)({
91 dict: dict,
92 key: 'baz',
93 getArray: true
94 })).toEqual(['qux', 'quux']);
95 });
96 it('fetches the property if it is inherited and present on one level', function () {
97 var ref = new _primitives.Ref(10, 0);
98 var xref = new _test_utils.XRefMock([{
99 ref: ref,
100 data: 'quux'
101 }]);
102 var firstDict = new _primitives.Dict(xref);
103 var secondDict = new _primitives.Dict(xref);
104 firstDict.set('Parent', secondDict);
105 secondDict.set('foo', 'bar');
106 expect((0, _util.getInheritableProperty)({
107 dict: firstDict,
108 key: 'foo'
109 })).toEqual('bar');
110 secondDict.set('baz', ['qux', ref]);
111 expect((0, _util.getInheritableProperty)({
112 dict: firstDict,
113 key: 'baz',
114 getArray: true
115 })).toEqual(['qux', 'quux']);
116 });
117 it('fetches the property if it is inherited and present on multiple levels', function () {
118 var ref = new _primitives.Ref(10, 0);
119 var xref = new _test_utils.XRefMock([{
120 ref: ref,
121 data: 'quux'
122 }]);
123 var firstDict = new _primitives.Dict(xref);
124 var secondDict = new _primitives.Dict(xref);
125 firstDict.set('Parent', secondDict);
126 firstDict.set('foo', 'bar1');
127 secondDict.set('foo', 'bar2');
128 expect((0, _util.getInheritableProperty)({
129 dict: firstDict,
130 key: 'foo'
131 })).toEqual('bar1');
132 expect((0, _util.getInheritableProperty)({
133 dict: firstDict,
134 key: 'foo',
135 getArray: false,
136 stopWhenFound: false
137 })).toEqual(['bar1', 'bar2']);
138 firstDict.set('baz', ['qux1', ref]);
139 secondDict.set('baz', ['qux2', ref]);
140 expect((0, _util.getInheritableProperty)({
141 dict: firstDict,
142 key: 'baz',
143 getArray: true,
144 stopWhenFound: false
145 })).toEqual([['qux1', 'quux'], ['qux2', 'quux']]);
146 });
147 it('stops searching when the loop limit is reached', function () {
148 var dict = new _primitives.Dict();
149 var currentDict = dict;
150 var parentDict = null;
151 for (var i = 0; i < 150; i++) {
152 parentDict = new _primitives.Dict();
153 currentDict.set('Parent', parentDict);
154 currentDict = parentDict;
155 }
156 parentDict.set('foo', 'bar');
157 expect((0, _util.getInheritableProperty)({
158 dict: dict,
159 key: 'foo'
160 })).toEqual(undefined);
161 dict.set('foo', 'baz');
162 expect((0, _util.getInheritableProperty)({
163 dict: dict,
164 key: 'foo',
165 getArray: false,
166 stopWhenFound: false
167 })).toEqual(['baz']);
168 });
169 });
170 describe('isArrayBuffer', function () {
171 it('handles array buffer values', function () {
172 expect((0, _util.isArrayBuffer)(new ArrayBuffer(0))).toEqual(true);
173 expect((0, _util.isArrayBuffer)(new Uint8Array(0))).toEqual(true);
174 });
175 it('handles non-array buffer values', function () {
176 expect((0, _util.isArrayBuffer)('true')).toEqual(false);
177 expect((0, _util.isArrayBuffer)(1)).toEqual(false);
178 expect((0, _util.isArrayBuffer)(null)).toEqual(false);
179 expect((0, _util.isArrayBuffer)(undefined)).toEqual(false);
180 });
181 });
182 describe('isBool', function () {
183 it('handles boolean values', function () {
184 expect((0, _util.isBool)(true)).toEqual(true);
185 expect((0, _util.isBool)(false)).toEqual(true);
186 });
187 it('handles non-boolean values', function () {
188 expect((0, _util.isBool)('true')).toEqual(false);
189 expect((0, _util.isBool)('false')).toEqual(false);
190 expect((0, _util.isBool)(1)).toEqual(false);
191 expect((0, _util.isBool)(0)).toEqual(false);
192 expect((0, _util.isBool)(null)).toEqual(false);
193 expect((0, _util.isBool)(undefined)).toEqual(false);
194 });
195 });
196 describe('isEmptyObj', function () {
197 it('handles empty objects', function () {
198 expect((0, _util.isEmptyObj)({})).toEqual(true);
199 });
200 it('handles non-empty objects', function () {
201 expect((0, _util.isEmptyObj)({ foo: 'bar' })).toEqual(false);
202 });
203 });
204 describe('isNum', function () {
205 it('handles numeric values', function () {
206 expect((0, _util.isNum)(1)).toEqual(true);
207 expect((0, _util.isNum)(0)).toEqual(true);
208 expect((0, _util.isNum)(-1)).toEqual(true);
209 expect((0, _util.isNum)(1000000000000000000)).toEqual(true);
210 expect((0, _util.isNum)(12.34)).toEqual(true);
211 });
212 it('handles non-numeric values', function () {
213 expect((0, _util.isNum)('true')).toEqual(false);
214 expect((0, _util.isNum)(true)).toEqual(false);
215 expect((0, _util.isNum)(null)).toEqual(false);
216 expect((0, _util.isNum)(undefined)).toEqual(false);
217 });
218 });
219 describe('isSpace', function () {
220 it('handles space characters', function () {
221 expect((0, _util.isSpace)(0x20)).toEqual(true);
222 expect((0, _util.isSpace)(0x09)).toEqual(true);
223 expect((0, _util.isSpace)(0x0D)).toEqual(true);
224 expect((0, _util.isSpace)(0x0A)).toEqual(true);
225 });
226 it('handles non-space characters', function () {
227 expect((0, _util.isSpace)(0x0B)).toEqual(false);
228 expect((0, _util.isSpace)(null)).toEqual(false);
229 expect((0, _util.isSpace)(undefined)).toEqual(false);
230 });
231 });
232 describe('isString', function () {
233 it('handles string values', function () {
234 expect((0, _util.isString)('foo')).toEqual(true);
235 expect((0, _util.isString)('')).toEqual(true);
236 });
237 it('handles non-string values', function () {
238 expect((0, _util.isString)(true)).toEqual(false);
239 expect((0, _util.isString)(1)).toEqual(false);
240 expect((0, _util.isString)(null)).toEqual(false);
241 expect((0, _util.isString)(undefined)).toEqual(false);
242 });
243 });
244 describe('log2', function () {
245 it('handles values smaller than/equal to zero', function () {
246 expect((0, _util.log2)(0)).toEqual(0);
247 expect((0, _util.log2)(-1)).toEqual(0);
248 });
249 it('handles values larger than zero', function () {
250 expect((0, _util.log2)(1)).toEqual(0);
251 expect((0, _util.log2)(2)).toEqual(1);
252 expect((0, _util.log2)(3)).toEqual(2);
253 expect((0, _util.log2)(3.14)).toEqual(2);
254 });
255 });
256 describe('stringToBytes', function () {
257 it('handles non-string arguments', function () {
258 expect(function () {
259 (0, _util.stringToBytes)(null);
260 }).toThrow(new Error('Invalid argument for stringToBytes'));
261 });
262 it('handles string arguments', function () {
263 expect((0, _util.stringToBytes)('')).toEqual(new Uint8Array([]));
264 expect((0, _util.stringToBytes)('foo')).toEqual(new Uint8Array([102, 111, 111]));
265 });
266 });
267 describe('stringToPDFString', function () {
268 it('handles ISO Latin 1 strings', function () {
269 var str = '\x8Dstring\x8E';
270 expect((0, _util.stringToPDFString)(str)).toEqual('\u201Cstring\u201D');
271 });
272 it('handles UTF-16BE strings', function () {
273 var str = '\xFE\xFF\x00\x73\x00\x74\x00\x72\x00\x69\x00\x6E\x00\x67';
274 expect((0, _util.stringToPDFString)(str)).toEqual('string');
275 });
276 it('handles empty strings', function () {
277 var str1 = '';
278 expect((0, _util.stringToPDFString)(str1)).toEqual('');
279 var str2 = '\xFE\xFF';
280 expect((0, _util.stringToPDFString)(str2)).toEqual('');
281 });
282 });
283 describe('removeNullCharacters', function () {
284 it('should not modify string without null characters', function () {
285 var str = 'string without null chars';
286 expect((0, _util.removeNullCharacters)(str)).toEqual('string without null chars');
287 });
288 it('should modify string with null characters', function () {
289 var str = 'string\x00With\x00Null\x00Chars';
290 expect((0, _util.removeNullCharacters)(str)).toEqual('stringWithNullChars');
291 });
292 });
293 describe('ReadableStream', function () {
294 it('should return an Object', function () {
295 var readable = new _util.ReadableStream();
296 expect(typeof readable === 'undefined' ? 'undefined' : _typeof(readable)).toEqual('object');
297 });
298 it('should have property getReader', function () {
299 var readable = new _util.ReadableStream();
300 expect(_typeof(readable.getReader)).toEqual('function');
301 });
302 });
303});
\No newline at end of file