Skip to content

Commit

Permalink
add assert_between to the gem
Browse files Browse the repository at this point in the history
  • Loading branch information
janxious committed Jan 21, 2010
1 parent 977d357 commit f4bd630
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
11 changes: 7 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@




= assertions
* Project Page: http://rubyforge.org/projects/assertions/

Expand All @@ -24,6 +20,8 @@ including:
* Assertions that verify enumerables are sorted
(Test::Unit::Assertions#assert_sorted, Test::Unit::Assertions#assert_sorted_desc,
Test::Unit::Assertions#assert_sorted_by, Test::Unit::Assertions#assert_sorted_by_desc)
* An assertion that verifies x1 < y < x2
(Test::Unit::Assertion#assert_between)

== PROBLEMS:
None (known).
Expand Down Expand Up @@ -92,6 +90,11 @@ None (known).
# Verify an enumerable is sorted descending based on a key
#
assert_sorted_by(:key, [{:key => 2}, {:key => 1}])

#
# Verify a thing is between two other things
#
assert_between(1, 3, 2)
end
end

Expand Down
1 change: 1 addition & 0 deletions README.txt
2 changes: 1 addition & 1 deletion assertions.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'assertions'
s.version = '1.5.0'
s.version = '1.6.0'
s.summary = "More Assertions for Test::Unit::Assertions"
s.description = %{This project adds some additional assertions to Test::Unit::Assertions, including assert_raise_message (allowing verification of error messages) and assert_greater_than.}
s.files = Dir['lib/**/*.rb'] + Dir['test/**/*.rb'] + Dir['examples/**/*.rb']
Expand Down
9 changes: 7 additions & 2 deletions examples/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def test_assertions
#
# Verify an enumerable is sorted
#
assert_sorted([1,2,3])
assert_sorted([1, 2, 3])

#
# Verify an enumerable is sorted descending
#
assert_sorted_desc([3,2,1])
assert_sorted_desc([3, 2, 1])

#
# Verify an enumerable is sorted based on a key
Expand All @@ -60,5 +60,10 @@ def test_assertions
# Verify an enumerable is sorted descending based on a key
#
assert_sorted_by(:key, [{:key => 2}, {:key => 1}])

#
# Verify a thing is between two other things
#
assert_between(1, 3, 2)
end
end
31 changes: 31 additions & 0 deletions lib/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,37 @@ def assert_sorted_by_desc(key, enum, message = "")
check_enum_for_key_sortability(key, enum)
assert_sorted_desc enum.map{ |x| x.is_a?(Hash) ? x[key] : x.send(key) }
end

#
# ====Description:
# Find out if a something is between two other things when compared.
# It asserts that lhs1 < rhs < lhs2 or lhs1 > rhs > lhs2
#
# ====Example:
# assert_between(Date.yesterday, Date.tomorrow, Date.today)
#
# ====Parameters:
# [lhs1]
# One of the things to compare against
# [lhs2]
# The other thing to compare against
# [rhs]
# The thing being tested for between-ness
# [message = ""]
# An optional additional message that will be displayed if the
# assertion fails.
#
def assert_between(lhs1, lhs2, rhs, message="")
if lhs1 == lhs2
full_message = build_message(message, "Gave the same value for both sides of range. <?> was not equal to <?>", rhs, lhs1)
assert_block(full_message) { lhs1 == rhs }
else
lower = lhs1 < lhs2 ? lhs1 : lhs2
higher = lhs1 < lhs2 ? lhs2 : lhs1
full_message = build_message(message, "<?> was not between <?> and <?>", rhs, lower, higher)
assert_block(full_message) { lower < rhs && rhs < higher }
end
end

#
# ====Description:
Expand Down
26 changes: 26 additions & 0 deletions test/test_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,32 @@ def test_assert_sorted_by_desc
end
end

def test_assert_between
assert_raise_message("Gave the same value for both sides of range. <5> was not equal to <4>", Test::Unit::AssertionFailedError) do
assert_between 4, 4, 5
end

assert_raise_message("<5> was not between <2> and <3>", Test::Unit::AssertionFailedError) do
assert_between 3, 2, 5
end

assert_between 4, 8, 6
assert_between 8, 4, 6
assert_fail do
assert_between 4, 6, 8
end

today = DateTime.now
yesterday = today - 1
tomorrow = today + 1
assert_between yesterday, tomorrow, today
assert_between tomorrow, yesterday, today

assert_fail do
assert_between tomorrow, today, yesterday
end
end

def test_assert_raise_message
#
# Verify that the assertion passes correctly.
Expand Down

0 comments on commit f4bd630

Please sign in to comment.