zebra-zpl 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,196 @@
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
+ barcode.position.should == [20,40]
7
+ barcode.x.should == 20
8
+ barcode.y.should == 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
+ barcode.rotation.should == 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
+ barcode.rotation.should == rotation
21
+ end
22
+
23
+ it "can be initialized with the barcode type" do
24
+ type = Zebra::Zpl::BarcodeType::CODE_128_C
25
+ barcode = described_class.new :type => type
26
+ barcode.type.should == type
27
+ end
28
+
29
+ it "can be initialized with the narrow bar width" do
30
+ barcode = described_class.new :narrow_bar_width => 3
31
+ barcode.narrow_bar_width.should == 3
32
+ end
33
+
34
+ it "can be initialized with the wide bar width" do
35
+ barcode = described_class.new :wide_bar_width => 10
36
+ barcode.wide_bar_width.should == 10
37
+ end
38
+
39
+ it "can be initialized with the barcode height" do
40
+ barcode = described_class.new :height => 20
41
+ barcode.height.should == 20
42
+ end
43
+
44
+ it "can be initialized informing if the human readable code should be printed" do
45
+ barcode = described_class.new :print_human_readable_code => true
46
+ barcode.print_human_readable_code.should == true
47
+ end
48
+
49
+ describe "#rotation=" do
50
+ it "raises an error if the received rotation is invalid" do
51
+ expect {
52
+ described_class.new.rotation = 4
53
+ }.to raise_error(Zebra::Zpl::Rotation::InvalidRotationError)
54
+ end
55
+ end
56
+
57
+ describe "#type=" do
58
+ it "raises an error if the received type is invalid" do
59
+ expect {
60
+ described_class.new.type = "ZZZ"
61
+ }.to raise_error(Zebra::Zpl::BarcodeType::InvalidBarcodeTypeError)
62
+ end
63
+ end
64
+
65
+ describe "#narrow_bar_width=" do
66
+ it "raises an error if the type is Code 128 and the width is invalid" do
67
+ expect {
68
+ described_class.new :type => Zebra::Zpl::BarcodeType::CODE_128_AUTO, :narrow_bar_width => 20
69
+ }.to raise_error(Zebra::Zpl::Barcode::InvalidNarrowBarWidthError)
70
+ end
71
+ end
72
+
73
+ describe "#wide_bar_width=" do
74
+ it "raises an error if the type is Code 128 and the width is invalid" do
75
+ expect {
76
+ described_class.new :type => Zebra::Zpl::BarcodeType::CODE_128_AUTO, :wide_bar_width => 40
77
+ }.to raise_error(Zebra::Zpl::Barcode::InvalidWideBarWidthError)
78
+ end
79
+ end
80
+
81
+ describe "#print_human_readable_code" do
82
+ it "defaults to false" do
83
+ described_class.new.print_human_readable_code.should == false
84
+ end
85
+ end
86
+
87
+ describe "#to_zpl" do
88
+ let(:valid_attributes) { {
89
+ :position => [100, 150],
90
+ :type => Zebra::Zpl::BarcodeType::CODE_128_AUTO,
91
+ :height => 20,
92
+ :narrow_bar_width => 4,
93
+ :wide_bar_width => 6,
94
+ :data => "foobar"
95
+ } }
96
+ let(:barcode) { described_class.new valid_attributes }
97
+ let(:tokens) { barcode.to_zpl.split(",") }
98
+
99
+ it "raises an error if the X position was not informed" do
100
+ barcode = described_class.new :position => [nil, 100], :data => "foobar"
101
+ expect {
102
+ barcode.to_zpl
103
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
104
+ end
105
+
106
+ it "raises an error if the Y position was not informed" do
107
+ barcode = described_class.new :position => [100, nil]
108
+ expect {
109
+ barcode.to_zpl
110
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
111
+ end
112
+
113
+ it "raises an error if the barcode type is not informed" do
114
+ barcode = described_class.new :position => [100, 100], :data => "foobar"
115
+ expect {
116
+ barcode.to_zpl
117
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the barcode type to be used is not given")
118
+ end
119
+
120
+ it "raises an error if the data to be printed was not informed" do
121
+ barcode.data = nil
122
+ expect {
123
+ barcode.to_zpl
124
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
125
+ end
126
+
127
+ it "raises an error if the height to be used was not informed" do
128
+ barcode.height = nil
129
+ expect {
130
+ barcode.to_zpl
131
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the height to be used is not given")
132
+ end
133
+
134
+ it "raises an error if the narrow bar width is not given" do
135
+ valid_attributes.delete :narrow_bar_width
136
+
137
+ expect {
138
+ barcode.to_zpl
139
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the narrow bar width to be used is not given")
140
+ end
141
+
142
+ it "raises an error if the wide bar width is not given" do
143
+ valid_attributes.delete :wide_bar_width
144
+
145
+ expect {
146
+ barcode.to_zpl
147
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the wide bar width to be used is not given")
148
+ end
149
+
150
+ it "begins with the command 'B'" do
151
+ barcode.to_zpl.should =~ /\AB/
152
+ end
153
+
154
+ it "contains the X position" do
155
+ tokens[0].match(/B(\d+)/)[1].should == "100"
156
+ end
157
+
158
+ it "contains the Y position" do
159
+ tokens[1].should == "150"
160
+ end
161
+
162
+ it "contains the barcode rotation" do
163
+ tokens[2].should == Zebra::Zpl::Rotation::NO_ROTATION.to_s
164
+ end
165
+
166
+ it "contains the barcode type" do
167
+ tokens[3].should == Zebra::Zpl::BarcodeType::CODE_128_AUTO
168
+ end
169
+
170
+ it "contains the barcode narrow bar width" do
171
+ tokens[4].should == "4"
172
+ end
173
+
174
+ it "contains the barcode wide bar width" do
175
+ tokens[5].should == "6"
176
+ end
177
+
178
+ it "contains the barcode height" do
179
+ tokens[6].should == "20"
180
+ end
181
+
182
+ it "contains the correct indication when the human readable code should be printed" do
183
+ valid_attributes.merge! :print_human_readable_code => true
184
+ tokens[7].should == "B"
185
+ end
186
+
187
+ it "contains the correct indication when the human readable code should not be printed" do
188
+ valid_attributes.merge! :print_human_readable_code => false
189
+ tokens[7].should == "N"
190
+ end
191
+
192
+ it "contains the data to be printed in the barcode" do
193
+ tokens[8].should == "\"foobar\""
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Zebra::Zpl::Box do
5
+ it "can be initialized with initial position" do
6
+ box = described_class.new :position => [20, 40]
7
+ box.position.should == [20, 40]
8
+ box.x.should == 20
9
+ box.y.should == 40
10
+ end
11
+
12
+ it "can be initialized with the end position" do
13
+ box = described_class.new :end_position => [20, 40]
14
+ box.end_position.should == [20, 40]
15
+ box.end_x.should == 20
16
+ box.end_y.should == 40
17
+ end
18
+
19
+ it "can be initialized with the line thckness " do
20
+ box = described_class.new :line_thickness => 3
21
+ box.line_thickness.should == 3
22
+ end
23
+
24
+ describe "#to_zpl" do
25
+ subject(:box) { described_class.new attributes }
26
+ let(:attributes) { { :position => [20,40], :end_position => [60, 100], :line_thickness => 3 } }
27
+
28
+ it "raises an error if the X position was not informed" do
29
+ box = described_class.new attributes.merge :position => [nil, 40]
30
+ expect {
31
+ box.to_zpl
32
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
33
+ end
34
+
35
+ it "raises an error if the Y position was not informed" do
36
+ box = described_class.new attributes.merge :position => [20, nil]
37
+ expect {
38
+ box.to_zpl
39
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
40
+ end
41
+
42
+ it "raises an error if the end X position was not informed" do
43
+ box = described_class.new attributes.merge :end_position => [nil, 40]
44
+ expect {
45
+ box.to_zpl
46
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the horizontal end position (X) is not given")
47
+ end
48
+
49
+ it "raises an error if the end Y position was not informed" do
50
+ box = described_class.new attributes.merge :end_position => [20, nil]
51
+ expect {
52
+ box.to_zpl
53
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the vertical end position (Y) is not given")
54
+ end
55
+
56
+ it "raises an error if the line thickness was not informed" do
57
+ box = described_class.new attributes.merge :line_thickness => nil
58
+ expect {
59
+ box.to_zpl
60
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the line thickness is not given")
61
+ end
62
+
63
+ it "begins with the 'X' command" do
64
+ box.to_zpl.should =~ /\AX/
65
+ end
66
+
67
+ it "contains the attributes in correct order" do
68
+ box.to_zpl.should == "X20,40,3,60,100"
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Zpl::CharacterSet do
4
+ it "can be initialized with the number of data bits" do
5
+ character_set = described_class.new :number_of_data_bits => 8
6
+ character_set.number_of_data_bits.should == 8
7
+ end
8
+
9
+ it "raises an error when receiving an invalid number of data bits" do
10
+ expect {
11
+ described_class.new :number_of_data_bits => 9
12
+ }.to raise_error(Zebra::Zpl::CharacterSet::InvalidNumberOfDataBits)
13
+ end
14
+
15
+ it "can be initialized with the language" do
16
+ character_set = described_class.new :language => Zebra::Zpl::Language::PORTUGUESE
17
+ character_set.language.should == Zebra::Zpl::Language::PORTUGUESE
18
+ end
19
+
20
+ it "raises an error with an invalid language" do
21
+ expect {
22
+ described_class.new :language => "G"
23
+ }.to raise_error(Zebra::Zpl::Language::InvalidLanguageError)
24
+ end
25
+
26
+ it "can be initialized with the country code" do
27
+ character_set = described_class.new :country_code => Zebra::Zpl::CountryCode::LATIN_AMERICA
28
+ character_set.country_code.should == Zebra::Zpl::CountryCode::LATIN_AMERICA
29
+ end
30
+
31
+ it "raises an error with an invalid country code" do
32
+ expect {
33
+ described_class.new :country_code => "999"
34
+ }.to raise_error(Zebra::Zpl::CountryCode::InvalidCountryCodeError)
35
+ end
36
+
37
+ describe "#to_zpl" do
38
+ let(:valid_attributes) { {
39
+ :number_of_data_bits => 8,
40
+ :language => Zebra::Zpl::Language::PORTUGUESE,
41
+ :country_code => Zebra::Zpl::CountryCode::LATIN_AMERICA
42
+ } }
43
+ let(:character_set) { described_class.new valid_attributes }
44
+ let(:tokens) { character_set.to_zpl.split(",") }
45
+
46
+ it "raises an error if the number of data bits was not informed" do
47
+ character_set = described_class.new valid_attributes.merge :number_of_data_bits => nil
48
+ expect {
49
+ character_set.to_zpl
50
+ }.to raise_error(Zebra::Zpl::CharacterSet::MissingAttributeError, "Can't set character set if the number of data bits is not given")
51
+ end
52
+
53
+ it "raises an error if the language was not informed" do
54
+ character_set = described_class.new valid_attributes.merge :language => nil
55
+ expect {
56
+ character_set.to_zpl
57
+ }.to raise_error(Zebra::Zpl::CharacterSet::MissingAttributeError, "Can't set character set if the language is not given")
58
+ end
59
+
60
+ it "raises an error if the country code was not informed and the number of bits is 8" do
61
+ character_set = described_class.new valid_attributes.merge :country_code => nil
62
+ expect {
63
+ character_set.to_zpl
64
+ }.to raise_error(Zebra::Zpl::CharacterSet::MissingAttributeError, "Can't set character set if the country code is not given")
65
+ end
66
+
67
+ it "raises an error if the country code was informed and the number of bits is 7" do
68
+ character_set = described_class.new valid_attributes.merge :country_code => Zebra::Zpl::CountryCode::LATIN_AMERICA, :number_of_data_bits => 7
69
+ expect {
70
+ character_set.to_zpl
71
+ }.to raise_error(Zebra::Zpl::CharacterSet::CountryCodeNotApplicableForNumberOfDataBits)
72
+ end
73
+
74
+ it "does not raise an error if the country code was not informed and the number of data bits is 7" do
75
+ character_set = described_class.new :number_of_data_bits => 7, :language => Zebra::Zpl::Language::USA, :country_code => nil
76
+ end
77
+
78
+ it "raises an error if the language is not supported by the given number of data bits" do
79
+ expect {
80
+ described_class.new(:number_of_data_bits => 7, :language => Zebra::Zpl::Language::LATIN_1_WINDOWS).to_zpl
81
+ }.to raise_error(Zebra::Zpl::Language::InvalidLanguageForNumberOfDataBitsError)
82
+ end
83
+
84
+ it "begins with the command 'I'" do
85
+ character_set.to_zpl.should =~ /\AI/
86
+ end
87
+
88
+ it "contains the number of data bits" do
89
+ tokens[0].match(/I(\d)/)[1].should == "8"
90
+ end
91
+
92
+ it "contains the language" do
93
+ tokens[1].should == Zebra::Zpl::Language::PORTUGUESE
94
+ end
95
+
96
+ it "contains the country code" do
97
+ tokens[2].should == Zebra::Zpl::CountryCode::LATIN_AMERICA
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,138 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Zebra::Zpl::Label do
6
+ subject(:label) { described_class.new :print_speed => 2 }
7
+
8
+ describe "#new" do
9
+ it "sets the label width" do
10
+ label = described_class.new :width => 300
11
+ label.width.should == 300
12
+ end
13
+
14
+ it "sets the label length/gap" do
15
+ label = described_class.new :length_and_gap => [400, 24]
16
+ label.length.should == 400
17
+ label.gap.should == 24
18
+ end
19
+
20
+ it "sets the printing speed" do
21
+ label = described_class.new :print_speed => 2
22
+ label.print_speed.should == 2
23
+ end
24
+
25
+ it "sets the number of copies" do
26
+ label = described_class.new :copies => 4
27
+ label.copies.should == 4
28
+ end
29
+
30
+ it "the number of copies defaults to 1" do
31
+ label = described_class.new
32
+ label.copies.should == 1
33
+ end
34
+
35
+ it "validates the printing speed" do
36
+ [-1, 8, "a"].each do |s|
37
+ expect {
38
+ described_class.new :print_speed => s
39
+ }.to raise_error(Zebra::Zpl::Label::InvalidPrintSpeedError)
40
+ end
41
+ end
42
+
43
+ it "sets the print density" do
44
+ label = described_class.new :print_density => 10
45
+ label.print_density.should == 10
46
+ end
47
+
48
+ it "validates the print density" do
49
+ [-1, 16, "a"].each do |d|
50
+ expect {
51
+ described_class.new :print_density => d
52
+ }.to raise_error(Zebra::Zpl::Label::InvalidPrintDensityError)
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "#<<" do
58
+ it "adds an item to the list of label elements" do
59
+ expect {
60
+ label << stub
61
+ }.to change { label.elements.count }.by 1
62
+ end
63
+ end
64
+
65
+ describe "#dump_contents" do
66
+ let(:io) { "" }
67
+
68
+ it "dumps its contents to the received IO" do
69
+ label << stub(:to_zpl => "foobar")
70
+ label << stub(:to_zpl => "blabla")
71
+ label.width = 100
72
+ label.length_and_gap = [200, 24]
73
+ label.print_speed = 3
74
+ label.print_density = 10
75
+ label.dump_contents(io)
76
+ io.should == "O\nQ200,24\nq100\nS3\nD10\n\nN\nfoobar\nblabla\nP1\n"
77
+ end
78
+
79
+ it "does not try to set the label width when it's not informed (falls back to autosense)" do
80
+ label.dump_contents(io)
81
+ io.should_not =~ /q/
82
+ end
83
+
84
+ it "does not try to set the length/gap when they were not informed (falls back to autosense)" do
85
+ label.dump_contents(io)
86
+ io.should_not =~ /Q/
87
+ end
88
+
89
+ it "does not try to set the print density when it's not informed (falls back to the default value)" do
90
+ label.dump_contents(io)
91
+ io.should_not =~ /D/
92
+ end
93
+
94
+ it "raises an error if the print speed was not informed" do
95
+ label = described_class.new
96
+ expect {
97
+ label.dump_contents(io)
98
+ }.to raise_error(Zebra::Zpl::Label::PrintSpeedNotInformedError)
99
+ end
100
+ end
101
+
102
+ describe "#persist" do
103
+ let(:tempfile) { stub.as_null_object }
104
+ let(:label) { described_class.new :print_speed => 2 }
105
+
106
+ before do
107
+ Tempfile.stub :new => tempfile
108
+ label << stub(:to_zpl => "foobar")
109
+ end
110
+
111
+ it "creates a tempfile" do
112
+ Tempfile.should_receive(:new).with("zebra_label").and_return(tempfile)
113
+ label.persist
114
+ end
115
+
116
+ it "returns the tempfile" do
117
+ label.persist.should == tempfile
118
+ end
119
+
120
+ it "sets the `tempfile` attribute" do
121
+ label.persist
122
+ label.tempfile.should == tempfile
123
+ end
124
+ end
125
+
126
+ describe "#persisted?" do
127
+ it "returns false if the `tempfile` attribute is nil" do
128
+ label = described_class.new :print_speed => 2
129
+ label.should_not be_persisted
130
+ end
131
+
132
+ it "returns true if the `tempfile` attribute is not nil" do
133
+ label = described_class.new :print_speed => 2
134
+ label.instance_variable_set(:@tempfile, stub)
135
+ label.should be_persisted
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Zpl::Qrcode do
4
+ it "can be initialized with the scale factor" do
5
+ qrcode = described_class.new :scale_factor => 3
6
+ expect(qrcode.scale_factor).to eq 3
7
+ end
8
+
9
+ it "can be initialized with the error correction level" do
10
+ qrcode = described_class.new :correction_level => "M"
11
+ expect(qrcode.correction_level).to eq "M"
12
+ end
13
+
14
+ describe "#scale_factor" do
15
+ it "raises an error if the scale factor is not within the range 1-99" do
16
+ expect{described_class.new :scale_factor=>100}.to raise_error(Zebra::Zpl::Qrcode::InvalidScaleFactorError)
17
+ end
18
+ end
19
+
20
+ describe "#correction_level" do
21
+ it "raises an error if the error correction_level not in [LMQH]" do
22
+ expect{described_class.new :correction_level=>"A"}.to raise_error(Zebra::Zpl::Qrcode::InvalidCorrectionLevelError)
23
+ end
24
+ end
25
+
26
+ describe "#to_zpl" do
27
+ let(:valid_attributes) { {
28
+ :position => [50, 50],
29
+ :scale_factor => 3,
30
+ :correction_level => "M",
31
+ :data => "foobar"
32
+ }}
33
+ let(:qrcode) { described_class.new valid_attributes }
34
+ let(:tokens) { qrcode.to_zpl.split(",") }
35
+
36
+ it "raises an error if the X position is not given" do
37
+ qrcode = described_class.new :position => [nil, 50], :data => "foobar"
38
+ expect {
39
+ qrcode.to_zpl
40
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
41
+ end
42
+
43
+ it "raises an error if the Y position is not given" do
44
+ qrcode = described_class.new :position => [50, nil], :data => "foobar"
45
+ expect {
46
+ qrcode.to_zpl
47
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
48
+ end
49
+
50
+ it "raises an error if the data to be printed was not informed" do
51
+ qrcode.data = nil
52
+ expect {
53
+ qrcode.to_zpl
54
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the data to be printed is not given")
55
+ end
56
+
57
+ it "raises an error if the scale factor is not given" do
58
+ valid_attributes.delete :scale_factor
59
+
60
+ expect {
61
+ qrcode.to_zpl
62
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the scale factor to be used is not given")
63
+ end
64
+
65
+ it "raises an error if the correction level is not given" do
66
+ valid_attributes.delete :correction_level
67
+
68
+ expect {
69
+ qrcode.to_zpl
70
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the error correction level to be used is not given")
71
+ end
72
+
73
+ it "begins with the command 'b'" do
74
+ qrcode.to_zpl.should =~ /\Ab/
75
+ end
76
+
77
+ it "contains the X position" do
78
+ tokens[0].match(/b(\d+)/)[1].should eq "50"
79
+ end
80
+
81
+ it "contains the Y position" do
82
+ tokens[1].should eq "50"
83
+ end
84
+
85
+ it "contains QR code type" do
86
+ tokens[2].should eq "Q"
87
+ end
88
+
89
+ it "contains the scale factor" do
90
+ tokens[3].should eq "s3"
91
+ end
92
+
93
+ it "contains the error correction level" do
94
+ tokens[4].should eq "eM"
95
+ end
96
+
97
+ it "contains the data to be printed in the qrcode" do
98
+ tokens[5].should eq "\"foobar\""
99
+ end
100
+ end
101
+ end