assert_difference 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,30 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## RubyMine
17
+ .idea
18
+
19
+ ## PROJECT::GENERAL
20
+ coverage
21
+ rdoc
22
+ pkg
23
+
24
+ ## PROJECT::SPECIFIC
25
+ *.gemspec
26
+
27
+ ## Yard
28
+ .yardoc
29
+ doc
30
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 José Pablo Fernández
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,34 @@
1
+ = assert_difference
2
+
3
+ Better assert_difference than Rails by providing a more compact and readable
4
+ syntax through hashes. Instead of writing:
5
+
6
+ assert_difference "Article.count" do
7
+ assert_difference "assigns(:article).comments(:reload).size" do
8
+ assert_difference "Article.count", -1 do
9
+ post :something
10
+ end
11
+ end
12
+ end
13
+
14
+ you can *now* write:
15
+
16
+ assert_difference "Article.count" => 1, "assigns(:article).comments(:reload).size" => 1, "Article.count" => -1 do
17
+ post :something
18
+ end
19
+
20
+ For some more information read http://pupeno.com/blog/better-assert-difference
21
+
22
+ == Note on Patches/Pull Requests
23
+
24
+ * Fork the project.
25
+ * Make your feature addition or bug fix.
26
+ * Add tests for it. This is important so I don't break it in a
27
+ future version unintentionally.
28
+ * Commit, do not mess with rakefile, version, or history.
29
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
30
+ * Send me a pull request. Bonus points for topic branches.
31
+
32
+ == Copyright
33
+
34
+ Copyright (c) 2010 José Pablo Fernández. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,79 @@
1
+ # Copyright © 2010, José Pablo Fernández
2
+
3
+ require "rubygems"
4
+ require "rake"
5
+
6
+ begin
7
+ require "jeweler"
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "assert_difference"
10
+ gem.summary = %Q{Better assert_difference than Rails}
11
+ gem.description = %Q{Better assert_difference than Rails by providing a more compact and readable syntax through hashes. For some more information read http://pupeno.com/blog/better-assert-difference}
12
+ gem.email = "pupeno@pupeno.com"
13
+ gem.homepage = "http://github.com/pupeno/assert_difference"
14
+ gem.authors = ["J. Pablo Fernández"]
15
+ gem.add_development_dependency "yard", ">= 0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require "rake/testtask"
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << "lib" << "test"
26
+ test.pattern = "test/**/test_*.rb"
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require "rcov/rcovtask"
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << "test"
34
+ test.pattern = "test/**/test_*.rb"
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ begin
46
+ require "reek/adapters/rake_task"
47
+ Reek::RakeTask.new do |t|
48
+ t.fail_on_error = true
49
+ t.verbose = false
50
+ t.source_files = "lib/**/*.rb"
51
+ end
52
+ rescue LoadError
53
+ task :reek do
54
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
55
+ end
56
+ end
57
+
58
+ begin
59
+ require "roodi"
60
+ require "roodi_task"
61
+ RoodiTask.new do |t|
62
+ t.verbose = false
63
+ end
64
+ rescue LoadError
65
+ task :roodi do
66
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
67
+ end
68
+ end
69
+
70
+ task :default => :test
71
+
72
+ begin
73
+ require "yard"
74
+ YARD::Rake::YardocTask.new
75
+ rescue LoadError
76
+ task :yardoc do
77
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
78
+ end
79
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,69 @@
1
+ # Copyright © 2010, José Pablo Fernández
2
+
3
+ class ActiveSupport::TestCase
4
+ # Test numeric difference between the return value of an expression as a result of what is evaluated
5
+ # in the yielded block.
6
+ #
7
+ # assert_difference "Article.count" do
8
+ # post :create, :article => {...}
9
+ # end
10
+ #
11
+ # An arbitrary expression is passed in and evaluated.
12
+ #
13
+ # assert_difference "assigns(:article).comments(:reload).size" do
14
+ # post :create, :comment => {...}
15
+ # end
16
+ #
17
+ # An arbitrary positive or negative difference can be specified. The default is +1.
18
+ #
19
+ # assert_difference "Article.count", -1 do
20
+ # post :delete, :id => ...
21
+ # end
22
+ #
23
+ # An array of expressions can also be passed in and evaluated.
24
+ #
25
+ # assert_difference [ "Article.count", "Post.count" ], +2 do
26
+ # post :create, :article => {...}
27
+ # end
28
+ #
29
+ # A error message can be specified.
30
+ #
31
+ # assert_difference "Article.count", -1, "An Article should be destroyed" do
32
+ # post :delete, :id => ...
33
+ # end
34
+ #
35
+ # Various assertions can be combined into one, instead of writing:
36
+ #
37
+ # assert_difference "Article.count" do
38
+ # assert_difference "assigns(:article).comments(:reload).size" do
39
+ # assert_difference "Article.count", -1 do
40
+ # post :something
41
+ # end
42
+ # end
43
+ # end
44
+ #
45
+ # you can *now* write:
46
+ #
47
+ # assert_difference "Article.count" => 1, "assigns(:article).comments(:reload).size" => 1, "Article.count" => -1 do
48
+ # post :something
49
+ # end
50
+ def assert_difference(expressions, difference = 1, message = nil, &block)
51
+ b = block.send(:binding)
52
+ if !expressions.is_a? Hash
53
+ exps = Array.wrap(expressions)
54
+ expressions = {}
55
+ exps.each { |e| expressions[e] = difference }
56
+ end
57
+
58
+ before = {}
59
+ expressions.each { |exp, _| before[exp] = eval(exp, b) }
60
+
61
+ yield
62
+
63
+ expressions.each do |exp, diff|
64
+ error = "#{exp.inspect} didn't change by #{diff}"
65
+ error = "#{message}.\n#{error}" if message
66
+ assert_equal(before[exp] + diff, eval(exp, b), error)
67
+ end
68
+ end
69
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ # Copyright © 2010, José Pablo Fernández
2
+
3
+ require "rubygems"
4
+ require "test/unit"
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
8
+ require "assert_difference"
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,7 @@
1
+ # Copyright © 2010, José Pablo Fernández
2
+
3
+ require "helper"
4
+
5
+ class TestAssertDifference < Test::Unit::TestCase
6
+ # Sorry, no tests yet.
7
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assert_difference
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - "J. Pablo Fern\xC3\xA1ndez"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-02 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yard
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Better assert_difference than Rails by providing a more compact and readable syntax through hashes. For some more information read http://pupeno.com/blog/better-assert-difference
36
+ email: pupeno@pupeno.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/assert_difference.rb
52
+ - test/helper.rb
53
+ - test/test_assert_difference.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/pupeno/assert_difference
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Better assert_difference than Rails
88
+ test_files:
89
+ - test/helper.rb
90
+ - test/test_assert_difference.rb