File tree 2 files changed +40
-2
lines changed
2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -24,13 +24,22 @@ def functype(funcname)
24
24
end
25
25
end
26
26
27
- base . class_eval do
27
+ # NOTE: Workaround for `defined?(super)` bug in ruby 1.9.2
28
+ # source: http://stackoverflow.com/a/11181685
29
+ # bug: https://bugs.ruby-lang.org/issues/6644
30
+ base . class_eval <<-RUBY
28
31
# TODO: deprecate
29
32
# Required when contracts are included in global scope
30
33
def Contract(*args)
31
- self . class . Contract ( *args )
34
+ if defined?(super)
35
+ super
36
+ else
37
+ self.class.Contract(*args)
38
+ end
32
39
end
40
+ RUBY
33
41
42
+ base . class_eval do
34
43
def functype ( funcname )
35
44
contracts = Engine . fetch_from ( self . class ) . decorated_methods_for ( :instance_methods , funcname )
36
45
if contracts . nil?
Original file line number Diff line number Diff line change @@ -717,4 +717,33 @@ def delim(match)
717
717
expect { c . double ( "asd" ) } . to raise_error
718
718
end
719
719
end
720
+
721
+ describe "classes with extended modules" do
722
+ let ( :klass ) do
723
+ m = Module . new do
724
+ include Contracts ::Core
725
+ end
726
+
727
+ Class . new do
728
+ include Contracts ::Core
729
+ extend m
730
+
731
+ Contract String => nil
732
+ def foo ( x )
733
+ end
734
+ end
735
+ end
736
+
737
+ it "is possible to define it" do
738
+ expect { klass } . not_to raise_error
739
+ end
740
+
741
+ it "works correctly with methods with passing contracts" do
742
+ expect { klass . new . foo ( "bar" ) } . not_to raise_error
743
+ end
744
+
745
+ it "works correctly with methods with passing contracts" do
746
+ expect { klass . new . foo ( 42 ) } . to raise_error ( ContractError , /Expected: String/ )
747
+ end
748
+ end
720
749
end
You can’t perform that action at this time.
0 commit comments