2db47680
hexiaodong
chore: 初始提交 php 遗...
|
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
|
const test = require('tap').test
const sinon = require('sinon')
const fs = require('fs')
const htmlparser = require('htmlparser2')
const QRCode = require('core/qrcode')
const SvgRenderer = require('renderer/svg')
function getExpectedViewbox (size, margin) {
const expectedQrCodeSize = size + margin * 2
return '0 0 ' + expectedQrCodeSize + ' ' + expectedQrCodeSize
}
function testSvgFragment (t, svgFragment, expectedTags) {
return new Promise(function (resolve, reject) {
const parser = new htmlparser.Parser({
onopentag: function (name, attribs) {
const tag = expectedTags.shift()
t.equal(tag.name, name,
'Should have a ' + tag.name + ' tag')
tag.attribs.forEach(function (attr) {
t.equal(attribs[attr.name], attr.value.toString(),
'Should have attrib ' + attr.name + ' with value ' + attr.value)
})
},
onend: function () {
resolve()
},
onerror: function (e) {
reject(e)
}
}, { decodeEntities: true })
parser.write(svgFragment)
parser.end()
})
}
function buildTest (t, data, opts, expectedTags) {
const svg = SvgRenderer.render(data, opts)
return testSvgFragment(t, svg, expectedTags.slice())
}
test('svgrender interface', function (t) {
t.type(SvgRenderer.render, 'function',
'Should have render function')
t.type(SvgRenderer.renderToFile, 'function',
'Should have renderToFile function')
t.end()
})
test('Svg render', function (t) {
const tests = []
const data = QRCode.create('sample text', { version: 2 })
const size = data.modules.size
tests.push(buildTest(t, data, {
scale: 4,
margin: 4,
color: {
light: '#ffffff80'
}
}, [
{
name: 'svg',
attribs: [
{ name: 'viewbox', value: getExpectedViewbox(size, 4) }
]
},
{
name: 'path',
attribs: [
{ name: 'fill', value: '#ffffff' },
{ name: 'fill-opacity', value: '.50' }
]
},
{
name: 'path',
attribs: [
{ name: 'stroke', value: '#000000' }
]
}
]))
tests.push(buildTest(t, data, {
scale: 0,
margin: 8,
color: {
light: '#0000',
dark: '#00000080'
}
}, [
{
name: 'svg',
attribs: [
{ name: 'viewbox', value: getExpectedViewbox(size, 8) }
]
},
{
name: 'path',
attribs: [
{ name: 'stroke', value: '#000000' },
{ name: 'stroke-opacity', value: '.50' }
]
}
]))
tests.push(buildTest(t, data, {}, [
{
name: 'svg',
attribs: [
{ name: 'viewbox', value: getExpectedViewbox(size, 4) }
]
},
{ name: 'path', attribs: [{ name: 'fill', value: '#ffffff' }] },
{ name: 'path', attribs: [{ name: 'stroke', value: '#000000' }] }
]))
tests.push(buildTest(t, data, { width: 250 }, [
{
name: 'svg',
attribs: [
{ name: 'width', value: '250' },
{ name: 'height', value: '250' },
{ name: 'viewbox', value: getExpectedViewbox(size, 4) }
]
},
{ name: 'path', attribs: [{ name: 'fill', value: '#ffffff' }] },
{ name: 'path', attribs: [{ name: 'stroke', value: '#000000' }] }
]))
Promise.all(tests).then(function () {
t.end()
})
})
test('Svg renderToFile', function (t) {
const sampleQrData = QRCode.create('sample text', { version: 2 })
const fileName = 'qrimage.svg'
let fsStub = sinon.stub(fs, 'writeFile').callsArg(2)
t.plan(5)
SvgRenderer.renderToFile(fileName, sampleQrData, function (err) {
t.ok(!err,
'Should not generate errors with only qrData param')
t.equal(fsStub.getCall(0).args[0], fileName,
'Should save file with correct file name')
})
SvgRenderer.renderToFile(fileName, sampleQrData, {
margin: 10,
scale: 1
}, function (err) {
t.ok(!err,
'Should not generate errors with options param')
t.equal(fsStub.getCall(0).args[0], fileName,
'Should save file with correct file name')
})
fsStub.restore()
fsStub = sinon.stub(fs, 'writeFile').callsArgWith(2, new Error())
SvgRenderer.renderToFile(fileName, sampleQrData, function (err) {
t.ok(err,
'Should fail if error occurs during save')
})
fsStub.restore()
})
|