Skip to content

Commit 14c12e5

Browse files
Add specs for IO#read_bytes with converter (#16250)
1 parent 6a14b81 commit 14c12e5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

spec/std/io/byte_format_spec.cr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ private def new_string_io(*bytes)
1919
io
2020
end
2121

22+
private module ReadBytesConverter
23+
def self.from_io(io : IO, format = IO::ByteFormat::NetworkEndian) : Int32
24+
io.read_bytes(Int32, format: format)
25+
end
26+
end
27+
2228
describe IO::ByteFormat do
2329
describe "little endian" do
2430
describe "encode" do
@@ -130,6 +136,11 @@ describe IO::ByteFormat do
130136
float = io.read_bytes Float64, IO::ByteFormat::LittleEndian
131137
float.should be_close(1.234, 0.0001)
132138
end
139+
140+
it "reads with converter" do
141+
io = new_string_io(0x78, 0x56, 0x34, 0x12)
142+
io.read_bytes(ReadBytesConverter, IO::ByteFormat::LittleEndian).should eq 0x12345678_i32
143+
end
133144
end
134145

135146
describe "from slice" do
@@ -248,6 +259,11 @@ describe IO::ByteFormat do
248259
float = io.read_bytes Float64, IO::ByteFormat::BigEndian
249260
float.should be_close(1.234, 0.0001)
250261
end
262+
263+
it "reads with converter" do
264+
io = new_string_io(0x12, 0x34, 0x56, 0x78)
265+
io.read_bytes(ReadBytesConverter, IO::ByteFormat::BigEndian).should eq 0x12345678_i32
266+
end
251267
end
252268

253269
describe "from slice" do

0 commit comments

Comments
 (0)