dao 8.0.0 → 8.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,295 +0,0 @@
1
- ##
2
- #
3
- module Wrap; end
4
-
5
- ##
6
- #
7
- class << Wrap
8
- Wrap::Version = '1.5.2' unless defined?(Wrap::Version)
9
-
10
- def version
11
- Wrap::Version
12
- end
13
-
14
- def dependencies
15
- {
16
- 'map' => [ 'map' , ' >= 4.7.1' ]
17
- }
18
- end
19
-
20
- def description
21
- 'non-sucking :before and :after filters for any ruby class'
22
- end
23
- end
24
-
25
- ##
26
- #
27
- begin
28
- require 'rubygems'
29
- Wrap.dependencies.each{|name, dependency| gem(*dependency)}
30
- rescue LoadError
31
- nil
32
- end
33
-
34
- ##
35
- #
36
- require 'map'
37
-
38
- ##
39
- #
40
- module Wrap
41
- def Wrap.included(other)
42
- super
43
- ensure
44
- other.send(:instance_eval, &ClassMethods)
45
- other.send(:class_eval, &InstanceMethods)
46
- end
47
-
48
- def Wrap.code_for(method)
49
- name = method.name.to_s
50
- arity = method.arity
51
-
52
- case
53
- when arity == 0
54
- signature = <<-__.strip
55
- def #{ name }(&block)
56
- args = []
57
- __
58
-
59
- when arity < 0
60
- argv = Array.new(arity.abs - 1){|i| "arg#{ i }"}
61
- argv.push('*args')
62
- argv = argv.join(', ')
63
-
64
- signature = <<-__.strip
65
- def #{ name }(#{ argv }, &block)
66
- args = [#{ argv }]
67
- __
68
-
69
- when arity > 0
70
- argv = Array.new(arity){|i| "arg#{ i }"}
71
- argv = argv.join(', ')
72
-
73
- signature = <<-__.strip
74
- def #{ name }(#{ argv }, &block)
75
- args = [#{ argv }]
76
- __
77
- end
78
-
79
- code =
80
- <<-__
81
- #{ signature.strip }
82
-
83
- if running_callbacks?(#{ name.inspect })
84
- return wrapped_#{ name }(*args, &block)
85
- end
86
-
87
- running_callbacks(#{ name.inspect }) do
88
- catch(:halt) do
89
- return false if run_callbacks(:before, #{ name.inspect }, args)==false
90
-
91
- begin
92
- result = wrapped_#{ name }(*args, &block)
93
- ensure
94
- run_callbacks(:after, #{ name.inspect }, [result]) unless $!
95
- end
96
- end
97
- end
98
- end
99
- __
100
- end
101
-
102
- ClassMethods = proc do
103
- def method_added(name)
104
- return super if wrapping?
105
- begin
106
- super
107
- ensure
108
- wrap!(name) if wrapped?(name)
109
- end
110
- end
111
-
112
- def include(other)
113
- super
114
- ensure
115
- other.instance_methods.each do |name|
116
- if wrapped?(name)
117
- begin
118
- remove_method(name)
119
- rescue NameError
120
- nil
121
- end
122
- wrap!(name)
123
- end
124
- end
125
- end
126
-
127
- def wrap(name, *args, &block)
128
- wrapped!(name)
129
-
130
- wrap!(name) if
131
- begin
132
- instance_method(name)
133
- true
134
- rescue NameError
135
- false
136
- end
137
- end
138
-
139
- def wrapped!(name)
140
- name = name.to_s
141
- wrapped.push(name) unless wrapped.include?(name)
142
- name
143
- end
144
-
145
- def wrapped
146
- @wrapped ||= []
147
- end
148
-
149
- def wrapped?(name)
150
- ancestors.any?{|ancestor| ancestor.respond_to?(:wrapped) and ancestor.wrapped.include?(name.to_s)}
151
- end
152
-
153
- def wrap!(name)
154
- name = name.to_s
155
- method = instance_method(name)
156
- arity = method.arity
157
-
158
- wrapping! name do
159
- name = name.to_s
160
- wrapped_name = "wrapped_#{ name }"
161
-
162
- begin
163
- remove_method(wrapped_name)
164
- rescue NameError
165
- nil
166
- end
167
-
168
- alias_method(wrapped_name, name)
169
-
170
- module_eval(Wrap.code_for(method))
171
- end
172
- end
173
-
174
- def wrapping!(name, &block)
175
- name = name.to_s
176
- @wrapping ||= []
177
-
178
- return if @wrapping.last == name
179
-
180
- @wrapping.push(name)
181
-
182
- begin
183
- block.call
184
- ensure
185
- @wrapping.pop
186
- end
187
- end
188
-
189
- def wrapping?(*name)
190
- @wrapping ||= []
191
-
192
- if name.empty?
193
- !@wrapping.empty?
194
- else
195
- @wrapping.last == name.last.to_s
196
- end
197
- end
198
-
199
- def callbacks
200
- @callbacks ||= Map.new
201
- end
202
-
203
- def initialize_callbacks!(name)
204
- callbacks[name] ||= Map[ :before, [], :after, [] ]
205
- callbacks[name]
206
- end
207
-
208
- def before(name, *args, &block)
209
- wrap(name) unless wrapped?(name)
210
- name = wrap_expand_aliases(name)
211
- cb = initialize_callbacks!(name)
212
- cb.before.push(args.shift || block)
213
- end
214
-
215
- def after(name, *args, &block)
216
- wrap(name) unless wrapped?(name)
217
- name = wrap_expand_aliases(name)
218
- cb = initialize_callbacks!(name)
219
- cb.after.push(args.shift || block)
220
- end
221
-
222
- def wrap_aliases
223
- @@wrap_aliases ||= Hash.new
224
- end
225
-
226
- def wrap_alias(dst, src)
227
- wrap_aliases[dst.to_s] = src.to_s
228
- end
229
-
230
- def wrap_expand_aliases(name)
231
- name = name.to_s
232
- loop do
233
- break unless wrap_aliases.has_key?(name)
234
- name = wrap_aliases[name]
235
- end
236
- name
237
- end
238
- end
239
-
240
- InstanceMethods = proc do
241
- def running_callbacks(name, &block)
242
- name = name.to_s
243
- @running_callbacks ||= []
244
- return block.call() if @running_callbacks.last == name
245
-
246
- @running_callbacks.push(name)
247
-
248
- begin
249
- block.call()
250
- ensure
251
- @running_callbacks.pop
252
- end
253
- end
254
-
255
- def running_callbacks?(*name)
256
- @running_callbacks ||= []
257
-
258
- if name.empty?
259
- @running_callbacks.last
260
- else
261
- @running_callbacks.last == name.last.to_s
262
- end
263
- end
264
-
265
- def run_callbacks(which, name, argv)
266
- which = which.to_s.to_sym
267
- name = name.to_s
268
- list = []
269
-
270
- self.class.ancestors.each do |ancestor|
271
- next unless ancestor.respond_to?(:callbacks)
272
-
273
- if ancestor.callbacks.is_a?(Map) and ancestor.callbacks[name].is_a?(Map)
274
- callbacks = ancestor.callbacks[name][which]
275
- accumulate = (which == :before ? :unshift : :push)
276
- list.send(accumulate, *callbacks) if callbacks.is_a?(Array)
277
- end
278
- end
279
-
280
- list.each do |callback|
281
- block = callback.respond_to?(:call) ? callback : proc{ send(callback.to_s.to_sym) }
282
- args = argv.slice(0 .. (block.arity > 0 ? block.arity : -1))
283
- result = instance_exec(*args, &block)
284
- return false if result == false
285
- end
286
-
287
- true
288
- end
289
-
290
- def halt!(*args)
291
- value = args.size == 0 ? false : args.shift
292
- throw(:halt, value)
293
- end
294
- end
295
- end
@@ -1,195 +0,0 @@
1
- require 'test/unit'
2
-
3
- testdir = File.expand_path(File.dirname(__FILE__))
4
- rootdir = File.dirname(testdir)
5
- libdir = File.join(rootdir, 'lib')
6
-
7
- STDOUT.sync = true
8
-
9
- $:.unshift(testdir) unless $:.include?(testdir)
10
- $:.unshift(libdir) unless $:.include?(libdir)
11
- $:.unshift(rootdir) unless $:.include?(rootdir)
12
-
13
- class Testing
14
- class Slug < ::String
15
- def Slug.for(*args)
16
- string = args.flatten.compact.join('-')
17
- words = string.to_s.scan(%r/\w+/)
18
- words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
19
- words.delete_if{|word| word.nil? or word.strip.empty?}
20
- new(words.join('-').downcase)
21
- end
22
- end
23
-
24
- class Context
25
- attr_accessor :name
26
-
27
- def initialize(name, *args)
28
- @name = name
29
- end
30
-
31
- def to_s
32
- Slug.for(name)
33
- end
34
- end
35
- end
36
-
37
- def Testing(*args, &block)
38
- Class.new(::Test::Unit::TestCase) do
39
-
40
- ## class methods
41
- #
42
- class << self
43
- def contexts
44
- @contexts ||= []
45
- end
46
-
47
- def context(*args, &block)
48
- return contexts.last if(args.empty? and block.nil?)
49
-
50
- context = Testing::Context.new(*args)
51
- contexts.push(context)
52
-
53
- begin
54
- block.call(context)
55
- ensure
56
- contexts.pop
57
- end
58
- end
59
-
60
- def slug_for(*args)
61
- string = [context, args].flatten.compact.join('-')
62
- words = string.to_s.scan(%r/\w+/)
63
- words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
64
- words.delete_if{|word| word.nil? or word.strip.empty?}
65
- words.join('-').downcase.sub(/_$/, '')
66
- end
67
-
68
- def name() const_get(:Name) end
69
-
70
- def testno()
71
- '%05d' % (@testno ||= 0)
72
- ensure
73
- @testno += 1
74
- end
75
-
76
- def testing(*args, &block)
77
- method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
78
- define_method(method, &block)
79
- end
80
-
81
- def test(*args, &block)
82
- testing(*args, &block)
83
- end
84
-
85
- def setup(&block)
86
- define_method(:setup, &block) if block
87
- end
88
-
89
- def teardown(&block)
90
- define_method(:teardown, &block) if block
91
- end
92
-
93
- def prepare(&block)
94
- @prepare ||= []
95
- @prepare.push(block) if block
96
- @prepare
97
- end
98
-
99
- def cleanup(&block)
100
- @cleanup ||= []
101
- @cleanup.push(block) if block
102
- @cleanup
103
- end
104
- end
105
-
106
- ## configure the subclass!
107
- #
108
- const_set(:Testno, '0')
109
- slug = slug_for(*args).gsub(%r/-/,'_')
110
- name = ['TESTING', '%03d' % const_get(:Testno), slug].delete_if{|part| part.empty?}.join('_')
111
- name = name.upcase!
112
- const_set(:Name, name)
113
- const_set(:Missing, Object.new.freeze)
114
-
115
- ## instance methods
116
- #
117
- alias_method('__assert__', 'assert')
118
-
119
- def assert(*args, &block)
120
- if args.size == 1 and args.first.is_a?(Hash)
121
- options = args.first
122
- expected = getopt(:expected, options){ missing }
123
- actual = getopt(:actual, options){ missing }
124
- if expected == missing and actual == missing
125
- actual, expected, *ignored = options.to_a.flatten
126
- end
127
- expected = expected.call() if expected.respond_to?(:call)
128
- actual = actual.call() if actual.respond_to?(:call)
129
- assert_equal(expected, actual)
130
- end
131
-
132
- if block
133
- label = "assert(#{ args.join(' ') })"
134
- result = nil
135
- assert_nothing_raised{ result = block.call }
136
- __assert__(result, label)
137
- result
138
- else
139
- result = args.shift
140
- label = "assert(#{ args.join(' ') })"
141
- __assert__(result, label)
142
- result
143
- end
144
- end
145
-
146
- def missing
147
- self.class.const_get(:Missing)
148
- end
149
-
150
- def getopt(opt, hash, options = nil, &block)
151
- [opt.to_s, opt.to_s.to_sym].each do |key|
152
- return hash[key] if hash.has_key?(key)
153
- end
154
- default =
155
- if block
156
- block.call
157
- else
158
- options.is_a?(Hash) ? options[:default] : nil
159
- end
160
- return default
161
- end
162
-
163
- def subclass_of exception
164
- class << exception
165
- def ==(other) super or self > other end
166
- end
167
- exception
168
- end
169
-
170
- ##
171
- #
172
- module_eval(&block)
173
-
174
- self.setup()
175
- self.prepare.each{|b| b.call()}
176
-
177
- at_exit{
178
- self.teardown()
179
- self.cleanup.each{|b| b.call()}
180
- }
181
-
182
- self
183
- end
184
- end
185
-
186
-
187
- if $0 == __FILE__
188
-
189
- Testing 'Testing' do
190
- testing('foo'){ assert true }
191
- test{ assert true }
192
- p instance_methods.grep(/test/)
193
- end
194
-
195
- end