ruby_contracts 0.2.3 → 0.2.4
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/README.md +1 -1
- data/Rakefile +11 -1
- data/lib/ruby_contracts/version.rb +1 -1
- data/lib/ruby_contracts.rb +4 -4
- data/ruby_contracts.gemspec +2 -2
- data/test/fixtures/inheritance_classes.rb +37 -0
- data/test/inheritance_test.rb +41 -0
- data/test/test_helper.rb +4 -0
- metadata +12 -6
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RubyContracts
|
2
2
|
|
3
|
-
RubyContracts is a small DSL to add pre & post condition to methods. It
|
3
|
+
RubyContracts is a small DSL to add pre & post condition to methods. It tries to bring some design by contract in the Ruby world.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
data/Rakefile
CHANGED
@@ -1,2 +1,12 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
require
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << 'test'
|
7
|
+
t.pattern = 'test/*_test.rb'
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Run tests"
|
12
|
+
task :default => :test
|
data/lib/ruby_contracts.rb
CHANGED
@@ -34,7 +34,7 @@ module Contracts
|
|
34
34
|
def __contracts_for(name, current_contracts=nil)
|
35
35
|
inherited_contracts = ancestors[1..-1].reduce(Contracts.empty_contracts) do |c, klass|
|
36
36
|
ancestor_hash = klass.instance_variable_get('@__contracts_for') || {}
|
37
|
-
c[:before]
|
37
|
+
c[:before] += ancestor_hash[name][:before] if ancestor_hash.has_key?(name)
|
38
38
|
c[:after] += ancestor_hash[name][:after] if ancestor_hash.has_key?(name)
|
39
39
|
c
|
40
40
|
end
|
@@ -115,8 +115,8 @@ module Contracts
|
|
115
115
|
code << "__before_contracts_disjunction << __before_contracts_conjunction\n"
|
116
116
|
code
|
117
117
|
end
|
118
|
-
before_contracts << "
|
119
|
-
before_contracts << " self.class.__contract_failure!(*__before_contracts_disjunction.
|
118
|
+
before_contracts << "unless __before_contracts_disjunction.any?{|conj| conj.empty?} then\n"
|
119
|
+
before_contracts << " self.class.__contract_failure!(*__before_contracts_disjunction.find{|conj| !conj.empty?}.first)\n"
|
120
120
|
before_contracts << "end\n"
|
121
121
|
|
122
122
|
after_contracts = __contracts[:after].reduce("") do |code, contract|
|
@@ -124,7 +124,7 @@ module Contracts
|
|
124
124
|
case type
|
125
125
|
when :type
|
126
126
|
code << "if !result.kind_of?(#{args[0]}) then\n"
|
127
|
-
code << "self.class.__contract_failure!(name, \"result must be a kind of '#{args[0]}' not '%s'\" % [result.class.to_s], result, *__args)\n"
|
127
|
+
code << " self.class.__contract_failure!(name, \"result must be a kind of '#{args[0]}' not '%s'\" % [result.class.to_s], result, *__args)\n"
|
128
128
|
code << "end\n"
|
129
129
|
code
|
130
130
|
when :result
|
data/ruby_contracts.gemspec
CHANGED
@@ -7,8 +7,8 @@ require 'ruby_contracts/version'
|
|
7
7
|
Gem::Specification.new do |gem|
|
8
8
|
gem.authors = ["nicoolas25"]
|
9
9
|
gem.email = ["nicoolas25@gmail.com"]
|
10
|
-
gem.description = %q{Micro DSL to add pre & post condition to methods. It
|
11
|
-
gem.summary = %q{Micro DSL to add pre & post condition to methods. It
|
10
|
+
gem.description = %q{Micro DSL to add pre & post condition to methods. It tries to bring some design by contract in the Ruby world.}
|
11
|
+
gem.summary = %q{Micro DSL to add pre & post condition to methods. It tries to bring some design by contract in the Ruby world.}
|
12
12
|
gem.homepage = ""
|
13
13
|
|
14
14
|
gem.files = `git ls-files`.split($\)
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Parent
|
2
|
+
include Contracts::DSL
|
3
|
+
EPSILON = 1e-7
|
4
|
+
attr_reader :value, :minimum_incr
|
5
|
+
|
6
|
+
type :in => [Numeric, Numeric]
|
7
|
+
pre "val >= 0, mininc > 0" do |v, mininc| v >= 0 && mininc > 0 end
|
8
|
+
post "initialized" do |result, v, mininc| @value == v && @minimum_incr == mininc end
|
9
|
+
def initialize(v, mininc)
|
10
|
+
@value = v
|
11
|
+
@minimum_incr = mininc
|
12
|
+
end
|
13
|
+
|
14
|
+
type :in => [Numeric], :out => Numeric
|
15
|
+
pre "n > minimum_incr" do |n| n > minimum_incr end
|
16
|
+
post "value == old value + n" do "[dummy]" end
|
17
|
+
def increment(n)
|
18
|
+
value += n
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
value.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ChildWithRedef < Parent
|
27
|
+
def increment(n)
|
28
|
+
@value += n
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class ChildWithAddedPrecondition < Parent
|
33
|
+
pre "n >= minimum_incr" do |n| n >= minimum_incr end
|
34
|
+
def increment(n)
|
35
|
+
@value += n
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require_relative 'fixtures/inheritance_classes'
|
4
|
+
|
5
|
+
describe 'the contracts behavior in an inheritance context' do
|
6
|
+
describe 'the initialization process' do
|
7
|
+
describe 'with valid arguments' do
|
8
|
+
it 'should not raise any exception' do
|
9
|
+
Parent.new(10, 2).wont_be_nil
|
10
|
+
ChildWithRedef.new(10, 2).wont_be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'with invalid arguments' do
|
15
|
+
it 'should raise an Contracts::Error' do
|
16
|
+
proc { ChildWithRedef.new(10, 0) }.must_raise Contracts::Error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
describe 'the redifinition of an existing method with precondition' do
|
23
|
+
describe 'the child\'s overriden method call' do
|
24
|
+
describe 'the child specifies no precondition' do
|
25
|
+
let(:child) { ChildWithRedef.new(10,3) }
|
26
|
+
it 'satisfies the parent preconditions' do
|
27
|
+
child.increment(4)
|
28
|
+
proc { child.increment(3) }.must_raise Contracts::Error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'the child specifies additionnal preconditions' do
|
33
|
+
let(:child) { ChildWithAddedPrecondition.new(10, 3) }
|
34
|
+
it 'satisfies the parent OR the child preconditions' do
|
35
|
+
child.increment(3)
|
36
|
+
proc { child.increment(2) }.must_raise Contracts::Error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/test/test_helper.rb
ADDED
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.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Micro DSL to add pre & post condition to methods. It
|
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.
|
16
16
|
email:
|
17
17
|
- nicoolas25@gmail.com
|
@@ -27,6 +27,9 @@ files:
|
|
27
27
|
- lib/ruby_contracts.rb
|
28
28
|
- lib/ruby_contracts/version.rb
|
29
29
|
- ruby_contracts.gemspec
|
30
|
+
- test/fixtures/inheritance_classes.rb
|
31
|
+
- test/inheritance_test.rb
|
32
|
+
- test/test_helper.rb
|
30
33
|
homepage: ''
|
31
34
|
licenses: []
|
32
35
|
post_install_message:
|
@@ -50,7 +53,10 @@ rubyforge_project:
|
|
50
53
|
rubygems_version: 1.8.23
|
51
54
|
signing_key:
|
52
55
|
specification_version: 3
|
53
|
-
summary: Micro DSL to add pre & post condition to methods. It
|
54
|
-
by contract in the Ruby world.
|
55
|
-
test_files:
|
56
|
+
summary: Micro DSL to add pre & post condition to methods. It tries to bring some
|
57
|
+
design by contract in the Ruby world.
|
58
|
+
test_files:
|
59
|
+
- test/fixtures/inheritance_classes.rb
|
60
|
+
- test/inheritance_test.rb
|
61
|
+
- test/test_helper.rb
|
56
62
|
has_rdoc:
|