Permalink
Browse files

Added some RSpec specs for the parser.

  • Loading branch information...
Twisol committed Dec 2, 2010
1 parent 3c22796 commit 367667d81718afc3cdc7b7f171fd43e1e0495cce
Showing with 59 additions and 14 deletions.
  1. +11 −11 Gemfile.lock
  2. +7 −0 Rakefile
  3. +1 −1 bin/lupin.rb
  4. +5 −1 lib/lupin/ast/literals.rb
  5. +1 −1 lupin.gemspec
  6. +34 −0 spec/parser_spec.rb
View
@@ -2,26 +2,26 @@ PATH
remote: .
specs:
lupin (0.0.1)
- citrus (~> 2.2.2)
+ citrus (~> 2.2.0)
GEM
specs:
citrus (2.2.2)
diff-lcs (1.1.2)
- rspec (2.1.0)
- rspec-core (~> 2.1.0)
- rspec-expectations (~> 2.1.0)
- rspec-mocks (~> 2.1.0)
- rspec-core (2.1.0)
- rspec-expectations (2.1.0)
+ rspec (2.2.0)
+ rspec-core (~> 2.2)
+ rspec-expectations (~> 2.2)
+ rspec-mocks (~> 2.2)
+ rspec-core (2.2.1)
+ rspec-expectations (2.2.0)
diff-lcs (~> 1.1.2)
- rspec-mocks (2.1.0)
+ rspec-mocks (2.2.0)
PLATFORMS
ruby
DEPENDENCIES
- bundler (~> 1.0.7)
- citrus (~> 2.2.2)
+ bundler (~> 1.0.0)
+ citrus (~> 2.2.0)
lupin!
- rspec (~> 2.1.0)
+ rspec (~> 2.2.0)
View
@@ -0,0 +1,7 @@
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.rspec_opts = ["--colour", "--format nested"]
+end
+task :default => :spec
+
View
@@ -3,6 +3,6 @@
gem "bundler"
require "bundler"
-Bundler.load
+Bundler.setup
# Eventually, a REPL will probably go here.
@@ -5,6 +5,10 @@ class Literal
def initialize (val)
@value = val
end
+
+ def == (literal)
+ @value == literal.value
+ end
end
class String < Literal
@@ -35,7 +39,7 @@ class LongString < Literal
class Number < Literal
def initialize (base, exponent)
- super(base * (10 ** exponent))
+ super(base.to_f * (10 ** exponent))
end
end
View
@@ -17,6 +17,6 @@ Gem::Specification.new do |s|
s.files = Dir['lib/**/*'].reject {|f| f =~ /\.rbc$/}
s.add_dependency 'citrus', '~> 2.2.0'
- s.add_development_dependency 'rspec', '~> 2.1.0'
+ s.add_development_dependency 'rspec', '~> 2.2.0'
s.add_development_dependency 'bundler', '~> 1.0.0'
end
View
@@ -0,0 +1,34 @@
+Bundler.setup
+require 'lupin'
+
+describe Lupin::Parser do
+ AST = Lupin::AST
+
+ def parse (*args, &blk)
+ Lupin::Parser.parse(*args, &blk)
+ end
+
+ def check (type, text, value)
+ m = parse(text, :root => type)
+ m.should_not == nil
+ m.should == text
+ m.value.should == value
+ end
+
+ it "matches numbers" do
+ check(:number, "10", AST::Number.new(10, 0))
+ check(:number, "10.", AST::Number.new(10, 0))
+ check(:number, "10e2", AST::Number.new(10, 2))
+ check(:number, "10e-1", AST::Number.new(10, -1))
+ check(:number, "10.e4", AST::Number.new(10, 4))
+ check(:number, ".5", AST::Number.new(0.5, 0))
+ check(:number, ".5e2", AST::Number.new(0.5, 2))
+ # Negative numbers are handled by the unary minus operator, not as a literal.
+ end
+
+ it "matches strings" do
+ check(:string, "'Hel\\\"lo.'", AST::String.new("Hel\"lo."))
+ check(:string, "\"Hel\\'lo.\"", AST::String.new("Hel'lo."))
+ check(:string, "[==[foo\\\"bar\\'baz\\r\\n]==]", AST::LongString.new("foo\\\"bar\\'baz\\r\\n"))
+ end
+end

0 comments on commit 367667d

Please sign in to comment.