wrong 0.4.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +300 -0
- data/lib/wrong.rb +27 -0
- data/lib/wrong/adapters/minitest.rb +14 -0
- data/lib/wrong/adapters/rspec.rb +21 -0
- data/lib/wrong/adapters/test_unit.rb +9 -0
- data/lib/wrong/assert.rb +105 -0
- data/lib/wrong/chunk.rb +233 -0
- data/lib/wrong/close_to.rb +9 -0
- data/lib/wrong/config.rb +29 -0
- data/lib/wrong/d.rb +42 -0
- data/lib/wrong/failure_message.rb +43 -0
- data/lib/wrong/helpers.rb +66 -0
- data/lib/wrong/irb.rb +16 -0
- data/lib/wrong/message/array_diff.rb +69 -0
- data/lib/wrong/message/string_comparison.rb +88 -0
- data/lib/wrong/message/test_context.rb +28 -0
- data/lib/wrong/rainbow.rb +127 -0
- data/lib/wrong/ruby2ruby_patch.rb +37 -0
- data/lib/wrong/sexp_ext.rb +49 -0
- data/lib/wrong/version.rb +3 -0
- data/test/adapters/minitest_test.rb +97 -0
- data/test/adapters/rspec1/failing_spec.rb +23 -0
- data/test/adapters/rspec2/failing_spec.rb +26 -0
- data/test/adapters/rspec_test.rb +104 -0
- data/test/adapters/test_unit_test.rb +59 -0
- data/test/assert_advanced_test.rb +51 -0
- data/test/assert_test.rb +76 -0
- data/test/capturing_test.rb +59 -0
- data/test/chunk_test.rb +264 -0
- data/test/close_to_test.rb +39 -0
- data/test/config_test.rb +89 -0
- data/test/d_test.rb +64 -0
- data/test/failure_message_test.rb +40 -0
- data/test/failures_test.rb +157 -0
- data/test/message/array_diff_test.rb +79 -0
- data/test/message/test_context_test.rb +69 -0
- data/test/rescuing_test.rb +17 -0
- data/test/separate.rb +4 -0
- data/test/sexp_ext_test.rb +80 -0
- data/test/string_comparison_test.rb +159 -0
- data/test/suite.rb +7 -0
- data/test/test_helper.rb +64 -0
- data/test/wrong_test.rb +60 -0
- metadata +215 -0
data/README.markdown
ADDED
@@ -0,0 +1,300 @@
|
|
1
|
+
## "Feels so right, it can't be Wrong"
|
2
|
+
|
3
|
+
![Someone is Wrong on the Internet](http://imgs.xkcd.com/comics/duty_calls.png)
|
4
|
+
|
5
|
+
## Abstract ##
|
6
|
+
|
7
|
+
Wrong provides a general assert method that takes a predicate block. Assertion failure messages are rich in detail. The Wrong idea is to replace all those countless assert\_this, assert\_that, should\_something library methods which only exist to give a more useful failure message than "assertion failed". Wrong replaces all of them in one fell swoop, since if you can write it in Ruby, Wrong can make a sensible failure message out of it.
|
8
|
+
|
9
|
+
We'd very much appreciate feedback and bug reports. There are plenty of things left to be done to make the results look uniformly clean and beautiful. We want your feedback, and especially to give us cases where either it blows up or the output is ugly or uninformative.
|
10
|
+
|
11
|
+
It relies on [Predicated](http://github.com/sconover/predicated) for its main failure message.
|
12
|
+
|
13
|
+
Inspired by [assert { 2.0 }](http://assert2.rubyforge.org/) but rewritten from scratch. Compatible with Ruby (MRI) 1.8, 1.9, and JRuby 1.5.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
gem install wrong
|
18
|
+
|
19
|
+
Under JRuby, the above may cause errors; if so, then try
|
20
|
+
|
21
|
+
gem install wrong-jruby
|
22
|
+
|
23
|
+
which untangles some dependencies.
|
24
|
+
|
25
|
+
## Usage ##
|
26
|
+
|
27
|
+
Wrong provides a simple assert method that takes a block:
|
28
|
+
|
29
|
+
require "wrong"
|
30
|
+
|
31
|
+
include Wrong
|
32
|
+
|
33
|
+
assert { 1 == 1 }
|
34
|
+
==> nil
|
35
|
+
|
36
|
+
assert { 2 == 1 }
|
37
|
+
==> Expected (2 == 1), but 2 is not equal to 1
|
38
|
+
|
39
|
+
If your assertion is more than a simple predicate, then Wrong will split it into parts and show you the values of all the relevant subexpressions.
|
40
|
+
|
41
|
+
x = 7; y = 10; assert { x == 7 && y == 11 }
|
42
|
+
==>
|
43
|
+
Expected ((x == 7) and (y == 11)), but
|
44
|
+
(x == 7) is true
|
45
|
+
x is 7
|
46
|
+
(y == 11) is false
|
47
|
+
y is 10
|
48
|
+
|
49
|
+
--
|
50
|
+
|
51
|
+
age = 24
|
52
|
+
name = "Gaga"
|
53
|
+
assert { age >= 18 && ["Britney", "Snooki"].include?(name) }
|
54
|
+
==>
|
55
|
+
Expected ((age >= 18) and ["Britney", "Snooki"].include?(name)), but
|
56
|
+
(age >= 18) is true
|
57
|
+
age is 24
|
58
|
+
["Britney", "Snooki"].include?(name) is false
|
59
|
+
name is "Gaga"
|
60
|
+
|
61
|
+
And a companion, 'deny':
|
62
|
+
|
63
|
+
deny{'abc'.include?('bc')}
|
64
|
+
==> Didn't expect "abc".include?("bc"), but 'abc' includes 'bc'
|
65
|
+
|
66
|
+
There's also a convenience method for catching errors:
|
67
|
+
|
68
|
+
assert{ rescuing{raise "vanilla"}.message == "chocolate" }
|
69
|
+
==>
|
70
|
+
Expected (rescuing { raise("vanilla") }.message == "chocolate"), but 'vanilla' is not equal to 'chocolate'
|
71
|
+
|
72
|
+
And one for capturing output streams:
|
73
|
+
|
74
|
+
assert { capturing { puts "hi" } == "hi\n" }
|
75
|
+
assert { capturing(:stderr) { $stderr.puts "hi" } == "hi\n" }
|
76
|
+
|
77
|
+
out, err = capturing(:stdout, :stderr) { ... }
|
78
|
+
assert { out == "something standard\n" }
|
79
|
+
assert { err =~ /something erroneous/ }
|
80
|
+
|
81
|
+
If you want to compare floats, try this:
|
82
|
+
|
83
|
+
assert { 5.0.close_to?(5.0001) } # default tolerance = 0.001
|
84
|
+
assert { 5.0.close_to?(5.1, 0.5) } # optional tolerance parameter
|
85
|
+
|
86
|
+
(If you don't want `close_to?` cluttering up `Float` in your test runs then use `include Wrong::Assert` instead of `include Wrong`.)
|
87
|
+
|
88
|
+
We also implement the most amazing debugging method ever, `d`, which gives you a sort of mini-wrong wherever you want it
|
89
|
+
, even in production code at runtime:
|
90
|
+
|
91
|
+
require 'wrong'
|
92
|
+
x = 7
|
93
|
+
d { x } # => prints "x is 7" to the console
|
94
|
+
d { x * 2 } # => prints "(x * 2) is 14" to the console
|
95
|
+
|
96
|
+
(`d` was originally implemented by Rob Sanheim in LogBuddy; as with Assert2 this is a rewrite and homage.) Remember, if you want `d` to work at runtime (e.g. in a webapp) then you must `include 'wrong/d'` inside your app, e.g. for in your `environment.rb` file.
|
97
|
+
|
98
|
+
More examples are in the file `examples.rb` <http://github.com/alexch/wrong/blob/master/examples.rb>
|
99
|
+
|
100
|
+
There's also a spreadsheet showing a translation from Test::Unit and RSpec to Wrong, with notes, at [this Google Doc](https://spreadsheets.google.com/pub?key=0AouPn6oLrimWdE0tZDVOWnFGMzVPZy0tWHZwdnhFYkE&hl=en&output=html). (Ask <alexch@gmail.com> if you want editing privileges.)
|
101
|
+
|
102
|
+
And don't miss the [slideshare presentation](http://www.slideshare.net/alexchaffee/wrong-5069976).
|
103
|
+
|
104
|
+
## Piecemeal Usage ##
|
105
|
+
|
106
|
+
We know that sometimes you don't want all the little doodads from a library cluttering up your namespace. If you **don't** do
|
107
|
+
|
108
|
+
require 'wrong'
|
109
|
+
include Wrong
|
110
|
+
|
111
|
+
then you can instead `require` and `include` just the bits you really want. For example:
|
112
|
+
|
113
|
+
require 'wrong/assert'
|
114
|
+
include Wrong::Assert
|
115
|
+
|
116
|
+
will give you the `assert` and `deny` methods but not the formatters or `rescuing` or `d` or `close_to?`. And if all you want is `d` then do:
|
117
|
+
|
118
|
+
require 'wrong/d'
|
119
|
+
include Wrong::D
|
120
|
+
|
121
|
+
To summarize: if you do `require 'wrong'` and `include Wrong` then you will get the whole ball of wax. Most people will probably want this since it's easier, but there is an alternative, whici is to `require` and `include` only what you want.
|
122
|
+
|
123
|
+
And beware: if you don't `require 'wrong'`, then `include Wrong` will not do anything at all.
|
124
|
+
|
125
|
+
## Apology ##
|
126
|
+
|
127
|
+
So does the world need another assertion framework? In fact, it does not! We actually believe the world needs **fewer** assert methods.
|
128
|
+
|
129
|
+
The Wrong idea is to replace all those countless assert\_this, assert\_that, should\_something library methods which only exist to give a more useful failure message than "assertion failed". Wrong replaces all of them in one fell swoop, since if you can write it in Ruby, Wrong can make a sensible failure message out of it.
|
130
|
+
|
131
|
+
Even the lowly workhorse `assert_equal` is bloated compared to Wrong: would you rather write this
|
132
|
+
|
133
|
+
assert_equal time, money
|
134
|
+
|
135
|
+
or this
|
136
|
+
|
137
|
+
assert { time == money }
|
138
|
+
|
139
|
+
? The Wrong way has the advantage of being plain, transparent Ruby code, not an awkward DSL that moves "equal" out of its natural place between the comparands. Plus, WYSIWYG! You know just from looking at it that "equal" means `==`, not `eql?` or `===` or `=~`.
|
140
|
+
|
141
|
+
Moreover, much like TDD itself, Wrong encourages you to write cleaner code. If your assertion messages are not clear and "Englishy", then maybe it's time for you to refactor a bit -- extract an informatively named variable or method, maybe push some function onto its natural object *a la* the [Law of Demeter](http://en.wikipedia.org/wiki/Law_of_Demeter)...
|
142
|
+
|
143
|
+
Wrong also lets you put the expected and actual values in any order you want! Consider the failure messages for
|
144
|
+
|
145
|
+
assert { current_user == "joe" } # => Expected (current_user == "joe") but current_user is "fred"
|
146
|
+
assert { "joe" == current_user } # => Expected ("joe" == current_user) but current_user is "fred"
|
147
|
+
|
148
|
+
You get just the information you want, and none you don't want. At least, that's the plan! :-)
|
149
|
+
|
150
|
+
## Algorithm ##
|
151
|
+
|
152
|
+
So wait a second. How do we do it? Doesn't Ruby have [poor support for AST introspection](http://blog.zenspider.com/2009/04/parsetree-eol.html)? Well, yes, it does, so we cheat: we figure out what file and line the assert block is defined in, then open the file, read the code, and parse it directly using Ryan Davis' amazing [RubyParser](http://parsetree.rubyforge.org/ruby_parser/) and [Ruby2Ruby](http://seattlerb.rubyforge.org/ruby2ruby/). You can bask in the kludge by examining `chunk.rb` and `assert.rb`. If you find some code it can't parse, please send it our way.
|
153
|
+
|
154
|
+
Before you get your knickers in a twist about how this is totally unacceptable because it doesn't support this or that use case, here are our caveats and excuses:
|
155
|
+
|
156
|
+
* It works! Tested in 1.8.6, 1.8.7, 1.9.1, and 1.9.2-rc2. (Thank you, [rvm](http://rvm.beginrescueend.com/)!)
|
157
|
+
* Your code needs to be in a file.
|
158
|
+
* If you're developing Ruby code without saving it to a mounted disk, then sorry, Wrong is not right for you.
|
159
|
+
* We monkey-patch IRB so if you do `irb -rwrong` it'll save off your session in a place Wrong can read it.
|
160
|
+
* It's a development-time testing library, not a production runtime library, so there are no security or filesystem issues.
|
161
|
+
* `eval` isn't evil, it's just misunderstood.
|
162
|
+
* It makes a few assumptions about the structure of your code, leading to some restrictions:
|
163
|
+
* You can't have more than one call to `assert` per line. (This should not be a problem since even if you're nesting asserts for some bizarre reason, we assume you know where your Return key is. And actually, technically you can put two asserts on a line, but it always describes the first one it sees, which means that if the second one executes, its failure message will be incorrect or broken.)
|
164
|
+
* You can't use metaprogramming to write your assert blocks.
|
165
|
+
* All variables and methods must be available in the binding of the assertion block.
|
166
|
+
|
167
|
+
## Adapters ##
|
168
|
+
|
169
|
+
Adapters for various test frameworks sit under wrong/adapters.
|
170
|
+
|
171
|
+
Currently we support
|
172
|
+
|
173
|
+
* Test::Unit - `require 'wrong/adapters/test_unit'`
|
174
|
+
* Minitest - `require 'wrong/adapters/minitest'`
|
175
|
+
* RSpec - `require 'wrong/adapters/rspec'`
|
176
|
+
|
177
|
+
To use these, put the appropriate `require` in your helper; it should extend the framework enough that you can use `assert { }` in your test cases without extra fussing around.
|
178
|
+
|
179
|
+
## Explanations ##
|
180
|
+
|
181
|
+
`assert` and `deny` can take an optional explanation, e.g.
|
182
|
+
|
183
|
+
assert("since we're on Earth") { sky.blue? }
|
184
|
+
|
185
|
+
Since the point of Wrong is to make asserts self-explanatory, you should feel free to use explanations only when they would add something that you couldn't get from reading the (failed) assertion code itself. Don't bother doing things like this:
|
186
|
+
|
187
|
+
assert("the sky should be blue") { sky.blue? } # redundant
|
188
|
+
|
189
|
+
The failure message of the above would be something like "`Expected sky.blue? but sky is :green`" which is not made clearer by the addition of "`the sky should be blue`". We already know it should be blue since we see right there ("`Expected (sky.blue?)`") that we're expecting it to be blue.
|
190
|
+
|
191
|
+
And if your assertion code isn't self-explanatory, then that's a hint that you might need to do some refactoring until it is. (Yes, even test code should be clean as a whistle. **Especially** test code.)
|
192
|
+
|
193
|
+
## Details ##
|
194
|
+
|
195
|
+
When a failure occurs, the exception message contains all the details you might need to make sense of it. Here's the breakdown:
|
196
|
+
|
197
|
+
Expected [CLAIM], but [SUMMARY]
|
198
|
+
[FORMATTER]
|
199
|
+
[SUBEXP] is [VALUE]
|
200
|
+
...
|
201
|
+
|
202
|
+
* CLAIM is the code inside your assert block, normalized
|
203
|
+
* SUMMARY is a to-English translation of the claim, via the Predicated library. This tries to be very intelligible; e.g. translating "include?" into "does not include" and so on.
|
204
|
+
* If there is a formatter registered for this type of predicate, its output will come next. (See below.)
|
205
|
+
* SUBEXP is each of the subtrees of the claim, minus duplicates and truisms (e.g. literals).
|
206
|
+
* The word "is" is a very nice separator since it doesn't look like code, but is short enough to be easily visually parsed.
|
207
|
+
* VALUE is `eval(SUBEXP).inspect`
|
208
|
+
|
209
|
+
We hope this structure lets your eyes focus on the meaningful values and differences in the message, rather than glossing over with stack-trace burnout. If you have any suggestions on how to improve it, please share them.
|
210
|
+
|
211
|
+
(Why does VALUE use `inspect` and not `to_s`? Because `inspect` on standard objects like String and Array are sure to show all relevant details, such as white space, in a console-safe way, and we hope other libraries follow suit. Also, `to_s` often inserts line breaks and that messes up formatting and legibility.)
|
212
|
+
|
213
|
+
## Formatters ##
|
214
|
+
|
215
|
+
Enhancements for error messages sit under wrong/message.
|
216
|
+
|
217
|
+
Currently we support special messages for
|
218
|
+
|
219
|
+
* String ==
|
220
|
+
* Enumerable ==
|
221
|
+
* including nested string elements
|
222
|
+
|
223
|
+
To use these formatters, you have to explicitly `require` them! You may also need to `gem install diff-lcs` (since it's an optional dependency).
|
224
|
+
|
225
|
+
require "wrong/message/string_diff"
|
226
|
+
assert { "the quick brown fox jumped over the lazy dog" ==
|
227
|
+
"the quick brown hamster jumped over the lazy gerbil" }
|
228
|
+
==>
|
229
|
+
Expected ("the quick brown fox jumped over the lazy dog" == "the quick brown hamster jumped over the lazy gerbil"), but "the quick brown fox jumped over the lazy dog" is not equal to "the quick brown hamster jumped over the lazy gerbil"
|
230
|
+
|
231
|
+
string diff:
|
232
|
+
the quick brown fox jumped over the lazy dog
|
233
|
+
^^^
|
234
|
+
the quick brown hamster jumped over the lazy gerbil
|
235
|
+
^^^^^^^
|
236
|
+
--
|
237
|
+
|
238
|
+
require "wrong/message/array_diff"
|
239
|
+
assert { ["venus", "mars", "pluto", "saturn"] ==
|
240
|
+
["venus", "earth", "pluto", "neptune"] }
|
241
|
+
==>
|
242
|
+
Expected (["venus", "mars", "pluto", "saturn"] == ["venus", "earth", "pluto", "neptune"]), but ["venus", "mars", "pluto", "saturn"] is not equal to ["venus", "earth", "pluto", "neptune"]
|
243
|
+
|
244
|
+
array diff:
|
245
|
+
["venus", "mars" , "pluto", "saturn" ]
|
246
|
+
["venus", "earth", "pluto", "neptune"]
|
247
|
+
^ ^
|
248
|
+
|
249
|
+
[Bug: turns out 'diff' and 'diff-lcs' are incompatible with each other. We're working on a fix.]
|
250
|
+
|
251
|
+
## Color ##
|
252
|
+
|
253
|
+
Apparently, no test framework is successful unless and until it supports console colors. So now we do. Put
|
254
|
+
|
255
|
+
Wrong.config[:color] = true
|
256
|
+
|
257
|
+
in your test helper or rakefile or wherever and get ready to be **bedazzled**. If you need custom colors, let us know.
|
258
|
+
|
259
|
+
## Aliases ##
|
260
|
+
|
261
|
+
An end to the language wars! Name your "assert" and "deny" methods anything you want. Here are some suggestions:
|
262
|
+
|
263
|
+
Wrong.config.alias_assert(:expect)
|
264
|
+
Wrong.config.alias_assert(:should) # This looks nice with RSpec
|
265
|
+
Wrong.config.alias_assert(:confirm)
|
266
|
+
Wrong.config.alias_assert(:be)
|
267
|
+
|
268
|
+
Wrong.config.alias_assert(:is)
|
269
|
+
Wrong.config.alias_deny(:aint)
|
270
|
+
|
271
|
+
Wrong.config.alias_assert(:assure)
|
272
|
+
Wrong.config.alias_deny(:refute)
|
273
|
+
|
274
|
+
Wrong.config.alias_assert(:yep)
|
275
|
+
Wrong.config.alias_deny(:nope)
|
276
|
+
|
277
|
+
Wrong.config.alias_assert(:yay!)
|
278
|
+
Wrong.config.alias_deny(:boo!)
|
279
|
+
|
280
|
+
Just don't use "`aver`" since we took that one for an internal method in `Wrong::Assert`.
|
281
|
+
|
282
|
+
## Helper Assert Methods ##
|
283
|
+
|
284
|
+
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.
|
285
|
+
|
286
|
+
If you're in Ruby 1.8, you **really** shouldn't do it! But if you do, you can use the "depth" parameter to give Wrong a better hint about how far up the stack it should crawl to find the code. See `assert_test.rb` for more details, if you dare.
|
287
|
+
|
288
|
+
## Authors ##
|
289
|
+
|
290
|
+
* Steve Conover - <sconover@gmail.com>
|
291
|
+
* Alex Chaffee - <alex@stinky.com> - <http://alexch.github.com>
|
292
|
+
* John Firebaugh
|
293
|
+
* Thierry Henrio
|
294
|
+
|
295
|
+
## Etc ##
|
296
|
+
|
297
|
+
* Github projects: <http://github.com/alexch/wrong>, <http://github.com/sconover/wrong>
|
298
|
+
* Tracker project: <http://www.pivotaltracker.com/projects/109993>
|
299
|
+
* the [Wrong way translation table (from RSpec and Test::Unit)](https://spreadsheets.google.com/pub?key=0AouPn6oLrimWdE0tZDVOWnFGMzVPZy0tWHZwdnhFYkE&hl=en&output=html). (Ask <alexch@gmail.com> if you want editing privileges.)
|
300
|
+
* the [Wrong slides](http://www.slideshare.net/alexchaffee/wrong-5069976) that Alex presented at Carbon Five and GoGaRuCo
|
data/lib/wrong.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
dir = File.expand_path(File.dirname(__FILE__))
|
2
|
+
$: << dir unless $:.include?(dir) # should we really have to do this? It's necessary to run examples.rb
|
3
|
+
|
4
|
+
require "predicated"
|
5
|
+
require "wrong/assert"
|
6
|
+
require "wrong/helpers"
|
7
|
+
require "wrong/chunk"
|
8
|
+
require "wrong/sexp_ext"
|
9
|
+
require "wrong/version"
|
10
|
+
require "wrong/config"
|
11
|
+
require "wrong/irb"
|
12
|
+
require "wrong/d"
|
13
|
+
require "wrong/message/array_diff"
|
14
|
+
require "wrong/message/string_comparison"
|
15
|
+
|
16
|
+
module Wrong
|
17
|
+
include Wrong::Assert
|
18
|
+
extend Wrong::Assert
|
19
|
+
include Wrong::Helpers
|
20
|
+
extend Wrong::Helpers
|
21
|
+
end
|
22
|
+
|
23
|
+
# this does some magic; if you don't like it, `require 'wrong/assert'` et al. individually and don't `require 'wrong/close_to'` or `require 'wrong'`
|
24
|
+
require "wrong/close_to"
|
25
|
+
|
26
|
+
# this does some magic; if you don't like it, `require 'wrong/assert'` et al. individually, don't `require 'wrong'`, and `include Wrong::D` only in the modules you want to call `d` from
|
27
|
+
Object.send :include, Wrong::D
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "wrong"
|
2
|
+
|
3
|
+
class MiniTest::Unit::TestCase
|
4
|
+
include Wrong
|
5
|
+
|
6
|
+
def failure_class
|
7
|
+
MiniTest::Assertion
|
8
|
+
end
|
9
|
+
|
10
|
+
def aver(valence, explanation = nil, depth = 0)
|
11
|
+
self._assertions += 1 # increment minitest's assert count
|
12
|
+
super(valence, explanation, depth + 1) # apparently this passes along the default block
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "wrong"
|
2
|
+
|
3
|
+
if Object.const_defined? :Spec
|
4
|
+
Spec::Runner.configure do |config|
|
5
|
+
include Wrong
|
6
|
+
|
7
|
+
def failure_class
|
8
|
+
Spec::Expectations::ExpectationNotMetError
|
9
|
+
end
|
10
|
+
end
|
11
|
+
elsif Object.const_defined? :RSpec
|
12
|
+
RSpec.configure do |config|
|
13
|
+
include Wrong
|
14
|
+
|
15
|
+
def failure_class
|
16
|
+
RSpec::Expectations::ExpectationNotMetError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
else
|
20
|
+
raise "Wrong's RSpec adapter can't find RSpec. Please require 'spec' or 'rspec' first."
|
21
|
+
end
|
data/lib/wrong/assert.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require "predicated/predicate"
|
2
|
+
require "predicated/from/ruby_code_string"
|
3
|
+
require "predicated/to/sentence"
|
4
|
+
|
5
|
+
require "wrong/chunk"
|
6
|
+
require "wrong/config"
|
7
|
+
require "wrong/failure_message"
|
8
|
+
require "wrong/ruby2ruby_patch" # need to patch it after some other stuff loads
|
9
|
+
|
10
|
+
module Wrong
|
11
|
+
module Assert
|
12
|
+
|
13
|
+
class AssertionFailedError < RuntimeError
|
14
|
+
end
|
15
|
+
|
16
|
+
def failure_class
|
17
|
+
AssertionFailedError
|
18
|
+
end
|
19
|
+
|
20
|
+
# Actual signature: assert(explanation = nil, depth = 0, &block)
|
21
|
+
def assert(*args, &block)
|
22
|
+
# to notice (and fail fast from) odd recursion problem
|
23
|
+
raise "Reentry bug while trying to assert(#{args.join(', ')})" if @_inside_wrong_assert
|
24
|
+
@_inside_wrong_assert = true
|
25
|
+
|
26
|
+
if block.nil?
|
27
|
+
begin
|
28
|
+
super(*args) # if there's a framework assert method (sans block), then call it
|
29
|
+
rescue NoMethodError => e
|
30
|
+
# note: we're not raising an AssertionFailedError because this is a programmer error, not a failed assertion
|
31
|
+
raise "You must pass a block to Wrong's assert and deny methods"
|
32
|
+
end
|
33
|
+
else
|
34
|
+
aver(:assert, *args, &block)
|
35
|
+
end
|
36
|
+
ensure
|
37
|
+
@_inside_wrong_assert = false
|
38
|
+
end
|
39
|
+
|
40
|
+
# Actual signature: deny(explanation = nil, depth = 0, &block)
|
41
|
+
def deny(*args, &block)
|
42
|
+
if block.nil?
|
43
|
+
test = args.first
|
44
|
+
msg = args[1]
|
45
|
+
assert !test, msg # this makes it get passed up to the framework
|
46
|
+
else
|
47
|
+
aver(:deny, *args, &block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def summary(method_sym, predicate)
|
52
|
+
method_sym == :deny ? predicate.to_sentence : predicate.to_negative_sentence
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
# for debugging -- if we couldn't make a predicate out of the code block, then this was why
|
58
|
+
def self.last_predicated_error
|
59
|
+
@@last_predicated_error ||= nil
|
60
|
+
end
|
61
|
+
|
62
|
+
# todo: move some/all of this into FailureMessage
|
63
|
+
def full_message(chunk, block, valence, explanation)
|
64
|
+
code = chunk.code
|
65
|
+
|
66
|
+
predicate = begin
|
67
|
+
Predicated::Predicate.from_ruby_code_string(code, block.binding)
|
68
|
+
rescue Predicated::Predicate::DontKnowWhatToDoWithThisSexpError, Exception => e
|
69
|
+
# save it off for debugging
|
70
|
+
@@last_predicated_error = e
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
|
74
|
+
code = code.color(:blue) if Wrong.config[:color]
|
75
|
+
message = ""
|
76
|
+
message << "#{explanation}: " if explanation
|
77
|
+
message << "#{valence == :deny ? "Didn't expect" : "Expected"} #{code}, but "
|
78
|
+
if predicate && !(predicate.is_a? Predicated::Conjunction)
|
79
|
+
message << summary(valence, predicate)
|
80
|
+
if formatter = FailureMessage.formatter_for(predicate)
|
81
|
+
failure = formatter.describe
|
82
|
+
failure = failure.bold if Wrong.config[:color]
|
83
|
+
message << failure
|
84
|
+
end
|
85
|
+
end
|
86
|
+
message << chunk.details
|
87
|
+
message
|
88
|
+
end
|
89
|
+
|
90
|
+
def aver(valence, explanation = nil, depth = 0, &block)
|
91
|
+
require "wrong/rainbow" if Wrong.config[:color]
|
92
|
+
|
93
|
+
value = block.call
|
94
|
+
value = !value if valence == :deny
|
95
|
+
unless value
|
96
|
+
|
97
|
+
chunk = Wrong::Chunk.from_block(block, depth + 2)
|
98
|
+
|
99
|
+
message = full_message(chunk, block, valence, explanation)
|
100
|
+
raise failure_class.new(message)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|