-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathtoFile.test.js
More file actions
267 lines (223 loc) · 8.93 KB
/
Copy pathtoFile.test.js
File metadata and controls
267 lines (223 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const test = require('tap').test
const fs = require('fs')
const path = require('path')
const os = require('os')
const sinon = require('sinon')
const QRCode = require('lib')
const Helpers = require('test/helpers')
const StreamMock = require('test/mocks/writable-stream')
test('toFile - no promise available', function (t) {
Helpers.removeNativePromise()
const fileName = path.join(os.tmpdir(), 'qrimage.png')
t.throw(function () { QRCode.toFile(fileName, 'some text') },
'Should throw if a callback is not provided')
t.throw(function () { QRCode.toFile(fileName, 'some text', {}) },
'Should throw if a callback is not a function')
t.end()
Helpers.restoreNativePromise()
})
test('toFile', function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage.png')
t.throw(function () { QRCode.toFile('some text', function () {}) },
'Should throw if path is not provided')
t.throw(function () { QRCode.toFile(fileName) },
'Should throw if text is not provided')
t.equal(typeof QRCode.toFile(fileName, 'some text').then, 'function',
'Should return a promise')
t.end()
})
test('toFile png', function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage.png')
const expectedBase64Output = [
'iVBORw0KGgoAAAANSUhEUgAAAHQAAAB0CAYAAABUmhYnAAAAAklEQVR4AewaftIAAAKzSU',
'RBVO3BQW7kQAwEwSxC//9y7h55akCQxvYQjIj/scYo1ijFGqVYoxRrlGKNUqxRijVKsUYp',
'1ijFGqVYoxRrlGKNUqxRijXKxUNJ+EkqdyShU+mS0Kl0SfhJKk8Ua5RijVKsUS5epvKmJD',
'yh8iaVNyXhTcUapVijFGuUiw9Lwh0qdyShU+mS0Kl0Kk8k4Q6VTyrWKMUapVijXHw5lROV',
'kyR0Kt+sWKMUa5RijXIxTBI6lS4JkxVrlGKNUqxRLj5M5Tcl4UTlCZW/pFijFGuUYo1y8b',
'Ik/KQkdCpdEjqVLgmdykkS/rJijVKsUYo1ysVDKt9M5UTlmxRrlGKNUqxRLh5Kwh0qXRJ+',
'UxLuULkjCZ3KJxVrlGKNUqxRLh5S6ZLQqXRJ6FS6JHQqXRKeSEKn0iWhUzlJwolKl4QTlS',
'eKNUqxRinWKBe/LAmdSpeETuUkCZ1Kl4QTlS4Jd6h0SehUuiS8qVijFGuUYo1y8WFJ6FS6',
'JJyofFISOpVOpUtCp3KicqLypmKNUqxRijXKxYep3JGEE5UuCZ3KHSp3qHRJ6FR+U7FGKd',
'YoxRol/scXS8ITKidJeEKlS8KJyhPFGqVYoxRrlIuHkvCTVE5U7kjCicpJEk6S8JOKNUqx',
'RinWKBcvU3lTEu5IwolKp/KEyh1J6FTeVKxRijVKsUa5+LAk3KHyJpWTJHQqdyShU/lNxR',
'qlWKMUa5SLL6fSJaFLwhNJeCIJP6lYoxRrlGKNcvHlknCicpKEE5UuCSdJOFHpktCpPFGs',
'UYo1SrFGufgwlZ+k0iWhU+lUnlDpktCpdEnoVN5UrFGKNUqxRrl4WRL+EpU7ktCpdCpdEj',
'qVO5LQqTxRrFGKNUqxRon/scYo1ijFGqVYoxRrlGKNUqxRijVKsUYp1ijFGqVYoxRrlGKN',
'UqxRijXKP0OHEepgrecVAAAAAElFTkSuQmCC'].join('')
t.plan(8)
QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L'
}, function (err) {
t.ok(!err, 'There should be no error')
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name')
})
fs.readFile(fileName, function (err, buffer) {
if (err) throw err
t.equal(buffer.toString('base64'), expectedBase64Output,
'Should write correct content')
})
})
QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L',
type: 'png'
}, function (err) {
t.ok(!err, 'There should be no errors if file type is specified')
})
QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L'
}).then(function () {
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name (promise)')
})
fs.readFile(fileName, function (err, buffer) {
if (err) throw err
t.equal(buffer.toString('base64'), expectedBase64Output,
'Should write correct content (promise)')
})
})
const fsStub = sinon.stub(fs, 'createWriteStream')
fsStub.returns(new StreamMock().forceErrorOnWrite())
QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L'
}, function (err) {
t.ok(err, 'There should be an error')
})
QRCode.toFile(fileName, 'i am a pony!', {
errorCorrectionLevel: 'L'
}).catch(function (err) {
t.ok(err, 'Should catch an error (promise)')
})
fsStub.restore()
})
test('toFile svg', function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage.svg')
const expectedOutput = fs.readFileSync(
path.join(__dirname, '/svg.expected.out'), 'UTF-8')
t.plan(6)
QRCode.toFile(fileName, 'http://www.google.com', {
errorCorrectionLevel: 'H'
}, function (err) {
t.ok(!err, 'There should be no error')
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name')
})
fs.readFile(fileName, 'utf8', function (err, content) {
if (err) throw err
t.equal(content, expectedOutput,
'Should write correct content')
})
})
QRCode.toFile(fileName, 'http://www.google.com', {
errorCorrectionLevel: 'H',
type: 'svg'
}, function (err) {
t.ok(!err, 'There should be no errors if file type is specified')
})
QRCode.toFile(fileName, 'http://www.google.com', {
errorCorrectionLevel: 'H'
}).then(function () {
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name (promise)')
})
fs.readFile(fileName, 'utf8', function (err, content) {
if (err) throw err
t.equal(content, expectedOutput,
'Should write correct content (promise)')
})
})
})
test('toFile utf8', function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage-utf8.txt')
const expectedOutput = [
' ',
' ',
' █▀▀▀▀▀█ █ ▄█ ▀ █ █▀▀▀▀▀█ ',
' █ ███ █ ▀█▄▀▄█ ▀▄ █ ███ █ ',
' █ ▀▀▀ █ ▀▄ ▄ ▄▀ █ █ ▀▀▀ █ ',
' ▀▀▀▀▀▀▀ ▀ ▀ █▄▀ █ ▀▀▀▀▀▀▀ ',
' ▀▄ ▀▀▀▀█▀▀█▄ ▄█▄▀█ ▄█▄██▀ ',
' █▄ ▄▀▀▀▄▄█ █▀▀▄█▀ ▀█ █▄▄█ ',
' █▄ ▄█▄▀█▄▄ ▀ ▄██▀▀ ▄ ▄▀ ',
' █▀▄▄▄▄▀▀█▀▀█▀▀▀█ ▀ ▄█▀█▀█ ',
' ▀ ▀▀▀▀▀▀███▄▄▄▀ █▀▀▀█ ▀█ ',
' █▀▀▀▀▀█ █▀█▀▄ ▄▄█ ▀ █▀ ▄█ ',
' █ ███ █ █ █ ▀▀██▀███▀█ ██ ',
' █ ▀▀▀ █ █▀ ▀ █ ▀▀▄██ ███ ',
' ▀▀▀▀▀▀▀ ▀▀▀ ▀▀ ▀ ▀ ▀ ',
' ',
' '].join('\n')
t.plan(6)
QRCode.toFile(fileName, 'http://www.google.com', function (err) {
t.ok(!err, 'There should be no error')
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name')
})
fs.readFile(fileName, 'utf8', function (err, content) {
if (err) throw err
t.equal(content, expectedOutput,
'Should write correct content')
})
})
QRCode.toFile(fileName, 'http://www.google.com', {
errorCorrectionLevel: 'M',
type: 'utf8'
}, function (err) {
t.ok(!err, 'There should be no errors if file type is specified')
})
QRCode.toFile(fileName, 'http://www.google.com')
.then(function () {
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name (promise)')
})
fs.readFile(fileName, 'utf8', function (err, content) {
if (err) throw err
t.equal(content, expectedOutput,
'Should write correct content (promise)')
})
})
})
test('toFile manual segments', function (t) {
const fileName = path.join(os.tmpdir(), 'qrimage-segments.txt')
const segs = [
{ data: 'ABCDEFG', mode: 'alphanumeric' },
{ data: '0123456', mode: 'numeric' }
]
const expectedOutput = [
' ',
' ',
' █▀▀▀▀▀█ ██▀██ █▀▀▀▀▀█ ',
' █ ███ █ █▀█▄ █ ███ █ ',
' █ ▀▀▀ █ █ ▄ ▀ █ ▀▀▀ █ ',
' ▀▀▀▀▀▀▀ █▄█▄▀ ▀▀▀▀▀▀▀ ',
' ▀██ ▄▀▀▄█▀▀▀▀██▀▀▄ █▀ ',
' ▀█▀▀█▀█▄ ▄ ▄█▀▀▀█▀ ',
' ▀ ▀▀▀ ▀ ▄▀ ▄ ▄▀▄ ▀▄ ',
' █▀▀▀▀▀█ ▄ █▀█ ▀▀▀▄█▄ ',
' █ ███ █ █▀▀▀ ██▀▀ ▀▀ ',
' █ ▀▀▀ █ ██ ▄▀▀▀▀▄▀▀█ ',
' ▀▀▀▀▀▀▀ ▀ ▀▀▀▀ ▀▀▀ ',
' ',
' '].join('\n')
t.plan(3)
QRCode.toFile(fileName, segs, {
errorCorrectionLevel: 'L'
}, function (err) {
t.ok(!err, 'There should be no errors if text is not string')
fs.stat(fileName, function (err) {
t.ok(!err,
'Should save file with correct file name')
})
fs.readFile(fileName, 'utf8', function (err, content) {
if (err) throw err
t.equal(content, expectedOutput,
'Should write correct content')
})
})
})