bindata 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,13 +3,13 @@
3
3
  require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
4
  require 'bindata/registry'
5
5
 
6
- describe "The Registry" do
6
+ describe BinData::Registry do
7
7
  before(:each) do
8
8
  @r = BinData::Registry.instance
9
9
  end
10
10
 
11
11
  it "should be a singleton" do
12
- BinData::Registry.instance.should equal(BinData::Registry.instance)
12
+ BinData::Registry.instance.should == BinData::Registry.instance
13
13
  end
14
14
 
15
15
  it "should lookup registered names" do
@@ -18,8 +18,8 @@ describe "The Registry" do
18
18
  @r.register('ASubClass', A)
19
19
  @r.register('AnotherSubClass', B)
20
20
 
21
- @r.lookup('a_sub_class').should eql(A)
22
- @r.lookup('another_sub_class').should eql(B)
21
+ @r.lookup('a_sub_class').should == A
22
+ @r.lookup('another_sub_class').should == B
23
23
  end
24
24
 
25
25
  it "should not lookup unregistered names" do
@@ -27,21 +27,21 @@ describe "The Registry" do
27
27
  end
28
28
 
29
29
  it "should convert CamelCase to underscores" do
30
- @r.register('CamelCase', A).should eql('camel_case')
30
+ @r.register('CamelCase', A).should == 'camel_case'
31
31
  end
32
32
 
33
33
  it "should convert adjacent caps camelCase to underscores" do
34
- @r.register('XYZCamelCase', A).should eql('xyz_camel_case')
34
+ @r.register('XYZCamelCase', A).should == 'xyz_camel_case'
35
35
  end
36
36
 
37
37
  it "should ignore the outer nestings of classes" do
38
- @r.register('A::B::C', A).should eql('c')
38
+ @r.register('A::B::C', A).should == 'c'
39
39
  end
40
40
 
41
41
  it "should allow overriding of registered classes" do
42
42
  @r.register('A', A)
43
43
  @r.register('A', B)
44
44
 
45
- @r.lookup('a').should eql(B)
45
+ @r.lookup('a').should == B
46
46
  end
47
47
  end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
+ require 'bindata/rest'
5
+
6
+ describe BinData::Rest do
7
+ it "should read till end of stream" do
8
+ data = "abcdefghij"
9
+ BinData::Rest.read(data).should == data
10
+ end
11
+
12
+ it "should default to the empty string" do
13
+ BinData::Rest.new.value.should == ""
14
+ end
15
+
16
+ it "should allow setting value for completeness" do
17
+ rest = BinData::Rest.new
18
+ rest.value = "123"
19
+ rest.value.should == "123"
20
+ rest.to_s.should == "123"
21
+ end
22
+
23
+ it "should accept BinData::Single parameters" do
24
+ rest = BinData::Rest.new(:check_value => "abc")
25
+ lambda {
26
+ rest.read("abc")
27
+ }.should_not raise_error(BinData::ValidityError)
28
+ end
29
+ end
30
+
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
+ require 'bindata/sanitize'
5
+
6
+ describe BinData::SanitizedParameters, "with nil parameters" do
7
+ before(:each) do
8
+ @mock = mock("dummy class")
9
+ @mock.stub!(:accepted_parameters).and_return([:a, :b, :c])
10
+ end
11
+
12
+ it "should return empty parameters" do
13
+ @mock.should_receive(:sanitize_parameters).with({}).and_return({})
14
+
15
+ sanitized = BinData::SanitizedParameters.new(@mock, nil)
16
+ sanitized.keys.should be_empty
17
+ end
18
+
19
+ it "should pass extra arguments" do
20
+ @mock.should_receive(:sanitize_parameters).with({}, 1, 2, 3).and_return({})
21
+
22
+ sanitized = BinData::SanitizedParameters.new(@mock, nil, 1, 2, 3)
23
+ sanitized.keys.should be_empty
24
+ end
25
+ end
26
+
27
+ describe BinData::SanitizedParameters, "with bad input" do
28
+ before(:each) do
29
+ @mock = mock("dummy class")
30
+ @mock.stub!(:accepted_parameters).and_return([:a, :b, :c])
31
+ @params = {:a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
32
+ end
33
+
34
+ it "should convert keys to symbols" do
35
+ the_params = {'a' => 1, 'b' => 2, 'e' => 5}
36
+ @mock.should_receive(:sanitize_parameters).with(the_params).
37
+ and_return { |params, *rest| params }
38
+
39
+ sanitized = BinData::SanitizedParameters.new(@mock, the_params)
40
+ sanitized.accepted_parameters.should == ({:a => 1, :b => 2})
41
+ sanitized.extra_parameters.should == ({:e => 5})
42
+ end
43
+
44
+ it "should raise error if parameter has nil value" do
45
+ the_params = {'a' => 1, 'b' => nil, 'e' => 5}
46
+ @mock.should_receive(:sanitize_parameters).with(the_params).
47
+ and_return { |params, *rest| params }
48
+
49
+ lambda {
50
+ BinData::SanitizedParameters.new(@mock, the_params)
51
+ }.should raise_error(ArgumentError)
52
+ end
53
+ end
54
+
55
+ describe BinData::SanitizedParameters do
56
+ before(:each) do
57
+ @mock = mock("dummy class")
58
+ @mock.stub!(:accepted_parameters).and_return([:a, :b, :c])
59
+ @params = {:a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
60
+ end
61
+
62
+ it "should pass extra arguments" do
63
+ @mock.should_receive(:sanitize_parameters).with(@params, 1, 2, 3).
64
+ and_return { |params, *rest| params }
65
+
66
+ sanitized = BinData::SanitizedParameters.new(@mock, @params, 1, 2, 3)
67
+ end
68
+
69
+ it "should partition" do
70
+ @mock.should_receive(:sanitize_parameters).with(@params).
71
+ and_return { |params, *rest| params }
72
+
73
+ sanitized = BinData::SanitizedParameters.new(@mock, @params)
74
+ sanitized.accepted_parameters.should == ({:a => 1, :b => 2, :c => 3})
75
+ sanitized.extra_parameters.should == ({:d => 4, :e => 5})
76
+ end
77
+
78
+ it "should respond_to keys" do
79
+ @mock.should_receive(:sanitize_parameters).with(@params).
80
+ and_return { |params, *rest| params }
81
+
82
+ sanitized = BinData::SanitizedParameters.new(@mock, @params)
83
+ sanitized.should respond_to(:keys)
84
+ keys = sanitized.keys.collect { |x| x.to_s }
85
+ keys.sort.should ==(['a', 'b', 'c', 'd', 'e'])
86
+ end
87
+
88
+ it "should respond_to has_key?" do
89
+ @mock.should_receive(:sanitize_parameters).with(@params).
90
+ and_return { |params, *rest| params }
91
+
92
+ sanitized = BinData::SanitizedParameters.new(@mock, @params)
93
+ sanitized.should respond_to(:has_key?)
94
+ sanitized.has_key?(:a).should be_true
95
+ sanitized.has_key?(:e).should be_true
96
+ sanitized.has_key?(:z).should be_false
97
+ end
98
+
99
+ it "should respond_to []" do
100
+ @mock.should_receive(:sanitize_parameters).with(@params).
101
+ and_return { |params, *rest| params }
102
+
103
+ sanitized = BinData::SanitizedParameters.new(@mock, @params)
104
+ sanitized.should respond_to(:[])
105
+ sanitized[:a].should == 1
106
+ sanitized[:d].should == 4
107
+ end
108
+ end
@@ -12,37 +12,11 @@ class ConcreteSingle < BinData::Single
12
12
  def in_read?() @in_read end
13
13
  end
14
14
 
15
- describe "The sample implementation of Single" do
16
- it "should have symmetric IO" do
17
- io = StringIO.new
18
- data = ConcreteSingle.new
19
- data.value = 42
20
- data.write(io)
21
-
22
- io.rewind
23
- data = ConcreteSingle.new
24
- data.read(io)
25
- data.value.should eql(42)
26
- end
27
- end
28
-
29
- describe "The class Single" do
30
- it "should register subclasses" do
31
- BinData::Single.lookup(:concrete_single).should eql(ConcreteSingle)
32
- end
33
-
34
- it "should read and return a value" do
35
- io = StringIO.new([123456].pack("V"))
36
- ConcreteSingle.read(io).should eql(123456)
37
- data = ConcreteSingle.new
38
- end
39
- end
40
-
41
- describe "A Single object" do
15
+ describe BinData::Single do
42
16
  it "should conform to rule 1 for returning a value" do
43
17
  data = ConcreteSingle.new(:value => 5)
44
18
  data.should_not be_in_read
45
- data.value.should eql(5)
19
+ data.value.should == 5
46
20
  end
47
21
 
48
22
  it "should conform to rule 2 for returning a value" do
@@ -50,37 +24,37 @@ describe "A Single object" do
50
24
  data = ConcreteSingle.new(:value => 5)
51
25
  data.do_read(io)
52
26
  data.should be_in_read
53
- data.value.should eql(42)
27
+ data.value.should == 42
54
28
  end
55
29
 
56
30
  it "should conform to rule 3 for returning a value" do
57
31
  data = ConcreteSingle.new(:initial_value => 5)
58
32
  data.should be_clear
59
- data.value.should eql(5)
33
+ data.value.should == 5
60
34
  end
61
35
 
62
36
  it "should conform to rule 4 for returning a value" do
63
37
  data = ConcreteSingle.new(:initial_value => 5)
64
38
  data.value = 17
65
39
  data.should_not be_clear
66
- data.value.should eql(17)
40
+ data.value.should == 17
67
41
  end
68
42
 
69
43
  it "should conform to rule 5 for returning a value" do
70
44
  data = ConcreteSingle.new
71
45
  data.should be_clear
72
- data.value.should eql(0)
46
+ data.value.should == 0
73
47
  end
74
48
 
75
49
  it "should conform to rule 6 for returning a value" do
76
50
  data = ConcreteSingle.new
77
51
  data.value = 8
78
52
  data.should_not be_clear
79
- data.value.should eql(8)
53
+ data.value.should == 8
80
54
  end
81
55
  end
82
56
 
83
- describe "A new Single object" do
57
+ describe BinData::Single, "after initialisation" do
84
58
  before(:each) do
85
59
  @data = ConcreteSingle.new
86
60
  end
@@ -90,13 +64,32 @@ describe "A new Single object" do
90
64
  lambda { ConcreteSingle.new(params) }.should raise_error(ArgumentError)
91
65
  end
92
66
 
67
+ it "should not have any field_names" do
68
+ BinData::Single.all_possible_field_names(nil).should be_empty
69
+ end
70
+
93
71
  it "should have a sensible value" do
94
- @data.value.should eql(0)
72
+ @data.value.should == 0
73
+ end
74
+
75
+ it "should have symmetric IO" do
76
+ io = StringIO.new
77
+ @data.value = 42
78
+ @data.write(io)
79
+
80
+ io.rewind
81
+ @data = ConcreteSingle.new
82
+ @data.read(io)
83
+ @data.value.should == 42
84
+ end
85
+
86
+ it "should be a single_value" do
87
+ @data.should be_single_value
95
88
  end
96
89
 
97
90
  it "should allowing setting and retrieving value" do
98
91
  @data.value = 5
99
- @data.value.should eql(5)
92
+ @data.value.should == 5
100
93
  end
101
94
 
102
95
  it "should be clear" do
@@ -115,7 +108,7 @@ describe "A new Single object" do
115
108
  end
116
109
 
117
110
  it "should return num_bytes" do
118
- @data.num_bytes.should eql(4)
111
+ @data.num_bytes.should == 4
119
112
  end
120
113
 
121
114
  it "should not contain any field names" do
@@ -124,66 +117,66 @@ describe "A new Single object" do
124
117
 
125
118
  it "should return a snapshot" do
126
119
  @data.value = 5
127
- @data.snapshot.should eql(5)
120
+ @data.snapshot.should == 5
128
121
  end
129
122
  end
130
123
 
131
- describe "A Single with :initial_value" do
124
+ describe BinData::Single, "with :initial_value" do
132
125
  before(:each) do
133
126
  @data = ConcreteSingle.new(:initial_value => 5)
134
127
  end
135
128
 
136
129
  it "should return that initial value before reading or being set" do
137
- @data.value.should eql(5)
130
+ @data.value.should == 5
138
131
  end
139
132
 
140
133
  it "should forget :initial_value after being set" do
141
134
  @data.value = 17
142
- @data.value.should_not eql(5)
135
+ @data.value.should_not == 5
143
136
  end
144
137
 
145
138
  it "should forget :initial_value after reading" do
146
139
  io = StringIO.new([56].pack("V"))
147
140
  @data.read(io)
148
- @data.value.should_not eql(5)
141
+ @data.value.should_not == 5
149
142
  end
150
143
 
151
144
  it "should remember :initial_value after being cleared" do
152
145
  @data.value = 17
153
146
  @data.clear
154
- @data.value.should eql(5)
147
+ @data.value.should == 5
155
148
  end
156
149
  end
157
150
 
158
- describe "A Single with :value" do
151
+ describe BinData::Single, "with :value" do
159
152
  before(:each) do
160
153
  @data = ConcreteSingle.new(:value => 5)
161
154
  end
162
155
 
163
156
  it "should return that :value" do
164
- @data.value.should eql(5)
157
+ @data.value.should == 5
165
158
  end
166
159
 
167
160
  it "should change during reading" do
168
161
  io = StringIO.new([56].pack("V"))
169
162
  @data.do_read(io)
170
- @data.value.should eql(56)
163
+ @data.value.should == 56
171
164
  @data.done_read
172
165
  end
173
166
 
174
167
  it "should not change after reading" do
175
168
  io = StringIO.new([56].pack("V"))
176
169
  @data.read(io)
177
- @data.value.should eql(5)
170
+ @data.value.should == 5
178
171
  end
179
172
 
180
173
  it "should not be able to change the value" do
181
174
  @data.value = 17
182
- @data.value.should eql(5)
175
+ @data.value.should == 5
183
176
  end
184
177
  end
185
178
 
186
- describe "A Single with :check_value" do
179
+ describe BinData::Single, "with :check_value" do
187
180
  before(:each) do
188
181
  @io = StringIO.new([34].pack("V"))
189
182
  end
@@ -208,3 +201,24 @@ describe "A Single with :check_value" do
208
201
  lambda { data.read(@io) }.should raise_error(BinData::ValidityError)
209
202
  end
210
203
  end
204
+
205
+ describe BinData::Single, "when subclassing" do
206
+ before(:all) do
207
+ eval <<-END
208
+ class SubClassOfSingle < BinData::Single
209
+ public :val_to_str, :read_val, :sensible_default
210
+ end
211
+ END
212
+ end
213
+
214
+ before(:each) do
215
+ @obj = SubClassOfSingle.new
216
+ end
217
+
218
+ it "should raise errors on unimplemented methods" do
219
+ lambda { @obj.val_to_str(nil) }.should raise_error(NotImplementedError)
220
+ lambda { @obj.read_val(nil) }.should raise_error(NotImplementedError)
221
+ lambda { @obj.sensible_default }.should raise_error(NotImplementedError)
222
+ end
223
+ end
224
+
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
+ require 'bindata'
5
+
6
+ describe BinData::SingleValue do
7
+ before(:all) do
8
+ eval <<-END
9
+ class SingleValueWithEndian < BinData::SingleValue
10
+ endian :little
11
+ int16 :a
12
+ def get; self.a; end
13
+ def set(v); self.a = v; end
14
+ end
15
+ END
16
+ end
17
+
18
+ before(:each) do
19
+ @obj = SingleValueWithEndian.new
20
+ end
21
+
22
+ it "should support endian" do
23
+ @obj.value = 5
24
+ @obj.to_s.should == "\x05\x00"
25
+ end
26
+
27
+ it "should set value" do
28
+ @obj.value = 5
29
+ @obj.to_s.should == "\x05\x00"
30
+ end
31
+
32
+ it "should read value" do
33
+ @obj.read("\x00\x01")
34
+ @obj.value.should == 0x100
35
+ end
36
+
37
+ it "should accept standard parameters" do
38
+ obj = SingleValueWithEndian.new(:initial_value => 2)
39
+ obj.to_s.should == "\x02\x00"
40
+ end
41
+
42
+ it "should return num_bytes" do
43
+ @obj.num_bytes.should == 2
44
+ end
45
+
46
+ it "should raise error on missing methods" do
47
+ lambda {
48
+ @obj.does_not_exist
49
+ }.should raise_error(NoMethodError)
50
+ end
51
+ end
52
+
53
+ describe BinData::SingleValue, "requiring extra parameters" do
54
+ before(:all) do
55
+ eval <<-END
56
+ class SingleValueWithExtra < BinData::SingleValue
57
+ int8 :a, :initial_value => :iv
58
+ def get; self.a; end
59
+ def set(v); self.a = v; end
60
+ end
61
+ END
62
+ end
63
+
64
+ it "should pass parameters correctly" do
65
+ obj = SingleValueWithExtra.new(:iv => 5)
66
+ obj.value.should == 5
67
+ end
68
+ end
69
+
70
+ describe BinData::SingleValue, "when subclassing" do
71
+ before(:all) do
72
+ eval <<-END
73
+ class SubClassOfSingleValue < BinData::SingleValue
74
+ public :get, :set
75
+ end
76
+ END
77
+ end
78
+
79
+ before(:each) do
80
+ @obj = SubClassOfSingleValue.new
81
+ end
82
+
83
+ it "should raise errors on unimplemented methods" do
84
+ lambda { @obj.set(nil) }.should raise_error(NotImplementedError)
85
+ lambda { @obj.get }.should raise_error(NotImplementedError)
86
+ end
87
+ end
88
+
89
+ describe BinData::SingleValue, "when defining" do
90
+ it "should fail on non registered types" do
91
+ lambda {
92
+ eval <<-END
93
+ class BadTypeSingleValue < BinData::SingleValue
94
+ non_registerd_type :a
95
+ end
96
+ END
97
+ }.should raise_error(TypeError)
98
+ end
99
+
100
+ it "should fail on duplicate names" do
101
+ lambda {
102
+ eval <<-END
103
+ class DuplicateNameSingleValue < BinData::SingleValue
104
+ int8 :a
105
+ int8 :b
106
+ int8 :a
107
+ end
108
+ END
109
+ }.should raise_error(SyntaxError)
110
+ end
111
+
112
+ it "should fail when field name shadows an existing method" do
113
+ lambda {
114
+ eval <<-END
115
+ class ExistingNameSingleValue < BinData::SingleValue
116
+ int8 :object_id
117
+ end
118
+ END
119
+ }.should raise_error(NameError)
120
+ end
121
+
122
+ it "should fail on unknown endian" do
123
+ lambda {
124
+ eval <<-END
125
+ class BadEndianSingleValue < BinData::SingleValue
126
+ endian 'a bad value'
127
+ end
128
+ END
129
+ }.should raise_error(ArgumentError)
130
+ end
131
+ end