Skip to content

Commit 44fb83a

Browse files
committed
verbose mode
1 parent bea4efd commit 44fb83a

File tree

6 files changed

+98
-2
lines changed

6 files changed

+98
-2
lines changed

HISTORY.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
== 0.6.1 2012/02/03
2+
3+
* verbose mode (inching towards a framework)
4+
15
== 0.6.0 2011/10/13
26

37
* "eventually" helper method

README.markdown

+12
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,18 @@ Here are some suggestions:
434434

435435
Just don't use "`aver`" since we took that one for an internal method in `Wrong::Assert`.
436436

437+
### Verbose ###
438+
439+
Wrong works inside frameworks like Test::Unit and RSpec, but sometimes you just want to stick a bunch of assertions in a file and run it. In that case, *verbose mode* might come in handy. It prints every *successful* assertion to the console (including explanations, if provided, and in color, if desired).
440+
441+
Wrong.config.verbose
442+
assert("basic math") { 2 + 2 == 4}
443+
444+
prints
445+
446+
basic math: ((2 + 2) == 4)
447+
448+
437449
## Helper Assert Methods ##
438450

439451
If you really want to, you can define your proc in one method, pass it in to another method, and have that method assert it. This is a challenge for Wrong and you probably shouldn't do it. Wrong will do its best to figure out where the actual assertion code is but it might not succeed.

examples.rb

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
Wrong.config.color # or just put the line "color" in a file called ".wrong" in the current dir
1414

15+
Wrong.config.verbose # you probably don't want to do this in your own code
16+
assert("basic math") { 2 + 2 == 4 }
17+
1518
def example(name = nil)
1619
puts "\n=== Example#{":" if name} #{name}"
1720
e = rescuing do

lib/wrong/assert.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "wrong/config"
77
require "wrong/failure_message"
88
require "wrong/ruby2ruby_patch" # need to patch it after some other stuff loads
9+
require "wrong/rainbow"
910

1011
module Wrong
1112
module Assert
@@ -65,7 +66,17 @@ def aver(valence, explanation = nil, depth = 0, &block)
6566

6667
value = block.call
6768
value = !value if valence == :deny
68-
unless value
69+
if value
70+
if Wrong.config[:verbose]
71+
code = Wrong::Chunk.from_block(block, depth + 2).code
72+
if Wrong.config[:color]
73+
explanation = explanation.color(:blue) if explanation
74+
code = code.color(:green)
75+
end
76+
message = "#{explanation + ": " if explanation}#{code}"
77+
puts message
78+
end
79+
else
6980
chunk = Wrong::Chunk.from_block(block, depth + 2)
7081

7182
message = FailureMessage.new(chunk, valence, explanation).full

lib/wrong/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Wrong
2-
VERSION = "0.6.0" unless defined?(Wrong::VERSION)
2+
VERSION = "0.6.1" unless defined?(Wrong::VERSION)
33
end

test/verbose_test.rb

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
here = File.dirname(__FILE__)
2+
3+
require "#{here}/test_helper"
4+
require "wrong/assert"
5+
require "wrong/helpers"
6+
require "wrong/d"
7+
require "wrong/rainbow"
8+
9+
describe "verbose assert" do
10+
11+
include Wrong::Helpers
12+
include Wrong::D
13+
14+
before do
15+
@m = Module.new do
16+
extend Wrong::Assert
17+
end
18+
Wrong.config.verbose
19+
@color_enabled = Sickill::Rainbow.enabled
20+
end
21+
22+
after do
23+
Wrong.config[:verbose] = nil
24+
Wrong.config[:color] = nil
25+
Sickill::Rainbow.enabled = @color_enabled
26+
end
27+
28+
it "sets the verbose flag" do
29+
assert Wrong.config[:verbose]
30+
end
31+
32+
it "prints the contents of a successful assert" do
33+
out = capturing {
34+
@m.assert { 2 + 2 == 4 }
35+
}
36+
assert_equal "((2 + 2) == 4)\n", out
37+
end
38+
39+
it "prints the message and contents of a successful assert" do
40+
out = capturing {
41+
@m.assert("basic math") { 2 + 2 == 4 }
42+
}
43+
assert_equal "basic math: ((2 + 2) == 4)\n", out
44+
end
45+
46+
it "prints in color" do
47+
Wrong.config.color
48+
Sickill::Rainbow.enabled = true
49+
out = capturing {
50+
@m.assert { 2 + 2 == 4 }
51+
}
52+
colored = ["((2 + 2) == 4)".color(:green), "\n"].join
53+
assert_equal colored, out
54+
end
55+
56+
it "prints in color with an explanation" do
57+
Wrong.config.color
58+
Sickill::Rainbow.enabled = true
59+
out = capturing {
60+
@m.assert("basic math") { 2 + 2 == 4 }
61+
}
62+
colored = ["basic math".color(:blue), ": ", "((2 + 2) == 4)".color(:green), "\n"].join
63+
assert_equal colored, out
64+
end
65+
66+
end

0 commit comments

Comments
 (0)