Skip to content

Commit ed47a37

Browse files
committed
spec: Add spec for BinaryParser::ByteSize
1 parent c93ca7e commit ed47a37

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

spec/byte_size_spec.cr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "./spec_helper"
2+
require "./fixture/*"
3+
4+
describe BinaryParser::ByteSize do
5+
describe "#bytesize" do
6+
it "calculate size in byte" do
7+
size = sizeof(UInt32) * 2 + sizeof(UInt8)
8+
parser = ByteSizeFixture.new
9+
10+
expect(parser.bytesize).to eq(size)
11+
parser.str = "foo"
12+
expect(parser.bytesize).to eq(size + 3)
13+
parser.arr = [0u8, 1u8, 2u8]
14+
expect(parser.bytesize).to eq(size + 3 + sizeof(UInt8) * 3)
15+
end
16+
end
17+
end

spec/fixture/byte_size.cr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require "../../src/binary_parser"
2+
3+
class ByteSizeFixture < BinaryParser
4+
uint8 :foo
5+
uint32 :bar
6+
uint32 :size
7+
string :str, { count: :size }
8+
array :arr, { count: :size, type: UInt8 }
9+
include BinaryParser::ByteSize
10+
end
11+

0 commit comments

Comments
 (0)