bindata 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
+ require 'bindata/stringz'
5
+
6
+ context "An empty Stringz data object" do
7
+ setup do
8
+ @str = BinData::Stringz.new
9
+ end
10
+
11
+ specify "should include the zero byte in num_bytes total" do
12
+ @str.num_bytes.should == 1
13
+ end
14
+
15
+ specify "should not append the zero byte terminator to the value" do
16
+ @str.value.should == ""
17
+ end
18
+
19
+ specify "should write the zero byte terminator" do
20
+ io = StringIO.new
21
+ @str.write(io)
22
+ io.rewind
23
+ io.read.should == "\0"
24
+ end
25
+ end
26
+
27
+ context "A Stringz data object with value set" do
28
+ setup do
29
+ @str = BinData::Stringz.new
30
+ @str.value = "abcd"
31
+ end
32
+
33
+ specify "should include the zero byte in num_bytes total" do
34
+ @str.num_bytes.should == 5
35
+ end
36
+
37
+ specify "should not append the zero byte terminator to the value" do
38
+ @str.value.should == "abcd"
39
+ end
40
+
41
+ specify "should write the zero byte terminator" do
42
+ io = StringIO.new
43
+ @str.write(io)
44
+ io.rewind
45
+ io.read.should == "abcd\0"
46
+ end
47
+ end
48
+
49
+ context "Reading with a Stringz data object" do
50
+ setup do
51
+ @str = BinData::Stringz.new
52
+ end
53
+
54
+ specify "should stop at the first zero byte" do
55
+ io = StringIO.new("abcd\0xyz\0")
56
+ @str.read(io)
57
+ @str.value.should == "abcd"
58
+ io.read(1).should == "x"
59
+ end
60
+
61
+ specify "should handle a zero length string" do
62
+ io = StringIO.new("\0abcd")
63
+ @str.read(io)
64
+ @str.value.should == ""
65
+ io.read(1).should == "a"
66
+ end
67
+
68
+ specify "should fail if no zero byte is found" do
69
+ io = StringIO.new("abcd")
70
+ lambda {@str.read(io) }.should raise_error(EOFError)
71
+ end
72
+ end
73
+
74
+ context "Setting the value of a Stringz data object" do
75
+ setup do
76
+ @str = BinData::Stringz.new
77
+ end
78
+
79
+ specify "should include the zero byte in num_bytes total" do
80
+ @str.value = "abcd"
81
+ @str.num_bytes.should == 5
82
+ end
83
+
84
+ specify "should accept empty strings" do
85
+ @str.value = ""
86
+ @str.value.should == ""
87
+ end
88
+
89
+ specify "should accept strings that aren't zero terminated" do
90
+ @str.value = "abcd"
91
+ @str.value.should == "abcd"
92
+ end
93
+
94
+ specify "should accept strings that are zero terminated" do
95
+ @str.value = "abcd\0"
96
+ @str.value.should == "abcd"
97
+ end
98
+
99
+ specify "should accept up to the first zero byte" do
100
+ @str.value = "abcd\0xyz\0"
101
+ @str.value.should == "abcd"
102
+ end
103
+ end
104
+
105
+ context "A Stringz data object with max_length" do
106
+ setup do
107
+ @str = BinData::Stringz.new(:max_length => 5)
108
+ end
109
+
110
+ specify "should read less than max_length" do
111
+ io = StringIO.new("abc\0xyz")
112
+ @str.read(io)
113
+ @str.value.should == "abc"
114
+ end
115
+
116
+ specify "should read exactly max_length" do
117
+ io = StringIO.new("abcd\0xyz")
118
+ @str.read(io)
119
+ @str.value.should == "abcd"
120
+ end
121
+
122
+ specify "should read no more than max_length" do
123
+ io = StringIO.new("abcdefg\0xyz")
124
+ @str.read(io)
125
+ @str.value.should == "abcd"
126
+ io.read(1).should == "f"
127
+ end
128
+
129
+ specify "should accept values less than max_length" do
130
+ @str.value = "abc"
131
+ @str.value.should == "abc"
132
+ end
133
+
134
+ specify "should accept values exactly max_length" do
135
+ @str.value = "abcd"
136
+ @str.value.should == "abcd"
137
+ end
138
+
139
+ specify "should trim values greater than max_length" do
140
+ @str.value = "abcde"
141
+ @str.value.should == "abcd"
142
+ end
143
+
144
+ specify "should write values less than max_length" do
145
+ io = StringIO.new
146
+ @str.value = "abc"
147
+ @str.write(io)
148
+ io.rewind
149
+ io.read.should == "abc\0"
150
+ end
151
+
152
+ specify "should write values exactly max_length" do
153
+ io = StringIO.new
154
+ @str.value = "abcd"
155
+ @str.write(io)
156
+ io.rewind
157
+ io.read.should == "abcd\0"
158
+ end
159
+ end
@@ -0,0 +1,190 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__)) + '/spec_common'
4
+ require 'bindata/struct'
5
+ require 'bindata/int'
6
+
7
+ context "A Struct with hidden fields" do
8
+ context_setup do
9
+ eval <<-END
10
+ class TestStruct < BinData::Struct
11
+ hide :b, 'c'
12
+ int8 :a
13
+ int8 'b', :initial_value => 10
14
+ int8 :c
15
+ int8 :d, :value => :b
16
+ end
17
+ END
18
+ @obj = TestStruct.new
19
+ end
20
+
21
+ specify "should only show fields that aren't hidden" do
22
+ @obj.field_names.should == ["a", "d"]
23
+ end
24
+
25
+ specify "should be able to access hidden fields directly" do
26
+ @obj.b.should == 10
27
+ @obj.c = 15
28
+ @obj.c.should == 15
29
+
30
+ @obj.should respond_to?(:b=)
31
+ end
32
+
33
+ specify "should not include hidden fields in snapshot" do
34
+ @obj.b = 5
35
+ @obj.snapshot.should == {"a" => 0, "d" => 5}
36
+ end
37
+ end
38
+
39
+ context "Defining a Struct" do
40
+ specify "should fail on non registered types" do
41
+ lambda {
42
+ eval <<-END
43
+ class BadType < BinData::Struct
44
+ non_registerd_type :a
45
+ end
46
+ END
47
+ }.should raise_error(TypeError)
48
+ end
49
+
50
+ specify "should fail on duplicate names" do
51
+ lambda {
52
+ eval <<-END
53
+ class DuplicateName < BinData::Struct
54
+ int8 :a
55
+ int8 :b
56
+ int8 :a
57
+ end
58
+ END
59
+ }.should raise_error(SyntaxError)
60
+ end
61
+ end
62
+
63
+ context "A Struct with multiple fields" do
64
+ setup do
65
+ fields = [ [:int8, :a], [:int8, :b] ]
66
+ @obj = BinData::Struct.new(:fields => fields)
67
+ @obj.a = 1
68
+ @obj.b = 2
69
+ end
70
+
71
+ specify "should return num_bytes" do
72
+ @obj.num_bytes(:a).should == 1
73
+ @obj.num_bytes(:b).should == 1
74
+ @obj.num_bytes.should == 2
75
+ end
76
+
77
+ specify "should clear" do
78
+ @obj.a = 6
79
+ @obj.clear
80
+ @obj.clear?.should be_true
81
+ end
82
+
83
+ specify "should clear individual elements" do
84
+ @obj.a = 6
85
+ @obj.b = 7
86
+ @obj.clear(:a)
87
+ @obj.clear?(:a).should be_true
88
+ @obj.clear?(:b).should be_false
89
+ end
90
+
91
+ specify "should write ordered" do
92
+ io = StringIO.new
93
+ @obj.write(io)
94
+
95
+ io.rewind
96
+ io.read.should == "\x01\x02"
97
+ end
98
+
99
+ specify "should read ordered" do
100
+ io = StringIO.new "\x03\x04"
101
+ @obj.read(io)
102
+
103
+ @obj.a.should == 3
104
+ @obj.b.should == 4
105
+ end
106
+
107
+ specify "should return a snapshot" do
108
+ snap = @obj.snapshot
109
+ snap.a.should == 1
110
+ snap.b.should == 2
111
+ snap.should == { "a" => 1, "b" => 2 }
112
+ end
113
+
114
+ specify "should return field_names" do
115
+ @obj.field_names.should == ["a", "b"]
116
+ end
117
+
118
+ specify "should fail on unknown method call" do
119
+ lambda { @obj.does_not_exist }.should raise_error(NoMethodError)
120
+ end
121
+ end
122
+
123
+ context "A Struct with a value method" do
124
+ context_setup do
125
+ eval <<-END
126
+ class StructWithValue < BinData::Struct
127
+ int8 :a
128
+ int8 :b
129
+
130
+ def value
131
+ a
132
+ end
133
+ end
134
+ END
135
+ @obj = StructWithValue.new
136
+ end
137
+
138
+ specify "should be single value object" do
139
+ @obj.should be_a_single_value
140
+ end
141
+
142
+ specify "should have no field names" do
143
+ @obj.field_names.should be_empty
144
+ end
145
+
146
+ specify "should not respond to field accesses" do
147
+ @obj.should_not respond_to?(:a)
148
+ end
149
+ end
150
+
151
+ context "A Struct with nested structs" do
152
+ context_setup do
153
+ eval <<-END
154
+ class StructInner1 < BinData::Struct
155
+ int8 :w, :initial_value => 3
156
+ int8 :x, :value => :the_val
157
+ end
158
+
159
+ class StructInner2 < BinData::Struct
160
+ int8 :y, :value => lambda { parent.b.w }
161
+ int8 :z
162
+ end
163
+
164
+ class StructOuter < BinData::Struct
165
+ int8 :a, :initial_value => 6
166
+ struct_inner1 :b, :the_val => :a
167
+ struct_inner2 nil
168
+ end
169
+ END
170
+ @obj = StructOuter.new
171
+ end
172
+
173
+ specify "should included nested field names" do
174
+ @obj.field_names.should == ["a", "b", "y", "z"]
175
+ end
176
+
177
+ specify "should access nested fields" do
178
+ @obj.a.should == 6
179
+ @obj.b.w.should == 3
180
+ @obj.b.x.should == 6
181
+ @obj.y.should == 3
182
+ end
183
+
184
+ specify "should return correct offset of" do
185
+ @obj.offset_of("b").should == 1
186
+ @obj.offset_of("y").should == 3
187
+ @obj.offset_of("z").should == 4
188
+ end
189
+ end
190
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: bindata
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.0
7
+ date: 2007-03-16 00:00:00 +09:00
8
+ summary: A declarative way to read and write binary file formats
9
+ require_paths:
10
+ - lib
11
+ email: dion@lostrealm.com
12
+ homepage: http://bindata.rubyforge.org
13
+ rubyforge_project: bindata
14
+ description:
15
+ autorequire: bindata
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Dion Mendel
31
+ files:
32
+ - COPYING
33
+ - GPL
34
+ - INSTALL
35
+ - README
36
+ - TODO
37
+ - ChangeLog
38
+ - examples/gzip.rb
39
+ - spec/lazy_spec.rb
40
+ - spec/base_spec.rb
41
+ - spec/int_spec.rb
42
+ - spec/stringz_spec.rb
43
+ - spec/choice_spec.rb
44
+ - spec/registry_spec.rb
45
+ - spec/single_spec.rb
46
+ - spec/string_spec.rb
47
+ - spec/spec_common.rb
48
+ - spec/array_spec.rb
49
+ - spec/struct_spec.rb
50
+ - lib/bindata
51
+ - lib/bindata.rb
52
+ - lib/bindata/int.rb
53
+ - lib/bindata/string.rb
54
+ - lib/bindata/base.rb
55
+ - lib/bindata/struct.rb
56
+ - lib/bindata/array.rb
57
+ - lib/bindata/stringz.rb
58
+ - lib/bindata/registry.rb
59
+ - lib/bindata/choice.rb
60
+ - lib/bindata/lazy.rb
61
+ - lib/bindata/single.rb
62
+ test_files: []
63
+
64
+ rdoc_options:
65
+ - README
66
+ - lib/bindata
67
+ - -m
68
+ - README
69
+ extra_rdoc_files: []
70
+
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ requirements: []
76
+
77
+ dependencies: []
78
+