Created
December 10, 2013 09:33
-
-
Save chikadance/7888016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RSpec | |
module DescribeExampleMethods | |
def initialize(before_method, let_method) | |
self.class.send(:define_method, :before_it) do |*args| | |
before_method.call | |
end | |
self.class.send(:define_method, :let_method) do |*args| | |
let_method.call | |
end | |
end | |
def let(shortcut, &blk) | |
define_singleton_method(shortcut, &blk) | |
end | |
def before(&blk) | |
p = method(:before_it).to_proc.dup | |
define_singleton_method(:before_it) do | |
p.call | |
blk.call | |
end | |
end | |
def describe(msg, &blk) | |
::RSpec::DescribeExample.new(method(:before_it).to_proc, method(:let_it).to_proc).instance_eval(&blk) | |
end | |
def before_it | |
end | |
def let_it | |
end | |
def it(&blk) | |
::RSpec::it_examples << ::RSpec::ItExample.new(method(:before_it), method(:let_it), &blk) | |
end | |
end | |
end | |
module RSpec | |
class ItExample | |
def initialize(before_method, let_method, &blk) | |
@before_method = before_method | |
@let_method = let_method | |
@blk = blk | |
end | |
def run | |
@before_method.call | |
@let_method.call | |
@blk.call | |
end | |
end | |
class << self | |
attr_accessor :it_examples | |
def it_examples | |
@it_examples.flatten! if defined?(@it_examples) and @it_examples.respond_to?(:flatten!) | |
@it_examples.uniq! if defined?(@it_examples) and @it_examples.respond_to?(:uniq!) | |
@it_examples ||= [] | |
end | |
end | |
class DescribeExample | |
include ::RSpec::DescribeExampleMethods | |
end | |
end | |
module RSpec | |
class << self | |
include ::RSpec::DescribeExampleMethods | |
end | |
end | |
module RSpec | |
class C | |
end | |
describe "a msg" do | |
#let(:c) { C.new } | |
before do | |
p '1 level before' | |
end | |
describe "2 level" do | |
before do | |
p '2 level before' | |
end | |
it do | |
p '2 level it' | |
end | |
end | |
it do | |
p '1 level it' | |
end | |
end | |
end | |
RSpec.module_eval File.read(File.expand_path("../ex3_spec.rb", __FILE__)) | |
RSpec.it_examples.each do |it_example| | |
it_example.run | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment