Sha256: a8e8f35b30a901a14c532bd136cb3842afe1bd83c0a1a3d38445d41fa951ad9a

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

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
    end

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

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

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

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/contracts-0.16.0/spec/methods_spec.rb
contracts-0.16.0 spec/methods_spec.rb
contracts-0.15.0 spec/methods_spec.rb