File: string_test.rb

package info (click to toggle)
ruby-bindata 2.4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 600 kB
  • sloc: ruby: 8,566; makefile: 4
file content (312 lines) | stat: -rwxr-xr-x 8,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))

describe BinData::String, "with mutually exclusive parameters" do
  it ":value and :initial_value" do
    params = {value: "", initial_value: ""}
    _ { BinData::String.new(params) }.must_raise ArgumentError
  end

  it ":length and :read_length" do
    params = {length: 5, read_length: 5}
    _ { BinData::String.new(params) }.must_raise ArgumentError
  end

  it ":value and :length" do
    params = {value: "", length: 5}
    _ { BinData::String.new(params) }.must_raise ArgumentError
  end
end

describe BinData::String, "when assigning" do
  let(:small) { BinData::String.new(length: 3, pad_byte: "A") }
  let(:large) { BinData::String.new(length: 5, pad_byte: "B") }

  it "copies data from small to large" do
    large.assign(small)
    _(large).must_equal "AAABB"
  end

  it "copies data from large to small" do
    small.assign(large)
    _(small).must_equal "BBB"
  end
end

describe BinData::String do
  let(:obj) { BinData::String.new("testing") }

  it "compares with regexp" do
    _((/es/ =~ obj)).must_equal 1
  end

  it "compares with regexp" do
    _((obj =~ /es/)).must_equal 1
  end
end

describe BinData::String, "with :read_length" do
  let(:obj) { BinData::String.new(read_length: 5) }

  specify { _(obj.num_bytes).must_equal 0 }
  specify { _(obj.value).must_equal "" }

  it "reads :read_length bytes" do
    obj.read("abcdefghij")
    _(obj).must_equal "abcde"
  end

  it "remembers :read_length after value is cleared" do
    obj.assign("abc")
    _(obj.num_bytes).must_equal 3
    obj.clear

    obj.read("abcdefghij")
    _(obj).must_equal "abcde"
  end
end

describe BinData::String, "with :length" do
  let(:obj) { BinData::String.new(length: 5) }

  specify { _(obj.num_bytes).must_equal 5 }
  specify { _(obj.value).must_equal "\0\0\0\0\0" }

  it "retains :length after value is set" do
    obj.assign("abcdefghij")
    _(obj.num_bytes).must_equal 5
  end

  it "reads :length bytes" do
    obj.read("abcdefghij")
    _(obj).must_equal "abcde"
  end

  it "pads values less than :length" do
    obj.assign("abc")
    _(obj).must_equal "abc\0\0"
  end

  it "accepts values exactly :length" do
    obj.assign("abcde")
    _(obj).must_equal "abcde"
  end

  it "truncates values greater than :length" do
    obj.assign("abcdefghij")
    _(obj).must_equal "abcde"
  end
end

describe BinData::String, "with :read_length and :initial_value" do
  let(:obj) { BinData::String.new(read_length: 5, initial_value: "abcdefghij") }

  specify { _(obj.num_bytes).must_equal 10 }
  specify { _(obj.value).must_equal "abcdefghij" }

  it "uses :read_length for reading" do
    io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
    obj.read(io)
    _(io.pos).must_equal 5
   end

  it "forgets :initial_value after reading" do
    obj.read("ABCDEFGHIJKLMNOPQRST")
    _(obj.num_bytes).must_equal 5
    _(obj).must_equal "ABCDE"
  end
end

describe BinData::String, "with :read_length and :value" do
  let(:obj) { BinData::String.new(read_length: 5, value: "abcdefghij") }

  specify { _(obj.num_bytes).must_equal 10 }
  specify { _(obj.value).must_equal "abcdefghij" }

  it "uses :read_length for reading" do
    io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
    obj.read(io)
    _(io.pos).must_equal 5
  end

  describe "after reading" do
    before(:each) do
      obj.read("ABCDEFGHIJKLMNOPQRST")
    end

    it "is not affected by :read_length after reading" do
      _(obj.num_bytes).must_equal 10
      _(obj).must_equal "abcdefghij"
    end

    it "returns read value while reading" do
      obj.stub :reading?, true do
        _(obj).must_equal "ABCDE"
      end
    end
  end
end

describe BinData::String, "with :length and :initial_value" do
  let(:obj) { BinData::String.new(length: 5, initial_value: "abcdefghij") }

  specify { _(obj.num_bytes).must_equal 5 }
  specify { _(obj.value).must_equal "abcde" }

  it "forgets :initial_value after reading" do
    io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
    obj.read(io)
    _(io.pos).must_equal 5
    _(obj.num_bytes).must_equal 5
    _(obj).must_equal "ABCDE"
  end
end

describe BinData::String, "with :pad_byte" do
  it "accepts a numeric value for :pad_byte" do
    str = BinData::String.new(length: 5, pad_byte: 6)
    str.assign("abc")
    _(str).must_equal "abc\x06\x06"
  end

  it "accepts a character for :pad_byte" do
    str = BinData::String.new(length: 5, pad_byte: "R")
    str.assign("abc")
    _(str).must_equal "abcRR"
  end

  it "does not accept a string for :pad_byte" do
    params = {length: 5, pad_byte: "RR"}
    _ { BinData::String.new(params) }.must_raise ArgumentError
  end
end

describe BinData::String, "with :trim_padding" do
  it "set false is the default" do
    str1 = BinData::String.new(length: 5)
    str2 = BinData::String.new(length: 5, trim_padding: false)
    str1.assign("abc")
    str2.assign("abc")
    _(str1).must_equal "abc\0\0"
    _(str2).must_equal "abc\0\0"
  end

  describe "trim padding set" do
    let(:obj) { BinData::String.new(pad_byte: 'R', trim_padding: true) }

    it "trims the value" do
      obj.assign("abcRR")
      _(obj).must_equal "abc"
    end

    it "does not affect num_bytes" do
      obj.assign("abcRR")
      _(obj.num_bytes).must_equal 5
    end

    it "trims if last char is :pad_byte" do
      obj.assign("abcRR")
      _(obj).must_equal "abc"
    end

    it "does not trim if value contains :pad_byte not at the end" do
      obj.assign("abcRRde")
      _(obj).must_equal "abcRRde"
    end
  end
end

describe BinData::String, "with :pad_front" do
  it "set false is the default" do
    str1 = BinData::String.new(length: 5)
    str2 = BinData::String.new(length: 5, pad_front: false)
    str1.assign("abc")
    str2.assign("abc")
    _(str1).must_equal "abc\0\0"
    _(str2).must_equal "abc\0\0"
  end

  it "pads to the front" do
    str = BinData::String.new(length: 5, pad_byte: 'R', pad_front: true)
    str.assign("abc")
    _(str).must_equal "RRabc"
  end

  it "can alternatively be accesses by :pad_left" do
    str = BinData::String.new(length: 5, pad_byte: 'R', pad_left: true)
    str.assign("abc")
    _(str).must_equal "RRabc"
  end

  describe "and :trim_padding" do
    let(:obj) { BinData::String.new(length: 5, pad_byte: 'R', pad_front: true, trim_padding: true) }

    it "assigns" do
      obj.assign("abc")
      _(obj).must_equal "abc"
    end

    it "has to_binary_s" do
      obj.assign("abc")
      _(obj.to_binary_s).must_equal_binary "RRabc"
    end

    it "reads" do
      obj.read "RRabc"
      _(obj).must_equal "abc"
    end
  end
end

describe BinData::String, "with Ruby 1.9 encodings" do
  class UTF8String < BinData::String
    def snapshot
      super.force_encoding('UTF-8')
    end
  end

  let(:obj) { UTF8String.new }
  let(:binary_str) { "\xC3\x85\xC3\x84\xC3\x96" }
  let(:utf8_str) { binary_str.dup.force_encoding('UTF-8') }

  it "stores assigned values as binary" do
    obj.assign(utf8_str)
    _(obj.to_binary_s).must_equal_binary binary_str
  end

  it "stores read values as binary" do
    obj = UTF8String.new(read_length: binary_str.bytesize)
    obj.read(binary_str)

    _(obj.to_binary_s).must_equal_binary binary_str
  end

  it "returns values in correct encoding" do
    obj.assign(utf8_str)

    _(obj.snapshot).must_equal utf8_str
  end

  it "has correct num_bytes" do
    obj.assign(utf8_str)

    _(obj.num_bytes).must_equal binary_str.bytesize
  end
end

describe BinData::String, "warnings" do
  it "warns if has :asserted_value but no :length" do
    obj = BinData::String.new(asserted_value: "ABC")
    obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
      _ { obj.read("abcde") }.must_raise BinData::ValidityError
    end
  end

  it "warns if has :value but no :read_length" do
    obj = BinData::String.new(value: "ABC")
    obj.must_warn "obj does not have a :read_length parameter - returning empty string" do
      obj.read("abcde")
    end
  end
end