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,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
+
@@ -0,0 +1,100 @@
1
+ require 'test_helper'
2
+ require 'barby/barcode/ean_8'
3
+
4
+ class EAN8Test < Barby::TestCase
5
+
6
+ describe 'validations' do
7
+
8
+ before do
9
+ @valid = EAN8.new('1234567')
10
+ end
11
+
12
+ it "should be valid with 7 digits" do
13
+ assert @valid.valid?
14
+ end
15
+
16
+ it "should not be valid with less than 7 digits" do
17
+ @valid.data = '123456'
18
+ refute @valid.valid?
19
+ end
20
+
21
+ it "should not be valid with more than 7 digits" do
22
+ @valid.data = '12345678'
23
+ refute @valid.valid?
24
+ end
25
+
26
+ it "should not be valid with non-digits" do
27
+ @valid.data = 'abcdefg'
28
+ refute @valid.valid?
29
+ end
30
+
31
+ end
32
+
33
+ describe 'checksum' do
34
+
35
+ before :each do
36
+ @code = EAN8.new('5512345')
37
+ end
38
+
39
+ it "should have the expected weighted_sum" do
40
+ @code.weighted_sum.must_equal 53
41
+ end
42
+
43
+ it "should have the expected checksum" do
44
+ @code.checksum.must_equal 7
45
+ end
46
+
47
+ end
48
+
49
+ describe 'data' do
50
+
51
+ before :each do
52
+ @data = '5512345'
53
+ @code = EAN8.new(@data)
54
+ end
55
+
56
+ it "should have the expected data" do
57
+ @code.data.must_equal @data
58
+ end
59
+
60
+ it "should have the expected odd_and_even_numbers" do
61
+ @code.odd_and_even_numbers.must_equal [[5,3,1,5],[4,2,5]]
62
+ end
63
+
64
+ it "should have the expected left_numbers" do
65
+ #EAN-8 includes the first character in the left-hand encoding, unlike EAN-13
66
+ @code.left_numbers.must_equal [5,5,1,2]
67
+ end
68
+
69
+ it "should have the expected right_numbers" do
70
+ @code.right_numbers.must_equal [3,4,5,7]
71
+ end
72
+
73
+ it "should return the data with checksum on to_s" do
74
+ @code.to_s.must_equal '55123457'
75
+ end
76
+
77
+ end
78
+
79
+ describe 'encoding' do
80
+
81
+ before :each do
82
+ @code = EAN8.new('5512345')
83
+ end
84
+
85
+ it "should have the expected left_parity_map" do
86
+ @code.left_parity_map.must_equal [:odd, :odd, :odd, :odd]
87
+ end
88
+
89
+ it "should have the expected left_encoding" do
90
+ @code.left_encoding.must_equal '0110001011000100110010010011'
91
+ end
92
+
93
+ it "should have the expected right_encoding" do
94
+ @code.right_encoding.must_equal '1000010101110010011101000100'
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
@@ -0,0 +1,129 @@
1
+ require 'test_helper'
2
+
3
+ class CairoOutputterTest < Barby::TestCase
4
+
5
+ def ps_available?
6
+ Cairo.const_defined?(:PSSurface)
7
+ end
8
+
9
+ def eps_available?
10
+ ps_available? and Cairo::PSSurface.method_defined?(:eps=)
11
+ end
12
+
13
+ def pdf_available?
14
+ Cairo.const_defined?(:PDFSurface)
15
+ end
16
+
17
+ def svg_available?
18
+ Cairo.const_defined?(:SVGSurface)
19
+ end
20
+
21
+ before do
22
+ load_outputter('cairo')
23
+ @barcode = Barby::Barcode.new
24
+ def @barcode.encoding; '101100111000'; end
25
+ @outputter = Barby::CairoOutputter.new(@barcode)
26
+ @outputters = Barby::Barcode.outputters
27
+ @surface = Cairo::ImageSurface.new(100, 100)
28
+ @context = Cairo::Context.new(@surface)
29
+ end
30
+
31
+ it "should have defined the render_to_cairo_context method" do
32
+ @outputters.must_include(:render_to_cairo_context)
33
+ end
34
+
35
+ it "should have defined the to_png method" do
36
+ @outputters.must_include(:to_png)
37
+ end
38
+
39
+ it "should have defined the to_ps and to_eps method if available" do
40
+ if ps_available?
41
+ @outputters.must_include(:to_ps)
42
+ if eps_available?
43
+ @outputters.must_include(:to_eps)
44
+ else
45
+ @outputters.wont_include(:to_eps)
46
+ end
47
+ else
48
+ @outputters.wont_include(:to_ps)
49
+ @outputters.wont_include(:to_eps)
50
+ end
51
+ end
52
+
53
+ it "should have defined the to_pdf method if available" do
54
+ if pdf_available?
55
+ @outputters.must_include(:to_pdf)
56
+ else
57
+ @outputters.wont_include(:to_pdf)
58
+ end
59
+ end
60
+
61
+ it "should have defined the to_svg method if available" do
62
+ if svg_available?
63
+ @outputters.must_include(:to_svg)
64
+ else
65
+ @outputters.wont_include(:to_svg)
66
+ end
67
+ end
68
+
69
+ it "should return the cairo context object it was given in render_to_cairo_context" do
70
+ @barcode.render_to_cairo_context(@context).object_id.must_equal @context.object_id
71
+ end
72
+
73
+ it "should return PNG image by the to_png method" do
74
+ png = @barcode.to_png
75
+ data = ruby_19_or_greater? ? png.force_encoding('BINARY') : png
76
+ data.must_match(/\A\x89PNG/n)
77
+ end
78
+
79
+ it "should return PS document by the to_ps method" do
80
+ if ps_available?
81
+ @barcode.to_ps.must_match(/\A%!PS-Adobe-[\d.]/)
82
+ end
83
+ end
84
+
85
+ it "should return EPS document by the to_eps method" do
86
+ if eps_available?
87
+ @barcode.to_eps.must_match(/\A%!PS-Adobe-[\d.]+ EPSF-[\d.]+/)
88
+ end
89
+ end
90
+
91
+ it "should return PDF document by the to_pdf method" do
92
+ if pdf_available?
93
+ pdf = @barcode.to_pdf
94
+ data = ruby_19_or_greater? ? pdf.force_encoding('BINARY') : pdf
95
+ data.must_match(/\A%PDF-[\d.]+/n)
96
+ end
97
+ end
98
+
99
+ it "should return SVG document by the to_svg method" do
100
+ if svg_available?
101
+ @barcode.to_svg.must_match(/<\/svg>\s*\Z/m)
102
+ end
103
+ end
104
+
105
+ it "should have x, y, width, height, full_width, full_height, xdim and margin attributes" do
106
+ @outputter.must_respond_to(:x)
107
+ @outputter.must_respond_to(:y)
108
+ @outputter.must_respond_to(:width)
109
+ @outputter.must_respond_to(:height)
110
+ @outputter.must_respond_to(:full_width)
111
+ @outputter.must_respond_to(:full_height)
112
+ @outputter.must_respond_to(:xdim)
113
+ @outputter.must_respond_to(:margin)
114
+ end
115
+
116
+ it "should not change attributes when given an options hash to render" do
117
+ %w(x y height xdim).each do |m|
118
+ @outputter.send("#{m}=", 10)
119
+ @outputter.send(m).must_equal 10
120
+ end
121
+ @outputter.render_to_cairo_context(@context,
122
+ :x => 20,
123
+ :y => 20,
124
+ :height => 20,
125
+ :xdim => 20)
126
+ %w(x y height xdim).each{|m| @outputter.send(m).must_equal 10 }
127
+ end
128
+
129
+ end