diff --git a/.document b/.document deleted file mode 100644 index ecf3673..0000000 --- a/.document +++ /dev/null @@ -1,5 +0,0 @@ -README.rdoc -lib/**/*.rb -bin/* -features/**/*.feature -LICENSE diff --git a/LICENSE b/LICENSE index 7e831f5..804de3a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2009, 2010, 2011, José Pablo Fernández +Copyright © 2009, 2010, 2011, José Pablo Fernández Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md new file mode 100644 index 0000000..1a3faba --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +assert_difference +================= + +A nice assert_difference method similar to the one provided by Rails but with +some improvements. For example: + + assert_difference "Company.count" => +1, "User.count" => +5, "Slot.count" => -1 do + post :something + end + +will assert that a company and 5 users were create (the plus sign is only for +the visual aid) and a slot was removed. + +[Rails' assert_difference](http://api.rubyonrails.org/classes/ActiveSupport/Testing/Assertions.html#method-i-assert_difference) +would require a more verbose syntax: + + assert_difference "Company.count" do + assert_difference "User.count", +5 do + assert_difference "Article.count", -1 do + post :something + end + end + end + +To use it with Test::Unit add this code (untested, please +[let me know](mailto:pupeno@pupeno.com) if it works): + + class Test::Unit::TestCase + include Factory::Syntax::Methods + end + +and to use it with RSpec: + + RSpec.configure do |config| + config.include Factory::Syntax::Methods + end + +For more information read http://pupeno.com/blog/better-assert-difference + +Note on Patches/Pull Requests +----------------------------- + +* Fork the project. +* Make your feature addition or bug fix. +* Add tests for it. This is important so I don't break it in a + future version unintentionally. +* Commit, do not mess with rakefile, version, or history. + (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) +* Send me a pull request. Bonus points for topic branches. + +Copyright +--------- + +Copyright (c) 2010, 2011, José Pablo Fernández. See LICENSE for details. diff --git a/README.rdoc b/README.rdoc deleted file mode 100644 index 03a5527..0000000 --- a/README.rdoc +++ /dev/null @@ -1,34 +0,0 @@ -= assert_difference - -Better assert_difference than Rails by providing a more compact and readable -syntax through hashes. Instead of writing: - - assert_difference "Article.count" do - assert_difference "assigns(:article).comments(:reload).size" do - assert_difference "Article.count", -1 do - post :something - end - end - end - -you can *now* write: - - assert_difference "Article.count" => 1, "assigns(:article).comments(:reload).size" => 1, "Article.count" => -1 do - post :something - end - -For some more information read http://pupeno.com/blog/better-assert-difference - -== Note on Patches/Pull Requests - -* Fork the project. -* Make your feature addition or bug fix. -* Add tests for it. This is important so I don't break it in a - future version unintentionally. -* Commit, do not mess with rakefile, version, or history. - (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) -* Send me a pull request. Bonus points for topic branches. - -== Copyright - -Copyright (c) 2010, 2011, José Pablo Fernández. See LICENSE for details. diff --git a/Rakefile b/Rakefile index b0b1527..144a269 100644 --- a/Rakefile +++ b/Rakefile @@ -21,4 +21,9 @@ Rcov::RcovTask.new do |test| end require "yard" -YARD::Rake::YardocTask.new +YARD::Rake::YardocTask.new do |yard| + # Use Markdown for documentation. + yard.options << "--markup" << "markdown" + # Extra file(s). + yard.options << "-" << "LICENSE" +end diff --git a/assert_difference.gemspec b/assert_difference.gemspec index 45542ab..b6cab8e 100644 --- a/assert_difference.gemspec +++ b/assert_difference.gemspec @@ -2,7 +2,7 @@ # Copyright © 2011, José Pablo Fernández $:.unshift File.expand_path("../lib", __FILE__) -require "assert_difference/version" +require "assert_difference" Gem::Specification.new do |s| s.name = "assert_difference" diff --git a/lib/assert_difference.rb b/lib/assert_difference.rb index 3c03771..e6554b8 100644 --- a/lib/assert_difference.rb +++ b/lib/assert_difference.rb @@ -1,4 +1,77 @@ -# Copyright © 2011, José Pablo Fernández +# -*- encoding: utf-8 -*- +# Copyright © 2010, 2011, José Pablo Fernández -require "assert_difference/version" -require "assert_difference/assert_difference" +module AssertDifference + VERSION = "0.3.0" unless defined?(::AssertDifference::VERSION) + + # Test numeric difference between the return value of an expression as a result of what is evaluated + # in the yielded block. + # + # assert_difference "Article.count" do + # post :create, :article => {...} + # end + # + # An arbitrary expression is passed in and evaluated. + # + # assert_difference "assigns(:article).comments(:reload).size" do + # post :create, :comment => {...} + # end + # + # An arbitrary positive or negative difference can be specified. The default is +1. + # + # assert_difference "Article.count", -1 do + # post :delete, :id => ... + # end + # + # An array of expressions can also be passed in and evaluated. + # + # assert_difference [ "Article.count", "Post.count" ], +2 do + # post :create, :article => {...} + # end + # + # A error message can be specified. + # + # assert_difference "Article.count", -1, "An Article should be destroyed" do + # post :delete, :id => ... + # end + # + # Various assertions can be combined into one, instead of writing: + # + # assert_difference "Company.count" do + # assert_difference "User.count", +5 do + # assert_difference "Article.count", -1 do + # post :something + # end + # end + # end + # + # you can *now* write: + # + # assert_difference "Article.count" => 1, "assigns(:article).comments(:reload).size" => 1, "Article.count" => -1 do + # post :something + # end + # + # @param [Array, Hash] expressions array of expressions to evaluate or hash + # table of expressions and expected difference. + # @param [Integer] difference expected difference when using an array or single expression. + # @param [String, nil] message error message to display. One would be constructed if nil. + def assert_difference(expressions, difference = 1, message = nil, &block) + b = block.send(:binding) + if !expressions.is_a? Hash + exps = Array.wrap(expressions) + expressions = {} + exps.each { |e| expressions[e] = difference } + end + + before = {} + expressions.each { |exp, _| before[exp] = eval(exp, b) } + + yield + + expressions.each do |exp, diff| + error = "#{exp.inspect} didn't change by #{diff}" + error = "#{message}.\n#{error}" if message + assert_equal(before[exp] + diff, eval(exp, b), error) + end + end +end diff --git a/lib/assert_difference/assert_difference.rb b/lib/assert_difference/assert_difference.rb deleted file mode 100644 index 4b6ee7d..0000000 --- a/lib/assert_difference/assert_difference.rb +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright © 2010, 2011, José Pablo Fernández - -module AssertDifference - # Test numeric difference between the return value of an expression as a result of what is evaluated - # in the yielded block. - # - # assert_difference "Article.count" do - # post :create, :article => {...} - # end - # - # An arbitrary expression is passed in and evaluated. - # - # assert_difference "assigns(:article).comments(:reload).size" do - # post :create, :comment => {...} - # end - # - # An arbitrary positive or negative difference can be specified. The default is +1. - # - # assert_difference "Article.count", -1 do - # post :delete, :id => ... - # end - # - # An array of expressions can also be passed in and evaluated. - # - # assert_difference [ "Article.count", "Post.count" ], +2 do - # post :create, :article => {...} - # end - # - # A error message can be specified. - # - # assert_difference "Article.count", -1, "An Article should be destroyed" do - # post :delete, :id => ... - # end - # - # Various assertions can be combined into one, instead of writing: - # - # assert_difference "Article.count" do - # assert_difference "assigns(:article).comments(:reload).size" do - # assert_difference "Article.count", -1 do - # post :something - # end - # end - # end - # - # you can *now* write: - # - # assert_difference "Article.count" => 1, "assigns(:article).comments(:reload).size" => 1, "Article.count" => -1 do - # post :something - # end - def assert_difference(expressions, difference = 1, message = nil, &block) - b = block.send(:binding) - if !expressions.is_a? Hash - exps = Array.wrap(expressions) - expressions = {} - exps.each { |e| expressions[e] = difference } - end - - before = {} - expressions.each { |exp, _| before[exp] = eval(exp, b) } - - yield - - expressions.each do |exp, diff| - error = "#{exp.inspect} didn't change by #{diff}" - error = "#{message}.\n#{error}" if message - assert_equal(before[exp] + diff, eval(exp, b), error) - end - end -end diff --git a/lib/assert_difference/version.rb b/lib/assert_difference/version.rb deleted file mode 100644 index 8a40ea0..0000000 --- a/lib/assert_difference/version.rb +++ /dev/null @@ -1,6 +0,0 @@ -# -*- encoding: utf-8 -*- -# Copyright © 2011, José Pablo Fernández - -module AssertDifference - VERSION = "0.3.0" unless defined?(::AssertDifference::VERSION) -end