Skip to content

Commit

Permalink
Added some RSpec specs for the parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
Twisol committed Dec 2, 2010
1 parent 3c22796 commit 367667d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
22 changes: 11 additions & 11 deletions Gemfile.lock
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions Rakefile
@@ -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

2 changes: 1 addition & 1 deletion bin/lupin.rb
Expand Up @@ -3,6 +3,6 @@
gem "bundler"
require "bundler"

Bundler.load
Bundler.setup

# Eventually, a REPL will probably go here.
6 changes: 5 additions & 1 deletion lib/lupin/ast/literals.rb
Expand Up @@ -5,6 +5,10 @@ class Literal
def initialize (val)
@value = val
end

def == (literal)
@value == literal.value
end
end

class String < Literal
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lupin.gemspec
Expand Up @@ -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
34 changes: 34 additions & 0 deletions spec/parser_spec.rb
@@ -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.