Permalink
Browse files

Allow use of objects for specs

  • Loading branch information...
1 parent 504eade commit 713d5a73a77be4ebe44a468c33ab4a07c26a12ee @namelessjon committed May 13, 2012
Showing with 22 additions and 1 deletion.
  1. +7 −1 lib/exalted_math/node/named.rb
  2. +15 −0 spec/node_spec.rb
@@ -13,7 +13,13 @@ def constant?
end
def value(context={})
- context.fetch(name) { raise ContextError, name }
+ if context.respond_to?(:fetch)
+ context.fetch(name) { raise ContextError, name }
+ elsif context.respond_to?(name)
+ context.public_send(name)
+ else
+ raise ContextError, name
+ end
end
def ==(o)
View
@@ -5,6 +5,12 @@
include ExaltedMath
+class Context
+ attr_reader :foo
+ def initialize(foo)
+ @foo = foo
+ end
+end
describe "ExaltedMath::Node" do
before do
@@ -25,6 +31,7 @@
@min = Node::Minimum.new([@three, @seven], 1)
@max = Node::Maximum.new([@three, @seven], 1 )
@context = { 'foo' => 3, 'bar' => 4 }
+ @class_context = Context.new(3)
end
it "a number is constant" do
@three.should.be.constant
@@ -117,6 +124,14 @@
@bar.value(@context).should == @context['bar']
end
+ it "allows the use of an object in the context" do
+ @foo.value(@class_context).should == @class_context.foo
+ end
+
+ it "raises an error on a missing context" do
+ lambda { @bar.value(@class_context) }.should.raise(ExaltedMath::ContextError)
+ end
+
it "the value of an add is the sum of the two children" do
@add.value(@context).should == 10
end

0 comments on commit 713d5a7

Please sign in to comment.