rdbc 0.0.1 → 0.0.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/README +53 -9
- data/Rakefile +17 -7
- data/lib/design_by_contract.rb +37 -38
- metadata +8 -7
- data/test/suite.rb +0 -12
data/README
CHANGED
@@ -4,11 +4,55 @@ This library supports Design by Contract for Ruby.
|
|
4
4
|
|
5
5
|
=Example
|
6
6
|
|
7
|
+
A a standard example for Design by Contract consider the following *Stack*
|
8
|
+
class and its contract *StackContract*.
|
9
|
+
|
7
10
|
===stack.rb
|
8
11
|
:include:test/stack.rb
|
9
12
|
===stack_contract.rb
|
10
13
|
:include:test/stack_contract.rb
|
11
14
|
|
15
|
+
=Mechanics
|
16
|
+
|
17
|
+
The mechanics are really simple. There is the class *Stack*.
|
18
|
+
For instances of this class you can define a contract by subclassing
|
19
|
+
<b>DesignByContract::Contract</b>, in the example the contract is named
|
20
|
+
*StackContract*.
|
21
|
+
Then connect the class *Stack* to its contract *StackContract* by using the line
|
22
|
+
contract StackContract
|
23
|
+
in the class definition of *Stack*.
|
24
|
+
|
25
|
+
Now the following happens for an instance method of Stack. Say its name is
|
26
|
+
*method*. When it is called the the following calling of methods happens.
|
27
|
+
|
28
|
+
* *method_pre* of the contract is called with the parameters:
|
29
|
+
* the object itself
|
30
|
+
* the parameters of the original method *method*
|
31
|
+
* the original *method* is called
|
32
|
+
* *method_post* of the contract is called with the parameters:
|
33
|
+
* the object before the execution of the original method
|
34
|
+
* the object after execution the original method
|
35
|
+
* the return value of the original method
|
36
|
+
* the parameters of the original method
|
37
|
+
* *invariant* is called with the parameters:
|
38
|
+
* the object before the execution of the original method
|
39
|
+
* the object after execution the method
|
40
|
+
|
41
|
+
The object before the execution of *method* in 3. and 4.
|
42
|
+
is in fact a copy of the object before the execution. So you have to implement
|
43
|
+
the method *initialize_copy* in the class under contract, i.e. the class
|
44
|
+
*Stack*.
|
45
|
+
|
46
|
+
In any of these contract methods you can use assertions as in Test::Unit tests.
|
47
|
+
When an assertion fails, the exception <b>Test::Unit::AssertionFailedError</b>
|
48
|
+
is raised.
|
49
|
+
|
50
|
+
=Operators
|
51
|
+
|
52
|
+
When the method is an operator the *_pre* and *_post* suffixes are appended
|
53
|
+
to the names of the operator according to the constant
|
54
|
+
<b>#DesignByContract::OPERATOR</b>.
|
55
|
+
|
12
56
|
=Support
|
13
57
|
|
14
58
|
The project home is at RubyForge: http://rubyforge.org/projects/rdbc
|
@@ -37,13 +81,13 @@ are met:
|
|
37
81
|
3. The names of the contributors may not be used to endorse or promote products
|
38
82
|
derived from this software without specific prior written permission.
|
39
83
|
|
40
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
41
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
42
|
-
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
43
|
-
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
44
|
-
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
45
|
-
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
46
|
-
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
47
|
-
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
48
|
-
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
84
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
85
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
86
|
+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
87
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
88
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
89
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
90
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
91
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
92
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
49
93
|
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/Rakefile
CHANGED
@@ -5,19 +5,24 @@ require 'rake/testtask'
|
|
5
5
|
|
6
6
|
task :default => [:test]
|
7
7
|
|
8
|
+
task :all => [:test, :doc, :package]
|
9
|
+
|
8
10
|
|
9
11
|
Rake::TestTask.new do |t|
|
10
12
|
t.libs << 'test'
|
11
|
-
t.test_files = 'test
|
13
|
+
t.test_files = FileList['test/**/*test.rb']
|
12
14
|
end
|
13
15
|
|
14
16
|
|
15
17
|
Rake::RDocTask.new(:doc) do |t|
|
16
|
-
t.main = 'README'
|
17
|
-
t.rdoc_files.include('README', 'lib/**/*.rb')
|
18
18
|
t.rdoc_dir = 'doc'
|
19
|
-
t.
|
20
|
-
t.options = [
|
19
|
+
t.rdoc_files.include('README', 'lib/**/*.rb')
|
20
|
+
t.options = [
|
21
|
+
'--all',
|
22
|
+
'--charset', 'utf8',
|
23
|
+
'--main', 'README',
|
24
|
+
'--title', 'Design by Contract for Ruby'
|
25
|
+
]
|
21
26
|
end
|
22
27
|
|
23
28
|
|
@@ -26,10 +31,15 @@ spec = Gem::Specification.new do |s|
|
|
26
31
|
s.email = 'armin.joellenbeck@googlemail.com'
|
27
32
|
s.name = 'rdbc'
|
28
33
|
s.summary = 'Design by Contract for Ruby.'
|
29
|
-
s.version = '0.0.
|
34
|
+
s.version = '0.0.2'
|
30
35
|
s.has_rdoc = true
|
31
36
|
s.extra_rdoc_files << 'README'
|
32
|
-
s.rdoc_options = [
|
37
|
+
s.rdoc_options = [
|
38
|
+
'--all',
|
39
|
+
'--charset', 'utf8',
|
40
|
+
'--main', 'README',
|
41
|
+
'--title', 'Design by Contract for Ruby'
|
42
|
+
]
|
33
43
|
s.files = FileList['lib/**/*', 'bin/*', '[A-Z]*', 'test/**/*'].to_a
|
34
44
|
end
|
35
45
|
|
data/lib/design_by_contract.rb
CHANGED
@@ -2,15 +2,15 @@ require 'test/unit/assertions'
|
|
2
2
|
|
3
3
|
|
4
4
|
class Object
|
5
|
-
|
5
|
+
|
6
6
|
protected
|
7
|
-
|
7
|
+
|
8
8
|
def singleton_class
|
9
9
|
class << self
|
10
10
|
self
|
11
11
|
end
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
def define_singleton_method(method, &block)
|
15
15
|
singleton_class.send(:define_method, method, &block)
|
16
16
|
end
|
@@ -28,12 +28,38 @@ class Class
|
|
28
28
|
@proxy = DesignByContract::Proxy.new(object, contract)
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
end
|
33
33
|
|
34
34
|
|
35
35
|
module DesignByContract
|
36
36
|
|
37
|
+
OPERATOR = {
|
38
|
+
:[] => :element_read,
|
39
|
+
:[]= => :element_write,
|
40
|
+
:** => :power,
|
41
|
+
:~ => :not,
|
42
|
+
:+@ => :unary_plus,
|
43
|
+
:-@ => :unary_minus,
|
44
|
+
:* => :product,
|
45
|
+
:/ => :quotient,
|
46
|
+
:% => :modulo,
|
47
|
+
:+ => :plus,
|
48
|
+
:- => :minus,
|
49
|
+
:>> => :right_shift,
|
50
|
+
:<< => :left_shift,
|
51
|
+
:& => :and,
|
52
|
+
:^ => :xor,
|
53
|
+
:| => :or,
|
54
|
+
:<= => :less_or_equal,
|
55
|
+
:< => :less,
|
56
|
+
:> => :greater,
|
57
|
+
:>= => :greater_or_equal,
|
58
|
+
:<=> => :comparison,
|
59
|
+
:== => :eql,
|
60
|
+
:=== => :case_comparison,
|
61
|
+
:=~ => :match
|
62
|
+
}
|
37
63
|
|
38
64
|
class Contract
|
39
65
|
include Test::Unit::Assertions
|
@@ -48,7 +74,7 @@ module DesignByContract
|
|
48
74
|
@object = object
|
49
75
|
@contract = contract
|
50
76
|
end
|
51
|
-
|
77
|
+
|
52
78
|
def method_missing(method, *args)
|
53
79
|
# duplicate the object under contract for use in the pre condition
|
54
80
|
object_pre = @object.dup
|
@@ -78,17 +104,17 @@ module DesignByContract
|
|
78
104
|
end
|
79
105
|
|
80
106
|
class << self
|
81
|
-
|
107
|
+
|
82
108
|
def method_pre(method)
|
83
109
|
self.method_with_suffix(method, :pre)
|
84
110
|
end
|
85
|
-
|
111
|
+
|
86
112
|
def method_post(method)
|
87
113
|
self.method_with_suffix(method, :post)
|
88
114
|
end
|
89
|
-
|
115
|
+
|
90
116
|
protected
|
91
|
-
|
117
|
+
|
92
118
|
def method_with_suffix(method, type)
|
93
119
|
operator = OPERATOR[method]
|
94
120
|
suffix = '_' + type.to_s
|
@@ -106,36 +132,9 @@ module DesignByContract
|
|
106
132
|
('op_' + operator.to_s + suffix).to_sym
|
107
133
|
end
|
108
134
|
end
|
109
|
-
|
135
|
+
|
110
136
|
end
|
111
|
-
|
112
|
-
OPERATOR = {
|
113
|
-
:[] => :element_read,
|
114
|
-
:[]= => :element_write,
|
115
|
-
:** => :power,
|
116
|
-
:~ => :not,
|
117
|
-
:+@ => :unary_plus,
|
118
|
-
:-@ => :unary_minus,
|
119
|
-
:* => :product,
|
120
|
-
:/ => :quotient,
|
121
|
-
:% => :modulo,
|
122
|
-
:+ => :plus,
|
123
|
-
:- => :minus,
|
124
|
-
:>> => :right_shift,
|
125
|
-
:<< => :left_shift,
|
126
|
-
:& => :and,
|
127
|
-
:^ => :xor,
|
128
|
-
:| => :or,
|
129
|
-
:<= => :less_or_equal,
|
130
|
-
:< => :less,
|
131
|
-
:> => :greater,
|
132
|
-
:>= => :greater_or_equal,
|
133
|
-
:<=> => :comparison,
|
134
|
-
:== => :eql,
|
135
|
-
:=== => :case_comparison,
|
136
|
-
:=~ => :match
|
137
|
-
}
|
138
|
-
|
137
|
+
|
139
138
|
end
|
140
139
|
|
141
140
|
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.4
|
3
3
|
specification_version: 1
|
4
4
|
name: rdbc
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2007-09-22 00:00:00 +02:00
|
8
8
|
summary: Design by Contract for Ruby.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -32,19 +32,20 @@ files:
|
|
32
32
|
- lib/design_by_contract.rb
|
33
33
|
- Rakefile
|
34
34
|
- README
|
35
|
+
- test/stack.rb
|
35
36
|
- test/design_by_contract_test.rb
|
36
37
|
- test/stack_contract.rb
|
37
38
|
- test/stack_test.rb
|
38
|
-
- test/stack.rb
|
39
|
-
- test/suite.rb
|
40
39
|
test_files: []
|
41
40
|
|
42
41
|
rdoc_options:
|
43
|
-
- --main
|
44
|
-
- README
|
45
42
|
- --all
|
46
43
|
- --charset
|
47
44
|
- utf8
|
45
|
+
- --main
|
46
|
+
- README
|
47
|
+
- --title
|
48
|
+
- Design by Contract for Ruby
|
48
49
|
extra_rdoc_files:
|
49
50
|
- README
|
50
51
|
executables: []
|