-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from grosser/grosser/helpers
new pending / with_env / capture_stdX helpers
- Loading branch information
Showing
7 changed files
with
182 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Maxitest | ||
module Helpers | ||
module InstanceMethods | ||
def with_env(env) | ||
_synchronize do | ||
old = ENV.to_h | ||
env.each { |k, v| ENV[k.to_s] = v } | ||
yield | ||
ensure | ||
ENV.replace old | ||
end | ||
end | ||
|
||
# stripped down version of capture_io | ||
def capture_stdout | ||
_synchronize do | ||
begin | ||
captured_stdout = StringIO.new | ||
orig_stdout = $stdout | ||
$stdout = captured_stdout | ||
yield | ||
return captured_stdout.string | ||
ensure | ||
$stdout = orig_stdout | ||
end | ||
end | ||
end | ||
|
||
# stripped down version of capture_io | ||
def capture_stderr | ||
_synchronize do | ||
begin | ||
captured_stderr = StringIO.new | ||
orig_stderr = $stderr | ||
$stderr = captured_stderr | ||
yield | ||
return captured_stderr.string | ||
ensure | ||
$stderr = orig_stderr | ||
end | ||
end | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def with_env(env) | ||
around { |t| with_env(env, &t) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
Minitest::Test.send(:include, Maxitest::Helpers::InstanceMethods) | ||
Minitest::Test.send(:extend, Maxitest::Helpers::ClassMethods) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "./spec/cases/helper" | ||
|
||
describe "helpers" do | ||
describe "#with_env" do | ||
it "changes env" do | ||
with_env A: "b" do | ||
_(ENV["A"]).must_equal "b" | ||
end | ||
end | ||
|
||
it "restores env" do | ||
ENV["A"] = "c" | ||
with_env A: "b" do | ||
ENV["B"] = "a" | ||
_(ENV["A"]).must_equal "b" | ||
end | ||
_(ENV["A"]).must_equal "c" | ||
_(ENV["B"]).must_be_nil | ||
end | ||
end | ||
|
||
describe ".with_env" do | ||
with_env A: "a" | ||
it "sets env" do | ||
_(ENV["A"]).must_equal "a" | ||
end | ||
end | ||
|
||
describe "#capture_stdout" do | ||
it "keeps stdout" do | ||
_(capture_stdout { puts "X" }).must_equal "X\n" | ||
end | ||
|
||
it "lets stderr through" do | ||
out, err = capture_io { capture_stdout { warn "X" } } | ||
_(out).must_equal "" | ||
_(err).must_equal "X\n" | ||
end | ||
end | ||
|
||
describe "#capture_stderr" do | ||
it "keeps stderr" do | ||
_(capture_stderr { warn "X" }).must_equal "X\n" | ||
end | ||
|
||
it "lets stdout through" do | ||
out, err = capture_io { capture_stderr { puts "X" } } | ||
_(out).must_equal "X\n" | ||
_(err).must_equal "" | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,67 @@ | ||
require "./spec/cases/helper" | ||
|
||
describe 2 do | ||
it "is even" do | ||
2.even?.must_equal true | ||
it "runs regular tests" do | ||
_(2.even?).must_equal true | ||
end | ||
|
||
it "is not odd" do | ||
pending "fail" do | ||
2.must_equal 2 | ||
it "fails when pending is fixed" do | ||
e = assert_raises Minitest::Assertion do | ||
pending "fail" do | ||
_(2).must_equal 2 | ||
end | ||
end | ||
_(e.message).must_include "fixed" | ||
end | ||
|
||
it "pends" do | ||
pending "Skipping with a reason" do | ||
2.must_equal 3 | ||
it "skips when pending still needed" do | ||
e = assert_raises Minitest::Skip do | ||
pending "Skipping with reason" do | ||
_(2).must_equal 3, "This should not fail" | ||
end | ||
end | ||
_(e.message).must_equal "Skipping with reason" | ||
end | ||
|
||
it "pends exceptions" do | ||
pending "Skipping with exception" do | ||
raise "Oh noes" | ||
it "skips exceptions" do | ||
e = assert_raises Minitest::Skip do | ||
pending "Skipping with exception" do | ||
raise "Oh noes" | ||
end | ||
end | ||
_(e.message).must_equal "Skipping with exception" | ||
end | ||
|
||
it "pends without text" do | ||
it "skips without reason" do | ||
pending do | ||
2.must_equal 3 | ||
_(2).must_equal 3 | ||
end | ||
end | ||
|
||
it "fails without block" do | ||
pending "success" | ||
e = assert_raises(ArgumentError) do | ||
pending "success" | ||
end | ||
_(e.message).must_equal "Need a block to execute" | ||
end | ||
|
||
it "can disable pending" do | ||
pending "Not skipping", if: false do | ||
_(2).must_equal 2 | ||
end | ||
end | ||
|
||
it "can enable pending" do | ||
pending "skipping conditionally", if: true do | ||
raise "Oh noes" | ||
end | ||
end | ||
|
||
it "fails when given unknown kwargs" do | ||
assert_raises(ArgumentError) do | ||
pending "skipping conditionally", unless: true do | ||
raise "Oh noes" | ||
end | ||
end | ||
end | ||
end |
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