ruby_contracts 0.3.1 → 0.3.2
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.
- data/lib/ruby_contracts/dsl.rb +10 -4
- data/lib/ruby_contracts/version.rb +1 -1
- data/test/dsl_test.rb +17 -0
- data/test/fixtures/dsl_classes.rb +19 -0
- data/test/fixtures/inheritance_classes.rb +8 -2
- data/test/inheritance_test.rb +22 -4
- metadata +6 -2
data/lib/ruby_contracts/dsl.rb
CHANGED
@@ -9,6 +9,10 @@ module Contracts
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
+
def implies(arg1, arg2)
|
13
|
+
! arg1 || arg2
|
14
|
+
end
|
15
|
+
|
12
16
|
module ClassMethods
|
13
17
|
def inherited(subclass)
|
14
18
|
super
|
@@ -77,9 +81,10 @@ module Contracts
|
|
77
81
|
def __eval_before_contracts(name, context, arguments)
|
78
82
|
last_failure_msg = nil
|
79
83
|
__contracts_for(name).each do |contracts|
|
80
|
-
|
84
|
+
before_contracts = contracts.before_contracts
|
85
|
+
next if before_contracts.empty?
|
81
86
|
success = true
|
82
|
-
|
87
|
+
before_contracts.each do |contract|
|
83
88
|
begin
|
84
89
|
unless satisfied = contract.satisfied?(context, arguments)
|
85
90
|
last_failure_msg = contract.message
|
@@ -100,8 +105,9 @@ module Contracts
|
|
100
105
|
|
101
106
|
def __eval_after_contracts(name, context, arguments, result)
|
102
107
|
__contracts_for(name).each do |contracts|
|
103
|
-
|
104
|
-
|
108
|
+
after_contracts = contracts.after_contracts
|
109
|
+
next if after_contracts.empty?
|
110
|
+
after_contracts.each do |contract|
|
105
111
|
begin
|
106
112
|
unless satisfied = contract.satisfied?(context, arguments, result)
|
107
113
|
__contract_failure!(name, contract.message, result, arguments)
|
data/test/dsl_test.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require_relative 'fixtures/dsl_classes'
|
4
|
+
|
5
|
+
describe 'the implies keyword' do
|
6
|
+
it 'should be false and raise a Contracts::Error when the condition is true but the implication is false' do
|
7
|
+
proc { FalseImplies.new.method1 }.must_raise Contracts::Error
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should be true and not produce any Contracts::Error when the condition is false' do
|
11
|
+
TrueImplies.new.method1.must_be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should be true and not produce any Contracts::Error when the condition is true and the implication is true' do
|
15
|
+
TrueImplies.new.method2.must_be_nil
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class FalseImplies
|
2
|
+
include Contracts::DSL
|
3
|
+
|
4
|
+
pre "true -> false" do implies(true, false) end
|
5
|
+
def method1
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class TrueImplies
|
10
|
+
include Contracts::DSL
|
11
|
+
|
12
|
+
pre "false -> true" do implies(false, true) end
|
13
|
+
def method1
|
14
|
+
end
|
15
|
+
|
16
|
+
pre "true -> true" do implies(true, true) end
|
17
|
+
def method2
|
18
|
+
end
|
19
|
+
end
|
@@ -1,6 +1,5 @@
|
|
1
1
|
class Parent
|
2
2
|
include Contracts::DSL
|
3
|
-
EPSILON = 1e-7
|
4
3
|
attr_reader :value, :minimum_incr
|
5
4
|
|
6
5
|
type :in => [Numeric, Numeric]
|
@@ -13,7 +12,7 @@ class Parent
|
|
13
12
|
|
14
13
|
type :in => [Numeric], :out => Numeric
|
15
14
|
pre "n > minimum_incr" do |n| n > minimum_incr end
|
16
|
-
post "
|
15
|
+
post "result >= n" do |result, n| result >= n end
|
17
16
|
def increment(n)
|
18
17
|
@value += n
|
19
18
|
end
|
@@ -36,6 +35,13 @@ class ChildWithAddedPrecondition < Parent
|
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
38
|
+
class ChildWithAddedPostcondition < Parent
|
39
|
+
post "true" do true end
|
40
|
+
def increment(n)
|
41
|
+
@value += n
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
39
45
|
class ChildWithSuper < Parent
|
40
46
|
def increment(n)
|
41
47
|
super
|
data/test/inheritance_test.rb
CHANGED
@@ -46,13 +46,31 @@ describe 'the contracts behavior in an inheritance context' do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
describe 'the redifinition of an existing method with postcondition' do
|
52
|
+
describe 'the child\'s overriden method call' do
|
53
|
+
describe 'the child specifies no postcondition' do
|
54
|
+
let(:child) { ChildWithRedef.new(10,3) }
|
55
|
+
it 'satisfies the parent postconditions' do
|
56
|
+
child.increment(4).wont_be_nil
|
57
|
+
end
|
58
|
+
end
|
52
59
|
|
53
|
-
|
54
|
-
child.
|
60
|
+
describe 'the child specifies additionnal postconditions' do
|
61
|
+
let(:child) { ChildWithAddedPostcondition.new(10, 3) }
|
62
|
+
it 'satisfies the parent preconditions' do
|
63
|
+
proc { child.increment(2) }.must_raise Contracts::Error
|
64
|
+
end
|
55
65
|
end
|
56
66
|
end
|
57
67
|
end
|
68
|
+
|
69
|
+
describe 'the super keyword usage' do
|
70
|
+
let(:child) { ChildWithSuper.new(10, 3) }
|
71
|
+
|
72
|
+
it 'does not raise any error' do
|
73
|
+
child.increment(5).wont_be_nil
|
74
|
+
end
|
75
|
+
end
|
58
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Micro DSL to add pre & post condition to methods. It tries to bring some
|
15
15
|
design by contract in the Ruby world.
|
@@ -34,6 +34,8 @@ files:
|
|
34
34
|
- lib/ruby_contracts/precondition.rb
|
35
35
|
- lib/ruby_contracts/version.rb
|
36
36
|
- ruby_contracts.gemspec
|
37
|
+
- test/dsl_test.rb
|
38
|
+
- test/fixtures/dsl_classes.rb
|
37
39
|
- test/fixtures/inheritance_classes.rb
|
38
40
|
- test/fixtures/preconditions_classes.rb
|
39
41
|
- test/inheritance_test.rb
|
@@ -65,6 +67,8 @@ specification_version: 3
|
|
65
67
|
summary: Micro DSL to add pre & post condition to methods. It tries to bring some
|
66
68
|
design by contract in the Ruby world.
|
67
69
|
test_files:
|
70
|
+
- test/dsl_test.rb
|
71
|
+
- test/fixtures/dsl_classes.rb
|
68
72
|
- test/fixtures/inheritance_classes.rb
|
69
73
|
- test/fixtures/preconditions_classes.rb
|
70
74
|
- test/inheritance_test.rb
|