Skip to content

Commit 4b0a444

Browse files
committed
spec: More spec
1 parent 3b9085c commit 4b0a444

File tree

8 files changed

+114
-10
lines changed

8 files changed

+114
-10
lines changed

spec/binary_parser_spec.cr

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,63 @@ require "./spec_helper"
22
require "./fixture/*"
33

44
describe BinaryParser do
5-
describe "uint32" do
5+
describe "#uint32" do
66
it "parse correct" do
7-
parser = UInt32Parser.new.load(UINT32_FIXTURE)
7+
parser = UInt32Parser.new.load(UInt32Fixture)
88
expect(parser.value).to eq(42)
99
end
1010
end
1111

12-
describe "uint8" do
12+
describe "#uint16" do
1313
it "parse correct" do
14-
parser = UInt8Parser.new.load(UINT8_FIXTURE)
14+
parser = UInt16Parser.new.load(UInt16Fixture)
1515
expect(parser.value).to eq(42)
1616
end
1717
end
18+
19+
describe "#uint8" do
20+
it "parse correct" do
21+
parser = UInt8Parser.new.load(UInt8Fixture)
22+
expect(parser.value).to eq(42)
23+
end
24+
end
25+
26+
describe "#int32" do
27+
it "parse correct" do
28+
parser = Int32Parser.new.load(Int32Fixture)
29+
expect(parser.value).to eq(42)
30+
end
31+
end
32+
33+
describe "#int16" do
34+
it "parse correct" do
35+
parser = Int16Parser.new.load(Int16Fixture)
36+
expect(parser.value).to eq(42)
37+
end
38+
end
39+
40+
describe "#int8" do
41+
it "parse correct" do
42+
parser = Int8Parser.new.load(Int8Fixture)
43+
expect(parser.value).to eq(42)
44+
end
45+
end
46+
47+
describe "#array" do
48+
context "when fixed length" do
49+
it "parse correct" do
50+
parser = FixedArrayParser.new.load(FixedArrayFixture)
51+
expect(parser.arr).to eq([42, 43, 44, 45, 46])
52+
end
53+
end
54+
55+
context "when variable length" do
56+
it "parse correct" do
57+
parser = VarArrayParser.new.load(VarArrayFixture)
58+
expect(parser.size).to eq(5)
59+
expect(parser.arr).to eq([42, 43, 44, 45, 46])
60+
end
61+
end
62+
end
1863
end
1964

spec/fixture/array.cr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "../../src/binary_parser"
2+
3+
class FixedArrayParser < BinaryParser
4+
array :arr, { type: UInt8, count: 5 }
5+
end
6+
7+
FixedArrayFixture = IO::Memory.new(sizeof(UInt8) * 5)
8+
5.times do |i|
9+
FixedArrayFixture.write_bytes(42u8 + i)
10+
end
11+
FixedArrayFixture.rewind
12+
13+
class VarArrayParser < BinaryParser
14+
uint32 :size
15+
array :arr, { type: UInt8, count: :size }
16+
end
17+
18+
VarArrayFixture = IO::Memory.new(sizeof(UInt32) + sizeof(UInt8) * 5)
19+
VarArrayFixture.write_bytes(5u32)
20+
5.times do |i|
21+
VarArrayFixture.write_bytes(42u8 + i)
22+
end
23+
VarArrayFixture.rewind

spec/fixture/int16.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "../../src/binary_parser"
2+
3+
class Int16Parser < BinaryParser
4+
int16 :value
5+
end
6+
7+
Int16Fixture = IO::Memory.new(sizeof(Int16))
8+
Int16Fixture.write_bytes(42i16)
9+
Int16Fixture.rewind

spec/fixture/int32.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "../../src/binary_parser"
2+
3+
class Int32Parser < BinaryParser
4+
int8 :value
5+
end
6+
7+
Int32Fixture = IO::Memory.new(sizeof(Int32))
8+
Int32Fixture.write_bytes(42i32)
9+
Int32Fixture.rewind

spec/fixture/int8.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "../../src/binary_parser"
2+
3+
class Int8Parser < BinaryParser
4+
int8 :value
5+
end
6+
7+
Int8Fixture = IO::Memory.new(sizeof(Int8))
8+
Int8Fixture.write_bytes(42i8)
9+
Int8Fixture.rewind

spec/fixture/uint16.cr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "../../src/binary_parser"
2+
3+
class UInt16Parser < BinaryParser
4+
uint16 :value
5+
end
6+
7+
UInt16Fixture = IO::Memory.new(sizeof(UInt16))
8+
UInt16Fixture.write_bytes(42u16)
9+
UInt16Fixture.rewind

spec/fixture/uint32.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class UInt32Parser < BinaryParser
44
uint32 :value
55
end
66

7-
UINT32_FIXTURE = IO::Memory.new(sizeof(UInt32))
8-
UINT32_FIXTURE.write_bytes(42u32)
9-
UINT32_FIXTURE.rewind
7+
UInt32Fixture = IO::Memory.new(sizeof(UInt32))
8+
UInt32Fixture.write_bytes(42u32)
9+
UInt32Fixture.rewind

spec/fixture/uint8.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ class UInt8Parser < BinaryParser
44
uint8 :value
55
end
66

7-
UINT8_FIXTURE = IO::Memory.new(sizeof(UInt8))
8-
UINT8_FIXTURE.write_bytes(42u8)
9-
UINT8_FIXTURE.rewind
7+
UInt8Fixture = IO::Memory.new(sizeof(UInt8))
8+
UInt8Fixture.write_bytes(42u8)
9+
UInt8Fixture.rewind

0 commit comments

Comments
 (0)