minitest-spec-magic 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/minitest-spec-magic.rb +2 -0
- data/lib/minitest/spec/magic.rb +46 -0
- data/lib/minitest/spec/magic/assertions.rb +96 -0
- data/lib/minitest/spec/magic/context.rb +22 -0
- data/lib/minitest/spec/magic/controller.rb +10 -0
- data/lib/minitest/spec/magic/expectations.rb +63 -0
- data/lib/test/unit/testcase.rb +19 -0
- metadata +86 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'test/unit/testcase'
|
3
|
+
|
4
|
+
# Define the main module
|
5
|
+
module MiniTest::Spec::Magic
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'minitest/spec/magic/context'
|
9
|
+
require 'minitest/spec/magic/expectations'
|
10
|
+
require 'minitest/spec/magic/assertions'
|
11
|
+
|
12
|
+
# Extend Test::Unit::TestCase
|
13
|
+
class Test::Unit::TestCase
|
14
|
+
extend MiniTest::Spec::Magic::Context
|
15
|
+
include MiniTest::Spec::Magic::Expectations
|
16
|
+
include MiniTest::Spec::Magic::Assertions
|
17
|
+
end
|
18
|
+
|
19
|
+
# Add magic for ActionController::TestCase
|
20
|
+
if defined?(ActionController)
|
21
|
+
require 'minitest/spec/magic/controller'
|
22
|
+
|
23
|
+
class ActionController::TestCase
|
24
|
+
extend MiniTest::Spec::Magic::Controller
|
25
|
+
subject { @controller }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Filter backtrace with Rails.backtrace_cleaner (Rails pre 3.1.2 only)
|
30
|
+
# Shamelessly "stolen" from https://github.com/metaskills/mini_backtrace
|
31
|
+
if defined?(::Rails) && Rails::VERSION::STRING < '3.1.2' && !MiniTest.method_defined?(:filter_backtrace_with_rails)
|
32
|
+
|
33
|
+
module MiniTest
|
34
|
+
class << self
|
35
|
+
|
36
|
+
def filter_backtrace_with_rails(bt)
|
37
|
+
filter_backtrace_without_rails Rails.backtrace_cleaner.clean(bt)
|
38
|
+
end
|
39
|
+
|
40
|
+
alias :filter_backtrace_without_rails :filter_backtrace
|
41
|
+
alias :filter_backtrace :filter_backtrace_with_rails
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# Shamelessly "stolen" from https://github.com/thoughtbot/shoulda-context
|
2
|
+
#
|
3
|
+
# Adds the following expectations to MiniTest:
|
4
|
+
#
|
5
|
+
# array.must_be_same_as(other) # => see #assert_same_elements
|
6
|
+
# array.must_contain(item) # => see #assert_contains
|
7
|
+
# array.wont_contain(item) # => see #assert_does_not_contain
|
8
|
+
#
|
9
|
+
module MiniTest::Spec::Magic::Assertions
|
10
|
+
|
11
|
+
def self.included(*)
|
12
|
+
MiniTest::Expectations.tap do |x|
|
13
|
+
x.infect_an_assertion :assert_contains, :must_contain, :reverse
|
14
|
+
x.infect_an_assertion :assert_does_not_contain, :wont_contain, :reverse
|
15
|
+
x.infect_an_assertion :assert_same_elements, :must_be_same_as
|
16
|
+
end
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
# Asserts that two arrays contain the same elements, the same number of times. Essentially ==, but unordered.
|
21
|
+
#
|
22
|
+
# assert_same_elements([:a, :b, :c], [:c, :a, :b]) => passes
|
23
|
+
def assert_same_elements(a1, a2, msg = nil)
|
24
|
+
[:select, :inject, :size].each do |m|
|
25
|
+
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
|
26
|
+
end
|
27
|
+
|
28
|
+
assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
|
29
|
+
assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
|
30
|
+
|
31
|
+
assert_equal(a1h, a2h, msg)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Asserts that the given collection contains item x. If x is a regular expression, ensure that
|
35
|
+
# at least one element from the collection matches x. +extra_msg+ is appended to the error message if the assertion fails.
|
36
|
+
#
|
37
|
+
# assert_contains(['a', '1'], /\d/) => passes
|
38
|
+
# assert_contains(['a', '1'], 'a') => passes
|
39
|
+
# assert_contains(['a', '1'], /not there/) => fails
|
40
|
+
def assert_contains(collection, x, extra_msg = "")
|
41
|
+
collection = [collection] unless collection.is_a?(Array)
|
42
|
+
msg = "#{x.inspect} not found in #{collection.to_a.inspect} #{extra_msg}"
|
43
|
+
case x
|
44
|
+
when Regexp
|
45
|
+
assert(collection.detect { |e| e =~ x }, msg)
|
46
|
+
else
|
47
|
+
assert(collection.include?(x), msg)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Asserts that the given collection does not contain item x. If x is a regular expression, ensure that
|
52
|
+
# none of the elements from the collection match x.
|
53
|
+
def assert_does_not_contain(collection, x, extra_msg = "")
|
54
|
+
collection = [collection] unless collection.is_a?(Array)
|
55
|
+
msg = "#{x.inspect} found in #{collection.to_a.inspect} " + extra_msg
|
56
|
+
case x
|
57
|
+
when Regexp
|
58
|
+
assert(!collection.detect { |e| e =~ x }, msg)
|
59
|
+
else
|
60
|
+
assert(!collection.include?(x), msg)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Asserts that the given matcher returns true when +target+ is passed to #matches?
|
65
|
+
def assert_accepts(matcher, target, options = {})
|
66
|
+
if matcher.respond_to?(:in_context)
|
67
|
+
matcher.in_context(self)
|
68
|
+
end
|
69
|
+
|
70
|
+
if matcher.matches?(target)
|
71
|
+
assert_block { true }
|
72
|
+
if options[:message]
|
73
|
+
assert_match options[:message], matcher.negative_failure_message
|
74
|
+
end
|
75
|
+
else
|
76
|
+
assert_block(matcher.failure_message) { false }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Asserts that the given matcher returns false when +target+ is passed to #matches?
|
81
|
+
def assert_rejects(matcher, target, options = {})
|
82
|
+
if matcher.respond_to?(:in_context)
|
83
|
+
matcher.in_context(self)
|
84
|
+
end
|
85
|
+
|
86
|
+
unless matcher.matches?(target)
|
87
|
+
assert_block { true }
|
88
|
+
if options[:message]
|
89
|
+
assert_match options[:message], matcher.failure_message
|
90
|
+
end
|
91
|
+
else
|
92
|
+
assert_block(matcher.negative_failure_message) { false }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MiniTest::Spec::Magic::Context
|
2
|
+
|
3
|
+
# Returns the test context. Example:
|
4
|
+
#
|
5
|
+
# class UserTest < ActiveSupport::TestCase
|
6
|
+
# self.described_type # => User
|
7
|
+
#
|
8
|
+
# describe "something" do
|
9
|
+
# self.described_type # => User, here too
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# @return [Class] the described type
|
14
|
+
def described_type
|
15
|
+
@described_type ||= begin
|
16
|
+
klass = self
|
17
|
+
klass = klass.superclass while klass && klass.name !~ /Test$/
|
18
|
+
klass.name.sub(/Test$/, '').constantize if klass
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module MiniTest::Spec::Magic::Expectations
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
# Tests a matcher for validity. Example:
|
10
|
+
#
|
11
|
+
# class UserTest < ActiveSupport::TestCase
|
12
|
+
# it { should validate_presesence_of(:email) }
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @param [Matcher] matcher the matcher to test
|
16
|
+
def should(matcher)
|
17
|
+
assert_accepts matcher, subject
|
18
|
+
end
|
19
|
+
|
20
|
+
# Tests a matcher for negative validity. Example:
|
21
|
+
#
|
22
|
+
# class UserTest < ActiveSupport::TestCase
|
23
|
+
# it { should_not validate_presesence_of(:email) }
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @param [Matcher] matcher the matcher to test
|
27
|
+
def should_not(matcher)
|
28
|
+
assert_rejects matcher, subject
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
|
33
|
+
# Accepts either a matcher or a message with a block. Example:
|
34
|
+
#
|
35
|
+
# class UserTest < ActiveSupport::TestCase
|
36
|
+
#
|
37
|
+
# should validate_presesence_of(:email)
|
38
|
+
#
|
39
|
+
# should "be valid" do
|
40
|
+
# subject.must_be :valid?
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# @param [Matcher|String] expectation either a matcher or a message
|
46
|
+
def should(expectation, &block)
|
47
|
+
message = if expectation.respond_to?(:description)
|
48
|
+
block = lambda { should expectation }
|
49
|
+
expectation.description
|
50
|
+
else
|
51
|
+
expectation
|
52
|
+
end
|
53
|
+
it "should #{message}", &block
|
54
|
+
end
|
55
|
+
|
56
|
+
# @param [Matcher] matcher a matcher
|
57
|
+
def should_not(matcher)
|
58
|
+
block = lambda { should_not matcher }
|
59
|
+
it "should not #{matcher.description}", &block
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Define Test::Unit::TestCase as a subclass of MiniTest::Spec
|
2
|
+
# Partially "stolen" from https://github.com/metaskills/mini_specunit
|
3
|
+
module Test
|
4
|
+
module Unit
|
5
|
+
class TestCase < ::MiniTest::Spec
|
6
|
+
|
7
|
+
# Ensure ActiveSupport::TestCase::Declarative doesn't override MiniTest::Spec's `describe`
|
8
|
+
def self.method_defined?(sym, *args)
|
9
|
+
sym.to_sym == :describe || super
|
10
|
+
end
|
11
|
+
|
12
|
+
def build_message(head, template=nil, *arguments)
|
13
|
+
template &&= template.chomp
|
14
|
+
template.gsub(/\?/) { arguments.shift.inspect.chomp }
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitest-spec-magic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dimitrij Denissenko
|
9
|
+
- Ken Collins
|
10
|
+
- thoughtbot, inc.
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2011-10-26 00:00:00.000000000Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: minitest
|
18
|
+
requirement: &10169260 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.6.0
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *10169260
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: &10168760 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *10168760
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bundler
|
40
|
+
requirement: &10167780 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *10167780
|
49
|
+
description: Combines Minitest::Spec with shoulda-like goodies
|
50
|
+
email: dimitrij@blacksquaremedia.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- lib/test/unit/testcase.rb
|
56
|
+
- lib/minitest/spec/magic/controller.rb
|
57
|
+
- lib/minitest/spec/magic/context.rb
|
58
|
+
- lib/minitest/spec/magic/expectations.rb
|
59
|
+
- lib/minitest/spec/magic/assertions.rb
|
60
|
+
- lib/minitest/spec/magic.rb
|
61
|
+
- lib/minitest-spec-magic.rb
|
62
|
+
homepage: https://github.com/bsm/minitest-spec-magic
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 1.8.7
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 1.6.0
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.10
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Minitest::Spec extensions for Rails and beyond
|
86
|
+
test_files: []
|