prepost 0.1.0
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.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +148 -0
- data/lib/prepost/source_extractor.rb +70 -0
- data/lib/prepost/version.rb +5 -0
- data/lib/prepost.rb +408 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d46796dec7b563a7baedd08c53ffd84fd779d59e34087d03ce91cd8b84a9fb2d
|
|
4
|
+
data.tar.gz: 4b984829398d8d4c1ac640aaa6033999912537ef644fec8bd17b8b4d086732ca
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6f6e177da3f48eba97ebe9b35a74f160cb1b0b8623fe4ac71698f0905e1885389b2b81fb72329f6875da8b3a4e0cb880ce41e86c914152f3047ba760e9a4b330
|
|
7
|
+
data.tar.gz: 84c73af484e6d0a3c5ef089a1e2eee35691a9aa0a4ee7e9434229f5643189771ef81286f633f742f7b2bb1fa5ffbf3f1e5c9605f0db0595ae5b5eddb7e922639
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# Prepost
|
|
2
|
+
|
|
3
|
+
Runtime preconditions, postconditions, and class invariants for Ruby.
|
|
4
|
+
|
|
5
|
+
Prepost has no dependency on `sorbet-runtime`. It can be used by itself or alongside Sorbet runtime signatures.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add the gem to your bundle:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem "prepost"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Then run `bundle install`.
|
|
16
|
+
|
|
17
|
+
Contract checks are disabled by default. Enable them explicitly in environments where they should run:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
Prepost.enable!
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Call `Prepost.disable!` to turn checks off again. `Prepost.enabled?` reports the current process-wide setting.
|
|
24
|
+
|
|
25
|
+
## Basic usage
|
|
26
|
+
|
|
27
|
+
Extend `Prepost`, declare class invariants with `inv`, and place method contracts immediately before the method they protect:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require "prepost"
|
|
31
|
+
|
|
32
|
+
Prepost.enable!
|
|
33
|
+
|
|
34
|
+
class Inventory
|
|
35
|
+
extend Prepost
|
|
36
|
+
|
|
37
|
+
inv { @stock >= 0 }
|
|
38
|
+
|
|
39
|
+
def initialize(stock)
|
|
40
|
+
@stock = stock
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
pre { quantity > 0 }
|
|
44
|
+
post { @stock == old.stock - quantity }
|
|
45
|
+
def buy(quantity)
|
|
46
|
+
@stock -= quantity
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
inventory = Inventory.new(10)
|
|
53
|
+
inventory.buy(3) # @stock is now 7
|
|
54
|
+
inventory.buy(0) # raises Prepost::PreconditionViolation
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`pre` and `post` apply only to the next method definition. Multiple contracts of the same kind run in declaration order.
|
|
58
|
+
|
|
59
|
+
`inv` is class-level. Invariants run after `initialize`, then before and after every successful public method call. Entry invariants run before preconditions. Exit invariants run after postconditions.
|
|
60
|
+
|
|
61
|
+
## Sorbet integration
|
|
62
|
+
|
|
63
|
+
Load and extend Sorbet explicitly before extending Prepost:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
require "sorbet-runtime"
|
|
67
|
+
require "prepost"
|
|
68
|
+
|
|
69
|
+
Prepost.enable!
|
|
70
|
+
|
|
71
|
+
class Inventory
|
|
72
|
+
extend T::Sig
|
|
73
|
+
extend Prepost
|
|
74
|
+
|
|
75
|
+
sig { params(quantity: Integer).void }
|
|
76
|
+
pre { quantity > 0 }
|
|
77
|
+
post { @stock == old.stock - quantity }
|
|
78
|
+
def buy(quantity)
|
|
79
|
+
@stock -= quantity
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Sorbet checks parameter and return types. Prepost checks the method contracts around the Sorbet-wrapped call.
|
|
85
|
+
|
|
86
|
+
## Condition context
|
|
87
|
+
|
|
88
|
+
Conditions execute with the method receiver as `self`. They can use instance variables, private methods, and bare method argument names:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
pre { quantity > 0 }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
If the receiver already defines a method with the same name as an argument, use explicit block parameters:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
pre { |quantity| quantity > 0 }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Postconditions can additionally read:
|
|
101
|
+
|
|
102
|
+
- `result`: the method return value.
|
|
103
|
+
- `old`: a shallow snapshot of the receiver's instance variables before the call.
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
post do |quantity, result, old|
|
|
107
|
+
result == old.stock - quantity && @stock == result
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`old.stock` and `old[:stock]` are equivalent. The snapshot duplicates mutable instance-variable values when possible, but nested mutable objects remain shared.
|
|
112
|
+
|
|
113
|
+
Preconditions cannot access `old` or `result`. Invariants receive no method arguments and cannot access either helper.
|
|
114
|
+
|
|
115
|
+
## Named contracts
|
|
116
|
+
|
|
117
|
+
Names are optional:
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
pre(:positive_quantity) { quantity > 0 }
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Without a name, Prepost lazily extracts the block source when a violation occurs:
|
|
124
|
+
|
|
125
|
+
```text
|
|
126
|
+
Precondition `quantity > 0` failed for Inventory#buy
|
|
127
|
+
Declared at examples/inventory.rb:17
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Violations
|
|
131
|
+
|
|
132
|
+
A false or nil condition raises:
|
|
133
|
+
|
|
134
|
+
- `Prepost::PreconditionViolation`
|
|
135
|
+
- `Prepost::PostconditionViolation`
|
|
136
|
+
- `Prepost::InvariantViolation`
|
|
137
|
+
|
|
138
|
+
Each error exposes `contract_name`, `contract_source`, `contract_source_location`, `contract_description`, `method_name`, and `receiver`.
|
|
139
|
+
|
|
140
|
+
Postconditions and exit invariants do not run when the method body raises. Recursive method calls made while evaluating a condition bypass contracts to prevent contract evaluation from re-entering itself.
|
|
141
|
+
|
|
142
|
+
## Development
|
|
143
|
+
|
|
144
|
+
```sh
|
|
145
|
+
bundle install
|
|
146
|
+
bundle exec rake test
|
|
147
|
+
bundle exec ruby -Ilib examples/inventory.rb
|
|
148
|
+
```
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "prism"
|
|
4
|
+
|
|
5
|
+
module Prepost
|
|
6
|
+
module SourceExtractor
|
|
7
|
+
CONTRACT_METHODS = [:inv, :post, :pre].freeze
|
|
8
|
+
|
|
9
|
+
@indexes = {}
|
|
10
|
+
@declarations = Hash.new(0)
|
|
11
|
+
|
|
12
|
+
module_function
|
|
13
|
+
|
|
14
|
+
def register(kind, predicate)
|
|
15
|
+
file, line = predicate.source_location
|
|
16
|
+
location = file && line ? "#{file}:#{line}" : nil
|
|
17
|
+
return [location, 0] unless file && line
|
|
18
|
+
|
|
19
|
+
key = [file, line, method_name(kind)]
|
|
20
|
+
ordinal = @declarations[key]
|
|
21
|
+
@declarations[key] += 1
|
|
22
|
+
[location, ordinal]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def extract(kind, predicate, ordinal)
|
|
26
|
+
file, line = predicate.source_location
|
|
27
|
+
return unless file && File.file?(file)
|
|
28
|
+
|
|
29
|
+
candidates = index(file).fetch([line, method_name(kind)], EMPTY_SOURCES)
|
|
30
|
+
candidates[ordinal % candidates.length] unless candidates.empty?
|
|
31
|
+
rescue SystemCallError, EncodingError
|
|
32
|
+
nil
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def method_name(kind)
|
|
36
|
+
kind == :invariant ? :inv : kind
|
|
37
|
+
end
|
|
38
|
+
private_class_method :method_name
|
|
39
|
+
|
|
40
|
+
EMPTY_SOURCES = [].freeze
|
|
41
|
+
private_constant :EMPTY_SOURCES
|
|
42
|
+
|
|
43
|
+
def index(file)
|
|
44
|
+
@indexes[file] ||= begin
|
|
45
|
+
result = Prism.parse_file(file)
|
|
46
|
+
if result.success?
|
|
47
|
+
sources = Hash.new { |hash, key| hash[key] = [] }
|
|
48
|
+
visit(result.value, sources)
|
|
49
|
+
sources
|
|
50
|
+
else
|
|
51
|
+
{}
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
private_class_method :index
|
|
56
|
+
|
|
57
|
+
def visit(node, sources)
|
|
58
|
+
if node.is_a?(Prism::CallNode) && CONTRACT_METHODS.include?(node.name) && node.block
|
|
59
|
+
body = node.block.body
|
|
60
|
+
if body
|
|
61
|
+
source = body.location.slice.strip
|
|
62
|
+
sources[[node.block.location.start_line, node.name]] << source
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
node.compact_child_nodes.each { |child| visit(child, sources) }
|
|
67
|
+
end
|
|
68
|
+
private_class_method :visit
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/prepost.rb
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "prepost/version"
|
|
4
|
+
require_relative "prepost/source_extractor"
|
|
5
|
+
|
|
6
|
+
module Prepost
|
|
7
|
+
Contract = Struct.new(:kind, :name, :predicate, :source_location, :source_ordinal, keyword_init: true)
|
|
8
|
+
ContractSet = Struct.new(:preconditions, :postconditions, :invariants, keyword_init: true)
|
|
9
|
+
MethodDefinition = Struct.new(:parameters, :visibility, :preconditions, :postconditions, keyword_init: true)
|
|
10
|
+
Invocation = Struct.new(:receiver, :arguments, :values, :old, :result, keyword_init: true)
|
|
11
|
+
|
|
12
|
+
UNDEFINED = Object.new.freeze
|
|
13
|
+
CONTEXT_KEY = :__prepost_context_stack
|
|
14
|
+
EMPTY_ARGUMENTS = {}.freeze
|
|
15
|
+
|
|
16
|
+
@enabled = false
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def enable!
|
|
20
|
+
@enabled = true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def disable!
|
|
24
|
+
@enabled = false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def enabled?
|
|
28
|
+
@enabled
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class Violation < StandardError
|
|
33
|
+
attr_reader :contract_name,
|
|
34
|
+
:contract_source,
|
|
35
|
+
:contract_source_location,
|
|
36
|
+
:contract_description,
|
|
37
|
+
:method_name,
|
|
38
|
+
:receiver
|
|
39
|
+
|
|
40
|
+
alias contract_location contract_source_location
|
|
41
|
+
|
|
42
|
+
def initialize(contract:, method_name:, receiver:)
|
|
43
|
+
@contract_name = contract.name
|
|
44
|
+
@contract_source = SourceExtractor.extract(contract.kind, contract.predicate, contract.source_ordinal)
|
|
45
|
+
@contract_source_location = contract.source_location
|
|
46
|
+
@contract_description = contract.name || @contract_source || contract.source_location
|
|
47
|
+
@method_name = method_name
|
|
48
|
+
@receiver = receiver
|
|
49
|
+
|
|
50
|
+
label = contract.name || @contract_source
|
|
51
|
+
display_label = label&.to_s
|
|
52
|
+
if display_label&.include?("\n")
|
|
53
|
+
message = +"#{self.class::KIND} failed for #{receiver.class}##{method_name}:\n"
|
|
54
|
+
message << display_label.lines.map { |line| " #{line}" }.join
|
|
55
|
+
else
|
|
56
|
+
message = +"#{self.class::KIND}"
|
|
57
|
+
message << " `#{display_label}`" if display_label
|
|
58
|
+
message << " failed for #{receiver.class}##{method_name}"
|
|
59
|
+
end
|
|
60
|
+
message << "\nDeclared at #{contract.source_location}" if contract.source_location
|
|
61
|
+
super(message)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class PreconditionViolation < Violation
|
|
66
|
+
KIND = "Precondition"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
class PostconditionViolation < Violation
|
|
70
|
+
KIND = "Postcondition"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
class InvariantViolation < Violation
|
|
74
|
+
KIND = "Invariant"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
class OldState
|
|
78
|
+
def self.capture(receiver)
|
|
79
|
+
values = receiver.instance_variables.to_h do |variable|
|
|
80
|
+
value = receiver.instance_variable_get(variable)
|
|
81
|
+
[variable.to_s.delete_prefix("@").to_sym, snapshot(value)]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
new(values)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.snapshot(value)
|
|
88
|
+
return value if value.nil? || value == true || value == false || value.is_a?(Numeric) || value.is_a?(Symbol)
|
|
89
|
+
return value if value.frozen?
|
|
90
|
+
|
|
91
|
+
value.dup
|
|
92
|
+
rescue TypeError
|
|
93
|
+
value
|
|
94
|
+
end
|
|
95
|
+
private_class_method :snapshot
|
|
96
|
+
|
|
97
|
+
def initialize(values)
|
|
98
|
+
@values = values.freeze
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def [](name)
|
|
102
|
+
@values.fetch(name.to_sym)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def method_missing(name, *arguments, &block)
|
|
106
|
+
return self[name] if arguments.empty? && block.nil? && @values.key?(name)
|
|
107
|
+
|
|
108
|
+
super
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def respond_to_missing?(name, include_private = false)
|
|
112
|
+
@values.key?(name) || super
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
module Context
|
|
117
|
+
module_function
|
|
118
|
+
|
|
119
|
+
def with(invocation)
|
|
120
|
+
stack = Thread.current[CONTEXT_KEY] ||= []
|
|
121
|
+
stack << invocation
|
|
122
|
+
yield
|
|
123
|
+
ensure
|
|
124
|
+
stack.pop
|
|
125
|
+
Thread.current[CONTEXT_KEY] = nil if stack.empty?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def evaluating?(receiver)
|
|
129
|
+
Thread.current[CONTEXT_KEY]&.any? { |invocation| invocation.receiver.equal?(receiver) }
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def lookup(receiver, name)
|
|
133
|
+
invocation = Thread.current[CONTEXT_KEY]&.last
|
|
134
|
+
return UNDEFINED unless invocation&.receiver.equal?(receiver)
|
|
135
|
+
return invocation.old if name == :old && !invocation.old.equal?(UNDEFINED)
|
|
136
|
+
return invocation.result if name == :result && !invocation.result.equal?(UNDEFINED)
|
|
137
|
+
|
|
138
|
+
invocation.arguments.fetch(name, UNDEFINED)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
module Runtime
|
|
143
|
+
VIOLATIONS = {
|
|
144
|
+
pre: PreconditionViolation,
|
|
145
|
+
post: PostconditionViolation,
|
|
146
|
+
invariant: InvariantViolation,
|
|
147
|
+
}.freeze
|
|
148
|
+
|
|
149
|
+
module_function
|
|
150
|
+
|
|
151
|
+
def install(owner, method_name, parameters, visibility, contracts)
|
|
152
|
+
wrapper = owner.instance_variable_get(:@__prepost_wrapper)
|
|
153
|
+
unless wrapper
|
|
154
|
+
wrapper = Module.new do
|
|
155
|
+
def method_missing(name, *arguments, **keywords, &block)
|
|
156
|
+
if arguments.empty? && keywords.empty? && block.nil?
|
|
157
|
+
value = Prepost::Context.lookup(self, name)
|
|
158
|
+
return value unless value.equal?(Prepost::UNDEFINED)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
super
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def respond_to_missing?(name, include_private = false)
|
|
165
|
+
!Prepost::Context.lookup(self, name).equal?(Prepost::UNDEFINED) || super
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
owner.instance_variable_set(:@__prepost_wrapper, wrapper)
|
|
169
|
+
owner.prepend(wrapper)
|
|
170
|
+
end
|
|
171
|
+
wrapper.define_method(method_name) do |*arguments, **keywords, &block|
|
|
172
|
+
Prepost::Runtime.invoke(
|
|
173
|
+
self,
|
|
174
|
+
method_name,
|
|
175
|
+
parameters,
|
|
176
|
+
contracts,
|
|
177
|
+
arguments,
|
|
178
|
+
keywords,
|
|
179
|
+
block,
|
|
180
|
+
) { super(*arguments, **keywords, &block) }
|
|
181
|
+
end
|
|
182
|
+
wrapper.send(visibility, method_name)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def invoke(receiver, method_name, parameters, contracts, positional, keywords, block)
|
|
186
|
+
return yield unless Prepost.enabled?
|
|
187
|
+
return yield if Context.evaluating?(receiver)
|
|
188
|
+
|
|
189
|
+
arguments, values = bind_arguments(parameters, positional, keywords, block)
|
|
190
|
+
invocation = Invocation.new(
|
|
191
|
+
receiver: receiver,
|
|
192
|
+
arguments: arguments,
|
|
193
|
+
values: values,
|
|
194
|
+
old: OldState.capture(receiver),
|
|
195
|
+
result: UNDEFINED,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
check_all(contracts.invariants, invocation, method_name) unless method_name == :initialize
|
|
199
|
+
check_all(contracts.preconditions, invocation, method_name)
|
|
200
|
+
|
|
201
|
+
invocation.result = yield
|
|
202
|
+
|
|
203
|
+
check_all(contracts.postconditions, invocation, method_name)
|
|
204
|
+
check_all(contracts.invariants, invocation, method_name)
|
|
205
|
+
invocation.result
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def bind_arguments(parameters, positional, keywords, block)
|
|
209
|
+
named = {}
|
|
210
|
+
values = []
|
|
211
|
+
positional_index = 0
|
|
212
|
+
|
|
213
|
+
parameters.each do |kind, name|
|
|
214
|
+
case kind
|
|
215
|
+
when :req, :opt
|
|
216
|
+
next if positional_index >= positional.length
|
|
217
|
+
|
|
218
|
+
value = positional[positional_index]
|
|
219
|
+
positional_index += 1
|
|
220
|
+
when :rest
|
|
221
|
+
value = positional.drop(positional_index)
|
|
222
|
+
positional_index = positional.length
|
|
223
|
+
when :keyreq, :key
|
|
224
|
+
next unless keywords.key?(name)
|
|
225
|
+
|
|
226
|
+
value = keywords.fetch(name)
|
|
227
|
+
when :keyrest
|
|
228
|
+
value = keywords
|
|
229
|
+
when :block
|
|
230
|
+
value = block
|
|
231
|
+
else
|
|
232
|
+
next
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
named[name] = value if name
|
|
236
|
+
values << value
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
[named.freeze, values.freeze]
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def check_all(contracts, invocation, method_name)
|
|
243
|
+
contracts.each do |contract|
|
|
244
|
+
original_arguments = invocation.arguments
|
|
245
|
+
original_old = invocation.old
|
|
246
|
+
original_result = invocation.result
|
|
247
|
+
block_arguments = case contract.kind
|
|
248
|
+
when :invariant
|
|
249
|
+
invocation.arguments = EMPTY_ARGUMENTS
|
|
250
|
+
invocation.old = UNDEFINED
|
|
251
|
+
invocation.result = UNDEFINED
|
|
252
|
+
[]
|
|
253
|
+
when :post
|
|
254
|
+
invocation.values + [invocation.result, invocation.old]
|
|
255
|
+
else
|
|
256
|
+
invocation.old = UNDEFINED
|
|
257
|
+
invocation.result = UNDEFINED
|
|
258
|
+
invocation.values
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
begin
|
|
262
|
+
passed = Context.with(invocation) do
|
|
263
|
+
invocation.receiver.instance_exec(*block_arguments, &contract.predicate)
|
|
264
|
+
end
|
|
265
|
+
ensure
|
|
266
|
+
invocation.arguments = original_arguments
|
|
267
|
+
invocation.old = original_old
|
|
268
|
+
invocation.result = original_result
|
|
269
|
+
end
|
|
270
|
+
next if passed
|
|
271
|
+
|
|
272
|
+
raise VIOLATIONS.fetch(contract.kind).new(
|
|
273
|
+
contract: contract,
|
|
274
|
+
method_name: method_name,
|
|
275
|
+
receiver: invocation.receiver,
|
|
276
|
+
)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
module MethodHooks
|
|
282
|
+
def method_added(method_name)
|
|
283
|
+
return super if @__prepost_handling_method
|
|
284
|
+
|
|
285
|
+
pending = @__prepost_pending ||= Prepost.empty_contract_set
|
|
286
|
+
definitions = @__prepost_methods ||= {}
|
|
287
|
+
return super if definitions.key?(method_name) && Prepost.method_contracts_empty?(pending)
|
|
288
|
+
|
|
289
|
+
method = instance_method(method_name)
|
|
290
|
+
parameters = method.parameters
|
|
291
|
+
visibility = if private_method_defined?(method_name)
|
|
292
|
+
:private
|
|
293
|
+
elsif protected_method_defined?(method_name)
|
|
294
|
+
:protected
|
|
295
|
+
else
|
|
296
|
+
:public
|
|
297
|
+
end
|
|
298
|
+
invariants = @__prepost_invariants ||= []
|
|
299
|
+
should_install = !Prepost.method_contracts_empty?(pending) ||
|
|
300
|
+
(!invariants.empty? && (visibility == :public || method_name == :initialize))
|
|
301
|
+
|
|
302
|
+
@__prepost_pending = Prepost.empty_contract_set
|
|
303
|
+
@__prepost_handling_method = true
|
|
304
|
+
begin
|
|
305
|
+
super
|
|
306
|
+
ensure
|
|
307
|
+
@__prepost_handling_method = false
|
|
308
|
+
end
|
|
309
|
+
return unless should_install
|
|
310
|
+
|
|
311
|
+
definition = MethodDefinition.new(
|
|
312
|
+
parameters: parameters,
|
|
313
|
+
visibility: visibility,
|
|
314
|
+
preconditions: pending.preconditions.dup.freeze,
|
|
315
|
+
postconditions: pending.postconditions.dup.freeze,
|
|
316
|
+
).freeze
|
|
317
|
+
definitions[method_name] = definition
|
|
318
|
+
Prepost.install(self, method_name, definition)
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def self.extended(owner)
|
|
323
|
+
owner.singleton_class.prepend(MethodHooks)
|
|
324
|
+
owner.instance_variable_set(:@__prepost_pending, empty_contract_set)
|
|
325
|
+
owner.instance_variable_set(:@__prepost_invariants, [])
|
|
326
|
+
owner.instance_variable_set(:@__prepost_methods, {})
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def self.empty_contract_set
|
|
330
|
+
ContractSet.new(preconditions: [], postconditions: [], invariants: [])
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
def self.method_contracts_empty?(contracts)
|
|
334
|
+
contracts.preconditions.empty? && contracts.postconditions.empty?
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def self.install(owner, method_name, definition)
|
|
338
|
+
contracts = ContractSet.new(
|
|
339
|
+
preconditions: definition.preconditions,
|
|
340
|
+
postconditions: definition.postconditions,
|
|
341
|
+
invariants: owner.instance_variable_get(:@__prepost_invariants),
|
|
342
|
+
)
|
|
343
|
+
Runtime.install(
|
|
344
|
+
owner,
|
|
345
|
+
method_name,
|
|
346
|
+
definition.parameters,
|
|
347
|
+
definition.visibility,
|
|
348
|
+
contracts,
|
|
349
|
+
)
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def pre(name = nil, &predicate)
|
|
353
|
+
add_contract(:pre, name, predicate)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def post(name = nil, &predicate)
|
|
357
|
+
add_contract(:post, name, predicate)
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
def inv(name = nil, &predicate)
|
|
361
|
+
contract = build_contract(:invariant, name, predicate)
|
|
362
|
+
invariants = @__prepost_invariants ||= []
|
|
363
|
+
invariants << contract
|
|
364
|
+
|
|
365
|
+
definitions = @__prepost_methods ||= {}
|
|
366
|
+
method_names = public_instance_methods(false)
|
|
367
|
+
method_names << :initialize if private_instance_methods(false).include?(:initialize)
|
|
368
|
+
method_names.each do |method_name|
|
|
369
|
+
next if definitions.key?(method_name)
|
|
370
|
+
|
|
371
|
+
method = instance_method(method_name)
|
|
372
|
+
visibility = method_name == :initialize ? :private : :public
|
|
373
|
+
definition = MethodDefinition.new(
|
|
374
|
+
parameters: method.parameters,
|
|
375
|
+
visibility: visibility,
|
|
376
|
+
preconditions: [].freeze,
|
|
377
|
+
postconditions: [].freeze,
|
|
378
|
+
).freeze
|
|
379
|
+
definitions[method_name] = definition
|
|
380
|
+
Prepost.install(self, method_name, definition)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
nil
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
private
|
|
387
|
+
|
|
388
|
+
def add_contract(kind, name, predicate)
|
|
389
|
+
contracts = @__prepost_pending ||= Prepost.empty_contract_set
|
|
390
|
+
collection = kind == :pre ? contracts.preconditions : contracts.postconditions
|
|
391
|
+
collection << build_contract(kind, name, predicate)
|
|
392
|
+
nil
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
def build_contract(kind, name, predicate)
|
|
396
|
+
raise ArgumentError, "contract name must be a Symbol or String" unless name.nil? || name.is_a?(Symbol) || name.is_a?(String)
|
|
397
|
+
raise ArgumentError, "contract requires a block" unless predicate
|
|
398
|
+
|
|
399
|
+
source_location, source_ordinal = SourceExtractor.register(kind, predicate)
|
|
400
|
+
Contract.new(
|
|
401
|
+
kind: kind,
|
|
402
|
+
name: name&.to_sym,
|
|
403
|
+
predicate: predicate,
|
|
404
|
+
source_location: source_location,
|
|
405
|
+
source_ordinal: source_ordinal,
|
|
406
|
+
).freeze
|
|
407
|
+
end
|
|
408
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: prepost
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alex
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: prism
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.24'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0.24'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: minitest
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '5.0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '5.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rake
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '13.0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '13.0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: sorbet-runtime
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0.5'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0.5'
|
|
68
|
+
description: Adds design-by-contract runtime checks to Ruby methods.
|
|
69
|
+
executables: []
|
|
70
|
+
extensions: []
|
|
71
|
+
extra_rdoc_files: []
|
|
72
|
+
files:
|
|
73
|
+
- LICENSE.txt
|
|
74
|
+
- README.md
|
|
75
|
+
- lib/prepost.rb
|
|
76
|
+
- lib/prepost/source_extractor.rb
|
|
77
|
+
- lib/prepost/version.rb
|
|
78
|
+
homepage: https://github.com/Morriar/prepost
|
|
79
|
+
licenses:
|
|
80
|
+
- MIT
|
|
81
|
+
metadata: {}
|
|
82
|
+
rdoc_options: []
|
|
83
|
+
require_paths:
|
|
84
|
+
- lib
|
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.1'
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
requirements: []
|
|
96
|
+
rubygems_version: 4.0.21
|
|
97
|
+
specification_version: 4
|
|
98
|
+
summary: Runtime preconditions, postconditions, and invariants for Ruby
|
|
99
|
+
test_files: []
|