Skip to content

Commit 0daa094

Browse files
committed
assert and deny methods that do a few things
0 parents  commit 0daa094

8 files changed

+237
-0
lines changed

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2010 Steve Conover
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.

README

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Wrong provides an assert method that takes a block, and interrogates the block to create rich assertion failure messages.
2+
3+
It's an offshoot of predicated.
4+
http://github.com/sconover/predicated
5+
6+
Inspired by assert { 2.0 }
7+
http://assert2.rubyforge.org/
8+
9+
Tracker project:
10+
http://www.pivotaltracker.com/projects/95014
11+
12+
“I think it's wrong that only one company makes the game Monopoly.” -Steven Wright

lib/wrong.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$LOAD_PATH.unshift(File.dirname(__FILE__))
2+
3+
require "wrong/assert"

lib/wrong/assert.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "predicated/predicate"
2+
require "predicated/from/callable_object"
3+
require "predicated/to/sentence"
4+
5+
module Wrong
6+
module Assert
7+
def assert(&block)
8+
raise FailureMessageForBlock.new(block).failure_str unless block.call
9+
end
10+
11+
def deny(&block)
12+
raise FailureMessageForBlock.new(block).reverse_failure_str if block.call
13+
end
14+
15+
class FailureMessageForBlock
16+
include Predicated
17+
18+
def initialize(block)
19+
@block = block
20+
end
21+
22+
def failure_str
23+
Predicate.from_callable_object(@block).to_negative_sentence
24+
end
25+
26+
def reverse_failure_str
27+
Predicate.from_callable_object(@block).to_sentence
28+
end
29+
end
30+
end
31+
end

test/basic_assert_test.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "test/test_helper"
2+
3+
require "wrong/assert"
4+
5+
apropos "basic assert features" do
6+
7+
before do
8+
@m = Module.new do
9+
extend Wrong::Assert
10+
end
11+
end
12+
13+
apropos "pass/fail basics" do
14+
test "passes when the result is true. deny does the reverse" do
15+
@m.assert{true}
16+
@m.assert{1==1}
17+
18+
@m.deny{false}
19+
@m.deny{1==2}
20+
end
21+
22+
test "fails when result is false. deny does the reverse" do
23+
assert_raises(RuntimeError) { @m.assert{false} }
24+
assert_raises(RuntimeError) { @m.assert{1==2} }
25+
26+
assert_raises(RuntimeError) { @m.deny{true} }
27+
assert_raises(RuntimeError) { @m.deny{1==1} }
28+
end
29+
30+
class MyError < StandardError; end
31+
32+
test "both deny and assert fail when an error is thrown. bubbles up the error." do
33+
assert_raises(MyError) { @m.assert{ raise MyError.new } }
34+
assert_raises(MyError) { @m.deny{ raise MyError.new } }
35+
end
36+
end
37+
38+
end

test/failures_test.rb

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require "test/test_helper"
2+
3+
require "wrong/assert"
4+
5+
apropos "failures" do
6+
7+
before do
8+
@m = Module.new do
9+
extend Wrong::Assert
10+
end
11+
end
12+
13+
def get_error
14+
error = nil
15+
begin
16+
yield
17+
rescue Exception => e
18+
error = e
19+
end
20+
e
21+
end
22+
23+
apropos "simple" do
24+
test "raw boolean failure" do
25+
assert_match "false", get_error{@m.assert{false}}.message
26+
assert_match "true", get_error{@m.deny{true}}.message
27+
end
28+
29+
test "equality failure" do
30+
assert_match "1 is not equal to 2", get_error{@m.assert{1==2}}.message
31+
assert_match "1 is equal to 1", get_error{@m.deny{1==1}}.message
32+
end
33+
34+
test "failure of basic operations" do
35+
assert_match "1 is not greater than 2", get_error{@m.assert{1>2}}.message
36+
assert_match "2 is not less than 1", get_error{@m.assert{2<1}}.message
37+
assert_match "1 is not greater than or equal to 2", get_error{@m.assert{1>=2}}.message
38+
assert_match "2 is not less than or equal to 1", get_error{@m.assert{2<=1}}.message
39+
40+
assert_match "2 is greater than 1", get_error{@m.deny{2>1}}.message
41+
assert_match "1 is less than 2", get_error{@m.deny{1<2}}.message
42+
assert_match "2 is greater than or equal to 1", get_error{@m.deny{2>=1}}.message
43+
assert_match "1 is less than or equal to 2", get_error{@m.deny{1<=2}}.message
44+
end
45+
46+
class Color
47+
attr_reader :name
48+
def initialize(name)
49+
@name = name
50+
end
51+
52+
def ==(other)
53+
other.is_a?(Color) && @name == other.name
54+
end
55+
56+
def inspect
57+
"Color:#{@name}"
58+
end
59+
end
60+
61+
test "object failure" do
62+
assert_match "'Color:red' is not equal to 2", get_error{@m.assert{Color.new("red")==2}}.message
63+
end
64+
65+
test %{multiline assert block shouldn't look any different
66+
than when there everything is on one line} do
67+
assert_match("1 is not equal to 2", get_error{@m.assert{
68+
1==
69+
2
70+
}}.message)
71+
end
72+
73+
end
74+
75+
apropos "accessing and printing values set outside of the assert" do
76+
test "use a value in the assert defined outside of it" do
77+
a = 1
78+
assert_match "1 is not equal to 2", get_error{@m.assert{a==2}}.message
79+
assert_match "1 is equal to 1", get_error{@m.deny{a==1}}.message
80+
end
81+
end
82+
83+
apropos "the assert block has many statements" do
84+
test "only pay attention to the final statement" do
85+
assert_match("1 is not equal to 2", get_error{@m.assert{
86+
a = "aaa"
87+
b = 1 + 2
88+
c = ["foo", "bar"].length / 3
89+
if a=="aaa"
90+
b = 4
91+
end; 1==2
92+
}}.message)
93+
end
94+
end
95+
end

test/suite.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#simple way to make sure requires are isolated
2+
result = Dir["test/**/*_test.rb"].collect{|test_file| system("ruby #{test_file}") }.uniq == [true]
3+
puts "suite " + (result ? "passed" : "FAILED")
4+
exit(result ? 0 : 1)

test/test_helper.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
dir = File.dirname(__FILE__)
2+
$LOAD_PATH.unshift "#{dir}/../lib"
3+
$LOAD_PATH.unshift "../predicated/lib"
4+
require "rubygems"
5+
require "minitest/spec"
6+
require "minitest/unit"
7+
require "pp"
8+
9+
class MiniTest::Unit::TestCase
10+
11+
end
12+
13+
module Kernel
14+
alias_method :apropos, :describe
15+
16+
def xapropos(str)
17+
puts "x'd out 'apropos \"#{str}\"'"
18+
end
19+
end
20+
21+
class MiniTest::Spec
22+
include MiniTest::Assertions
23+
24+
class << self
25+
alias_method :test, :it
26+
27+
def xtest(str)
28+
puts "x'd out 'test \"#{str}\"'"
29+
end
30+
31+
end
32+
end
33+
34+
MiniTest::Unit.autorun

0 commit comments

Comments
 (0)