Permalink
Browse files

Fix for issue #10: Failing for methods inside a module

  • Loading branch information...
egonSchiele committed Jul 3, 2012
1 parent ee9957d commit 891dd9a47b0ee3ce0b58fdeec346872d81332e76
Showing with 25 additions and 8 deletions.
  1. +1 −1 VERSION
  2. +1 −1 contracts.gemspec
  3. +2 −2 lib/contracts.rb
  4. +1 −4 spec/contracts_specs.rb
  5. +20 −0 spec/module_specs.rb
View
@@ -1 +1 @@
-0.1.1
+0.1.2
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "contracts"
- s.version = "0.1.1"
+ s.version = "0.1.2"
s.date = "2012-06-07"
s.summary = "Contracts for Ruby."
s.description = "This library provides contracts for Ruby. Contracts let you clearly express how your code behaves, and free you from writing tons of boilerplate, defensive code."
View
@@ -113,7 +113,7 @@ def self.valid?(arg, contract)
validate_hash(arg, contract)
when Contracts::Args
valid? arg, contract.contract
- when Func
+ when Contracts::Func
arg.is_a?(Method) || arg.is_a?(Proc)
else
if contract.respond_to? :valid?
@@ -140,7 +140,7 @@ def call_with(this, *args, &blk)
# contracts on methods
contracts.each_with_index do |contract, i|
- if contract.is_a? Func
+ if contract.is_a? Contracts::Func
args[i] = Contract.new(@klass, args[i], *contract.contracts)
end
end
View
@@ -137,7 +137,6 @@
describe "failure callbacks" do
before :each do
- @old = (::Contract).method(:failure_callback)
def (::Contract).failure_callback(data)
false
end
@@ -150,14 +149,13 @@ def (::Contract).failure_callback(data)
after :each do
def (::Contract).failure_callback(data)
- @old.bind(self).call(data)
+ raise failure_msg(data)
end
end
end
describe "success callbacks" do
before :each do
- @old = (::Contract).method(:success_callback)
def (::Contract).success_callback(data)
false
end
@@ -170,7 +168,6 @@ def (::Contract).success_callback(data)
after :each do
def (::Contract).success_callback(data)
- @old.bind(self).call(data)
end
end
end
View
@@ -0,0 +1,20 @@
+require 'lib/contracts'
+require 'rubygems'
+require 'ruby-debug'
+module Mod
+ include Contracts
+ Contract Num => Num
+ def self.a_module_method a
+ a + 1
+ end
+end
+
+describe "module methods" do
+ it "should pass for correct input" do
+ expect { Mod.a_module_method(2) }.to_not raise_error
+ end
+
+ it "should fail for incorrect input" do
+ expect { Mod.a_module_method("bad") }.to raise_error
+ end
+end

0 comments on commit 891dd9a

Please sign in to comment.