minitest-matchers 1.0.3 → 1.1.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest CHANGED
@@ -3,24 +3,6 @@
3
3
  require "autotest/restart"
4
4
 
5
5
  Autotest.add_hook :initialize do |at|
6
- at.testlib = "minitest/autorun"
6
+ at.testlib = ".minitest"
7
7
  at.add_exception "tmp"
8
-
9
- # at.extra_files << "../some/external/dependency.rb"
10
- #
11
- # at.libs << ":../some/external"
12
- #
13
- # at.add_exception "vendor"
14
- #
15
- # at.add_mapping(/dependency.rb/) do |f, _|
16
- # at.files_matching(/test_.*rb$/)
17
- # end
18
- #
19
- # %w(TestA TestB).each do |klass|
20
- # at.extra_class_map[klass] = "test/test_misc.rb"
21
- # end
22
8
  end
23
-
24
- # Autotest.add_hook :run_command do |at|
25
- # system "rake build"
26
- # end
data/README.txt CHANGED
@@ -5,33 +5,71 @@
5
5
  == DESCRIPTION:
6
6
 
7
7
  minitest-matchers adds support for RSpec/Shoulda-style matchers to
8
- MiniTest::Spec.
8
+ minitest/unit and minitest/spec.
9
9
 
10
- A matcher is a class that must implement #description and #matches?
11
- methods. Expactations are then builded using these two methods.
10
+ A matcher is a class that must implement #description, #matches?,
11
+ #failure_message and #negative_failure_message methods.
12
+ Expactations are then builded using these methods.
12
13
 
13
14
  == FEATURES/PROBLEMS:
14
15
 
15
- * Enables you to define reusable matcher classes
16
+ * Enables you to use existing matcher classes from projects like
17
+ valid_attribute, (with some additional work) shoulda-matchers and remarkable.
16
18
 
17
19
  == SYNOPSIS:
18
20
 
19
- * see https://github.com/bcardarella/valid_attribute (support coming soon hopefully!)
21
+ * see example matcher: https://github.com/bcardarella/valid_attribute/blob/master/lib/valid_attribute/matcher.rb
20
22
 
21
- class Post < ActiveRecord::Base
23
+ gem "minitest"
24
+ require "minitest/matchers"
25
+ require "minitest/autorun"
26
+ require "valid_attribute"
27
+ require "active_model"
28
+
29
+ class Post
30
+ include ActiveModel::Validations
31
+ attr_accessor :title
22
32
  validates :title, :presence => true, :length => 4..20
23
33
  end
24
34
 
35
+ # Using minitest/unit
36
+
37
+ class PostTest < MiniTest::Unit::TestCase
38
+ include ValidAttribute::Method
39
+
40
+ def test_validations
41
+ post = Post.new
42
+
43
+ assert_must have_valid(:title).when("Good"), post
44
+ assert_wont have_valid(:title).when(""), post
45
+ end
46
+ end
47
+
48
+ # Using minitest/spec
49
+
50
+ describe Post do
51
+ include ValidAttribute::Method
52
+
53
+ it "should have validations" do
54
+ post = Post.new
55
+
56
+ post.must have_valid(:title).when("Good")
57
+ post.wont have_valid(:title).when("")
58
+ end
59
+ end
60
+
61
+ # Using minitest/spec with subject
62
+
25
63
  describe Post do
26
64
  subject { Post.new }
27
65
 
28
- must { have_valid(:title).when("Hello") }
29
- wont { have_valid(:title).when(nil, "", "Bad") }
66
+ it { must have_valid(:title).when("Hello") }
67
+ it { wont have_valid(:title).when("", nil, "Bad") }
30
68
  end
31
69
 
32
70
  == REQUIREMENTS:
33
71
 
34
- * minitest > 2.5.0
72
+ * minitest >= 2.7.0
35
73
 
36
74
  == INSTALL:
37
75
 
@@ -1,59 +1,124 @@
1
1
  require "minitest/spec"
2
2
 
3
3
  module MiniTest::Matchers
4
- VERSION = "1.0.3"
4
+ VERSION = "1.1.0.rc2" # :nodoc:
5
5
 
6
- def must &block
7
- matcher = yield
8
- check_matcher matcher
6
+ ##
7
+ # Every matcher must respond to following methods:
8
+ # - #description
9
+ # - #matches?
10
+ # - #failure_message
11
+ # - #negative_failure_message
9
12
 
10
- it "must #{matcher.description}" do
13
+ def self.check_matcher matcher
14
+ [:description, :matches?, :failure_message, :negative_failure_message].each do |m|
15
+ unless matcher.respond_to? m
16
+ fail "Matcher must respond to #{m}."
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module MiniTest
23
+ module Assertions
24
+ ##
25
+ # Fails unless matcher.matches?(subject) returns true
26
+ #
27
+ # Example:
28
+ #
29
+ # def test_validations
30
+ # assert_must be_valid, @user
31
+ # end
32
+
33
+ def assert_must matcher, subject, msg = nil
34
+ MiniTest::Matchers.check_matcher matcher
11
35
  result = matcher.matches? subject
12
36
 
13
- failure_message = if matcher.respond_to? :failure_message
14
- matcher.failure_message
15
- else
16
- "expected to " + matcher.description
17
- end
37
+ msg = message(msg) { matcher.failure_message }
18
38
 
19
- assert result, failure_message
39
+ assert result, msg
20
40
  end
21
41
 
22
- matcher
42
+ ##
43
+ # Fails if matcher.matches?(subject) returns true
44
+ #
45
+ # Example:
46
+ #
47
+ # def test_validations
48
+ # assert_wont be_valid, @user
49
+ # end
50
+
51
+ def assert_wont matcher, subject, msg = nil
52
+ MiniTest::Matchers.check_matcher matcher
53
+ result = matcher.matches? subject
54
+
55
+ msg = message(msg) { matcher.negative_failure_message }
56
+
57
+ refute result, msg
58
+ end
23
59
  end
24
60
 
25
- def wont &block
26
- matcher = yield
27
- check_matcher matcher
61
+ module Expectations
62
+ ##
63
+ # See MiniTest::Assertions#assert_must
64
+ #
65
+ # user.must have_valid(:email).when("user@example.com")
66
+ #
67
+ # :method: must
28
68
 
29
- it "wont #{matcher.description}" do
30
- result = if matcher.respond_to? :does_not_match?
31
- matcher.does_not_match?(subject)
32
- else
33
- !matcher.matches?(subject)
34
- end
69
+ infect_an_assertion :assert_must, :must
35
70
 
36
- failure_message = if matcher.respond_to? :negative_failure_message
37
- matcher.negative_failure_message
38
- else
39
- "expected not to " + matcher.description
40
- end
71
+ ##
72
+ # See MiniTest::Assertions#assert_wont
73
+ #
74
+ # user.wont have_valid(:email).when("bad@email")
75
+ #
76
+ # :method: wont
41
77
 
42
- assert result, failure_message
43
- end
78
+ infect_an_assertion :assert_wont, :wont
79
+ end
80
+ end
81
+
82
+ class MiniTest::Spec
83
+ ##
84
+ # Define a `must` expectation for implicit subject
85
+ #
86
+ # Example:
87
+ #
88
+ # describe Post do
89
+ # subject { Post.new }
90
+ #
91
+ # it { must have_valid(:title).when("Good") }
92
+ # end
44
93
 
45
- matcher
94
+ def must(*args, &block)
95
+ if respond_to?(:subject)
96
+ subject.must(*args, &block)
97
+ else
98
+ super
99
+ end
46
100
  end
47
101
 
48
- def check_matcher matcher
49
- [:description, :matches?].each do |m|
50
- if !matcher.respond_to?(m) || matcher.send(:description).nil?
51
- fail "Matcher must respond to #{m}"
52
- end
102
+ ##
103
+ # Define a `wont` expectation for implicit subject
104
+ #
105
+ # Example:
106
+ #
107
+ # describe Post do
108
+ # subject { Post.new }
109
+ #
110
+ # it { wont have_valid(:title).when("") }
111
+ # end
112
+
113
+ def wont(*args, &block)
114
+ if respond_to?(:subject)
115
+ subject.wont(*args, &block)
116
+ else
117
+ super
53
118
  end
54
119
  end
55
120
  end
56
121
 
57
- class MiniTest::Spec
58
- extend MiniTest::Matchers
122
+ class MiniTest::Spec # :nodoc:
123
+ include MiniTest::Matchers
59
124
  end
@@ -1,3 +1,5 @@
1
+ gem "minitest"
2
+ require "minitest/spec"
1
3
  require "minitest/autorun"
2
4
  require "minitest/matchers"
3
5
 
@@ -8,75 +10,83 @@ class KindOfMatcher
8
10
  @klass = klass
9
11
  end
10
12
 
11
- def description; "be kind of #{@klass}" end
13
+ def description
14
+ "be kind of #{@klass}"
15
+ end
16
+
17
+ def matches? subject
18
+ subject.kind_of? @klass
19
+ end
12
20
 
13
- def matches? subject; subject.kind_of? @klass end
21
+ def failure_message
22
+ "expected to " + description
23
+ end
24
+
25
+ def negative_failure_message
26
+ "expected not to " + description
27
+ end
14
28
  end
15
29
 
16
- class BadSpec < MiniTest::Spec; end
17
- class GoodSpec < MiniTest::Spec
18
- subject { [1, 2, 3] }
30
+ def be_kind_of klass
31
+ KindOfMatcher.new klass
19
32
  end
20
33
 
21
- describe MiniTest::Matchers do
22
- let(:bad_spec) { Class.new(BadSpec) }
23
- let(:good_spec) { Class.new(GoodSpec) }
34
+ describe MiniTest::Unit::TestCase do
35
+ it "needs to verify matcher has #description and #matches?" do
36
+ proc { assert_must BadMatcher.new, [] }.must_raise RuntimeError
37
+ end
24
38
 
25
- describe "check_matcher" do
26
- it "requires spec to have subject" do
27
- proc { bad_spec.must { BadMatcher.new } }.must_raise RuntimeError
28
- end
39
+ it "needs to verify assert_must" do
40
+ assert_must(be_kind_of(Array), []).must_equal true
41
+ proc { assert_must be_kind_of(String), [] }.must_raise MiniTest::Assertion
42
+ end
29
43
 
30
- it "requires matcher to have #description and #matches?" do
31
- proc { good_spec.must { BadMatcher.new } }.must_raise RuntimeError
32
- good_spec.must { KindOfMatcher.new(Object) }.must_be_kind_of KindOfMatcher
33
- end
44
+ it "needs to verify assert_wont" do
45
+ assert_wont(be_kind_of(String), []).must_equal false
46
+ proc { assert_wont be_kind_of(Array), [] }.must_raise MiniTest::Assertion
47
+ end
48
+ end
49
+
50
+ describe MiniTest::Spec do
51
+ it "needs to verify matcher has #description and #matches?" do
52
+ proc { [].must BadMatcher.new, [] }.must_raise RuntimeError
34
53
  end
35
54
 
36
- describe "must" do
37
- subject do
38
- Class.new(good_spec) do
39
- must { KindOfMatcher.new String }
40
- end
41
- end
55
+ it "needs to verify must" do
56
+ [].must(be_kind_of(Array)).must_equal true
57
+ proc { [].must be_kind_of(String) }.must_raise MiniTest::Assertion
58
+ end
42
59
 
43
- let(:spec) { subject.new "A spec" }
60
+ it "needs to verify wont" do
61
+ [].wont(be_kind_of(String)).must_equal false
62
+ proc { [].wont be_kind_of(Array) }.must_raise MiniTest::Assertion
63
+ end
44
64
 
45
- it "defines must expectation" do
46
- subject.test_methods.grep(/must_be_kind_of/).size.must_equal 1
47
- end
65
+ it "needs to verify must with implicit subject" do
66
+ spec_class = Class.new(MiniTest::Spec) do
67
+ subject { [1, 2, 3] }
48
68
 
49
- it "passes when matches" do
50
- subject.send(:subject) { "hello" }
51
- proc { spec.send subject.test_methods.first }
69
+ it("1") { must be_kind_of(Array) }
70
+ it("2") { must be_kind_of(String) }
52
71
  end
53
72
 
54
- it "raises error when does not match" do
55
- subject.send(:subject) { 1 }
56
- proc { spec.send subject.test_methods.first }.must_raise MiniTest::Assertion
57
- end
73
+ spec = spec_class.new("A spec")
74
+
75
+ spec.send "test_0001_1"
76
+ proc { spec.send "test_0002_2"}.must_raise MiniTest::Assertion
58
77
  end
59
78
 
60
- describe "wont" do
61
- subject do
62
- Class.new(good_spec) do
63
- wont { KindOfMatcher.new String }
64
- end
65
- end
66
- let(:spec) { subject.new "A spec" }
79
+ it "needs to verify wont with implicit subject" do
80
+ spec_class = Class.new(MiniTest::Spec) do
81
+ subject { [1, 2, 3] }
67
82
 
68
- it "defines wont expectation" do
69
- subject.test_methods.grep(/wont_be_kind_of/).size.must_equal 1
83
+ it("1") { wont be_kind_of(String) }
84
+ it("2") { wont be_kind_of(Array) }
70
85
  end
71
86
 
72
- it "passes when does not match" do
73
- subject.send(:subject) { 1 }
74
- proc { spec.send subject.test_methods.first }
75
- end
87
+ spec = spec_class.new("A spec")
76
88
 
77
- it "raises error when matches" do
78
- subject.send(:subject) { "hello" }
79
- proc { spec.send subject.test_methods.first }.must_raise MiniTest::Assertion
80
- end
89
+ spec.send "test_0001_1"
90
+ proc { spec.send "test_0002_2"}.must_raise MiniTest::Assertion
81
91
  end
82
92
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
5
- prerelease:
4
+ version: 1.1.0.rc2
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Davis
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-09-05 00:00:00.000000000Z
13
+ date: 2011-11-03 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
17
- requirement: &70212276375280 !ruby/object:Gem::Requirement
17
+ requirement: &70279503203140 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,21 +22,21 @@ dependencies:
22
22
  version: 2.5.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70212276375280
25
+ version_requirements: *70279503203140
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: minitest
28
- requirement: &70212276374860 !ruby/object:Gem::Requirement
28
+ requirement: &70279503202320 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '1.6'
33
+ version: '2.7'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70212276374860
36
+ version_requirements: *70279503202320
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: hoe
39
- requirement: &70212276374440 !ruby/object:Gem::Requirement
39
+ requirement: &70279503201140 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,15 +44,8 @@ dependencies:
44
44
  version: '2.12'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70212276374440
48
- description: ! 'minitest-matchers adds support for RSpec/Shoulda-style matchers to
49
-
50
- MiniTest::Spec.
51
-
52
-
53
- A matcher is a class that must implement #description and #matches?
54
-
55
- methods. Expactations are then builded using these two methods.'
47
+ version_requirements: *70279503201140
48
+ description: methods.
56
49
  email:
57
50
  - ryand-ruby@zenspider.com
58
51
  - wojtek@wojtekmach.pl
@@ -88,14 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
81
  required_rubygems_version: !ruby/object:Gem::Requirement
89
82
  none: false
90
83
  requirements:
91
- - - ! '>='
84
+ - - ! '>'
92
85
  - !ruby/object:Gem::Version
93
- version: '0'
86
+ version: 1.3.1
94
87
  requirements: []
95
88
  rubyforge_project: minitest-matchers
96
- rubygems_version: 1.8.7
89
+ rubygems_version: 1.8.10
97
90
  signing_key:
98
91
  specification_version: 3
99
- summary: minitest-matchers adds support for RSpec/Shoulda-style matchers to MiniTest::Spec
92
+ summary: methods.
100
93
  test_files:
101
94
  - test/test_minitest_matchers.rb