rdbc 0.0.3 → 0.2.5
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.rdoc +83 -0
- data/lib/rdbc/contract.rb +56 -0
- data/lib/rdbc/decorator.rb +19 -0
- data/lib/rdbc/translating.rb +50 -0
- data/lib/rdbc.rb +16 -0
- metadata +48 -42
- data/README +0 -93
- data/Rakefile +0 -48
- data/lib/design_by_contract.rb +0 -140
- data/test/design_by_contract_test.rb +0 -34
- data/test/stack.rb +0 -41
- data/test/stack_contract.rb +0 -33
- data/test/stack_test.rb +0 -37
data/README.rdoc
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
=Rdbc -- Design by Contract for Ruby
|
2
|
+
|
3
|
+
This library supports Design by Contract for Ruby.
|
4
|
+
|
5
|
+
=Installation
|
6
|
+
|
7
|
+
Install the *rdbc* gem itself by the following command:
|
8
|
+
|
9
|
+
$ gem install armin-joellenbeck-rdbc --source http://gems.github.com
|
10
|
+
|
11
|
+
=Usage
|
12
|
+
|
13
|
+
Given there is a the class Foo.
|
14
|
+
For instances of this class you can define a contract by subclassing
|
15
|
+
<b>Rdbc::Contract</b>, in the example the contract is named
|
16
|
+
FooContract.
|
17
|
+
Then connect the class Foo to its contract FooContract by using the line
|
18
|
+
contract FooContract
|
19
|
+
in the class definition of Foo.
|
20
|
+
|
21
|
+
Then the following happens when a method of a Foo instance is called:
|
22
|
+
|
23
|
+
1. the corresponding pre condition is called with the parameters:
|
24
|
+
* the instance itself
|
25
|
+
* the parameters of the method call
|
26
|
+
|
27
|
+
2. the method is called
|
28
|
+
|
29
|
+
3. the corresponding post condition is called with the parameters:
|
30
|
+
* the instance before the method is called
|
31
|
+
* the instance after the method is called
|
32
|
+
* the return value of the method call
|
33
|
+
* the parameters of the method call
|
34
|
+
|
35
|
+
4. the invariant is called with the parameters:
|
36
|
+
* the instance before the method is called
|
37
|
+
* the instance after the method is called
|
38
|
+
|
39
|
+
The instance before the execution of *method* in 3. and 4.
|
40
|
+
is in fact a copy of the instance before the execution. So you have to implement
|
41
|
+
the method *initialize_copy* in the class under contract, i.e. the class
|
42
|
+
Foo.
|
43
|
+
|
44
|
+
=Support
|
45
|
+
|
46
|
+
The project home is at
|
47
|
+
GitHub[http://github.com/armin-joellenbeck/rdbc/tree/master]:
|
48
|
+
http://github.com/armin-joellenbeck/rdbc/tree/master
|
49
|
+
|
50
|
+
Feel free to send:
|
51
|
+
* bug reports
|
52
|
+
* support requests
|
53
|
+
* feature requests
|
54
|
+
* patches
|
55
|
+
|
56
|
+
=Copyright
|
57
|
+
|
58
|
+
Copyright (c) 2007 - 2009 by Armin Jöllenbeck
|
59
|
+
<armin@joellenbeck.net[mailto:armin@joellenbeck.net]>
|
60
|
+
|
61
|
+
All rights reserved.
|
62
|
+
|
63
|
+
Redistribution and use in source and binary forms, with or without
|
64
|
+
modification, are permitted provided that the following conditions
|
65
|
+
are met:
|
66
|
+
1. Redistributions of source code must retain the above copyright
|
67
|
+
notice, this list of conditions and the following disclaimer.
|
68
|
+
2. Redistributions in binary form must reproduce the above copyright
|
69
|
+
notice, this list of conditions and the following disclaimer in the
|
70
|
+
documentation and/or other materials provided with the distribution.
|
71
|
+
3. The names of the contributors may not be used to endorse or promote products
|
72
|
+
derived from this software without specific prior written permission.
|
73
|
+
|
74
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
75
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
76
|
+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
77
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
78
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
79
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
80
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
81
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
82
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
83
|
+
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Rdbc
|
2
|
+
class Contract
|
3
|
+
class << self
|
4
|
+
include Translating
|
5
|
+
|
6
|
+
def pre(method, &block)
|
7
|
+
define_method(method_pre(method), &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def post(method, &block)
|
11
|
+
define_method(method_post(method), &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def invariant(&block)
|
15
|
+
define_method(:invariant, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
include Translating
|
20
|
+
|
21
|
+
def delegate_and_verify(object_pre, object)
|
22
|
+
send_method_pre(method, object, *args)
|
23
|
+
begin
|
24
|
+
result = object.send(method, *args)
|
25
|
+
rescue => exception
|
26
|
+
send_invariant(object)
|
27
|
+
send_method_post(method, object_pre, object, exception, result, *args)
|
28
|
+
raise exception
|
29
|
+
else
|
30
|
+
send_invariant(@object)
|
31
|
+
send_method_post(method, object_pre, object, exception, result, *args)
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def send_method_pre(method, object, *args)
|
37
|
+
method_pre = method_pre(method)
|
38
|
+
if respond_to?(method_pre)
|
39
|
+
send(method_pre, object, *args)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def send_invariant(object)
|
44
|
+
if respond_to?(:invariant)
|
45
|
+
invariant(object)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def send_method_post(method, object_pre, object, exception, result, *args)
|
50
|
+
method_post = method_post(method)
|
51
|
+
if respond_to?(method_post)
|
52
|
+
send(method_post, object_pre, object, exception, result, *args)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Rdbc
|
2
|
+
class Decorator
|
3
|
+
instance_methods.reject do |method|
|
4
|
+
/__.*__/ === method
|
5
|
+
end.each do |method|
|
6
|
+
undef_method(method)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(object, contract)
|
10
|
+
@object = object
|
11
|
+
@contract = contract
|
12
|
+
@contract.send_invariant(@object)
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(method, *args)
|
16
|
+
@contract.delegate_and_verify(@object.clone, @object)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Rdbc
|
2
|
+
module Translating
|
3
|
+
def method_pre(method)
|
4
|
+
method_with_prefix(method, 'pre')
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_post(method)
|
8
|
+
method_with_prefix(method, 'post')
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_with_prefix(method, type)
|
12
|
+
(type + '_' + (operator?(method) ? operator(method) : method.to_s)).to_sym
|
13
|
+
end
|
14
|
+
|
15
|
+
def operator?(method)
|
16
|
+
/^[A-Za-z_]/ !~ method.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
def operator(method)
|
20
|
+
'op_' + OPERATOR[method]
|
21
|
+
end
|
22
|
+
|
23
|
+
OPERATOR = {
|
24
|
+
:[] => 'element_read',
|
25
|
+
:[]= => 'element_write',
|
26
|
+
:** => 'power',
|
27
|
+
:~ => 'not',
|
28
|
+
:+@ => 'unary_plus',
|
29
|
+
:-@ => 'unary_minus',
|
30
|
+
:* => 'product',
|
31
|
+
:/ => 'quotient',
|
32
|
+
:% => 'modulo',
|
33
|
+
:+ => 'plus',
|
34
|
+
:- => 'minus',
|
35
|
+
:>> => 'right_shift',
|
36
|
+
:<< => 'left_shift',
|
37
|
+
:& => 'and',
|
38
|
+
:^ => 'xor',
|
39
|
+
:| => 'or',
|
40
|
+
:<= => 'less_or_equal',
|
41
|
+
:< => 'less',
|
42
|
+
:> => 'greater',
|
43
|
+
:>= => 'greater_or_equal',
|
44
|
+
:<=> => 'comparison',
|
45
|
+
:== => 'eql',
|
46
|
+
:=== => 'case_comparison',
|
47
|
+
:=~ => 'match'
|
48
|
+
}
|
49
|
+
end
|
50
|
+
end
|
data/lib/rdbc.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module Rdbc
|
2
|
+
autoload 'Contract', 'rdbc/contract'
|
3
|
+
autoload 'Decorator', 'rdbc/decorator'
|
4
|
+
autoload 'Translating', 'rdbc/translating'
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
class Class
|
9
|
+
def contract(contract_class)
|
10
|
+
old_new = self.method(:new)
|
11
|
+
(class << self; self; end).send(:define_method, :new) do |*args|
|
12
|
+
object = old_new.call(*args)
|
13
|
+
Rdbc::Decorator.new(object, contract_class.new)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
CHANGED
@@ -1,59 +1,65 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.4
|
3
|
-
specification_version: 1
|
4
2
|
name: rdbc
|
5
3
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-09-22 00:00:00 +02:00
|
8
|
-
summary: Design by Contract for Ruby.
|
9
|
-
require_paths:
|
10
|
-
- lib
|
11
|
-
email: armin.joellenbeck@googlemail.com
|
12
|
-
homepage:
|
13
|
-
rubyforge_project:
|
14
|
-
description:
|
15
|
-
autorequire:
|
16
|
-
default_executable:
|
17
|
-
bindir: bin
|
18
|
-
has_rdoc: true
|
19
|
-
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">"
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.0.0
|
24
|
-
version:
|
4
|
+
version: 0.2.5
|
25
5
|
platform: ruby
|
26
|
-
signing_key:
|
27
|
-
cert_chain:
|
28
|
-
post_install_message:
|
29
6
|
authors:
|
30
7
|
- Armin Joellenbeck
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-08 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: " This library supports Design by Contract for Ruby.\n"
|
17
|
+
email: armin@joellenbeck.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
31
24
|
files:
|
32
|
-
-
|
33
|
-
-
|
34
|
-
-
|
35
|
-
-
|
36
|
-
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
25
|
+
- README.rdoc
|
26
|
+
- lib/rdbc.rb
|
27
|
+
- lib/rdbc/contract.rb
|
28
|
+
- lib/rdbc/decorator.rb
|
29
|
+
- lib/rdbc/translating.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/armin-joellenbeck/rdbc/tree/master
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
42
35
|
rdoc_options:
|
43
36
|
- --all
|
44
37
|
- --charset
|
45
38
|
- utf8
|
46
39
|
- --main
|
47
|
-
- README
|
40
|
+
- README.rdoc
|
48
41
|
- --title
|
49
42
|
- Design by Contract for Ruby
|
50
|
-
|
51
|
-
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
56
57
|
requirements: []
|
57
58
|
|
58
|
-
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Design by Contract for Ruby.
|
64
|
+
test_files: []
|
59
65
|
|
data/README
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
=Design by Contract for Ruby
|
2
|
-
|
3
|
-
This library supports Design by Contract for Ruby.
|
4
|
-
|
5
|
-
=Example
|
6
|
-
|
7
|
-
A a standard example for Design by Contract consider the following *Stack*
|
8
|
-
class and its contract *StackContract*.
|
9
|
-
|
10
|
-
===stack.rb
|
11
|
-
:include:test/stack.rb
|
12
|
-
===stack_contract.rb
|
13
|
-
:include:test/stack_contract.rb
|
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
|
-
|
56
|
-
=Support
|
57
|
-
|
58
|
-
The project home is at RubyForge: http://rubyforge.org/projects/rdbc
|
59
|
-
|
60
|
-
Feel free to send:
|
61
|
-
* bug reports
|
62
|
-
* support requests
|
63
|
-
* feature requests
|
64
|
-
* patches
|
65
|
-
|
66
|
-
=Copyright
|
67
|
-
|
68
|
-
Copyright (c) 2007 by Armin Jöllenbeck
|
69
|
-
<armin.joellenbeck@googlemail.com[mailto:armin.joellenbeck@googlemail.com]>
|
70
|
-
|
71
|
-
All rights reserved.
|
72
|
-
|
73
|
-
Redistribution and use in source and binary forms, with or without
|
74
|
-
modification, are permitted provided that the following conditions
|
75
|
-
are met:
|
76
|
-
1. Redistributions of source code must retain the above copyright
|
77
|
-
notice, this list of conditions and the following disclaimer.
|
78
|
-
2. Redistributions in binary form must reproduce the above copyright
|
79
|
-
notice, this list of conditions and the following disclaimer in the
|
80
|
-
documentation and/or other materials provided with the distribution.
|
81
|
-
3. The names of the contributors may not be used to endorse or promote products
|
82
|
-
derived from this software without specific prior written permission.
|
83
|
-
|
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
|
93
|
-
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/Rakefile
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
require 'rake/gempackagetask'
|
2
|
-
require 'rake/rdoctask'
|
3
|
-
require 'rake/testtask'
|
4
|
-
|
5
|
-
|
6
|
-
task :default => [:test]
|
7
|
-
|
8
|
-
task :all => [:test, :doc, :package]
|
9
|
-
|
10
|
-
|
11
|
-
Rake::TestTask.new do |t|
|
12
|
-
t.libs << 'test'
|
13
|
-
t.test_files = FileList['test/**/*test.rb']
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
Rake::RDocTask.new(:doc) do |t|
|
18
|
-
t.rdoc_dir = 'doc'
|
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
|
-
]
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
spec = Gem::Specification.new do |s|
|
30
|
-
s.author = 'Armin Joellenbeck'
|
31
|
-
s.email = 'armin.joellenbeck@googlemail.com'
|
32
|
-
s.name = 'rdbc'
|
33
|
-
s.summary = 'Design by Contract for Ruby.'
|
34
|
-
s.version = '0.0.3'
|
35
|
-
s.has_rdoc = true
|
36
|
-
s.extra_rdoc_files << 'README'
|
37
|
-
s.rdoc_options = [
|
38
|
-
'--all',
|
39
|
-
'--charset', 'utf8',
|
40
|
-
'--main', 'README',
|
41
|
-
'--title', 'Design by Contract for Ruby'
|
42
|
-
]
|
43
|
-
s.files = FileList['lib/**/*', 'bin/*', '[A-Z]*', 'test/**/*'].to_a
|
44
|
-
s.test_files = FileList['test/**/*_test.rb'].to_a
|
45
|
-
end
|
46
|
-
|
47
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
48
|
-
end
|
data/lib/design_by_contract.rb
DELETED
@@ -1,140 +0,0 @@
|
|
1
|
-
require 'test/unit/assertions'
|
2
|
-
|
3
|
-
|
4
|
-
class Object
|
5
|
-
|
6
|
-
protected
|
7
|
-
|
8
|
-
def singleton_class
|
9
|
-
class << self
|
10
|
-
self
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def define_singleton_method(method, &block)
|
15
|
-
singleton_class.send(:define_method, method, &block)
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
class Class
|
22
|
-
|
23
|
-
def contract(klass)
|
24
|
-
@old_new = self.method(:new)
|
25
|
-
self.define_singleton_method(:new) do |*args|
|
26
|
-
object = @old_new.call(*args)
|
27
|
-
contract = klass.new
|
28
|
-
@proxy = DesignByContract::Proxy.new(object, contract)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
module DesignByContract
|
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
|
-
}
|
63
|
-
|
64
|
-
class Contract
|
65
|
-
include Test::Unit::Assertions
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
class Proxy
|
70
|
-
|
71
|
-
private
|
72
|
-
|
73
|
-
def initialize(object, contract)
|
74
|
-
@object = object
|
75
|
-
@contract = contract
|
76
|
-
end
|
77
|
-
|
78
|
-
def method_missing(method, *args)
|
79
|
-
# duplicate the object under contract for use in the pre condition
|
80
|
-
object_pre = @object.dup
|
81
|
-
|
82
|
-
# call pre condition
|
83
|
-
method_pre = self.class.method_pre(method)
|
84
|
-
if @contract.respond_to?(method_pre)
|
85
|
-
@contract.send(method_pre, @object, *args)
|
86
|
-
end
|
87
|
-
|
88
|
-
# call the wrapped method
|
89
|
-
result = @object.send(method, *args)
|
90
|
-
|
91
|
-
# call the invariant condition
|
92
|
-
if @contract.respond_to?(:invariant)
|
93
|
-
@contract.invariant(object_pre, @object)
|
94
|
-
end
|
95
|
-
|
96
|
-
# call the post condition
|
97
|
-
method_post = self.class.method_post(method)
|
98
|
-
if @contract.respond_to?(method_post)
|
99
|
-
@contract.send(method_post, object_pre, @object, result, *args)
|
100
|
-
end
|
101
|
-
|
102
|
-
# return the return value of the call to the wrapped method
|
103
|
-
result
|
104
|
-
end
|
105
|
-
|
106
|
-
class << self
|
107
|
-
|
108
|
-
def method_pre(method)
|
109
|
-
self.method_with_suffix(method, :pre)
|
110
|
-
end
|
111
|
-
|
112
|
-
def method_post(method)
|
113
|
-
self.method_with_suffix(method, :post)
|
114
|
-
end
|
115
|
-
|
116
|
-
protected
|
117
|
-
|
118
|
-
def method_with_suffix(method, type)
|
119
|
-
operator = OPERATOR[method]
|
120
|
-
suffix = '_' + type.to_s
|
121
|
-
if operator.nil?
|
122
|
-
method_string = method.to_s
|
123
|
-
length = method_string.length
|
124
|
-
head = method_string[0...length-1]
|
125
|
-
tail = method_string[length-1...length]
|
126
|
-
if ['?', '!', '='].include?(tail)
|
127
|
-
(head + suffix + tail).to_sym
|
128
|
-
else
|
129
|
-
(method_string + suffix).to_sym
|
130
|
-
end
|
131
|
-
else
|
132
|
-
('op_' + operator.to_s + suffix).to_sym
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
end
|
139
|
-
|
140
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
|
-
require 'design_by_contract'
|
4
|
-
|
5
|
-
|
6
|
-
class DesignByContractTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
def assert_method_pre(expected_method_pre, method)
|
9
|
-
assert_equal(expected_method_pre,
|
10
|
-
DesignByContract::Proxy.method_pre(method))
|
11
|
-
end
|
12
|
-
|
13
|
-
def assert_method_post(expected_method_post, method)
|
14
|
-
assert_equal(expected_method_post,
|
15
|
-
DesignByContract::Proxy.method_post(method))
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_method_pre
|
19
|
-
assert_method_pre(:method_pre, :method)
|
20
|
-
assert_method_pre(:method_pre?, :method?)
|
21
|
-
assert_method_pre(:method_pre!, :method!)
|
22
|
-
assert_method_pre(:method_pre=, :method=)
|
23
|
-
assert_method_pre(:op_plus_pre, :+)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_method_post
|
27
|
-
assert_method_post(:method_post, :method)
|
28
|
-
assert_method_post(:method_post?, :method?)
|
29
|
-
assert_method_post(:method_post!, :method!)
|
30
|
-
assert_method_post(:method_post=, :method=)
|
31
|
-
assert_method_post(:op_plus_post, :+)
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
data/test/stack.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/stack_contract'
|
2
|
-
|
3
|
-
|
4
|
-
class Stack
|
5
|
-
|
6
|
-
contract StackContract
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@elements = []
|
10
|
-
end
|
11
|
-
|
12
|
-
def initialize_copy(orig)
|
13
|
-
@elements = orig.elements
|
14
|
-
end
|
15
|
-
|
16
|
-
def elements
|
17
|
-
@elements.dup
|
18
|
-
end
|
19
|
-
|
20
|
-
def size
|
21
|
-
@elements.size
|
22
|
-
end
|
23
|
-
|
24
|
-
def empty?
|
25
|
-
self.size == 0
|
26
|
-
end
|
27
|
-
|
28
|
-
def top
|
29
|
-
@elements.last
|
30
|
-
end
|
31
|
-
|
32
|
-
def push(element)
|
33
|
-
@elements.push(element)
|
34
|
-
nil
|
35
|
-
end
|
36
|
-
|
37
|
-
def pop
|
38
|
-
@elements.pop
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
data/test/stack_contract.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'design_by_contract'
|
2
|
-
|
3
|
-
|
4
|
-
class StackContract < DesignByContract::Contract
|
5
|
-
|
6
|
-
def push_post(stack_pre, stack, result, element)
|
7
|
-
assert_nil(result)
|
8
|
-
assert_equal(element, stack.top)
|
9
|
-
assert_equal(stack.size, stack_pre.size + 1)
|
10
|
-
end
|
11
|
-
|
12
|
-
def pop_pre(stack)
|
13
|
-
assert_operator(stack.size, :>=, 1)
|
14
|
-
end
|
15
|
-
|
16
|
-
def pop_post(stack_pre, stack, result)
|
17
|
-
assert_equal(stack_pre.top, result)
|
18
|
-
assert_equal(stack_pre.size - 1, stack.size)
|
19
|
-
end
|
20
|
-
|
21
|
-
def top_pre(stack)
|
22
|
-
assert_operator(stack.size, :>=, 1)
|
23
|
-
end
|
24
|
-
|
25
|
-
def top_post(stack_pre, stack, result)
|
26
|
-
assert_equal(stack_pre.size, stack.size)
|
27
|
-
end
|
28
|
-
|
29
|
-
def invariant(stack_pre, stack)
|
30
|
-
assert_operator(stack.size, :>=, 0)
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
data/test/stack_test.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
|
-
require File.dirname(__FILE__) + '/stack'
|
4
|
-
|
5
|
-
|
6
|
-
class StackTest < Test::Unit::TestCase
|
7
|
-
|
8
|
-
def setup
|
9
|
-
@stack = Stack.new
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_empty
|
13
|
-
assert(@stack.empty?)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_top
|
17
|
-
assert_raise Test::Unit::AssertionFailedError do
|
18
|
-
@stack.top
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_pop
|
23
|
-
assert_raise Test::Unit::AssertionFailedError do
|
24
|
-
@stack.pop
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_push_top_pop
|
29
|
-
element = 0
|
30
|
-
@stack.push(element)
|
31
|
-
assert(!@stack.empty?)
|
32
|
-
assert_equal(element, @stack.top)
|
33
|
-
assert_equal(element, @stack.pop)
|
34
|
-
assert(@stack.empty?)
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|