Skip to content

Commit

Permalink
Clean up documentation and pack everything in a single file (KISS).
Browse files Browse the repository at this point in the history
  • Loading branch information
pupeno committed Aug 24, 2011
1 parent 6659fda commit 8ca3b00
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 120 deletions.
5 changes: 0 additions & 5 deletions .document

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:[email protected]) 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.
34 changes: 0 additions & 34 deletions README.rdoc

This file was deleted.

7 changes: 6 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion assert_difference.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
79 changes: 76 additions & 3 deletions lib/assert_difference.rb
Original file line number Diff line number Diff line change
@@ -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
69 changes: 0 additions & 69 deletions lib/assert_difference/assert_difference.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/assert_difference/version.rb

This file was deleted.

0 comments on commit 8ca3b00

Please sign in to comment.