spec/string_spec.rb in bindata-0.9.3 vs spec/string_spec.rb in bindata-0.10.0
- old
+ new
@@ -20,18 +20,34 @@
lambda { BinData::String.new(params) }.should raise_error(ArgumentError)
end
end
describe BinData::String, "with deprecated parameters" do
- it "should substitude :read_length for :initial_length" do
- obj = BinData::String.new(:initial_length => 3)
- io = StringIO.new("abcdefghij")
- obj.read(io)
+ it "should substitude :trim_padding for :trim_value" do
+ obj = BinData::String.new(:trim_value => true)
+ obj.value = "abc\0"
obj.value.should == "abc"
end
end
+describe BinData::String, "when assigning" do
+ before(:each) do
+ @small = BinData::String.new(:length => 3, :pad_char => "A")
+ @large = BinData::String.new(:length => 5, :pad_char => "B")
+ end
+
+ it "should copy data from small to large" do
+ @large.value = @small
+ @large.value.should == "AAABB"
+ end
+
+ it "should copy data from large to small" do
+ @small.value = @large
+ @small.value.should == "BBB"
+ end
+end
+
describe BinData::String, "with :read_length" do
before(:each) do
@str = BinData::String.new(:read_length => 5)
end
@@ -39,21 +55,20 @@
@str.num_bytes.should == 0
@str.value.should == ""
end
it "should read :read_length bytes" do
- io = StringIO.new("abcdefghij")
- @str.read(io)
+ @str.read("abcdefghij")
@str.value.should == "abcde"
end
it "should remember :read_length after value is cleared" do
@str.value = "abc"
@str.num_bytes.should == 3
@str.clear
- io = StringIO.new("abcdefghij")
- @str.read(io)
+
+ @str.read("abcdefghij")
@str.value.should == "abcde"
end
end
describe BinData::String, "with :length" do
@@ -73,12 +88,11 @@
@str.value = "abcdefghij"
@str.num_bytes.should == 5
end
it "should read :length bytes" do
- io = StringIO.new("abcdefghij")
- @str.read(io)
+ @str.read("abcdefghij")
@str.value.should == "abcde"
end
it "should pad values less than :length" do
@str.value = "abc"
@@ -111,21 +125,21 @@
@str.read(io)
io.pos.should == 5
end
it "should forget :initial_value after reading" do
- io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
- @str.read(io)
+ @str.read("ABCDEFGHIJKLMNOPQRST")
@str.num_bytes.should == 5
@str.value.should == "ABCDE"
end
end
describe BinData::String, "with :read_length and :value" do
before(:each) do
@str = BinData::String.new(:read_length => 5, :value => "abcdefghij")
+ @str.expose_methods_for_testing
end
it "should not be affected by :read_length before value is read" do
@str.num_bytes.should == 10
@str.value.should == "abcdefghij"
@@ -136,20 +150,17 @@
@str.read(io)
io.pos.should == 5
end
it "should not be affected by :read_length after reading" do
- io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
- @str.read(io)
+ @str.read("ABCDEFGHIJKLMNOPQRST")
@str.num_bytes.should == 10
@str.value.should == "abcdefghij"
end
it "should return read value before calling done_read" do
- io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
-
- @str.do_read(BinData::IO.new(io))
+ @str.do_read(BinData::IO.new("ABCDEFGHIJKLMNOPQRST"))
@str.value.should == "ABCDE"
@str.done_read
@str.value.should == "abcdefghij"
end
@@ -191,39 +202,39 @@
params = {:length => 5, :pad_char => "RR"}
lambda { BinData::String.new(params) }.should raise_error(ArgumentError)
end
end
-describe BinData::String, "with :trim_value" do
+describe BinData::String, "with :trim_padding" do
it "set false is the default" do
str1 = BinData::String.new(:length => 5)
- str2 = BinData::String.new(:length => 5, :trim_value => false)
+ str2 = BinData::String.new(:length => 5, :trim_padding => false)
str1.value = "abc"
str2.value = "abc"
str1.value.should == "abc\0\0"
str2.value.should == "abc\0\0"
end
it "should trim the value" do
- str = BinData::String.new(:pad_char => 'R', :trim_value => true)
+ str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
str.value = "abcRR"
str.value.should == "abc"
end
it "should not affect num_bytes" do
- str = BinData::String.new(:pad_char => 'R', :trim_value => true)
+ str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
str.value = "abcRR"
str.num_bytes.should == 5
end
it "should trim if last char is :pad_char" do
- str = BinData::String.new(:pad_char => 'R', :trim_value => true)
+ str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
str.value = "abcRR"
str.value.should == "abc"
end
it "should not trim if value contains :pad_char not at the end" do
- str = BinData::String.new(:pad_char => 'R', :trim_value => true)
+ str = BinData::String.new(:pad_char => 'R', :trim_padding => true)
str.value = "abcRRde"
str.value.should == "abcRRde"
end
end