barby 0.6.7 → 0.6.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
+
@@ -0,0 +1,144 @@
1
+ #encoding: ASCII
2
+ require 'test_helper'
3
+ require 'barby/barcode/code_93'
4
+
5
+ class Code93Test < Barby::TestCase
6
+
7
+ before do
8
+ @data = 'TEST93'
9
+ @code = Code93.new(@data)
10
+ end
11
+
12
+ it "should return the same data we put in" do
13
+ @code.data.must_equal @data
14
+ end
15
+
16
+ it "should have the expected characters" do
17
+ @code.characters.must_equal @data.split(//)
18
+ end
19
+
20
+ it "should have the expected start_encoding" do
21
+ @code.start_encoding.must_equal '101011110'
22
+ end
23
+
24
+ it "should have the expected stop_encoding" do
25
+ # STOP TERM
26
+ @code.stop_encoding.must_equal '1010111101'
27
+ end
28
+
29
+ it "should have the expected encoded characters" do
30
+ # T E S T 9 3
31
+ @code.encoded_characters.must_equal %w(110100110 110010010 110101100 110100110 100001010 101000010)
32
+ end
33
+
34
+ it "should have the expected data_encoding" do
35
+ # T E S T 9 3
36
+ @code.data_encoding.must_equal "110100110110010010110101100110100110100001010101000010"
37
+ end
38
+
39
+ it "should have the expected data_encoding_with_checksums" do
40
+ # T E S T 9 3 + (C) 6 (K)
41
+ @code.data_encoding_with_checksums.must_equal "110100110110010010110101100110100110100001010101000010101110110100100010"
42
+ end
43
+
44
+ it "should have the expected encoding" do
45
+ # START T E S T 9 3 + (C) 6 (K) STOP TERM
46
+ @code.encoding.must_equal "1010111101101001101100100101101011001101001101000010101010000101011101101001000101010111101"
47
+ end
48
+
49
+ it "should have the expected checksum_values" do
50
+ @code.checksum_values.must_equal [29, 14, 28, 29, 9, 3].reverse #!
51
+ end
52
+
53
+ it "should have the expected c_checksum" do
54
+ @code.c_checksum.must_equal 41 #calculate this first!
55
+ end
56
+
57
+ it "should have the expected c_checksum_character" do
58
+ @code.c_checksum_character.must_equal '+'
59
+ end
60
+
61
+ it "should have the expected c_checksum_encoding" do
62
+ @code.c_checksum_encoding.must_equal '101110110'
63
+ end
64
+
65
+ it "should have the expected checksum_values_with_c_checksum" do
66
+ @code.checksum_values_with_c_checksum.must_equal [29, 14, 28, 29, 9, 3, 41].reverse #!
67
+ end
68
+
69
+ it "should have the expected k_checksum" do
70
+ @code.k_checksum.must_equal 6 #calculate this first!
71
+ end
72
+
73
+ it "should have the expected k_checksum_character" do
74
+ @code.k_checksum_character.must_equal '6'
75
+ end
76
+
77
+ it "should have the expected k_checksum_encoding" do
78
+ @code.k_checksum_encoding.must_equal '100100010'
79
+ end
80
+
81
+ it "should have the expected checksums" do
82
+ @code.checksums.must_equal [41, 6]
83
+ end
84
+
85
+ it "should have the expected checksum_characters" do
86
+ @code.checksum_characters.must_equal ['+', '6']
87
+ end
88
+
89
+ it "should have the expected checksum_encodings" do
90
+ @code.checksum_encodings.must_equal %w(101110110 100100010)
91
+ end
92
+
93
+ it "should have the expected checksum_encoding" do
94
+ @code.checksum_encoding.must_equal '101110110100100010'
95
+ end
96
+
97
+ it "should be valid" do
98
+ assert @code.valid?
99
+ end
100
+
101
+ it "should not be valid when not in extended mode" do
102
+ @code.data = 'not extended'
103
+ end
104
+
105
+ it "should return data with no checksums on to_s" do
106
+ @code.to_s.must_equal 'TEST93'
107
+ end
108
+
109
+ describe "Extended mode" do
110
+
111
+ before do
112
+ @data = "Extended!"
113
+ @code = Code93.new(@data)
114
+ end
115
+
116
+ it "should be extended" do
117
+ assert @code.extended?
118
+ end
119
+
120
+ it "should convert extended characters to special shift characters" do
121
+ @code.characters.must_equal ["E", "\304", "X", "\304", "T", "\304", "E", "\304", "N", "\304", "D", "\304", "E", "\304", "D", "\303", "A"]
122
+ end
123
+
124
+ it "should have the expected data_encoding" do
125
+ @code.data_encoding.must_equal '110010010100110010101100110100110010110100110100110010110010010'+
126
+ '100110010101000110100110010110010100100110010110010010100110010110010100111010110110101000'
127
+ end
128
+
129
+ it "should have the expected c_checksum" do
130
+ @code.c_checksum.must_equal 9
131
+ end
132
+
133
+ it "should have the expected k_checksum" do
134
+ @code.k_checksum.must_equal 46
135
+ end
136
+
137
+ it "should return the original data on to_s with no checksums" do
138
+ @code.to_s.must_equal 'Extended!'
139
+ end
140
+
141
+ end
142
+
143
+ end
144
+
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/data_matrix'
3
+
4
+ class DataMatrixTest < Barby::TestCase
5
+
6
+ before do
7
+ @data = "humbaba"
8
+ @code = Barby::DataMatrix.new(@data)
9
+ end
10
+
11
+ it "should have the expected encoding" do
12
+ @code.encoding.must_equal ["1010101010101010", "1011111000011111", "1110111000010100",
13
+ "1110100100000111", "1101111010101000", "1101111011110011",
14
+ "1111111100000100", "1100101111110001", "1001000010001010",
15
+ "1101010110111011", "1000000100011110", "1001010010000011",
16
+ "1101100111011110", "1110111010000101", "1110010110001010",
17
+ "1111111111111111"]
18
+ end
19
+
20
+ it "should return data on to_s" do
21
+ @code.to_s.must_equal @data
22
+ end
23
+
24
+ it "should be able to change its data" do
25
+ prev_encoding = @code.encoding
26
+ @code.data = "after eight"
27
+ @code.encoding.wont_equal prev_encoding
28
+ end
29
+
30
+ end
@@ -0,0 +1,169 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/ean_13'
3
+
4
+ class EAN13Test < Barby::TestCase
5
+
6
+ describe 'validations' do
7
+
8
+ before do
9
+ @valid = EAN13.new('123456789012')
10
+ end
11
+
12
+ it "should be valid with 12 digits" do
13
+ assert @valid.valid?
14
+ end
15
+
16
+ it "should not be valid with non-digit characters" do
17
+ @valid.data = "The shit apple doesn't fall far from the shit tree"
18
+ refute @valid.valid?
19
+ end
20
+
21
+ it "should not be valid with less than 12 digits" do
22
+ @valid.data = "12345678901"
23
+ refute @valid.valid?
24
+ end
25
+
26
+ it "should not be valid with more than 12 digits" do
27
+ @valid.data = "1234567890123"
28
+ refute @valid.valid?
29
+ end
30
+
31
+ it "should raise an exception when data is invalid" do
32
+ lambda{ EAN13.new('123') }.must_raise(ArgumentError)
33
+ end
34
+
35
+ end
36
+
37
+ describe 'data' do
38
+
39
+ before do
40
+ @data = '007567816412'
41
+ @code = EAN13.new(@data)
42
+ end
43
+
44
+ it "should have the same data as was passed to it" do
45
+ @code.data.must_equal @data
46
+ end
47
+
48
+ it "should have the expected characters" do
49
+ @code.characters.must_equal @data.split(//)
50
+ end
51
+
52
+ it "should have the expected numbers" do
53
+ @code.numbers.must_equal @data.split(//).map{|s| s.to_i }
54
+ end
55
+
56
+ it "should have the expected odd_and_even_numbers" do
57
+ @code.odd_and_even_numbers.must_equal [[2,4,1,7,5,0], [1,6,8,6,7,0]]
58
+ end
59
+
60
+ it "should have the expected left_numbers" do
61
+ #0=second number in number system code
62
+ @code.left_numbers.must_equal [0,7,5,6,7,8]
63
+ end
64
+
65
+ it "should have the expected right_numbers" do
66
+ @code.right_numbers.must_equal [1,6,4,1,2,5]#5=checksum
67
+ end
68
+
69
+ it "should have the expected numbers_with_checksum" do
70
+ @code.numbers_with_checksum.must_equal @data.split(//).map{|s| s.to_i } + [5]
71
+ end
72
+
73
+ it "should have the expected data_with_checksum" do
74
+ @code.data_with_checksum.must_equal @data+'5'
75
+ end
76
+
77
+ it "should return all digits and the checksum on to_s" do
78
+ @code.to_s.must_equal '0075678164125'
79
+ end
80
+
81
+ end
82
+
83
+ describe 'checksum' do
84
+
85
+ before do
86
+ @code = EAN13.new('007567816412')
87
+ end
88
+
89
+ it "should have the expected weighted_sum" do
90
+ @code.weighted_sum.must_equal 85
91
+ @code.data = '007567816413'
92
+ @code.weighted_sum.must_equal 88
93
+ end
94
+
95
+ it "should have the correct checksum" do
96
+ @code.checksum.must_equal 5
97
+ @code.data = '007567816413'
98
+ @code.checksum.must_equal 2
99
+ end
100
+
101
+ it "should have the correct checksum_encoding" do
102
+ @code.checksum_encoding.must_equal '1001110'
103
+ end
104
+
105
+ end
106
+
107
+ describe 'encoding' do
108
+
109
+ before do
110
+ @code = EAN13.new('750103131130')
111
+ end
112
+
113
+ it "should have the expected checksum" do
114
+ @code.checksum.must_equal 9
115
+ end
116
+
117
+ it "should have the expected checksum_encoding" do
118
+ @code.checksum_encoding.must_equal '1110100'
119
+ end
120
+
121
+ it "should have the expected left_parity_map" do
122
+ @code.left_parity_map.must_equal [:odd, :even, :odd, :even, :odd, :even]
123
+ end
124
+
125
+ it "should have the expected left_encodings" do
126
+ @code.left_encodings.must_equal %w(0110001 0100111 0011001 0100111 0111101 0110011)
127
+ end
128
+
129
+ it "should have the expected right_encodings" do
130
+ @code.right_encodings.must_equal %w(1000010 1100110 1100110 1000010 1110010 1110100)
131
+ end
132
+
133
+ it "should have the expected left_encoding" do
134
+ @code.left_encoding.must_equal '011000101001110011001010011101111010110011'
135
+ end
136
+
137
+ it "should have the expected right_encoding" do
138
+ @code.right_encoding.must_equal '100001011001101100110100001011100101110100'
139
+ end
140
+
141
+ it "should have the expected encoding" do
142
+ #Start Left Center Right Stop
143
+ @code.encoding.must_equal '101' + '011000101001110011001010011101111010110011' + '01010' + '100001011001101100110100001011100101110100' + '101'
144
+ end
145
+
146
+ end
147
+
148
+ describe 'static data' do
149
+
150
+ before :each do
151
+ @code = EAN13.new('123456789012')
152
+ end
153
+
154
+ it "should have the expected start_encoding" do
155
+ @code.start_encoding.must_equal '101'
156
+ end
157
+
158
+ it "should have the expected stop_encoding" do
159
+ @code.stop_encoding.must_equal '101'
160
+ end
161
+
162
+ it "should have the expected center_encoding" do
163
+ @code.center_encoding.must_equal '01010'
164
+ end
165
+
166
+ end
167
+
168
+ end
169
+