spec/edn_spec.rb in edn-1.0.3 vs spec/edn_spec.rb in edn-1.0.5
- old
+ new
@@ -1,11 +1,41 @@
require 'spec_helper'
+require 'stringio'
describe EDN do
include RantlyHelpers
context "#read" do
+ it "reads from a stream" do
+ io = StringIO.new("123")
+ EDN.read(io).should == 123
+ end
+
+ it "reads mutiple values from a stream" do
+ io = StringIO.new("123 456 789")
+ EDN.read(io).should == 123
+ EDN.read(io).should == 456
+ EDN.read(io).should == 789
+ end
+
+ it "raises an exception on eof by default" do
+ expect { EDN.read('') }.to raise_error
+ end
+
+ it "allows you to specify an eof value" do
+ io = StringIO.new("123 456")
+ EDN.read(io, :my_eof).should == 123
+ EDN.read(io, :my_eof).should == 456
+ EDN.read(io, :my_eof).should == :my_eof
+ end
+
+ it "allows you to specify nil as an eof value" do
+ EDN.read("", nil).should == nil
+ end
+ end
+
+ context "reading data" do
it "reads single elements" do
EDN.read(%q{""}).should == ""
EDN.read("1").should == 1
EDN.read("3.14").should == 3.14
EDN.read("3.14M").should == BigDecimal("3.14")
@@ -18,34 +48,38 @@
EDN.read('false').should == false
EDN.read('nil').should == nil
EDN.read('\c').should == "c"
end
- it "should support M suffix without decimals" do
+ it "should support M suffix without decimals" do
EDN.read(123412341231212121241234.to_edn).should == 123412341231212121241234
EDN.read("123412341231212121241234M").should == 123412341231212121241234
end
- it "reads #inst tagged elements" do
- EDN.read('#inst "2012-09-10T16:16:03-04:00"').should == DateTime.new(2012, 9, 10, 16, 16, 3, '-04:00')
- end
-
it "reads vectors" do
EDN.read('[]').should == []
+ EDN.read('()').should be_a(Array)
EDN.read('[1]').should == [1]
EDN.read('["hello" 1 2]').should == ['hello', 1, 2]
EDN.read('[[1 [:hi]]]').should == [[1, [:hi]]]
end
+ it "reads tagged elements" do
+ EDN.read('#inst "2012-09-10T16:16:03-04:00"').should == DateTime.rfc3339("2012-09-10T16:16:03-04:00")
+ EDN.read('#uuid "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"').should == "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
+ end
+
it "reads lists" do
EDN.read('()').should == []
+ EDN.read('()').should be_a(EDN::Type::List)
EDN.read('(1)').should == [1]
EDN.read('("hello" 1 2)').should == ['hello', 1, 2]
EDN.read('((1 (:hi)))').should == [[1, [:hi]]]
end
it "reads maps" do
EDN.read('{}').should == {}
+ EDN.read('{}').should be_a(Hash)
EDN.read('{:a :b}').should == {:a => :b}
EDN.read('{:a 1, :b 2}').should == {:a => 1, :b => 2}
EDN.read('{:a {:b :c}}').should == {:a => {:b => :c}}
end