barby 0.6.7 → 0.6.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/code_25_iata'
3
+
4
+ class Code25IATATest < Barby::TestCase
5
+
6
+ before do
7
+ @data = '0123456789'
8
+ @code = Code25IATA.new(@data)
9
+ end
10
+
11
+ it "should have the expected start_encoding" do
12
+ @code.start_encoding.must_equal '1010'
13
+ end
14
+
15
+ it "should have the expected stop_encoding" do
16
+ @code.stop_encoding.must_equal '11101'
17
+ end
18
+
19
+ end
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/code_25_interleaved'
3
+
4
+ class Code25InterleavedTest < Barby::TestCase
5
+
6
+ before do
7
+ @data = '12345670'
8
+ @code = Code25Interleaved.new(@data)
9
+ end
10
+
11
+ it "should have the expected digit_pairs" do
12
+ @code.digit_pairs.must_equal [[1,2],[3,4],[5,6],[7,0]]
13
+ end
14
+
15
+ it "should have the expected digit_encodings" do
16
+ @code.digit_encodings.must_equal %w(111010001010111000 111011101000101000 111010001110001010 101010001110001110)
17
+ end
18
+
19
+ it "should have the expected start_encoding" do
20
+ @code.start_encoding.must_equal '1010'
21
+ end
22
+
23
+ it "should have the expected stop_encoding" do
24
+ @code.stop_encoding.must_equal '11101'
25
+ end
26
+
27
+ it "should have the expected data_encoding" do
28
+ @code.data_encoding.must_equal "111010001010111000111011101000101000111010001110001010101010001110001110"
29
+ end
30
+
31
+ it "should have the expected encoding" do
32
+ @code.encoding.must_equal "101011101000101011100011101110100010100011101000111000101010101000111000111011101"
33
+ end
34
+
35
+ it "should be valid" do
36
+ assert @code.valid?
37
+ end
38
+
39
+ it "should return the expected encoding for parameters passed to encoding_for_interleaved" do
40
+ w, n = Code25Interleaved::WIDE, Code25Interleaved::NARROW
41
+ # 1 2 1 2 1 2 1 2 1 2 digits 1 and 2
42
+ # B S B S B S B S B S bars and spaces
43
+ @code.encoding_for_interleaved(w,n,n,w,n,n,n,n,w,w).must_equal '111010001010111000'
44
+ # 3 4 3 4 3 4 3 4 3 4 digits 3 and 4
45
+ # B S B S B S B S B S bars and spaces
46
+ @code.encoding_for_interleaved(w,n,w,n,n,w,n,n,n,w).must_equal '111011101000101000'
47
+ end
48
+
49
+ it "should return all characters in sequence for to_s" do
50
+ @code.to_s.must_equal @code.characters.join
51
+ end
52
+
53
+ describe "with checksum" do
54
+
55
+ before do
56
+ @data = '1234567'
57
+ @code = Code25Interleaved.new(@data)
58
+ @code.include_checksum = true
59
+ end
60
+
61
+ it "should have the expected digit_pairs_with_checksum" do
62
+ @code.digit_pairs_with_checksum.must_equal [[1,2],[3,4],[5,6],[7,0]]
63
+ end
64
+
65
+ it "should have the expected digit_encodings_with_checksum" do
66
+ @code.digit_encodings_with_checksum.must_equal %w(111010001010111000 111011101000101000 111010001110001010 101010001110001110)
67
+ end
68
+
69
+ it "should have the expected data_encoding_with_checksum" do
70
+ @code.data_encoding_with_checksum.must_equal "111010001010111000111011101000101000111010001110001010101010001110001110"
71
+ end
72
+
73
+ it "should have the expected encoding" do
74
+ @code.encoding.must_equal "101011101000101011100011101110100010100011101000111000101010101000111000111011101"
75
+ end
76
+
77
+ it "should be valid" do
78
+ assert @code.valid?
79
+ end
80
+
81
+ it "should return all characters including checksum in sequence on to_s" do
82
+ @code.to_s.must_equal @code.characters_with_checksum.join
83
+ end
84
+
85
+ end
86
+
87
+ describe "with invalid number of digits" do
88
+
89
+ before do
90
+ @data = '1234567'
91
+ @code = Code25Interleaved.new(@data)
92
+ end
93
+
94
+ it "should not be valid" do
95
+ refute @code.valid?
96
+ end
97
+
98
+ it "should raise ArgumentError on all encoding methods" do
99
+ lambda{ @code.encoding }.must_raise(ArgumentError)
100
+ lambda{ @code.data_encoding }.must_raise(ArgumentError)
101
+ lambda{ @code.digit_encodings }.must_raise(ArgumentError)
102
+ end
103
+
104
+ it "should not raise ArgumentError on encoding methods that include checksum" do
105
+ b = Code25Interleaved.new(@data)
106
+ b.include_checksum = true
107
+ b.encoding
108
+ @code.data_encoding_with_checksum
109
+ @code.digit_encodings_with_checksum
110
+ end
111
+
112
+ end
113
+
114
+ end
115
+
116
+
@@ -0,0 +1,110 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/code_25'
3
+
4
+ class Code25Test < Barby::TestCase
5
+
6
+ before do
7
+ @data = "1234567"
8
+ @code = Code25.new(@data)
9
+ end
10
+
11
+ it "should return the same data it was given" do
12
+ @code.data.must_equal @data
13
+ end
14
+
15
+ it "should have the expected characters" do
16
+ @code.characters.must_equal %w(1 2 3 4 5 6 7)
17
+ end
18
+
19
+ it "should have the expected characters_with_checksum" do
20
+ @code.characters_with_checksum.must_equal %w(1 2 3 4 5 6 7 0)
21
+ end
22
+
23
+ it "should have the expected digits" do
24
+ @code.digits.must_equal [1,2,3,4,5,6,7]
25
+ end
26
+
27
+ it "should have the expected digits_with_checksum" do
28
+ @code.digits_with_checksum.must_equal [1,2,3,4,5,6,7,0]
29
+ end
30
+
31
+ it "should have the expected even_and_odd_digits" do
32
+ @code.even_and_odd_digits.must_equal [[7,5,3,1], [6,4,2]]
33
+ end
34
+
35
+ it "should have the expected start_encoding" do
36
+ @code.start_encoding.must_equal '1110111010'
37
+ end
38
+
39
+ it "should have the expected stop_encoding" do
40
+ @code.stop_encoding.must_equal '111010111'
41
+ end
42
+
43
+ it "should have a default narrow_width of 1" do
44
+ @code.narrow_width.must_equal 1
45
+ end
46
+
47
+ it "should have a default wide_width equal to narrow_width * 3" do
48
+ @code.wide_width.must_equal @code.narrow_width * 3
49
+ @code.narrow_width = 2
50
+ @code.wide_width.must_equal 6
51
+ end
52
+
53
+ it "should have a default space_width equal to narrow_width" do
54
+ @code.space_width.must_equal @code.narrow_width
55
+ @code.narrow_width = 23
56
+ @code.space_width.must_equal 23
57
+ end
58
+
59
+ it "should have the expected digit_encodings" do
60
+ @code.digit_encodings.must_equal %w(11101010101110 10111010101110 11101110101010 10101110101110 11101011101010 10111011101010 10101011101110)
61
+ end
62
+
63
+ it "should have the expected digit_encodings_with_checksum" do
64
+ @code.digit_encodings_with_checksum.must_equal %w(11101010101110 10111010101110 11101110101010 10101110101110 11101011101010 10111011101010 10101011101110 10101110111010)
65
+ end
66
+
67
+ it "should have the expected data_encoding" do
68
+ @code.data_encoding.must_equal "11101010101110101110101011101110111010101010101110101110111010111010101011101110101010101011101110"
69
+ end
70
+
71
+ it "should have the expected checksum" do
72
+ @code.checksum.must_equal 0
73
+ end
74
+
75
+ it "should have the expected checksum_encoding" do
76
+ @code.checksum_encoding.must_equal '10101110111010'
77
+ end
78
+
79
+ it "should have the expected encoding" do
80
+ @code.encoding.must_equal "111011101011101010101110101110101011101110111010101010101110101110111010111010101011101110101010101011101110111010111"
81
+ end
82
+
83
+ it "should be valid" do
84
+ assert @code.valid?
85
+ end
86
+
87
+ it "should not be valid" do
88
+ @code.data = 'abc'
89
+ refute @code.valid?
90
+ end
91
+
92
+ it "should raise on encoding methods that include data encoding if not valid" do
93
+ @code.data = 'abc'
94
+ lambda{ @code.encoding }.must_raise ArgumentError
95
+ lambda{ @code.data_encoding }.must_raise ArgumentError
96
+ lambda{ @code.data_encoding_with_checksum }.must_raise ArgumentError
97
+ lambda{ @code.digit_encodings }.must_raise ArgumentError
98
+ lambda{ @code.digit_encodings_with_checksum }.must_raise ArgumentError
99
+ end
100
+
101
+ it "should return all characters in sequence on to_s" do
102
+ @code.to_s.must_equal @code.characters.join
103
+ end
104
+
105
+ it "should include checksum in to_s when include_checksum is true" do
106
+ @code.include_checksum = true
107
+ @code.to_s.must_equal @code.characters_with_checksum.join
108
+ end
109
+
110
+ end
@@ -0,0 +1,210 @@
1
+ #encoding: ASCII
2
+ require 'test_helper'
3
+ require 'barby/barcode/code_39'
4
+
5
+ class Code39Test < Barby::TestCase
6
+
7
+ before do
8
+ @data = 'TEST8052'
9
+ @code = Code39.new(@data)
10
+ @code.spacing = 3
11
+ end
12
+
13
+ it "should yield self on initialize" do
14
+ c1 = nil
15
+ c2 = Code39.new('TEST'){|c| c1 = c }
16
+ c1.must_equal c2
17
+ end
18
+
19
+ it "should have the expected data" do
20
+ @code.data.must_equal @data
21
+ end
22
+
23
+ it "should have the expected characters" do
24
+ @code.characters.must_equal @data.split(//)
25
+ end
26
+
27
+ it "should have the expected start_encoding" do
28
+ @code.start_encoding.must_equal '100101101101'
29
+ end
30
+
31
+ it "should have the expected stop_encoding" do
32
+ @code.stop_encoding.must_equal '100101101101'
33
+ end
34
+
35
+ it "should have the expected spacing_encoding" do
36
+ @code.spacing_encoding.must_equal '000'
37
+ end
38
+
39
+ it "should have the expected encoded characters" do
40
+ @code.encoded_characters.must_equal %w(101011011001 110101100101 101101011001 101011011001 110100101101 101001101101 110100110101 101100101011)
41
+ end
42
+
43
+ it "should have the expected data_encoding" do
44
+ @code.data_encoding.must_equal '101011011001000110101100101000101101011001000101011011001000110100101101000101001101101000110100110101000101100101011'
45
+ end
46
+
47
+ it "should have the expected encoding" do
48
+ @code.encoding.must_equal '100101101101000101011011001000110101100101000101101011001000101011011001000110100101101000101001101101000110100110101000101100101011000100101101101'
49
+ end
50
+
51
+ it "should be valid" do
52
+ assert @code.valid?
53
+ end
54
+
55
+ it "should not be valid" do
56
+ @code.data = "123\200456"
57
+ refute @code.valid?
58
+ end
59
+
60
+ it "should raise an exception when data is not valid on initialization" do
61
+ lambda{ Code39.new('abc') }.must_raise(ArgumentError)
62
+ end
63
+
64
+ it "should return all characters in sequence without checksum on to_s" do
65
+ @code.to_s.must_equal @data
66
+ end
67
+
68
+ describe "Checksumming" do
69
+
70
+ before do
71
+ @code = Code39.new('CODE39')
72
+ end
73
+
74
+ it "should have the expected checksum" do
75
+ @code.checksum.must_equal 32
76
+ end
77
+
78
+ it "should have the expected checksum_character" do
79
+ @code.checksum_character.must_equal 'W'
80
+ end
81
+
82
+ it "should have the expected checksum_encoding" do
83
+ @code.checksum_encoding.must_equal '110011010101'
84
+ end
85
+
86
+ it "should have the expected characters_with_checksum" do
87
+ @code.characters_with_checksum.must_equal %w(C O D E 3 9 W)
88
+ end
89
+
90
+ it "should have the expected encoded_characters_with_checksum" do
91
+ @code.encoded_characters_with_checksum.must_equal %w(110110100101 110101101001 101011001011 110101100101 110110010101 101100101101 110011010101)
92
+ end
93
+
94
+ it "should have the expected data_encoding_with_checksum" do
95
+ @code.data_encoding_with_checksum.must_equal "110110100101011010110100101010110010110110101100101011011001010101011001011010110011010101"
96
+ end
97
+
98
+ it "should have the expected encoding_with_checksum" do
99
+ @code.encoding_with_checksum.must_equal "10010110110101101101001010110101101001010101100101101101011001010110110010101010110010110101100110101010100101101101"
100
+ end
101
+
102
+ it "should return the encoding with checksum when include_checksum == true" do
103
+ @code.include_checksum = true
104
+ @code.encoding.must_equal "10010110110101101101001010110101101001010101100101101101011001010110110010101010110010110101100110101010100101101101"
105
+ end
106
+
107
+ end
108
+
109
+ describe "Normal encoding" do
110
+
111
+ before do
112
+ @data = 'ABC$%'
113
+ @code = Code39.new(@data)
114
+ end
115
+
116
+ it "should have the expected characters" do
117
+ @code.characters.must_equal %w(A B C $ %)
118
+ end
119
+
120
+ it "should have the expected encoded_characters" do
121
+ @code.encoded_characters.must_equal %w(110101001011 101101001011 110110100101 100100100101 101001001001)
122
+ end
123
+
124
+ it "should have the expected data_encoding" do
125
+ @code.data_encoding.must_equal '1101010010110101101001011011011010010101001001001010101001001001'
126
+ end
127
+
128
+ it "should not be valid" do
129
+ @code.data = 'abc'
130
+ refute @code.valid?
131
+ end
132
+
133
+ end
134
+
135
+ describe "Extended encoding" do
136
+
137
+ before do
138
+ @data = '<abc>'
139
+ @code = Code39.new(@data, true)
140
+ end
141
+
142
+ it "should return true on extended?" do
143
+ assert @code.extended?
144
+ end
145
+
146
+ it "should have the expected characters" do
147
+ @code.characters.must_equal %w(% G + A + B + C % I)
148
+ end
149
+
150
+ it "should have the expected encoded_characters" do
151
+ @code.encoded_characters.must_equal %w(101001001001 101010011011 100101001001 110101001011 100101001001 101101001011 100101001001 110110100101 101001001001 101101001101)
152
+ end
153
+
154
+ it "should have the expected data_encoding" do
155
+ @code.data_encoding.must_equal '101001001001010101001101101001010010010110101001011010010100100101011010010110100101001001011011010010101010010010010101101001101'
156
+ end
157
+
158
+ it "should have the expected encoding" do
159
+ @code.encoding.must_equal '10010110110101010010010010101010011011010010100100101101010010110100101001001'+
160
+ '010110100101101001010010010110110100101010100100100101011010011010100101101101'
161
+ end
162
+
163
+ it "should take a second parameter on initialize indicating it is extended" do
164
+ assert Code39.new('abc', true).extended?
165
+ refute Code39.new('ABC', false).extended?
166
+ refute Code39.new('ABC').extended?
167
+ end
168
+
169
+ it "should be valid" do
170
+ assert @code.valid?
171
+ end
172
+
173
+ it "should not be valid" do
174
+ @code.data = "abc\200123"
175
+ refute @code.valid?
176
+ end
177
+
178
+ it "should return all characters in sequence without checksum on to_s" do
179
+ @code.to_s.must_equal @data
180
+ end
181
+
182
+ end
183
+
184
+ describe "Variable widths" do
185
+
186
+ before do
187
+ @data = 'ABC$%'
188
+ @code = Code39.new(@data)
189
+ @code.narrow_width = 2
190
+ @code.wide_width = 5
191
+ end
192
+
193
+ it "should have the expected encoded_characters" do
194
+ @code.encoded_characters.must_equal %w(111110011001100000110011111 110011111001100000110011111 111110011111001100000110011 110000011000001100000110011 110011000001100000110000011)
195
+ end
196
+
197
+ it "should have the expected data_encoding" do
198
+ # A SB SC S$ S%
199
+ @code.data_encoding.must_equal '1111100110011000001100111110110011111001100000110011111011111001111100110000011001101100000110000011000001100110110011000001100000110000011'
200
+
201
+ @code.spacing = 3
202
+ # A S B S C S $ S %
203
+ @code.data_encoding.must_equal '111110011001100000110011111000110011111001100000110011111000111110011111001100000110011000110000011000001100000110011000110011000001100000110000011'
204
+ end
205
+
206
+ end
207
+
208
+ end
209
+
210
+