Skip to content

Commit

Permalink
parser_spec: add partial_array_int + partial_array_struct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyCat committed Jul 28, 2024
1 parent 61509e9 commit 231beb5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/formats/partial_array_int.ksy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Input file only has 7 bytes, but we aim to load 5x 2-byte objects as array.
# Expected to have 3 objects + 1 partial object.

meta:
id: partial_array_int
seq:
- id: entries
repeat: expr
repeat-expr: 5
type: entry
types:
entry:
seq:
- id: a
type: u1
- id: b
type: u1
17 changes: 17 additions & 0 deletions spec/formats/partial_array_struct.ksy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Input file only has 7 bytes, but we aim to load 5x 2-byte objects as array.
# Expected to have 3 objects + 1 partial object.

meta:
id: partial_array_struct
seq:
- id: entries
repeat: expr
repeat-expr: 5
type: entry
types:
entry:
seq:
- id: a
type: u1
- id: b
type: u1
28 changes: 28 additions & 0 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ module Kaitai::Struct::Visualizer
expect(parser.data._debug['bar']).to eq({ start: 5 })
end

it 'handles EOF error in array' do
opts = {}
compiler = KSYCompiler.new(opts)
parser = Parser.new(compiler, 'input/7bytes.bin', ['formats/partial_array_struct.ksy'], opts)

exc = parser.load

# we want 5 entries of 2 bytes each = 10 bytes, only 7 bytes available
expect(exc).to be_a(EOFError)

expect(parser.data.class::SEQ_FIELDS).to eq(%w[entries])

# 4 entries: 3 full, last one partial
expect(parser.data.entries.size).to eq(4)

# 3 full entries
expect(parser.data.entries[0].a).to eq(49)
expect(parser.data.entries[0].b).to eq(50)
expect(parser.data.entries[1].a).to eq(51)
expect(parser.data.entries[1].b).to eq(52)
expect(parser.data.entries[2].a).to eq(53)
expect(parser.data.entries[2].b).to eq(54)

# last entry is partial
expect(parser.data.entries[3].a).to eq(55)
expect(parser.data.entries[3].b).to be_nil
end

it 'handles validation error in seq' do
opts = {}
compiler = KSYCompiler.new(opts)
Expand Down

0 comments on commit 231beb5

Please sign in to comment.