Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

after all / before all #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Features
- **red-green** output
- `mtest` executable to **run by line number** and by folder
- full backtrace for errors and assertions with verbose (`-v`)
- `before :all` + `after :all` with `require 'maxitest/before_and_after_all'`
- `let!`
- `order_dependent!` to make your tests run in given order
- `context` for more expression
Expand Down
52 changes: 52 additions & 0 deletions lib/maxitest/before_and_after_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Maxitest
def self.before_all_ran
@before_all_ran ||= {}
end
end

Minitest::Spec::DSL.class_eval do
alias_method :before_without_all, :before

def before(*args, &block)
if args.first == :all
id = rand(99999999999999)
before_without_all(*args[1..-1]) do
ran = Maxitest.before_all_ran
if variables = ran[id]
variables.each { |k,v,| instance_variable_set(k,v) }
else
old_variables = instance_variables
instance_exec(&block)
new_variables = instance_variables - old_variables - [:@_memoized]
ran[id] = new_variables.map { |k| [k, instance_variable_get(k)] }
end
end
else
before_without_all(*args, &block)
end
end

alias_method :after_without_all, :after

def after(*args, &block)
if args.first == :all
after_without_all(*args[1..-1]) do
c = self.class
runnable_methods_count = if c.class_variable_defined?(:@@runnable_methods)
c.class_variable_get(:@@runnable_methods)
else
c.class_variable_set(:@@runnable_methods, c.runnable_methods.size)
end

current = c.instance_variable_get(:@after_ran_count) || 0
if current == runnable_methods_count
instance_exec(&block)
else
c.instance_variable_set(:@after_ran_count, current + 1)
end
end
else
after_without_all(*args, &block)
end
end
end
61 changes: 61 additions & 0 deletions spec/cases/before_and_after_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require "./spec/cases/helper"
require "maxitest/before_and_after_all"

describe 2 do
order_dependent!

let(:from_let) { "from let" }

before :all do
puts "BEFORE ALL A -- #{from_let}"
@before_all = true
end

after :all do
puts "AFTER ALL A -- #{from_let}"
end

before do
puts "BEFORE A -- #{@before_all}"
end

after do
puts "AFTER A"
end

it "a 1" do
true.must_equal true
end

describe "nested" do
order_dependent!

before :all do
puts "BEFORE ALL B -- #{from_let}"
end

after :all do
puts "AFTER ALL B -- #{from_let}"
end

before do
puts "BEFORE B -- #{@before_all}"
end

after do
puts "AFTER B"
end

it "b 1" do
true.must_equal true
end

it "b 2" do
true.must_equal true
end
end

it "a 2" do
true.must_equal true
end
end