zebra-zpl 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,146 +1,146 @@
1
- require 'spec_helper'
2
-
3
- describe Zebra::Zpl::Image do
4
-
5
- it 'can be initialized with a path' do
6
- img = described_class.new path: 'spec/fixtures/default.jpg'
7
- expect(img.path).to match /^(.+)\/([^\/]+)$/
8
- end
9
-
10
- it 'can be initialized with dimensions (width & height)' do
11
- img = described_class.new width: 800, height: 600
12
- expect(img.width).to eq 800
13
- expect(img.height).to eq 600
14
- end
15
-
16
- it 'can be initialized with a rotation amount' do
17
- img = described_class.new rotation: 90
18
- expect(img.rotation).to eq 90
19
- end
20
-
21
- it 'can be initialized with a black threshold' do
22
- img = described_class.new black_threshold: 0.25
23
- expect(img.black_threshold).to eq 0.25
24
- end
25
-
26
- it 'can be initialized with the invert flag' do
27
- img = described_class.new invert: true
28
- expect(img.invert).to eq true
29
- end
30
-
31
- it 'can be initialized with the compress flag' do
32
- img = described_class.new compress: true
33
- expect(img.compress).to eq true
34
- end
35
-
36
- describe '#width' do
37
- it 'raises an error if an invalid width is given' do
38
- expect { described_class.new width: -10 }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
39
- expect { described_class.new width: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
40
- end
41
- end
42
-
43
- describe '#height' do
44
- it 'raises an error if an invalid height is given' do
45
- expect { described_class.new height: -10 }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
46
- expect { described_class.new height: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
47
- end
48
- end
49
-
50
- describe '#rotation' do
51
- it 'raises an error if an invalid rotation value is given' do
52
- expect { described_class.new rotation: '90d' }.to raise_error(Zebra::Zpl::Image::InvalidRotationError)
53
- expect { described_class.new rotation: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidRotationError)
54
- end
55
- end
56
-
57
- describe '#black_threshold' do
58
- it 'raises an error if an invalid black threshold is given' do
59
- expect { described_class.new black_threshold: -5 }.to raise_error(Zebra::Zpl::Image::InvalidThresholdError)
60
- expect { described_class.new black_threshold: 1.1 }.to raise_error(Zebra::Zpl::Image::InvalidThresholdError)
61
- end
62
- end
63
-
64
- context "instance methods" do
65
- let(:valid_attributes) { {
66
- path: 'spec/fixtures/default.jpg',
67
- position: [50, 50],
68
- width: 100,
69
- height: 150
70
- }}
71
- let(:image) { described_class.new valid_attributes }
72
-
73
- describe '#source' do
74
- it 'returns an Img2Zpl::Image object' do
75
- expect(image.source.class).to eq Img2Zpl::Image
76
- end
77
-
78
- it 'responds to ImageMagick (MiniMagick::Image) commands' do
79
- attr = valid_attributes
80
- attr.delete(:width)
81
- attr.delete(:height)
82
- img = described_class.new attr
83
- src = img.src
84
- expect(src.respond_to?(:resize)).to be true
85
- expect(src.respond_to?(:trim)).to be true
86
- expect(src.respond_to?(:crop)).to be true
87
- expect(src.respond_to?(:flatten)).to be true
88
- expect(src.respond_to?(:rotate)).to be true
89
- end
90
-
91
- it 'properly manipulates the image with ImageMagick (MiniMagick::Image) commands' do
92
- attr = valid_attributes
93
- attr.delete(:width)
94
- attr.delete(:height)
95
- img = described_class.new attr
96
-
97
- img.source.resize '123x'
98
- expect(img.width).to eq 123
99
- img.source.resize 'x321'
100
- expect(img.height).to eq 321
101
- end
102
- end
103
-
104
- describe '#to_zpl' do
105
- let(:tokens) { image.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
106
-
107
- it 'raises an error if the X position is not given' do
108
- qrcode = described_class.new position: [nil, 50]
109
- expect {
110
- qrcode.to_zpl
111
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
112
- end
113
-
114
- it 'raises an error if the Y position is not given' do
115
- qrcode = described_class.new position: [50, nil]
116
- expect {
117
- qrcode.to_zpl
118
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
119
- end
120
-
121
- it 'raises an error if the path is not given' do
122
- valid_attributes.delete :path
123
-
124
- expect {
125
- image.to_zpl
126
- }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the path is invalid or not given")
127
- end
128
-
129
- it "contains the Graphics Field command '^GF'" do
130
- expect(image.to_zpl).to match /\^GF/
131
- end
132
-
133
- it 'contains the X position' do
134
- expect(tokens[1]).to eq '50'
135
- end
136
-
137
- it 'contains the Y position' do
138
- expect(tokens[2]).to eq '50'
139
- end
140
-
141
- it 'contains the properly encoded image' do
142
- expect(tokens[3..-1].join).to eq '^GFA1079107913:::::K078Q03J07FF8P038I01JFP078I03JF8O0FCI0KFCO0FC001KFEN01FE003LFN01FE003LF8M03FF007LFCM03FF8007LFCM07FF800MFCM0IFC00MFEM0IFC00MFEL01IFE01MFEL01IFE01MFEL03JF01NFL07JF8:01NFL0KFC:01MFEK01KFE00MFEK01LF00MFEK03LF00MFEK07LF8007LFCK07LF8007LFCK0MFC003LF8K0MFC003LFK01MFE001KFEK01NFI0KFCK03NFI07JF8K07NF8I01JFL04N08J07FFCK0FC::::::N01MFE:::::::::::::::::::::::::::::::::::^FS'
143
- end
144
- end
145
- end
146
- end
1
+ require 'spec_helper'
2
+
3
+ describe Zebra::Zpl::Image do
4
+
5
+ it 'can be initialized with a path' do
6
+ img = described_class.new path: 'spec/fixtures/default.jpg'
7
+ expect(img.path).to match /^(.+)\/([^\/]+)$/
8
+ end
9
+
10
+ it 'can be initialized with dimensions (width & height)' do
11
+ img = described_class.new width: 800, height: 600
12
+ expect(img.width).to eq 800
13
+ expect(img.height).to eq 600
14
+ end
15
+
16
+ it 'can be initialized with a rotation amount' do
17
+ img = described_class.new rotation: 90
18
+ expect(img.rotation).to eq 90
19
+ end
20
+
21
+ it 'can be initialized with a black threshold' do
22
+ img = described_class.new black_threshold: 0.25
23
+ expect(img.black_threshold).to eq 0.25
24
+ end
25
+
26
+ it 'can be initialized with the invert flag' do
27
+ img = described_class.new invert: true
28
+ expect(img.invert).to eq true
29
+ end
30
+
31
+ it 'can be initialized with the compress flag' do
32
+ img = described_class.new compress: true
33
+ expect(img.compress).to eq true
34
+ end
35
+
36
+ describe '#width' do
37
+ it 'raises an error if an invalid width is given' do
38
+ expect { described_class.new width: -10 }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
39
+ expect { described_class.new width: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
40
+ end
41
+ end
42
+
43
+ describe '#height' do
44
+ it 'raises an error if an invalid height is given' do
45
+ expect { described_class.new height: -10 }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
46
+ expect { described_class.new height: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidSizeError)
47
+ end
48
+ end
49
+
50
+ describe '#rotation' do
51
+ it 'raises an error if an invalid rotation value is given' do
52
+ expect { described_class.new rotation: '90d' }.to raise_error(Zebra::Zpl::Image::InvalidRotationError)
53
+ expect { described_class.new rotation: 'abc' }.to raise_error(Zebra::Zpl::Image::InvalidRotationError)
54
+ end
55
+ end
56
+
57
+ describe '#black_threshold' do
58
+ it 'raises an error if an invalid black threshold is given' do
59
+ expect { described_class.new black_threshold: -5 }.to raise_error(Zebra::Zpl::Image::InvalidThresholdError)
60
+ expect { described_class.new black_threshold: 1.1 }.to raise_error(Zebra::Zpl::Image::InvalidThresholdError)
61
+ end
62
+ end
63
+
64
+ context "instance methods" do
65
+ let(:valid_attributes) { {
66
+ path: 'spec/fixtures/default.jpg',
67
+ position: [50, 50],
68
+ width: 100,
69
+ height: 150
70
+ }}
71
+ let(:image) { described_class.new valid_attributes }
72
+
73
+ describe '#source' do
74
+ it 'returns an Img2Zpl::Image object' do
75
+ expect(image.source.class).to eq Img2Zpl::Image
76
+ end
77
+
78
+ it 'responds to ImageMagick (MiniMagick::Image) commands' do
79
+ attr = valid_attributes
80
+ attr.delete(:width)
81
+ attr.delete(:height)
82
+ img = described_class.new attr
83
+ src = img.src
84
+ expect(src.respond_to?(:resize)).to be true
85
+ expect(src.respond_to?(:trim)).to be true
86
+ expect(src.respond_to?(:crop)).to be true
87
+ expect(src.respond_to?(:flatten)).to be true
88
+ expect(src.respond_to?(:rotate)).to be true
89
+ end
90
+
91
+ it 'properly manipulates the image with ImageMagick (MiniMagick::Image) commands' do
92
+ attr = valid_attributes
93
+ attr.delete(:width)
94
+ attr.delete(:height)
95
+ img = described_class.new attr
96
+
97
+ img.source.resize '123x'
98
+ expect(img.width).to eq 123
99
+ img.source.resize 'x321'
100
+ expect(img.height).to eq 321
101
+ end
102
+ end
103
+
104
+ describe '#to_zpl' do
105
+ let(:tokens) { image.to_zpl.split(/(\^[A-Z]+|\,)/).reject{ |e| ['', ',', nil].include?(e) } }
106
+
107
+ it 'raises an error if the X position is not given' do
108
+ qrcode = described_class.new position: [nil, 50]
109
+ expect {
110
+ qrcode.to_zpl
111
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the X value is not given")
112
+ end
113
+
114
+ it 'raises an error if the Y position is not given' do
115
+ qrcode = described_class.new position: [50, nil]
116
+ expect {
117
+ qrcode.to_zpl
118
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the Y value is not given")
119
+ end
120
+
121
+ it 'raises an error if the path is not given' do
122
+ valid_attributes.delete :path
123
+
124
+ expect {
125
+ image.to_zpl
126
+ }.to raise_error(Zebra::Zpl::Printable::MissingAttributeError, "Can't print if the path is invalid or not given")
127
+ end
128
+
129
+ it "contains the Graphics Field command '^GF'" do
130
+ expect(image.to_zpl).to match /\^GF/
131
+ end
132
+
133
+ it 'contains the X position' do
134
+ expect(tokens[1]).to eq '50'
135
+ end
136
+
137
+ it 'contains the Y position' do
138
+ expect(tokens[2]).to eq '50'
139
+ end
140
+
141
+ it 'contains the properly encoded image' do
142
+ expect(tokens[3..-1].join).to eq '^GFA1079107913:::::K07CQ03J07FF8P038I01IFEP078I03JF8O0FCI0KFCO0FC001KFEN01FE001LFN01FE003LF8M03FF007LFCM03FF8007LFCM07FF800MFCM0IFC00MFEM0IFC00MFEL01IFE01MFEL01IFE01MFEL03JF01NFL07JF8:01NFL0KFC:01MFEK01KFE00MFEK01LF00MFEK03LF00MFEK07LF8007LFCK07LF8007LFCK0MFC003LF8K0MFC003LFK01MFE001KFEK01NFI0KFCK03NFI07JF8K07NF8I01JFL04J07FFCK0FC::::::N01MFE:::::::::::::::::::::::::::::::::::^FS'
143
+ end
144
+ end
145
+ end
146
+ end
@@ -1,126 +1,110 @@
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
- expect(label.width).to eq 300
12
- end
13
-
14
- it "sets the label length" do
15
- label = described_class.new length: 400
16
- expect(label.length).to eq 400
17
- end
18
-
19
- it "sets the printing speed" do
20
- label = described_class.new print_speed: 2
21
- expect(label.print_speed).to eq 2
22
- end
23
-
24
- it "sets the number of copies" do
25
- label = described_class.new copies: 4
26
- expect(label.copies).to eq 4
27
- end
28
-
29
- it "the number of copies defaults to 1" do
30
- label = described_class.new
31
- expect(label.copies).to eq 1
32
- end
33
-
34
- it "validates the printing speed" do
35
- [-1, 15, "a"].each do |s|
36
- expect {
37
- described_class.new print_speed: s
38
- }.to raise_error(Zebra::Zpl::Label::InvalidPrintSpeedError)
39
- end
40
- end
41
- end
42
-
43
- describe "#<<" do
44
- it "adds an item to the list of label elements" do
45
- expect {
46
- obj = {}
47
- allow(obj).to receive(:to_zpl) { 'foobar' }
48
- label << obj
49
- }.to change { label.elements.count }.by 1
50
- end
51
- end
52
-
53
- describe "#dump_contents" do
54
- let(:io) { "" }
55
-
56
- it "dumps its contents to the received IO" do
57
- obj1, obj2 = {}, {}
58
- allow(obj1).to receive(:to_zpl) { 'foobar' }
59
- allow(obj2).to receive(:to_zpl) { 'blabla' }
60
- label << obj1
61
- label << obj2
62
- label.width = 100
63
- label.length = 200
64
- label.print_speed = 3
65
- label.dump_contents(io)
66
- expect(io).to eq '^XA^LL200^LH0,0^LS10^PW100^PR3foobarblabla^PQ1^XZ'
67
- end
68
-
69
- it "does not try to set the label width when it's not informed (falls back to autosense)" do
70
- label.dump_contents(io)
71
- expect(io).to_not match /^LL/
72
- end
73
-
74
- it "does not try to set the length when it is not informed (falls back to autosense)" do
75
- label.dump_contents(io)
76
- expect(io).to_not match /^PW/
77
- end
78
-
79
- it "raises an error if the print speed was not informed" do
80
- label = described_class.new
81
- expect {
82
- label.dump_contents(io)
83
- }.to raise_error(Zebra::Zpl::Label::PrintSpeedNotInformedError)
84
- end
85
- end
86
-
87
- describe "#persist" do
88
-
89
- let(:tempfile) { StringIO.new }
90
- let(:label) { described_class.new print_speed: 2 }
91
-
92
- before do
93
- allow(Tempfile).to receive(:new) { tempfile }
94
- obj = {}
95
- allow(obj).to receive(:to_zpl) { 'foobar' }
96
- label << obj
97
- end
98
-
99
- it "creates a tempfile" do
100
- expect(Tempfile).to receive(:new).with('zebra_label').and_return(tempfile)
101
- label.persist
102
- end
103
-
104
- it "returns the tempfile" do
105
- expect(label.persist).to eq tempfile
106
- end
107
-
108
- it "sets the `tempfile` attribute" do
109
- label.persist
110
- expect(label.tempfile).to eq tempfile
111
- end
112
- end
113
-
114
- describe "#persisted?" do
115
- it "returns false if the `tempfile` attribute is nil" do
116
- label = described_class.new print_speed: 2
117
- expect(label).to_not be_persisted
118
- end
119
-
120
- it "returns true if the `tempfile` attribute is not nil" do
121
- label = described_class.new print_speed: 2
122
- label.instance_variable_set(:@tempfile, Tempfile.new('zebra_label'))
123
- expect(label).to be_persisted
124
- end
125
- end
126
- end
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 default values" do
10
+ expect(described_class.new).to have_attributes({ label_shift: 10, copies: 1 })
11
+ end
12
+
13
+ it "sets attributes" do
14
+ attributes = { width: 300, length: 400, print_speed: 2, copies: 4, label_shift: 15 }
15
+ expect(described_class.new(attributes)).to have_attributes(attributes)
16
+ end
17
+
18
+ it "validates the printing speed" do
19
+ [-1, 15, "a"].each do |s|
20
+ expect {
21
+ described_class.new print_speed: s
22
+ }.to raise_error(Zebra::Zpl::Label::InvalidPrintSpeedError)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "#<<" do
28
+ it "adds an item to the list of label elements" do
29
+ expect {
30
+ obj = {}
31
+ allow(obj).to receive(:to_zpl) { 'foobar' }
32
+ label << obj
33
+ }.to change { label.elements.count }.by 1
34
+ end
35
+ end
36
+
37
+ describe "#dump_contents" do
38
+ let(:io) { "" }
39
+
40
+ it "dumps its contents to the received IO" do
41
+ obj1, obj2 = {}, {}
42
+ allow(obj1).to receive(:to_zpl) { 'foobar' }
43
+ allow(obj2).to receive(:to_zpl) { 'blabla' }
44
+ label << obj1
45
+ label << obj2
46
+ label.width = 100
47
+ label.length = 200
48
+ label.print_speed = 3
49
+ label.dump_contents(io)
50
+ expect(io).to eq '^XA^LL200^LH0,0^LS10^PW100^PR3foobarblabla^PQ1^XZ'
51
+ end
52
+
53
+ it "does not try to set the label width when it's not informed (falls back to autosense)" do
54
+ label.dump_contents(io)
55
+ expect(io).to_not match /^LL/
56
+ end
57
+
58
+ it "does not try to set the length when it is not informed (falls back to autosense)" do
59
+ label.dump_contents(io)
60
+ expect(io).to_not match /^PW/
61
+ end
62
+
63
+ it "raises an error if the print speed was not informed" do
64
+ label = described_class.new
65
+ expect {
66
+ label.dump_contents(io)
67
+ }.to raise_error(Zebra::Zpl::Label::PrintSpeedNotInformedError)
68
+ end
69
+ end
70
+
71
+ describe "#persist" do
72
+
73
+ let(:tempfile) { StringIO.new }
74
+ let(:label) { described_class.new print_speed: 2 }
75
+
76
+ before do
77
+ allow(Tempfile).to receive(:new) { tempfile }
78
+ obj = {}
79
+ allow(obj).to receive(:to_zpl) { 'foobar' }
80
+ label << obj
81
+ end
82
+
83
+ it "creates a tempfile" do
84
+ expect(Tempfile).to receive(:new).with('zebra_label').and_return(tempfile)
85
+ label.persist
86
+ end
87
+
88
+ it "returns the tempfile" do
89
+ expect(label.persist).to eq tempfile
90
+ end
91
+
92
+ it "sets the `tempfile` attribute" do
93
+ label.persist
94
+ expect(label.tempfile).to eq tempfile
95
+ end
96
+ end
97
+
98
+ describe "#persisted?" do
99
+ it "returns false if the `tempfile` attribute is nil" do
100
+ label = described_class.new print_speed: 2
101
+ expect(label).to_not be_persisted
102
+ end
103
+
104
+ it "returns true if the `tempfile` attribute is not nil" do
105
+ label = described_class.new print_speed: 2
106
+ label.instance_variable_set(:@tempfile, Tempfile.new('zebra_label'))
107
+ expect(label).to be_persisted
108
+ end
109
+ end
110
+ end