Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bignum rspec #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/edn/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def allows_metadata?
end
end

module Bignum
def to_edn
self.to_s + 'M'
end
end
# module Bignum
# def to_edn
# self.to_s + 'M'
# end
# end

module BigDecimal
def to_edn
Expand Down Expand Up @@ -94,7 +94,7 @@ def to_edn
end

Numeric.send(:include, EDN::CoreExt::Unquoted)
Bignum.send(:include, EDN::CoreExt::Bignum)
#Bignum.send(:include, EDN::CoreExt::Bignum)
BigDecimal.send(:include, EDN::CoreExt::BigDecimal)
TrueClass.send(:include, EDN::CoreExt::Unquoted)
FalseClass.send(:include, EDN::CoreExt::Unquoted)
Expand Down
84 changes: 42 additions & 42 deletions spec/edn/char_stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,110 @@

describe EDN::CharStream do
it "reads a stream in order" do
s = EDN::CharStream.new(io_for("abc"))
s.current.should == "a"
s.advance.should == "b"
s.advance.should == "c"
s.advance.should == :eof
s.current.should == :eof
s = EDN::CharStream.new(io_for('abc'))
expect(s.current).to eq('a')
expect(s.advance).to eq('b')
expect(s.advance).to eq('c')
expect(s.advance).to eq(:eof)
expect(s.current).to eq(:eof)
end

it "keeps returning the current until your advance" do
s = EDN::CharStream.new(io_for("abc"))
s.current.should == "a"
s.current.should == "a"
s.advance.should == "b"
s = EDN::CharStream.new(io_for('abc'))
expect(s.current).to eq('a')
expect(s.current).to eq('a')
expect(s.advance).to eq('b')
end

it "knows if the current char is a digit" do
s = EDN::CharStream.new(io_for("4f"))
s.digit?.should be_truthy
s = EDN::CharStream.new(io_for('4f'))
expect(s.digit?).to be_truthy
s.advance
s.digit?.should be_falsey
expect(s.digit?).to be_falsey
end

it "knows if the current char is an alpha" do
s = EDN::CharStream.new(io_for("a9"))
s.alpha?.should be_truthy
s = EDN::CharStream.new(io_for('a9'))
expect(s.alpha?).to be_truthy
s.advance
s.alpha?.should be_falsey
expect(s.alpha?).to be_falsey
end

it "knows if the current char is whitespace" do
s = EDN::CharStream.new(io_for("a b\nc\td,"))
s.ws?.should be_falsey # a
expect(s.ws?).to be_falsey # a

s.advance
s.ws?.should be_truthy # " "
expect(s.ws?).to be_truthy # " "

s.advance
s.ws?.should be_falsey # b
expect(s.ws?).to be_falsey # b

s.advance
s.ws?.should be_truthy # \n
expect(s.ws?).to be_truthy # \n

s.advance
s.ws?.should be_falsey # c
expect(s.ws?).to be_falsey # c

s.advance
s.ws?.should be_truthy # \t
expect(s.ws?).to be_truthy # \t

s.advance
s.ws?.should be_falsey # d
expect(s.ws?).to be_falsey # d

s.advance
s.ws?.should be_truthy # ,
expect(s.ws?).to be_truthy # ,
end

it "knows if the current char is a newline" do
s = EDN::CharStream.new(io_for("a\nb\rc"))
s.newline?.should be_falsey # a
expect(s.newline?).to be_falsey # a

s.advance
s.newline?.should be_truthy # \n
expect(s.newline?).to be_truthy # \n

s.advance
s.newline?.should be_falsey # b
expect(s.newline?).to be_falsey # b

s.advance
s.newline?.should be_truthy # \r
expect(s.newline?).to be_truthy # \r

s.advance
s.newline?.should be_falsey # c
expect(s.newline?).to be_falsey # c
end

it "knows if it is at the eof" do
s = EDN::CharStream.new(io_for("abc"))
s.eof?.should be_falsey # a
s = EDN::CharStream.new(io_for('abc'))
expect(s.eof?).to be_falsey # a
s.advance
s.eof?.should be_falsey # b
expect(s.eof?).to be_falsey # b
s.advance
s.eof?.should be_falsey # c
expect(s.eof?).to be_falsey # c
s.advance
s.eof?.should be_truthy
expect(s.eof?).to be_truthy
end

it "knows how to skip past a char" do
s = EDN::CharStream.new(io_for("abc"))
s.skip_past("a").should == "a"
s.current.should == "b"
s = EDN::CharStream.new(io_for('abc'))
expect(s.skip_past('a')).to eq('a')
expect(s.current).to eq('b')
end

it "knows how not to skip a char" do
s = EDN::CharStream.new(io_for("abc"))
s.skip_past("X").should be_nil
s = EDN::CharStream.new(io_for('abc'))
expect(s.skip_past('X')).to be_nil
end

it "knows how skip to the end of a line" do
s = EDN::CharStream.new(io_for("abc\ndef"))
s.skip_to_eol
s.current.should == "\n"
s.advance.should == "d"
expect(s.current).to eq("\n")
expect(s.advance).to eq('d')
end

it "knows how skip whitespace" do
s = EDN::CharStream.new(io_for(" \n \t,,,,abc"))
s.skip_ws
s.current.should == "a"
expect(s.current).to eq('a')
end
end
20 changes: 10 additions & 10 deletions spec/edn/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
describe EDN do
describe "metadata" do
it "reads metadata, which does not change the element's equality" do
EDN.read('[1 2 3]').should == EDN.read('^{:doc "My vec"} [1 2 3]')
expect(EDN.read('[1 2 3]')).to eq(EDN.read('^{:doc "My vec"} [1 2 3]'))
end

it "reads metadata recursively from right to left" do
element = EDN.read('^String ^:foo ^{:foo false :tag Boolean :bar 2} [1 2]')
element.should == [1, 2]
element.metadata.should == {:tag => ~"String", :foo => true, :bar => 2}
expect(element).to eq([1, 2])
expect(element.metadata).to eq({:tag => ~"String", :foo => true, :bar => 2})
end

it "writes metadata" do
element = EDN.read('^{:doc "My vec"} [1 2 3]')
element.to_edn.should == '^{:doc "My vec"} [1 2 3]'
expect(element.to_edn).to eq('^{:doc "My vec"} [1 2 3]')
end

it "only writes metadata for elements that can have it" do
Expand All @@ -24,13 +24,13 @@
o
}

apply_metadata[[1, 2]].to_edn.should == '^{:foo 1} [1 2]'
apply_metadata[~[1, 2]].to_edn.should == '^{:foo 1} (1 2)'
apply_metadata[{1 => 2}].to_edn.should == '^{:foo 1} {1 2}'
apply_metadata[Set.new([1, 2])].to_edn.should == '^{:foo 1} #{1 2}'
apply_metadata[~"bar"].to_edn.should == '^{:foo 1} bar'
expect(apply_metadata[[1, 2]].to_edn).to eq('^{:foo 1} [1 2]')
expect(apply_metadata[~[1, 2]].to_edn).to eq('^{:foo 1} (1 2)')
expect(apply_metadata[{1 => 2}].to_edn).to eq('^{:foo 1} {1 2}')
expect(apply_metadata[Set.new([1, 2])].to_edn).to eq('^{:foo 1} #{1 2}')
expect(apply_metadata[~'bar'].to_edn).to eq('^{:foo 1} bar')

apply_metadata["bar"].to_edn.should == '"bar"'
expect(apply_metadata['bar'].to_edn).to eq('"bar"')

# Cannot extend symbols, booleans, and nil, so no test for them.
end
Expand Down
44 changes: 22 additions & 22 deletions spec/edn/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,100 +7,100 @@

it "can contain comments" do
edn = ";; This is some sample data\n[1 2 ;; the first two values\n3]"
EDN.read(edn).should == [1, 2, 3]
expect(EDN.read(edn)).to eq([1, 2, 3])
end

it "can discard using the discard reader macro" do
edn = "[1 2 #_3 {:foo #_bar :baz}]"
EDN.read(edn).should == [1, 2, {:foo => :baz}]
edn = '[1 2 #_3 {:foo #_bar :baz}]'
expect(EDN.read(edn)).to eq([1, 2, {:foo => :baz}])
end

context "element" do
it "should consume metadata with the element" do
x = EDN.read('^{:doc "test"} [1 2]')
x.should == [1, 2]
x.metadata.should == {doc: "test"}
expect(x).to eq([1, 2])
expect(x.metadata).to eq({doc: 'test'})
end
end

context "integer" do
it "should consume integers" do
rant(RantlyHelpers::INTEGER).each do |int|
(EDN.read int.to_s).should == int.to_i
expect((EDN.read int.to_s)).to eq(int.to_i)
end
end

it "should consume integers prefixed with a +" do
rant(RantlyHelpers::INTEGER).each do |int|
(EDN.read "+#{int.to_i.abs.to_s}").should == int.to_i.abs
expect((EDN.read "+#{int.to_i.abs.to_s}")).to eq(int.to_i.abs)
end
end
end

context "float" do
it "should consume simple floats" do
rant(RantlyHelpers::FLOAT).each do |float|
EDN.read(float.to_s).should == float.to_f
expect(EDN.read(float.to_s)).to eq(float.to_f)
end
end

it "should consume floats with exponents" do
rant(RantlyHelpers::FLOAT_WITH_EXP).each do |float|
EDN.read(float.to_s).should == float.to_f
expect(EDN.read(float.to_s)).to eq(float.to_f)
end
end

it "should consume floats prefixed with a +" do
rant(RantlyHelpers::FLOAT).each do |float|
EDN.read("+#{float.to_f.abs.to_s}").should == float.to_f.abs
expect(EDN.read("+#{float.to_f.abs.to_s}")).to eq(float.to_f.abs)
end
end
end

context "symbol" do
context "special cases" do
it "should consume '/'" do
EDN.read('/').should == EDN::Type::Symbol.new(:"/")
expect(EDN.read('/')).to eq(EDN::Type::Symbol.new(:'/'))
end

it "should consume '.'" do
EDN.read('.').should == EDN::Type::Symbol.new(:".")
expect(EDN.read('.')).to eq(EDN::Type::Symbol.new(:'.'))
end

it "should consume '-'" do
EDN.read('-').should == EDN::Type::Symbol.new(:"-")
expect(EDN.read('-')).to eq(EDN::Type::Symbol.new(:'-'))
end
end
end

context "keyword" do
it "should consume any keywords" do
rant(RantlyHelpers::SYMBOL).each do |symbol|
EDN.read(":#{symbol}").should == symbol.to_sym
expect(EDN.read(":#{symbol}")).to eq(symbol.to_sym)
end
end
end

context "string" do
it "should consume any string" do
rant(RantlyHelpers::RUBY_STRING).each do |string|
EDN.read(string.to_edn).should == string
expect(EDN.read(string.to_edn)).to eq(string)
end
end
end

context "character" do
it "should consume any character" do
rant(RantlyHelpers::RUBY_CHAR).each do |char|
EDN.read(char.to_edn).should == char
expect(EDN.read(char.to_edn)).to eq(char)
end
end
end

context "vector" do
it "should consume an empty vector" do
EDN.read('[]').should == []
EDN.read('[ ]').should == []
expect(EDN.read('[]')).to eq([])
expect(EDN.read('[ ]')).to eq([])
end

it "should consume vectors of mixed elements" do
Expand All @@ -112,8 +112,8 @@

context "list" do
it "should consume an empty list" do
EDN.read('()').should == []
EDN.read('( )').should == []
expect(EDN.read('()')).to eq([])
expect(EDN.read('( )')).to eq([])
end

it "should consume lists of mixed elements" do
Expand All @@ -125,8 +125,8 @@

context "set" do
it "should consume an empty set" do
EDN.read('#{}').should == Set.new
EDN.read('#{ }').should == Set.new
expect(EDN.read('#{}')).to eq(Set.new)
expect(EDN.read('#{ }')).to eq(Set.new)
end

it "should consume sets of mixed elements" do
Expand Down
16 changes: 8 additions & 8 deletions spec/edn/reader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
require 'spec_helper'

describe EDN::Reader do
let(:reader) { EDN::Reader.new("[1 2] 3 :a {:b c} ") }
let(:reader) { EDN::Reader.new('[1 2] 3 :a {:b c} ') }

it "should read each value" do
reader.read.should == [1, 2]
reader.read.should == 3
reader.read.should == :a
reader.read.should == {:b => ~"c"}
expect(reader.read).to eq([1, 2])
expect(reader.read).to eq(3)
expect(reader.read).to eq(:a)
expect(reader.read).to eq({:b => ~'c'})
end

it "should respond to each" do
reader.each do |element|
element.should_not be_nil
expect(element).to_not be_nil
end
end

it "returns a special end of file value if asked" do
4.times { reader.read(:the_end).should_not == :the_end }
reader.read(:no_more).should == :no_more
4.times { expect(reader.read(:the_end)).to_not eq(:the_end) }
expect(reader.read(:no_more)).to eq(:no_more)
end
end
Loading