1 |
|
2 |
|
3 |
|
4 |
|
5 | import test from "ava";
|
6 | import * as sinon from "sinon";
|
7 |
|
8 | import { BrowserHelper, configure, Parser, ParserResult, Scanner } from "..";
|
9 |
|
10 | declare const global: any;
|
11 |
|
12 | global.Worker = sinon.stub().returns({
|
13 | postMessage: sinon.stub(),
|
14 | terminate: sinon.stub()
|
15 | });
|
16 | URL.createObjectURL = sinon.stub();
|
17 |
|
18 | async function prepareBrowserAndLibrary(): Promise<void> {
|
19 | BrowserHelper.checkBrowserCompatibility = () => {
|
20 | return {
|
21 | fullSupport: true,
|
22 | scannerSupport: true,
|
23 | missingFeatures: []
|
24 | };
|
25 | };
|
26 | await configure("#".repeat(64));
|
27 | }
|
28 |
|
29 | test("constructor", async t => {
|
30 | await prepareBrowserAndLibrary();
|
31 | const s: Scanner = new Scanner();
|
32 | const p: Parser = new Parser(s, Parser.DataFormat.DLID);
|
33 | t.is((<any>p).scanner, s);
|
34 | t.is((<any>p).dataFormat, Parser.DataFormat.DLID);
|
35 | t.is((<any>p).options, undefined);
|
36 | });
|
37 |
|
38 | test("setOptions", async t => {
|
39 | await prepareBrowserAndLibrary();
|
40 | const s: Scanner = new Scanner();
|
41 | const p: Parser = new Parser(s, Parser.DataFormat.DLID);
|
42 | t.is((<any>p).options, undefined);
|
43 | p.setOptions({});
|
44 | t.deepEqual((<any>p).options, {});
|
45 | p.setOptions({
|
46 | option1: true
|
47 | });
|
48 | t.deepEqual((<any>p).options, {
|
49 | option1: true
|
50 | });
|
51 | });
|
52 |
|
53 | test("parseString", async t => {
|
54 | await prepareBrowserAndLibrary();
|
55 | const s: Scanner = new Scanner();
|
56 | const p: Parser = new Parser(s, Parser.DataFormat.DLID);
|
57 | const parseStringSpy: sinon.SinonSpy<[Parser.DataFormat, string, any?], Promise<ParserResult>> = sinon.spy(
|
58 | s,
|
59 | "parseString"
|
60 | );
|
61 |
|
62 | p.parseString("");
|
63 | t.is(parseStringSpy.callCount, 1);
|
64 | t.deepEqual(parseStringSpy.getCall(0).args, [Parser.DataFormat.DLID, "", undefined]);
|
65 |
|
66 | p.parseString("abcd");
|
67 | t.is(parseStringSpy.callCount, 2);
|
68 | t.deepEqual(parseStringSpy.getCall(1).args, [Parser.DataFormat.DLID, "abcd", undefined]);
|
69 | p.setOptions({
|
70 | option1: true
|
71 | });
|
72 |
|
73 | p.parseString("abcd");
|
74 | t.is(parseStringSpy.callCount, 3);
|
75 | t.deepEqual(parseStringSpy.getCall(2).args, [
|
76 | Parser.DataFormat.DLID,
|
77 | "abcd",
|
78 | {
|
79 | option1: true
|
80 | }
|
81 | ]);
|
82 | });
|
83 |
|
84 | test("parseRawData", async t => {
|
85 | await prepareBrowserAndLibrary();
|
86 | const s: Scanner = new Scanner();
|
87 | const p: Parser = new Parser(s, Parser.DataFormat.DLID);
|
88 | const parseStringSpy: sinon.SinonSpy<[Parser.DataFormat, string, any?], Promise<ParserResult>> = sinon.spy(
|
89 | s,
|
90 | "parseString"
|
91 | );
|
92 |
|
93 | p.parseRawData(new Uint8Array([]));
|
94 | t.is(parseStringSpy.callCount, 1);
|
95 | t.deepEqual(parseStringSpy.getCall(0).args, [Parser.DataFormat.DLID, "", undefined]);
|
96 |
|
97 | p.parseRawData(new Uint8Array([97, 98, 99, 100]));
|
98 | t.is(parseStringSpy.callCount, 2);
|
99 | t.deepEqual(parseStringSpy.getCall(1).args, [Parser.DataFormat.DLID, "abcd", undefined]);
|
100 | p.setOptions({
|
101 | option1: true
|
102 | });
|
103 |
|
104 | p.parseRawData(new Uint8Array([97, 98, 99, 100]));
|
105 | t.is(parseStringSpy.callCount, 3);
|
106 | t.deepEqual(parseStringSpy.getCall(2).args, [
|
107 | Parser.DataFormat.DLID,
|
108 | "abcd",
|
109 | {
|
110 | option1: true
|
111 | }
|
112 | ]);
|
113 | p.setOptions();
|
114 |
|
115 | p.parseRawData(new Uint8Array([255]));
|
116 | t.is(parseStringSpy.callCount, 4);
|
117 | t.deepEqual(parseStringSpy.getCall(3).args, [Parser.DataFormat.DLID, "", undefined]);
|
118 |
|
119 | p.parseRawData(new Uint8Array([97, 98, 99, 100, 128]));
|
120 | t.is(parseStringSpy.callCount, 5);
|
121 | t.deepEqual(parseStringSpy.getCall(4).args, [Parser.DataFormat.DLID, "", undefined]);
|
122 |
|
123 | p.parseRawData(new Uint8Array([1, 2, 9, 32, 13, 10]));
|
124 | t.is(parseStringSpy.callCount, 6);
|
125 | t.deepEqual(parseStringSpy.getCall(5).args, [Parser.DataFormat.DLID, "\u{1}\u{2}\t \r\n", undefined]);
|
126 | });
|