File: methods_spec.rb

package info (click to toggle)
ruby-contracts 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 624 kB
  • sloc: ruby: 3,805; makefile: 4; sh: 2
file content (54 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
RSpec.describe "Contracts:" do
  describe "method called with blocks" do
    module FuncTest
      include Contracts::Core
      include Contracts::Builtin

      Contract Func[Num=>Num] => nil
      def foo(&blk)
        _ = blk.call(2)
        nil
      end

      Contract Num, Func[Num=>Num] => nil
      def foo2(a, &blk)
        _ = blk.call(2)
        nil
      end

      Contract Func[Num=>Num] => nil
      def bar(blk)
        _ = blk.call(2)
        nil
      end

      Contract Num, Func[Num=>Num] => nil
      def bar2(a, blk)
        _ = blk.call(2)
        nil
      end
    end

    def obj
      Object.new.tap do |o|
        o.extend(FuncTest)
      end
    end

    it "should enforce return value inside block with no other parameter" do
      expect { obj.foo(&:to_s) }.to raise_error ReturnContractError
    end

    it "should enforce return value inside block with other parameter" do
      expect { obj.foo2(2) { |x| x.to_s } }.to raise_error ReturnContractError
    end

    it "should enforce return value inside lambda with no other parameter" do
      expect { obj.bar lambda { |x| x.to_s } }.to raise_error ReturnContractError
    end

    it "should enforce return value inside lambda with other parameter" do
      expect { obj.bar2(2, lambda { |x| x.to_s }) }.to raise_error ReturnContractError
    end
  end
end