Skip to content

Commit

Permalink
Add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
spuun committed Oct 23, 2024
1 parent c1da414 commit 47a944e
Showing 1 changed file with 52 additions and 14 deletions.
66 changes: 52 additions & 14 deletions spec/payload_spec.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
require "./spec_helper"

class NonPositionIO < ::IO
def initialize(@data : Bytes)
end

def read(slice : Bytes)
slice.size.times { |i| slice[i] = @data[i] }
slice.size
end

def write(slice : Bytes) : Nil
raise NotImplementedError.new("write")
end
end

describe MQTT::Protocol::Payload do
it ".new(Bytes) returns a BytesPayload" do
obj = MQTT::Protocol::Payload.new("foo".to_slice)
Expand Down Expand Up @@ -76,27 +90,51 @@ describe MQTT::Protocol::Payload do
end

describe "IOPayload" do
it "#to_slice should peek if possible" do
io = IO::Memory.new("foo".to_slice)
io.rewind
describe "#to_slice" do
it "should peek if possible" do
io = IO::Memory.new("foo".to_slice)
io.rewind

obj = MQTT::Protocol::IOPayload.new(io, 3)
data = obj.to_slice
obj = MQTT::Protocol::IOPayload.new(io, 3)
data = obj.to_slice

Check warning on line 99 in spec/payload_spec.cr

View workflow job for this annotation

GitHub Actions / Ameba

Lint/UselessAssign

Useless assignment to variable `data`
Raw output
> data = obj.to_slice
  ^

obj.@data.should be_nil
obj.@data.should be_nil
end

it "should copy data if peek isn't possible" do
io = NonPositionIO.new("foo".to_slice)

obj = MQTT::Protocol::IOPayload.new(io, 3)
data = obj.to_slice

Check warning on line 108 in spec/payload_spec.cr

View workflow job for this annotation

GitHub Actions / Ameba

Lint/UselessAssign

Useless assignment to variable `data`
Raw output
> data = obj.to_slice
  ^

obj.@data.should eq "foo".to_slice
end
end

it "#to_io should not affect position" do
io = IO::Memory.new("foo".to_slice)
io.rewind
describe "#to_io" do
it "should not affect position if io support pos/pos=" do
io = IO::Memory.new("foo".to_slice)
io.rewind

obj = MQTT::Protocol::IOPayload.new(io, 3)

dst = IO::Memory.new
obj.to_io(dst)

obj.@data.should be_nil
obj.@io.pos.should eq 0
end

it "should copy data if io doesn't support pos/pos=" do
io = NonPositionIO.new("foo".to_slice)

obj = MQTT::Protocol::IOPayload.new(io, 3)
obj = MQTT::Protocol::IOPayload.new(io, 3)

dst = IO::Memory.new
obj.to_io(dst)
dst = IO::Memory.new
obj.to_io(dst)

obj.@data.should be_nil
obj.@io.pos.should eq(0)
obj.@data.should eq "foo".to_slice
end
end
end
end

0 comments on commit 47a944e

Please sign in to comment.