|
Revision 10184, 1.3 kB
(checked in by yugui, 4 years ago)
|
|
lang/ruby/misc/spec_qualifier.rb:
|
-
Property svn:keywords set to
Id Author
|
| Line | |
|---|
| 1 | # spec_qualifiers.rb |
|---|
| 2 | # $Id$ |
|---|
| 3 | # |
|---|
| 4 | # :brief |
|---|
| 5 | # An implementation of `each_of' qualifier. |
|---|
| 6 | # (see http://d.hatena.ne.jp/moro/20061029/1162116778) |
|---|
| 7 | # :description |
|---|
| 8 | # Provides `each_of', `all_of' qualifier to RSpec. This version of spec_qualifiers.rb is for RSpec 1.1.3. |
|---|
| 9 | # :synoposis |
|---|
| 10 | # like bellow: |
|---|
| 11 | # describe Foo do |
|---|
| 12 | # before do |
|---|
| 13 | # @array = Foo.some_method |
|---|
| 14 | # end |
|---|
| 15 | # it "all should be nil" do |
|---|
| 16 | # # This is equivalent to @array.each{|item| item.should be_nil}. |
|---|
| 17 | # all_of(@array).should be_nil |
|---|
| 18 | # end |
|---|
| 19 | # end |
|---|
| 20 | # :author |
|---|
| 21 | # $Author$ |
|---|
| 22 | # :licence |
|---|
| 23 | # MIT license |
|---|
| 24 | |
|---|
| 25 | module Spec |
|---|
| 26 | module Example |
|---|
| 27 | if RUBY_VERSION >= '1.9' |
|---|
| 28 | BasicObject = Kernel::BasicObject |
|---|
| 29 | else |
|---|
| 30 | class BasicObject |
|---|
| 31 | instance_methods.each do |name| |
|---|
| 32 | unless %w[ object_id __id__ __send__ respond_to? ].include?(name) |
|---|
| 33 | undef_method(name) |
|---|
| 34 | end |
|---|
| 35 | end |
|---|
| 36 | self |
|---|
| 37 | end |
|---|
| 38 | end |
|---|
| 39 | |
|---|
| 40 | class EachOfProxy < BasicObject |
|---|
| 41 | def initialize(targets) |
|---|
| 42 | @targets = targets |
|---|
| 43 | end |
|---|
| 44 | def method_missing(msg, *args) |
|---|
| 45 | new_targets = @targets.map { |target| |
|---|
| 46 | target.__send__(msg, *args) |
|---|
| 47 | } |
|---|
| 48 | EachOfProxy.new(new_targets) |
|---|
| 49 | end |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | module ExampleMethods |
|---|
| 53 | def each_of(targets) |
|---|
| 54 | EachOfProxy.new(targets) |
|---|
| 55 | end |
|---|
| 56 | alias all_of each_of |
|---|
| 57 | end |
|---|
| 58 | end |
|---|
| 59 | end |
|---|