contracts 0.11.0 → 0.12.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.
Files changed (50) hide show
  1. checksums.yaml +13 -5
  2. data/CHANGELOG.markdown +8 -0
  3. data/Gemfile +3 -0
  4. data/README.md +14 -10
  5. data/TUTORIAL.md +34 -1
  6. data/benchmarks/io.rb +3 -3
  7. data/cucumber.yml +1 -0
  8. data/features/README.md +17 -0
  9. data/features/basics/functype.feature +71 -0
  10. data/features/basics/simple_example.feature +210 -0
  11. data/features/builtin_contracts/README.md +22 -0
  12. data/features/builtin_contracts/and.feature +103 -0
  13. data/features/builtin_contracts/any.feature +44 -0
  14. data/features/builtin_contracts/args.feature +1 -0
  15. data/features/builtin_contracts/array_of.feature +1 -0
  16. data/features/builtin_contracts/bool.feature +64 -0
  17. data/features/builtin_contracts/enum.feature +1 -0
  18. data/features/builtin_contracts/eq.feature +1 -0
  19. data/features/builtin_contracts/exactly.feature +1 -0
  20. data/features/builtin_contracts/func.feature +1 -0
  21. data/features/builtin_contracts/hash_of.feature +1 -0
  22. data/features/builtin_contracts/keyword_args.feature +1 -0
  23. data/features/builtin_contracts/maybe.feature +1 -0
  24. data/features/builtin_contracts/nat.feature +115 -0
  25. data/features/builtin_contracts/neg.feature +115 -0
  26. data/features/builtin_contracts/none.feature +145 -0
  27. data/features/builtin_contracts/not.feature +1 -0
  28. data/features/builtin_contracts/num.feature +64 -0
  29. data/features/builtin_contracts/or.feature +83 -0
  30. data/features/builtin_contracts/pos.feature +116 -0
  31. data/features/builtin_contracts/range_of.feature +1 -0
  32. data/features/builtin_contracts/respond_to.feature +78 -0
  33. data/features/builtin_contracts/send.feature +147 -0
  34. data/features/builtin_contracts/set_of.feature +1 -0
  35. data/features/builtin_contracts/xor.feature +99 -0
  36. data/features/support/env.rb +6 -0
  37. data/lib/contracts.rb +1 -1
  38. data/lib/contracts/builtin_contracts.rb +356 -351
  39. data/lib/contracts/core.rb +11 -2
  40. data/lib/contracts/formatters.rb +2 -2
  41. data/lib/contracts/validators.rb +6 -0
  42. data/lib/contracts/version.rb +1 -1
  43. data/script/cucumber +5 -0
  44. data/script/docs-release +3 -0
  45. data/script/docs-staging +3 -0
  46. data/spec/builtin_contracts_spec.rb +1 -1
  47. data/spec/contracts_spec.rb +29 -0
  48. data/spec/fixtures/fixtures.rb +12 -0
  49. data/spec/validators_spec.rb +25 -3
  50. metadata +42 -9
@@ -24,13 +24,22 @@ module Contracts
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?
@@ -90,7 +90,7 @@ module Contracts
90
90
 
91
91
  def plain?
92
92
  # Not a type of contract that can have a custom to_s defined
93
- !@value.is_a?(CallableClass) && @value.class != Class
93
+ !@value.is_a?(Builtin::CallableClass) && @value.class != Class
94
94
  end
95
95
 
96
96
  def useful_to_s?
@@ -103,7 +103,7 @@ module Contracts
103
103
  end
104
104
 
105
105
  def strip_prefix(val)
106
- val.gsub(/^Contracts::/, "")
106
+ val.gsub(/^Contracts::Builtin::/, "")
107
107
  end
108
108
  end
109
109
 
@@ -31,6 +31,12 @@ module Contracts
31
31
  end
32
32
  end,
33
33
 
34
+ Regexp => lambda do |contract|
35
+ lambda do |arg|
36
+ arg =~ contract
37
+ end
38
+ end,
39
+
34
40
  Contracts::Args => lambda do |contract|
35
41
  lambda do |arg|
36
42
  Contract.valid?(arg, contract.contract)
@@ -1,3 +1,3 @@
1
1
  module Contracts
2
- VERSION = "0.11.0"
2
+ VERSION = "0.12.0"
3
3
  end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ if ruby -e 'exit(1) unless RUBY_VERSION.to_f >= 2.0'; then
4
+ bundle exec cucumber
5
+ fi
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+
3
+ relish push contracts/contracts-ruby
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+
3
+ relish push contracts-staging/contracts-ruby
@@ -392,7 +392,7 @@ RSpec.describe "Contracts:" do
392
392
  end
393
393
 
394
394
  context "given String => Num" do
395
- it { expect(Contracts::HashOf[String, Contracts::Num].to_s).to eq("Hash<String, Contracts::Num>") }
395
+ it { expect(Contracts::HashOf[String, Contracts::Num].to_s).to eq("Hash<String, Contracts::Builtin::Num>") }
396
396
  end
397
397
  end
398
398
  end
@@ -717,4 +717,33 @@ RSpec.describe "Contracts:" do
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
@@ -123,6 +123,18 @@ class GenericExample
123
123
  def hash_keywordargs(data)
124
124
  end
125
125
 
126
+ Contract (/foo/) => nil
127
+ def should_contain_foo(s)
128
+ end
129
+
130
+ Contract ({ :host => /foo/ }) => nil
131
+ def hash_containing_foo(s)
132
+ end
133
+
134
+ Contract C::ArrayOf[/foo/] => nil
135
+ def array_containing_foo(s)
136
+ end
137
+
126
138
  Contract [C::Or[TrueClass, FalseClass]] => nil
127
139
  def array_complex_contracts(data)
128
140
  end
@@ -1,9 +1,9 @@
1
1
  require "spec_helper"
2
2
 
3
- module Contracts
4
- RSpec.describe "range contract validator" do
5
- subject(:o) { GenericExample.new }
3
+ RSpec.describe "Contract validators" do
4
+ subject(:o) { GenericExample.new }
6
5
 
6
+ describe "Range" do
7
7
  it "passes when value is in range" do
8
8
  expect do
9
9
  o.method_with_range_contract(5)
@@ -22,4 +22,26 @@ module Contracts
22
22
  end.to raise_error(ContractError, /Expected: 1\.\.10.*Actual: "hello world"/m)
23
23
  end
24
24
  end
25
+
26
+ describe "Regexp" do
27
+ it "should pass for a matching string" do
28
+ expect { o.should_contain_foo("containing foo") }.to_not raise_error
29
+ end
30
+
31
+ it "should fail for a non-matching string" do
32
+ expect { o.should_contain_foo("that's not F00") }.to raise_error(ContractError)
33
+ end
34
+
35
+ describe "within a hash" do
36
+ it "should pass for a matching string" do
37
+ expect { o.hash_containing_foo(:host => "foo.example.org") }.to_not raise_error
38
+ end
39
+ end
40
+
41
+ describe "within an array" do
42
+ it "should pass for a matching string" do
43
+ expect { o.array_containing_foo(["foo"]) }.to_not raise_error
44
+ end
45
+ end
46
+ end
25
47
  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.11.0
4
+ version: 0.12.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-30 00:00:00.000000000 Z
11
+ date: 2015-09-20 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
@@ -18,10 +18,10 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - ".gitignore"
22
- - ".rspec"
23
- - ".rubocop.yml"
24
- - ".travis.yml"
21
+ - .gitignore
22
+ - .rspec
23
+ - .rubocop.yml
24
+ - .travis.yml
25
25
  - CHANGELOG.markdown
26
26
  - Gemfile
27
27
  - README.md
@@ -34,6 +34,36 @@ files:
34
34
  - benchmarks/io.rb
35
35
  - benchmarks/wrap_test.rb
36
36
  - contracts.gemspec
37
+ - cucumber.yml
38
+ - features/README.md
39
+ - features/basics/functype.feature
40
+ - features/basics/simple_example.feature
41
+ - features/builtin_contracts/README.md
42
+ - features/builtin_contracts/and.feature
43
+ - features/builtin_contracts/any.feature
44
+ - features/builtin_contracts/args.feature
45
+ - features/builtin_contracts/array_of.feature
46
+ - features/builtin_contracts/bool.feature
47
+ - features/builtin_contracts/enum.feature
48
+ - features/builtin_contracts/eq.feature
49
+ - features/builtin_contracts/exactly.feature
50
+ - features/builtin_contracts/func.feature
51
+ - features/builtin_contracts/hash_of.feature
52
+ - features/builtin_contracts/keyword_args.feature
53
+ - features/builtin_contracts/maybe.feature
54
+ - features/builtin_contracts/nat.feature
55
+ - features/builtin_contracts/neg.feature
56
+ - features/builtin_contracts/none.feature
57
+ - features/builtin_contracts/not.feature
58
+ - features/builtin_contracts/num.feature
59
+ - features/builtin_contracts/or.feature
60
+ - features/builtin_contracts/pos.feature
61
+ - features/builtin_contracts/range_of.feature
62
+ - features/builtin_contracts/respond_to.feature
63
+ - features/builtin_contracts/send.feature
64
+ - features/builtin_contracts/set_of.feature
65
+ - features/builtin_contracts/xor.feature
66
+ - features/support/env.rb
37
67
  - lib/contracts.rb
38
68
  - lib/contracts/builtin_contracts.rb
39
69
  - lib/contracts/call_with.rb
@@ -51,6 +81,9 @@ files:
51
81
  - lib/contracts/support.rb
52
82
  - lib/contracts/validators.rb
53
83
  - lib/contracts/version.rb
84
+ - script/cucumber
85
+ - script/docs-release
86
+ - script/docs-staging
54
87
  - script/rubocop.rb
55
88
  - spec/builtin_contracts_spec.rb
56
89
  - spec/contracts_spec.rb
@@ -74,17 +107,17 @@ require_paths:
74
107
  - lib
75
108
  required_ruby_version: !ruby/object:Gem::Requirement
76
109
  requirements:
77
- - - ">="
110
+ - - ! '>='
78
111
  - !ruby/object:Gem::Version
79
112
  version: '0'
80
113
  required_rubygems_version: !ruby/object:Gem::Requirement
81
114
  requirements:
82
- - - ">="
115
+ - - ! '>='
83
116
  - !ruby/object:Gem::Version
84
117
  version: '0'
85
118
  requirements: []
86
119
  rubyforge_project:
87
- rubygems_version: 2.4.6
120
+ rubygems_version: 2.4.8
88
121
  signing_key:
89
122
  specification_version: 4
90
123
  summary: Contracts for Ruby.