ruby_contracts 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruby_contracts/version.rb +1 -1
- data/lib/ruby_contracts.rb +11 -7
- data/test/fixtures/preconditions_classes.rb +14 -0
- data/test/preconditions_test.rb +26 -0
- metadata +6 -2
data/lib/ruby_contracts.rb
CHANGED
@@ -87,13 +87,15 @@ module Contracts
|
|
87
87
|
case type
|
88
88
|
when :type
|
89
89
|
classes = args[0]
|
90
|
-
code << "if
|
91
|
-
code << "
|
92
|
-
code << "
|
90
|
+
code << "if __before_contracts_conjunction.empty? then\n"
|
91
|
+
code << " if __args.size < #{classes.size} then\n"
|
92
|
+
code << " __before_contracts_conjunction << ['#{name}', \"need at least #{classes.size} arguments (%i given)\" % [__args.size], nil, *args]\n"
|
93
|
+
code << " else\n"
|
93
94
|
conditions = []
|
94
95
|
classes.each_with_index{ |klass, i| conditions << "__args[#{i}].kind_of?(#{klass})" }
|
95
|
-
code << "
|
96
|
-
code << "
|
96
|
+
code << " if !(#{conditions.join(' && ')}) then\n"
|
97
|
+
code << " __before_contracts_conjunction << ['#{name}', 'input type error', nil, *__args]\n"
|
98
|
+
code << " end\n"
|
97
99
|
code << " end\n"
|
98
100
|
code << "end\n"
|
99
101
|
code
|
@@ -103,8 +105,10 @@ module Contracts
|
|
103
105
|
contract_method_name = "__verify_contract_#{name}_in_#{count = count + 1}"
|
104
106
|
define_method(contract_method_name) { |*params| self.instance_exec(*params, &args[1]) }
|
105
107
|
|
106
|
-
code << "if
|
107
|
-
code << "
|
108
|
+
code << "if __before_contracts_conjunction.empty? then\n"
|
109
|
+
code << " if !#{contract_method_name}(*__args) then\n"
|
110
|
+
code << " __before_contracts_conjunction << ['#{name}', \"invalid precondition: #{args[0]}\", nil, *__args]\n"
|
111
|
+
code << " end\n"
|
108
112
|
code << "end\n"
|
109
113
|
code
|
110
114
|
else
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require_relative 'fixtures/preconditions_classes'
|
4
|
+
|
5
|
+
|
6
|
+
describe 'the type precondition' do
|
7
|
+
# method contains only one precondition about the input type
|
8
|
+
# method2 is used to see the behaviour in presence of other preconditions
|
9
|
+
|
10
|
+
let(:instance) { SomeClass.new }
|
11
|
+
|
12
|
+
it 'should raise an exception if there is an unexpected number of arguments' do
|
13
|
+
proc { instance.method() }.must_raise Contracts::Error
|
14
|
+
proc { instance.method2() }.must_raise Contracts::Error
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should raise an exception if an argument have the wrong type' do
|
18
|
+
proc { instance.method('five') }.must_raise Contracts::Error
|
19
|
+
proc { instance.method2('five') }.must_raise Contracts::Error
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should run and return normally if there is the expected arguments' do
|
23
|
+
instance.method(5).must_equal 10
|
24
|
+
instance.method2(5).must_equal 10
|
25
|
+
end
|
26
|
+
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.2.
|
4
|
+
version: 0.2.5
|
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-15 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.
|
@@ -28,7 +28,9 @@ files:
|
|
28
28
|
- lib/ruby_contracts/version.rb
|
29
29
|
- ruby_contracts.gemspec
|
30
30
|
- test/fixtures/inheritance_classes.rb
|
31
|
+
- test/fixtures/preconditions_classes.rb
|
31
32
|
- test/inheritance_test.rb
|
33
|
+
- test/preconditions_test.rb
|
32
34
|
- test/test_helper.rb
|
33
35
|
homepage: ''
|
34
36
|
licenses: []
|
@@ -57,6 +59,8 @@ summary: Micro DSL to add pre & post condition to methods. It tries to bring som
|
|
57
59
|
design by contract in the Ruby world.
|
58
60
|
test_files:
|
59
61
|
- test/fixtures/inheritance_classes.rb
|
62
|
+
- test/fixtures/preconditions_classes.rb
|
60
63
|
- test/inheritance_test.rb
|
64
|
+
- test/preconditions_test.rb
|
61
65
|
- test/test_helper.rb
|
62
66
|
has_rdoc:
|