Skip to content
Browse files

Eval to matcher

  • Loading branch information...
1 parent 52589f5 commit deb3c6a830267e63959feead17779b7bd7638df7 @txus committed May 13, 2012
Showing with 39 additions and 10 deletions.
  1. +8 −8 lib/lambra/bootstrap.rb
  2. +9 −2 spec/compiler_spec.rb
  3. +1 −0 spec/custom.rb
  4. +21 −0 spec/custom/matchers/eval_to.rb
View
16 lib/lambra/bootstrap.rb
@@ -9,15 +9,15 @@ def call(*args)
end
end
-class GlobalScope < Hash
+module GlobalScope
def self.bootstrap
- scope = new
- scope[:println] = Function.new { |*args| puts *args }
- scope[:+] = Function.new { |*args| args.inject(:+) }
- scope[:-] = Function.new { |*args| args.inject(:-) }
- scope[:/] = Function.new { |a, b| a / b }
- scope[:*] = Function.new { |a, b| a * b }
- scope
+ {
+ :println => Function.new { |*args| puts *args },
+ :+ => Function.new { |*args| args.inject(:+) },
+ :- => Function.new { |*args| args.inject(:-) },
+ :/ => Function.new { |a, b| a / b },
+ :* => Function.new { |a, b| a * b },
+ }
end
end
View
11 spec/compiler_spec.rb
@@ -1,7 +1,14 @@
require 'spec_helper'
describe "Environment bootstrap" do
- describe 'println' do
- Lambra::CodeLoader.evaluate '(println "hello")'
+ it 'defines println' do
+ '(println "hello")'.should eval_to nil
+ end
+
+ it 'defines arithmetic functions' do
+ '(+ 8 4)'.should eval_to 12
+ '(- 8 4)'.should eval_to 4
+ '(* 8 4)'.should eval_to 32
+ '(/ 8 4)'.should eval_to 2
end
end
View
1 spec/custom.rb
@@ -1,5 +1,6 @@
require 'custom/runner/relates'
require 'custom/matchers/parse_as'
require 'custom/matchers/compile_as'
+require 'custom/matchers/eval_to'
require 'custom/utils/options'
require 'custom/utils/script'
View
21 spec/custom/matchers/eval_to.rb
@@ -0,0 +1,21 @@
+class EvalToMatcher
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def matches?(actual)
+ @actual = Lambra::CodeLoader.evaluate actual
+ @actual == @expected
+ end
+
+ def failure_message
+ ["Expected:\n#{@actual.inspect}\n",
+ "to evaluate to:\n#{@expected.inspect}"]
+ end
+end
+
+class Object
+ def eval_to(result)
+ EvalToMatcher.new result
+ end
+end

0 comments on commit deb3c6a

Please sign in to comment.
Something went wrong with that request. Please try again.