contracts 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/CHANGELOG.markdown +9 -1
 - data/TUTORIAL.md +51 -34
 - data/lib/contracts.rb +10 -38
 - data/lib/contracts/builtin_contracts.rb +16 -0
 - data/lib/contracts/call_with.rb +3 -2
 - data/lib/contracts/core.rb +45 -0
 - data/lib/contracts/validators.rb +6 -0
 - data/lib/contracts/version.rb +1 -1
 - data/spec/builtin_contracts_spec.rb +36 -4
 - data/spec/contracts_spec.rb +12 -2
 - data/spec/fixtures/fixtures.rb +103 -84
 - data/spec/module_spec.rb +2 -2
 - data/spec/override_validators_spec.rb +4 -4
 - data/spec/ruby_version_specific/contracts_spec_1.9.rb +2 -2
 - data/spec/ruby_version_specific/contracts_spec_2.0.rb +2 -2
 - data/spec/ruby_version_specific/contracts_spec_2.1.rb +4 -4
 - data/spec/validators_spec.rb +25 -0
 - metadata +5 -4
 
    
        data/spec/module_spec.rb
    CHANGED
    
    
| 
         @@ -19,7 +19,7 @@ RSpec.describe Contract do 
     | 
|
| 
       19 
19 
     | 
    
         
             
                  end
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
21 
     | 
    
         
             
                  klass = Class.new do
         
     | 
| 
       22 
     | 
    
         
            -
                    include Contracts
         
     | 
| 
      
 22 
     | 
    
         
            +
                    include Contracts::Core
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
24 
     | 
    
         
             
                    Contract ({ :a => Contracts::Num, :b => String }) => nil
         
     | 
| 
       25 
25 
     | 
    
         
             
                    def something(opts)
         
     | 
| 
         @@ -70,7 +70,7 @@ RSpec.describe Contract do 
     | 
|
| 
       70 
70 
     | 
    
         
             
                  end
         
     | 
| 
       71 
71 
     | 
    
         | 
| 
       72 
72 
     | 
    
         
             
                  klass = Class.new do
         
     | 
| 
       73 
     | 
    
         
            -
                    include Contracts
         
     | 
| 
      
 73 
     | 
    
         
            +
                    include Contracts::Core
         
     | 
| 
       74 
74 
     | 
    
         | 
| 
       75 
75 
     | 
    
         
             
                    Contract stateful_contract[Contracts::Num] => Contracts::Num
         
     | 
| 
       76 
76 
     | 
    
         
             
                    def only_three_times(x)
         
     | 
| 
         @@ -103,7 +103,7 @@ RSpec.describe Contract do 
     | 
|
| 
       103 
103 
     | 
    
         
             
                  end
         
     | 
| 
       104 
104 
     | 
    
         | 
| 
       105 
105 
     | 
    
         
             
                  klass = Class.new do
         
     | 
| 
       106 
     | 
    
         
            -
                    include Contracts
         
     | 
| 
      
 106 
     | 
    
         
            +
                    include Contracts::Core
         
     | 
| 
       107 
107 
     | 
    
         | 
| 
       108 
108 
     | 
    
         
             
                    Contract String => String
         
     | 
| 
       109 
109 
     | 
    
         
             
                    def greet(name)
         
     | 
| 
         @@ -135,7 +135,7 @@ RSpec.describe Contract do 
     | 
|
| 
       135 
135 
     | 
    
         
             
                  end
         
     | 
| 
       136 
136 
     | 
    
         | 
| 
       137 
137 
     | 
    
         
             
                  klass = Class.new do
         
     | 
| 
       138 
     | 
    
         
            -
                    include Contracts
         
     | 
| 
      
 138 
     | 
    
         
            +
                    include Contracts::Core
         
     | 
| 
       139 
139 
     | 
    
         | 
| 
       140 
140 
     | 
    
         
             
                    Contract 1, Contracts::Num => Contracts::Num
         
     | 
| 
       141 
141 
     | 
    
         
             
                    def gcd(_, b)
         
     | 
| 
         @@ -1,11 +1,11 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class GenericExample
         
     | 
| 
       2 
     | 
    
         
            -
              Contract Args[String], Num => ArrayOf[String]
         
     | 
| 
      
 2 
     | 
    
         
            +
              Contract C::Args[String], C::Num => C::ArrayOf[String]
         
     | 
| 
       3 
3 
     | 
    
         
             
              def splat_then_arg(*vals, n)
         
     | 
| 
       4 
4 
     | 
    
         
             
                vals.map { |v| v * n }
         
     | 
| 
       5 
5 
     | 
    
         
             
              end
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
7 
     | 
    
         
             
              if ruby_version <= 1.9
         
     | 
| 
       8 
     | 
    
         
            -
                Contract ({:foo => Nat}) => nil
         
     | 
| 
      
 8 
     | 
    
         
            +
                Contract ({:foo => C::Nat}) => nil
         
     | 
| 
       9 
9 
     | 
    
         
             
                def nat_test_with_kwarg(a_hash)
         
     | 
| 
       10 
10 
     | 
    
         
             
                end
         
     | 
| 
       11 
11 
     | 
    
         
             
              end
         
     | 
| 
         @@ -1,10 +1,10 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class GenericExample
         
     | 
| 
       2 
     | 
    
         
            -
              Contract Args[String], { repeat: Maybe[Num] } => ArrayOf[String]
         
     | 
| 
      
 2 
     | 
    
         
            +
              Contract C::Args[String], { repeat: C::Maybe[C::Num] } => C::ArrayOf[String]
         
     | 
| 
       3 
3 
     | 
    
         
             
              def splat_then_optional_named(*vals, repeat: 2)
         
     | 
| 
       4 
4 
     | 
    
         
             
                vals.map { |v| v * repeat }
         
     | 
| 
       5 
5 
     | 
    
         
             
              end
         
     | 
| 
       6 
6 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
              Contract ({foo: Nat}) => nil
         
     | 
| 
      
 7 
     | 
    
         
            +
              Contract ({foo: C::Nat}) => nil
         
     | 
| 
       8 
8 
     | 
    
         
             
              def nat_test_with_kwarg(foo: 10)
         
     | 
| 
       9 
9 
     | 
    
         
             
              end
         
     | 
| 
       10 
10 
     | 
    
         
             
            end
         
     | 
| 
         @@ -1,6 +1,6 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class GenericExample
         
     | 
| 
       2 
     | 
    
         
            -
              Contract String, Bool, Args[Symbol], Float, KeywordArgs[e: Range, f: Optional[Num]], Proc =>
         
     | 
| 
       3 
     | 
    
         
            -
                       [Proc, Hash, Maybe[Num], Range, Float, ArrayOf[Symbol], Bool, String]
         
     | 
| 
      
 2 
     | 
    
         
            +
              Contract String, C::Bool, C::Args[Symbol], Float, C::KeywordArgs[e: Range, f: C::Optional[C::Num], g: Symbol], Proc =>
         
     | 
| 
      
 3 
     | 
    
         
            +
                       [Proc, Hash, C::Maybe[C::Num], Range, Float, C::ArrayOf[Symbol], C::Bool, String]
         
     | 
| 
       4 
4 
     | 
    
         
             
              def complicated(a, b = true, *c, d, e:, f:2, **g, &h)
         
     | 
| 
       5 
5 
     | 
    
         
             
                h.call [h, g, f, e, d, c, b, a]
         
     | 
| 
       6 
6 
     | 
    
         
             
              end
         
     | 
| 
         @@ -15,7 +15,7 @@ RSpec.describe "Contracts:" do 
     | 
|
| 
       15 
15 
     | 
    
         
             
                describe "really complicated method signature" do
         
     | 
| 
       16 
16 
     | 
    
         
             
                  it "should work with default named args used" do
         
     | 
| 
       17 
17 
     | 
    
         
             
                    expect do
         
     | 
| 
       18 
     | 
    
         
            -
                      @o.complicated("a", false, :b, 2.0, e: (1..5)) { |x| x }
         
     | 
| 
      
 18 
     | 
    
         
            +
                      @o.complicated("a", false, :b, 2.0, e: (1..5), g: :d) { |x| x }
         
     | 
| 
       19 
19 
     | 
    
         
             
                    end.to_not raise_error
         
     | 
| 
       20 
20 
     | 
    
         
             
                  end
         
     | 
| 
       21 
21 
     | 
    
         | 
| 
         @@ -56,7 +56,7 @@ RSpec.describe "Contracts:" do 
     | 
|
| 
       56 
56 
     | 
    
         
             
                      @o.complicated("a", true, :b, :c, 2.0, e: (1..5), f: nil, g: :d) do |x|
         
     | 
| 
       57 
57 
     | 
    
         
             
                        x
         
     | 
| 
       58 
58 
     | 
    
         
             
                      end
         
     | 
| 
       59 
     | 
    
         
            -
                    end.to raise_error(ContractError, /Expected: \(KeywordArgs\[{:e=>Range, :f=>Optional\[Num\]}\]\)/)
         
     | 
| 
      
 59 
     | 
    
         
            +
                    end.to raise_error(ContractError, /Expected: \(KeywordArgs\[{:e=>Range, :f=>Optional\[Num\], :g=>Symbol}\]\)/)
         
     | 
| 
       60 
60 
     | 
    
         
             
                  end
         
     | 
| 
       61 
61 
     | 
    
         
             
                end
         
     | 
| 
       62 
62 
     | 
    
         
             
              end
         
     | 
| 
         @@ -0,0 +1,25 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require "spec_helper"
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module Contracts
         
     | 
| 
      
 4 
     | 
    
         
            +
              RSpec.describe "range contract validator" do
         
     | 
| 
      
 5 
     | 
    
         
            +
                subject(:o) { GenericExample.new }
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                it "passes when value is in range" do
         
     | 
| 
      
 8 
     | 
    
         
            +
                  expect do
         
     | 
| 
      
 9 
     | 
    
         
            +
                    o.method_with_range_contract(5)
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end.not_to raise_error(ContractError)
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                it "fails when value is not in range" do
         
     | 
| 
      
 14 
     | 
    
         
            +
                  expect do
         
     | 
| 
      
 15 
     | 
    
         
            +
                    o.method_with_range_contract(300)
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end.to raise_error(ContractError, /Expected: 1\.\.10/)
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                it "fails when value is incorrect" do
         
     | 
| 
      
 20 
     | 
    
         
            +
                  expect do
         
     | 
| 
      
 21 
     | 
    
         
            +
                    o.method_with_range_contract("hello world")
         
     | 
| 
      
 22 
     | 
    
         
            +
                  end.to raise_error(ContractError, /Expected: 1\.\.10.*Actual: "hello world"/m)
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
              end
         
     | 
| 
      
 25 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: contracts
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.11.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Aditya Bhargava
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2015-07- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2015-07-30 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies: []
         
     | 
| 
       13 
13 
     | 
    
         
             
            description: This library provides contracts for Ruby. Contracts let you clearly express
         
     | 
| 
       14 
14 
     | 
    
         
             
              how your code behaves, and free you from writing tons of boilerplate, defensive
         
     | 
| 
         @@ -37,6 +37,7 @@ files: 
     | 
|
| 
       37 
37 
     | 
    
         
             
            - lib/contracts.rb
         
     | 
| 
       38 
38 
     | 
    
         
             
            - lib/contracts/builtin_contracts.rb
         
     | 
| 
       39 
39 
     | 
    
         
             
            - lib/contracts/call_with.rb
         
     | 
| 
      
 40 
     | 
    
         
            +
            - lib/contracts/core.rb
         
     | 
| 
       40 
41 
     | 
    
         
             
            - lib/contracts/decorators.rb
         
     | 
| 
       41 
42 
     | 
    
         
             
            - lib/contracts/engine.rb
         
     | 
| 
       42 
43 
     | 
    
         
             
            - lib/contracts/engine/base.rb
         
     | 
| 
         @@ -63,6 +64,7 @@ files: 
     | 
|
| 
       63 
64 
     | 
    
         
             
            - spec/spec_helper.rb
         
     | 
| 
       64 
65 
     | 
    
         
             
            - spec/support.rb
         
     | 
| 
       65 
66 
     | 
    
         
             
            - spec/support_spec.rb
         
     | 
| 
      
 67 
     | 
    
         
            +
            - spec/validators_spec.rb
         
     | 
| 
       66 
68 
     | 
    
         
             
            homepage: http://github.com/egonSchiele/contracts.ruby
         
     | 
| 
       67 
69 
     | 
    
         
             
            licenses: []
         
     | 
| 
       68 
70 
     | 
    
         
             
            metadata: {}
         
     | 
| 
         @@ -82,9 +84,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       82 
84 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       83 
85 
     | 
    
         
             
            requirements: []
         
     | 
| 
       84 
86 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       85 
     | 
    
         
            -
            rubygems_version: 2. 
     | 
| 
      
 87 
     | 
    
         
            +
            rubygems_version: 2.4.6
         
     | 
| 
       86 
88 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       87 
89 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       88 
90 
     | 
    
         
             
            summary: Contracts for Ruby.
         
     | 
| 
       89 
91 
     | 
    
         
             
            test_files: []
         
     | 
| 
       90 
     | 
    
         
            -
            has_rdoc: 
         
     |