shoulda-change_matchers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/Appraisals +7 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +34 -0
- data/Rakefile +10 -0
- data/Readme.md +43 -0
- data/gemfiles/shoulda2.gemfile +10 -0
- data/gemfiles/shoulda2.gemfile.lock +24 -0
- data/gemfiles/shoulda3.gemfile +10 -0
- data/gemfiles/shoulda3.gemfile.lock +34 -0
- data/lib/shoulda/change_matchers.rb +165 -0
- data/shoulda-change_matchers.gemspec +10 -0
- data/test/shoulda_change_matchers_test.rb +44 -0
- metadata +65 -0
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
shoulda-change_matchers (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (3.2.8)
|
10
|
+
i18n (~> 0.6)
|
11
|
+
multi_json (~> 1.0)
|
12
|
+
appraisal (0.4.1)
|
13
|
+
bundler
|
14
|
+
rake
|
15
|
+
i18n (0.6.0)
|
16
|
+
multi_json (1.3.6)
|
17
|
+
rake (0.9.2)
|
18
|
+
shoulda (3.1.1)
|
19
|
+
shoulda-context (~> 1.0)
|
20
|
+
shoulda-matchers (~> 1.2)
|
21
|
+
shoulda-context (1.0.0)
|
22
|
+
shoulda-matchers (1.2.0)
|
23
|
+
activesupport (>= 3.0.0)
|
24
|
+
test-unit (2.5.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
appraisal
|
31
|
+
rake
|
32
|
+
shoulda
|
33
|
+
shoulda-change_matchers!
|
34
|
+
test-unit (>= 2.5.1)
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
should_change / should_create / should_destroy matchers for shoulda 3 backported from shoulda 2<br/>
|
2
|
+
so you can upgrade to shoulda 3 without rewriting major parts of your test-base.
|
3
|
+
|
4
|
+
gem install shoulda-change_matchers
|
5
|
+
|
6
|
+
# Gemfile
|
7
|
+
gem "shoulda-let", :require => "shoulda/change_matchers"
|
8
|
+
|
9
|
+
Usage
|
10
|
+
=====
|
11
|
+
|
12
|
+
<!-- example -->
|
13
|
+
context "creating" do
|
14
|
+
setup do
|
15
|
+
User.create!
|
16
|
+
User.create!
|
17
|
+
end
|
18
|
+
|
19
|
+
should_change("the number of users", :by => 2) { User.count }
|
20
|
+
end
|
21
|
+
|
22
|
+
context "doing nothing" do
|
23
|
+
should_not_change("the number of users") { User.count }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "creating" do
|
27
|
+
setup{ User.create! }
|
28
|
+
should_create :user
|
29
|
+
end
|
30
|
+
|
31
|
+
context "destroying" do
|
32
|
+
setup{ User.delete_all }
|
33
|
+
should_destroy :user
|
34
|
+
end
|
35
|
+
<!-- example -->
|
36
|
+
|
37
|
+
|
38
|
+
Author
|
39
|
+
======
|
40
|
+
[Michael Grosser](http://grosser.it)<br/>
|
41
|
+
michael@grosser.it<br/>
|
42
|
+
License: MIT<br/>
|
43
|
+
[![Build Status](https://secure.travis-ci.org/grosser/shoulda_change_matchers.png)](http://travis-ci.org/grosser/shoulda_change_matchers)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/shoulda-change_matchers
|
3
|
+
specs:
|
4
|
+
shoulda-change_matchers (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
appraisal (0.4.1)
|
10
|
+
bundler
|
11
|
+
rake
|
12
|
+
rake (0.9.2.2)
|
13
|
+
shoulda (2.11.3)
|
14
|
+
test-unit (2.5.2)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
appraisal
|
21
|
+
rake
|
22
|
+
shoulda (~> 2.11)
|
23
|
+
shoulda-change_matchers!
|
24
|
+
test-unit (>= 2.5.1)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: /Users/mgrosser/code/tools/shoulda-change_matchers
|
3
|
+
specs:
|
4
|
+
shoulda-change_matchers (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (3.2.8)
|
10
|
+
i18n (~> 0.6)
|
11
|
+
multi_json (~> 1.0)
|
12
|
+
appraisal (0.4.1)
|
13
|
+
bundler
|
14
|
+
rake
|
15
|
+
i18n (0.6.0)
|
16
|
+
multi_json (1.3.6)
|
17
|
+
rake (0.9.2.2)
|
18
|
+
shoulda (3.1.1)
|
19
|
+
shoulda-context (~> 1.0)
|
20
|
+
shoulda-matchers (~> 1.2)
|
21
|
+
shoulda-context (1.0.0)
|
22
|
+
shoulda-matchers (1.3.0)
|
23
|
+
activesupport (>= 3.0.0)
|
24
|
+
test-unit (2.5.2)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
appraisal
|
31
|
+
rake
|
32
|
+
shoulda (~> 3.1)
|
33
|
+
shoulda-change_matchers!
|
34
|
+
test-unit (>= 2.5.1)
|
@@ -0,0 +1,165 @@
|
|
1
|
+
require "shoulda"
|
2
|
+
require "shoulda/context"
|
3
|
+
|
4
|
+
module Shoulda
|
5
|
+
module ChangeMatchers
|
6
|
+
# Macro that creates a test asserting a change between the return value
|
7
|
+
# of a block that is run before and after the current setup block
|
8
|
+
# is run. This is similar to Active Support's <tt>assert_difference</tt>
|
9
|
+
# assertion, but supports more than just numeric values. See also
|
10
|
+
# should_not_change.
|
11
|
+
#
|
12
|
+
# The passed description will be used when generating the test name and failure messages.
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# context "Creating a post" do
|
17
|
+
# setup { Post.create }
|
18
|
+
# should_change("the number of posts", :by => 1) { Post.count }
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# As shown in this example, the <tt>:by</tt> option expects a numeric
|
22
|
+
# difference between the before and after values of the expression. You
|
23
|
+
# may also specify <tt>:from</tt> and <tt>:to</tt> options:
|
24
|
+
#
|
25
|
+
# should_change("the number of posts", :from => 0, :to => 1) { Post.count }
|
26
|
+
# should_change("the post title", :from => "old", :to => "new") { @post.title }
|
27
|
+
#
|
28
|
+
# Combinations of <tt>:by</tt>, <tt>:from</tt>, and <tt>:to</tt> are allowed:
|
29
|
+
#
|
30
|
+
# # Assert the value changed in some way:
|
31
|
+
# should_change("the post title") { @post.title }
|
32
|
+
#
|
33
|
+
# # Assert the value changed to anything other than "old:"
|
34
|
+
# should_change("the post title", :from => "old") { @post.title }
|
35
|
+
#
|
36
|
+
# # Assert the value changed to "new:"
|
37
|
+
# should_change("the post title", :to => "new") { @post.title }
|
38
|
+
#
|
39
|
+
# This macro was deprecated because these tests aren't as valuable as
|
40
|
+
# alternative tests that explicitly test the final state.
|
41
|
+
#
|
42
|
+
# Consider an alternative:
|
43
|
+
#
|
44
|
+
# context "updating a post" do
|
45
|
+
# setup do
|
46
|
+
# @post = Post.create(:title => "old")
|
47
|
+
# put :update, :post => {:title => "new"}, :id => @post.to_param
|
48
|
+
# end
|
49
|
+
# should "update the title" do
|
50
|
+
# assert_equal "new", @post.reload.title
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
def should_change(description, options = {}, &block)
|
54
|
+
by, from, to = shoulda_get_options!([options], :by, :from, :to)
|
55
|
+
stmt = "change #{description}"
|
56
|
+
stmt << " from #{from.inspect}" if from
|
57
|
+
stmt << " to #{to.inspect}" if to
|
58
|
+
stmt << " by #{by.inspect}" if by
|
59
|
+
|
60
|
+
before = lambda { @_before_should_change = block.bind(self).call }
|
61
|
+
should stmt, :before => before do
|
62
|
+
old_value = @_before_should_change
|
63
|
+
new_value = block.bind(self).call
|
64
|
+
assert_operator from, :===, old_value, "#{description} did not originally match #{from.inspect}" if from
|
65
|
+
assert_not_equal old_value, new_value, "#{description} did not change" unless by == 0
|
66
|
+
assert_operator to, :===, new_value, "#{description} was not changed to match #{to.inspect}" if to
|
67
|
+
assert_equal old_value + by, new_value if by
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Deprecated.
|
72
|
+
#
|
73
|
+
# Macro that creates a test asserting no change between the return value
|
74
|
+
# of a block that is run before and after the current setup block
|
75
|
+
# is run. This is the logical opposite of should_change.
|
76
|
+
#
|
77
|
+
# The passed description will be used when generating the test name and failure message.
|
78
|
+
#
|
79
|
+
# Example:
|
80
|
+
#
|
81
|
+
# context "Updating a post" do
|
82
|
+
# setup { @post.update_attributes(:title => "new") }
|
83
|
+
# should_not_change("the number of posts") { Post.count }
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# This macro was deprecated because these tests aren't as valuable as
|
87
|
+
# alternative tests that explicitly test the final state.
|
88
|
+
#
|
89
|
+
# Consider an alternative:
|
90
|
+
#
|
91
|
+
# context "updating a post" do
|
92
|
+
# setup do
|
93
|
+
# @post = Post.create(:title => "old")
|
94
|
+
# put :update, :post => {:title => ""}, :id => @post.to_param
|
95
|
+
# end
|
96
|
+
# should "not update the title" do
|
97
|
+
# assert_equal "old", @post.reload.title
|
98
|
+
# end
|
99
|
+
# end
|
100
|
+
def should_not_change(description, &block)
|
101
|
+
before = lambda { @_before_should_not_change = block.bind(self).call }
|
102
|
+
should "not change #{description}", :before => before do
|
103
|
+
new_value = block.bind(self).call
|
104
|
+
assert_equal @_before_should_not_change, new_value, "#{description} changed"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Deprecated.
|
109
|
+
#
|
110
|
+
# Macro that creates a test asserting that a record of the given class was
|
111
|
+
# created.
|
112
|
+
#
|
113
|
+
# Example:
|
114
|
+
#
|
115
|
+
# context "creating a post" do
|
116
|
+
# setup { Post.create(post_attributes) }
|
117
|
+
# should_create :post
|
118
|
+
# end
|
119
|
+
def should_create(class_name)
|
120
|
+
should_change_record_count_of(class_name, 1, 'create')
|
121
|
+
end
|
122
|
+
|
123
|
+
# Deprecated.
|
124
|
+
#
|
125
|
+
# Macro that creates a test asserting that a record of the given class was
|
126
|
+
# destroyed.
|
127
|
+
#
|
128
|
+
# Example:
|
129
|
+
#
|
130
|
+
# context "destroying a post" do
|
131
|
+
# setup { Post.first.destroy }
|
132
|
+
# should_destroy :post
|
133
|
+
# end
|
134
|
+
def should_destroy(class_name)
|
135
|
+
should_change_record_count_of(class_name, -1, 'destroy')
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
|
140
|
+
def should_change_record_count_of(class_name, amount, action) # :nodoc:
|
141
|
+
klass = class_name.to_s.camelize.constantize
|
142
|
+
before = lambda do
|
143
|
+
@_before_change_record_count = klass.count
|
144
|
+
end
|
145
|
+
human_name = class_name.to_s.humanize.downcase
|
146
|
+
should "#{action} a #{human_name}", :before => before do
|
147
|
+
assert_equal @_before_change_record_count + amount,
|
148
|
+
klass.count,
|
149
|
+
"Expected to #{action} a #{human_name}"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Returns the values for the entries in the args hash who's keys are listed in the wanted array.
|
154
|
+
# Will raise if there are keys in the args hash that aren't listed.
|
155
|
+
def shoulda_get_options!(args, *wanted)
|
156
|
+
ret = []
|
157
|
+
opts = (args.last.is_a?(Hash) ? args.pop : {})
|
158
|
+
wanted.each {|w| ret << opts.delete(w)}
|
159
|
+
raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
|
160
|
+
return wanted.size == 1 ? ret.first : ret
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
Test::Unit::TestCase.send :extend, Shoulda::ChangeMatchers
|
@@ -0,0 +1,10 @@
|
|
1
|
+
name = "shoulda-change_matchers"
|
2
|
+
|
3
|
+
Gem::Specification.new name, "0.0.1" do |s|
|
4
|
+
s.summary = "should_change / should_create / should_destroy matchers for shoulda 3 backported from shoulda 2"
|
5
|
+
s.authors = ["Michael Grosser"]
|
6
|
+
s.email = "michael@grosser.it"
|
7
|
+
s.homepage = "http://github.com/grosser/#{name}"
|
8
|
+
s.files = `git ls-files`.split("\n")
|
9
|
+
s.license = "MIT"
|
10
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "shoulda"
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
4
|
+
#require "shoulda/change_matchers"
|
5
|
+
|
6
|
+
# simulate active_support being here ...
|
7
|
+
class String
|
8
|
+
def camelize
|
9
|
+
"User"
|
10
|
+
end
|
11
|
+
|
12
|
+
def constantize
|
13
|
+
eval self
|
14
|
+
end
|
15
|
+
|
16
|
+
def humanize
|
17
|
+
camelize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class User
|
22
|
+
class << self
|
23
|
+
attr_accessor :count
|
24
|
+
|
25
|
+
def create!
|
26
|
+
self.count += 1
|
27
|
+
end
|
28
|
+
|
29
|
+
def delete_all
|
30
|
+
self.count = 0
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class ShouldaLetTest < Test::Unit::TestCase
|
36
|
+
setup do
|
37
|
+
User.count = 1
|
38
|
+
end
|
39
|
+
|
40
|
+
context "Readme" do
|
41
|
+
example = File.read(File.expand_path("../../Readme.md", __FILE__)).match(/<!-- example -->(.*)<!-- example -->/m)[1]
|
42
|
+
eval example
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shoulda-change_matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Grosser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-31 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: michael@grosser.it
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .travis.yml
|
21
|
+
- Appraisals
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- Rakefile
|
25
|
+
- Readme.md
|
26
|
+
- gemfiles/shoulda2.gemfile
|
27
|
+
- gemfiles/shoulda2.gemfile.lock
|
28
|
+
- gemfiles/shoulda3.gemfile
|
29
|
+
- gemfiles/shoulda3.gemfile.lock
|
30
|
+
- lib/shoulda/change_matchers.rb
|
31
|
+
- shoulda-change_matchers.gemspec
|
32
|
+
- test/shoulda_change_matchers_test.rb
|
33
|
+
homepage: http://github.com/grosser/shoulda-change_matchers
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
hash: 3014986726780787426
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
hash: 3014986726780787426
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.24
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: should_change / should_create / should_destroy matchers for shoulda 3 backported
|
64
|
+
from shoulda 2
|
65
|
+
test_files: []
|