zebra-zpl 1.1.2 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,203 +1,270 @@
1
- require 'spec_helper'
2
-
3
- describe Zebra::Zpl::Barcode do
4
- it "can be initialized with the position of the text to be printed" do
5
- barcode = described_class.new position: [20, 40]
6
- expect(barcode.position).to eq [20,40]
7
- expect(barcode.x).to eq 20
8
- expect(barcode.y).to eq 40
9
- end
10
-
11
- it "can be initialized with the barcode rotation" do
12
- rotation = Zebra::Zpl::Rotation::DEGREES_90
13
- barcode = described_class.new rotation: rotation
14
- expect(barcode.rotation).to eq rotation
15
- end
16
-
17
- it "can be initialized with the barcode rotation" do
18
- rotation = Zebra::Zpl::Rotation::DEGREES_90
19
- barcode = described_class.new rotation: rotation
20
- expect(barcode.rotation).to eq rotation
21
- end
22
-
23
- it "can be initialized with the barcode type" do
24
- type = Zebra::Zpl::BarcodeType::CODE_128_AUTO
25
- barcode = described_class.new type: type
26
- expect(barcode.type).to eq type
27
- end
28
-
29
- it "can be initialized with the barcode width" do
30
- barcode = described_class.new width: 5
31
- expect(barcode.width).to eq 5
32
- end
33
-
34
- it "can be initialized with the barcode height" do
35
- barcode = described_class.new height: 20
36
- expect(barcode.height).to eq 20
37
- end
38
-
39
- it "can be initialized with the ratio" do
40
- barcode = described_class.new ratio: 3.0
41
- expect(barcode.ratio).to eq 3.0
42
- end
43
-
44
- it "can be initialized with the narrow bar width" do
45
- barcode = described_class.new narrow_bar_width: 3
46
- expect(barcode.narrow_bar_width).to eq 3
47
- end
48
-
49
- it "can be initialized with the wide bar width" do
50
- barcode = described_class.new wide_bar_width: 10
51
- expect(barcode.wide_bar_width).to eq 10
52
- end
53
-
54
- it "can be initialized informing if the human readable code should be printed" do
55
- barcode = described_class.new print_human_readable_code: true
56
- expect(barcode.print_human_readable_code).to eq true
57
- end
58
-
59
- describe "#rotation=" do
60
- it "raises an error if the received rotation is invalid" do
61
- expect {
62
- described_class.new.rotation = 4
63
- }.to raise_error(Zebra::Zpl::Rotation::InvalidRotationError)
64
- end
65
- end
66
-
67
- describe "#type=" do
68
- it "raises an error if the received type is invalid" do
69
- expect {
70
- described_class.new.type = "ZZZ"
71
- }.to raise_error(Zebra::Zpl::BarcodeType::InvalidBarcodeTypeError)
72
- end
73
- end
74
-
75
- describe "#narrow_bar_width=" do
76
- it "raises an error if the type is Code 128 and the width is invalid" do
77
- expect {
78
- described_class.new type: Zebra::Zpl::BarcodeType::CODE_128_AUTO, narrow_bar_width: 20
79
- }.to raise_error(Zebra::Zpl::Barcode::InvalidNarrowBarWidthError)
80
- end
81
- end
82
-
83
- describe "#wide_bar_width=" do
84
- it "raises an error if the type is Code 128 and the width is invalid" do
85
- expect {
86
- described_class.new type: Zebra::Zpl::BarcodeType::CODE_128_AUTO, wide_bar_width: 40
87
- }.to raise_error(Zebra::Zpl::Barcode::InvalidWideBarWidthError)
88
- end
89
- end
90
-
91
- describe "#print_human_readable_code" do
92
- it "defaults to false" do
93
- expect(described_class.new.print_human_readable_code).to eq false
94
- end
95
- end
96
-
97
- describe "#to_zpl" do
98
- let(:valid_attributes) { {
99
- position: [100, 150],
100
- type: Zebra::Zpl::BarcodeType::CODE_128_AUTO,
101
- height: 20,
102
- width: 5,
103
- narrow_bar_width: 3,
104
- wide_bar_width: 6,
105
- data: "foobar"
106
- } }
107
- let(:barcode) { described_class.new valid_attributes }
108
- let(:tokens) { barcode.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
109
-
110
- it "raises an error if the X position was not informed" do
111
- barcode = described_class.new position: [nil, 100], data: "foobar"
112
- expect {
113
- barcode.to_zpl
114
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
115
- end
116
-
117
- it "raises an error if the Y position was not informed" do
118
- barcode = described_class.new position: [100, nil]
119
- expect {
120
- barcode.to_zpl
121
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
122
- end
123
-
124
- it "raises an error if the barcode type is not informed" do
125
- barcode = described_class.new position: [100, 100], data: "foobar"
126
- expect {
127
- barcode.to_zpl
128
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the barcode type to be used is not given")
129
- end
130
-
131
- it "raises an error if the data to be printed was not informed" do
132
- barcode.data = nil
133
- expect {
134
- barcode.to_zpl
135
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
136
- end
137
-
138
- it "raises an error if the height to be used was not informed" do
139
- barcode.height = nil
140
- expect {
141
- barcode.to_zpl
142
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the height to be used is not given")
143
- end
144
-
145
- it "raises an error if the neither of with, narrow_bar_width, or wide_bar_width are not given" do
146
- valid_attributes.delete :width
147
- valid_attributes.delete :narrow_bar_width
148
- valid_attributes.delete :wide_bar_width
149
- expect {
150
- barcode.to_zpl
151
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the width to be used is not given")
152
- end
153
-
154
- it "begins with the command 'B'" do
155
- expect(tokens[4]).to eq '^BY'
156
- end
157
-
158
- it "contains the X position" do
159
- expect(tokens[2]).to eq '100'
160
- end
161
-
162
- it "contains the Y position" do
163
- expect(tokens[3]).to eq '150'
164
- end
165
-
166
- it "contains the width" do
167
- expect(tokens[5]).to eq '5'
168
- end
169
-
170
- it "contains the ratio" do
171
- expect(tokens[6]).to eq '2.0'
172
- end
173
-
174
- it "contains the barcode height" do
175
- expect(tokens[7]).to eq '20'
176
- end
177
-
178
- it "contains the barcode type" do
179
- expect(tokens[8]).to start_with "^B#{Zebra::Zpl::BarcodeType::CODE_128_AUTO}"
180
- end
181
-
182
- it "contains the barcode rotation" do
183
- expect(tokens[0]).to start_with '^FW'
184
- expect(tokens[0]).to end_with Zebra::Zpl::Rotation::NO_ROTATION.to_s
185
- expect(tokens[8]).to start_with '^B'
186
- expect(tokens[8]).to end_with Zebra::Zpl::Rotation::NO_ROTATION.to_s
187
- end
188
-
189
- it "contains the correct indication when the human readable code should be printed" do
190
- valid_attributes.merge! print_human_readable_code: true
191
- expect(tokens[9]).to eq 'Y'
192
- end
193
-
194
- it "contains the correct indication when the human readable code should not be printed" do
195
- valid_attributes.merge! print_human_readable_code: false
196
- expect(tokens[9]).to eq 'N'
197
- end
198
-
199
- it "contains the data to be printed in the barcode" do
200
- expect(tokens[11]).to eq 'foobar'
201
- end
202
- end
203
- end
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Zpl::Barcode do
4
+ it "can be initialized with the position of the text to be printed" do
5
+ barcode = described_class.new position: [20, 40]
6
+ expect(barcode.position).to eq [20,40]
7
+ expect(barcode.x).to eq 20
8
+ expect(barcode.y).to eq 40
9
+ end
10
+
11
+ it "can be initialized with the barcode rotation" do
12
+ rotation = Zebra::Zpl::Rotation::DEGREES_90
13
+ barcode = described_class.new rotation: rotation
14
+ expect(barcode.rotation).to eq rotation
15
+ end
16
+
17
+ it "can be initialized with the barcode rotation" do
18
+ rotation = Zebra::Zpl::Rotation::DEGREES_90
19
+ barcode = described_class.new rotation: rotation
20
+ expect(barcode.rotation).to eq rotation
21
+ end
22
+
23
+ it "can be initialized with the barcode type" do
24
+ type = Zebra::Zpl::BarcodeType::CODE_128_AUTO
25
+ barcode = described_class.new type: type
26
+ expect(barcode.type).to eq type
27
+ end
28
+
29
+ it "can be initialized with the barcode width" do
30
+ barcode = described_class.new width: 5
31
+ expect(barcode.width).to eq 5
32
+ end
33
+
34
+ it "can be initialized with the barcode height" do
35
+ barcode = described_class.new height: 20
36
+ expect(barcode.height).to eq 20
37
+ end
38
+
39
+ it "can be initialized with the ratio" do
40
+ barcode = described_class.new ratio: 3.0
41
+ expect(barcode.ratio).to eq 3.0
42
+ end
43
+
44
+ it "can be initialized with the narrow bar width" do
45
+ barcode = described_class.new narrow_bar_width: 3
46
+ expect(barcode.narrow_bar_width).to eq 3
47
+ end
48
+
49
+ it "can be initialized with the wide bar width" do
50
+ barcode = described_class.new wide_bar_width: 10
51
+ expect(barcode.wide_bar_width).to eq 10
52
+ end
53
+
54
+ it "can be initialized informing if the human readable code should be printed" do
55
+ barcode = described_class.new print_human_readable_code: true
56
+ expect(barcode.print_human_readable_code).to eq true
57
+ end
58
+
59
+ it "can be initialized informing if the human readable code should be printed above" do
60
+ barcode = described_class.new print_text_above: true
61
+ expect(barcode.print_text_above).to eq true
62
+ end
63
+
64
+ it "can be initialized with the barcode mode" do
65
+ barcode = described_class.new mode: 'U'
66
+ expect(barcode.mode).to eq 'U'
67
+ end
68
+
69
+ describe "#rotation=" do
70
+ it "raises an error if the received rotation is invalid" do
71
+ expect {
72
+ described_class.new.rotation = 4
73
+ }.to raise_error(Zebra::Zpl::Rotation::InvalidRotationError)
74
+ end
75
+ end
76
+
77
+ describe "#type=" do
78
+ it "raises an error if the received type is invalid" do
79
+ expect {
80
+ described_class.new.type = "ZZZ"
81
+ }.to raise_error(Zebra::Zpl::BarcodeType::InvalidBarcodeTypeError)
82
+ end
83
+ end
84
+
85
+ describe "#narrow_bar_width=" do
86
+ it "raises an error if the type is Code 128 and the width is invalid" do
87
+ expect {
88
+ described_class.new type: Zebra::Zpl::BarcodeType::CODE_128_AUTO, narrow_bar_width: 20
89
+ }.to raise_error(Zebra::Zpl::Barcode::InvalidNarrowBarWidthError)
90
+ end
91
+ end
92
+
93
+ describe "#wide_bar_width=" do
94
+ it "raises an error if the type is Code 128 and the width is invalid" do
95
+ expect {
96
+ described_class.new type: Zebra::Zpl::BarcodeType::CODE_128_AUTO, wide_bar_width: 40
97
+ }.to raise_error(Zebra::Zpl::Barcode::InvalidWideBarWidthError)
98
+ end
99
+ end
100
+
101
+ describe "#print_human_readable_code" do
102
+ it "defaults to false" do
103
+ expect(described_class.new.print_human_readable_code).to eq false
104
+ end
105
+ end
106
+
107
+ describe "#print_text_above" do
108
+ it "defaults to false" do
109
+ expect(described_class.new.print_text_above).to eq false
110
+ end
111
+ end
112
+
113
+ describe "#to_zpl" do
114
+ let(:valid_attributes) { {
115
+ position: [100, 150],
116
+ type: Zebra::Zpl::BarcodeType::CODE_128_AUTO,
117
+ height: 20,
118
+ width: 5,
119
+ narrow_bar_width: 3,
120
+ wide_bar_width: 6,
121
+ data: "foobar"
122
+ } }
123
+ let(:barcode) { described_class.new valid_attributes }
124
+ let(:tokens) { barcode.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
125
+
126
+ it "raises an error if the X position was not informed" do
127
+ barcode = described_class.new position: [nil, 100], data: "foobar"
128
+ expect {
129
+ barcode.to_zpl
130
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
131
+ end
132
+
133
+ it "raises an error if the Y position was not informed" do
134
+ barcode = described_class.new position: [100, nil]
135
+ expect {
136
+ barcode.to_zpl
137
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
138
+ end
139
+
140
+ it "raises an error if the barcode type is not informed" do
141
+ barcode = described_class.new position: [100, 100], data: "foobar"
142
+ expect {
143
+ barcode.to_zpl
144
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the barcode type to be used is not given")
145
+ end
146
+
147
+ it "raises an error if the data to be printed was not informed" do
148
+ barcode.data = nil
149
+ expect {
150
+ barcode.to_zpl
151
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
152
+ end
153
+
154
+ it "raises an error if the height to be used was not informed" do
155
+ barcode.height = nil
156
+ expect {
157
+ barcode.to_zpl
158
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the height to be used is not given")
159
+ end
160
+
161
+ it "raises an error if the neither of with, narrow_bar_width, or wide_bar_width are not given" do
162
+ valid_attributes.delete :width
163
+ valid_attributes.delete :narrow_bar_width
164
+ valid_attributes.delete :wide_bar_width
165
+ expect {
166
+ barcode.to_zpl
167
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the width to be used is not given")
168
+ end
169
+
170
+ it "begins with the command 'B'" do
171
+ expect(tokens[4]).to eq '^BY'
172
+ end
173
+
174
+ it "contains the X position" do
175
+ expect(tokens[2]).to eq '100'
176
+ end
177
+
178
+ it "contains the Y position" do
179
+ expect(tokens[3]).to eq '150'
180
+ end
181
+
182
+ it "contains the width" do
183
+ expect(tokens[5]).to eq '5'
184
+ end
185
+
186
+ it "contains the ratio" do
187
+ expect(tokens[6]).to eq '2.0'
188
+ end
189
+
190
+ it "contains the barcode height" do
191
+ expect(tokens[7]).to eq '20'
192
+ end
193
+
194
+ it "contains the barcode type" do
195
+ expect(tokens[8]).to start_with "^B#{Zebra::Zpl::BarcodeType::CODE_128_AUTO}"
196
+ end
197
+
198
+ it "contains the barcode rotation" do
199
+ expect(tokens[0]).to start_with '^FW'
200
+ expect(tokens[0]).to end_with Zebra::Zpl::Rotation::NO_ROTATION.to_s
201
+ expect(tokens[8]).to start_with '^B'
202
+ expect(tokens[8]).to end_with Zebra::Zpl::Rotation::NO_ROTATION.to_s
203
+ end
204
+
205
+ it "contains the correct indication when the human readable code should be printed" do
206
+ valid_attributes.merge! print_human_readable_code: true
207
+ expect(tokens[9]).to eq 'Y'
208
+ end
209
+
210
+ it "contains the correct indication when the human readable code should not be printed" do
211
+ valid_attributes.merge! print_human_readable_code: false
212
+ expect(tokens[9]).to eq 'N'
213
+ end
214
+
215
+ it "contains the correct indication when the human readable code should be printed above or below" do
216
+ valid_attributes.merge! print_text_above: true
217
+ expect(tokens[10]).to eq 'Y'
218
+ end
219
+
220
+ it "contains the correct indication when the human readable code should not be printed above or below" do
221
+ valid_attributes.merge! print_text_above: false
222
+ expect(tokens[10]).to eq 'N'
223
+ end
224
+
225
+ it "contains the correct indication of a mode in barcode" do
226
+ valid_attributes.merge! mode: 'U'
227
+ expect(tokens[11]).to eq 'U'
228
+ end
229
+
230
+ it "contains the data to be printed in the barcode" do
231
+ expect(tokens[12]).to eq 'foobar'
232
+ end
233
+ end
234
+
235
+ describe "#to_zpl with CODE_39" do
236
+ let(:valid_attributes) { {
237
+ position: [100, 150],
238
+ type: Zebra::Zpl::BarcodeType::CODE_39,
239
+ height: 20,
240
+ width: 5,
241
+ narrow_bar_width: 3,
242
+ wide_bar_width: 6,
243
+ print_human_readable_code: true,
244
+ print_text_above: true,
245
+ data: "foobar"
246
+ } }
247
+ let(:barcode) { described_class.new valid_attributes }
248
+
249
+ it "contains the barcode type" do
250
+ expect(barcode.to_zpl).to end_with "^B#{Zebra::Zpl::BarcodeType::CODE_39}N,,,Y,Y^FDfoobar^FS"
251
+ end
252
+ end
253
+
254
+ describe "#to_zpl with CODE_UPS_MAXICODE" do
255
+ let(:valid_attributes) { {
256
+ position: [100, 150],
257
+ type: Zebra::Zpl::BarcodeType::CODE_UPS_MAXICODE,
258
+ height: 20,
259
+ width: 5,
260
+ narrow_bar_width: 3,
261
+ wide_bar_width: 6,
262
+ data: "foobar"
263
+ } }
264
+ let(:barcode) { described_class.new valid_attributes }
265
+
266
+ it "contains the barcode type" do
267
+ expect(barcode.to_zpl).to end_with "^B#{Zebra::Zpl::BarcodeType::CODE_UPS_MAXICODE}^FDfoobar^FS"
268
+ end
269
+ end
270
+ end