You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.markdown
+50-32
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
## Abstract ##
6
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.
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 failure message that's not simply "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
8
9
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
10
@@ -16,11 +16,7 @@ Inspired by [assert { 2.0 }](http://assert2.rubyforge.org/) but rewritten from s
16
16
17
17
gem install wrong
18
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.
19
+
We have deployed gems for both Ruby and JRuby; if you get dependency issues on your platform, please let us know what Ruby interpreter and version you're using and what errors you get, and we'll try to track it down.
24
20
25
21
## Usage ##
26
22
@@ -139,30 +135,33 @@ or this
139
135
? 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
136
141
137
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)...
138
+
Also, try not to call any methods with side effects inside an assert. In addition to being bad form, this can cause messed-up failure messages, since the side effects may occur several times in the process of building the message.
142
139
143
140
Wrong also lets you put the expected and actual values in any order you want! Consider the failure messages for
144
141
145
142
assert { current_user == "joe" } # => Expected (current_user == "joe") but current_user is "fred"
146
143
assert { "joe" == current_user } # => Expected ("joe" == current_user) but current_user is "fred"
147
144
148
-
You get just the information you want, and none you don't want. At least, that's the plan! :-)
145
+
You get all the information you want, and none you don't want. At least, that's the plan! :-)
149
146
150
147
## Algorithm ##
151
148
152
149
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
150
154
151
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
152
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/)!)
153
+
* It works! Tested in MRI 1.8.6, 1.8.7, 1.9.1, 1.9.2, and JRuby 1.5.3. (Thank you, [rvm](http://rvm.beginrescueend.com/)!)
157
154
* Your code needs to be in a file.
158
155
* 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.
156
+
* We monkey-patch IRB so if you do `irb -rwrong` it'll save off your session in memory where Wrong can read it.
157
+
* It'd be nice if it could work inside a `-e` block but as far as we can tell, there's no way to grab that `-e` code from inside Ruby.
160
158
* It's a development-time testing library, not a production runtime library, so there are no security or filesystem issues.
161
159
*`eval` isn't evil, it's just misunderstood.
162
160
* 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.)
161
+
* 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.)
164
162
* 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.
163
+
* All variables and methods must be available in the binding of the assert block.
164
+
* Passing a proc around and eventually calling assert on it might not work in some Ruby implementations.
* RSpec - `require 'wrong/adapters/rspec'` (now supports both 1.3 and 2.0)
176
175
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.
176
+
To use these, put the appropriate `require` in your helper, **after** requiring your test framework; it should extend the framework enough that you can use `assert { }` in your test cases without extra fussing around.
178
177
179
178
## Explanations ##
180
179
@@ -248,34 +247,52 @@ To use these formatters, you have to explicitly `require` them! You may also nee
248
247
249
248
[Bug: turns out 'diff' and 'diff-lcs' are incompatible with each other. We're working on a fix.]
250
249
251
-
## Color ##
250
+
## Config ##
251
+
252
+
These settings can either be set at runtime on the `Wrong.config` singleton, or inside a `.wrong` file in the current directory or a parent. In the `.wrong` file just pretend every line is preceded with `Wrong.config.` -- e.g. if there's a setting called `ice_cream`, you can do any of these in your `.wrong` file
Apparently, no test framework is successful unless and until it supports console colors. So now we do. Call
267
+
268
+
Wrong.config.color
252
269
253
-
Apparently, no test framework is successful unless and until it supports console colors. So now we do. Put
270
+
in your test helper or rakefile or wherever, or put
254
271
255
-
Wrong.config[:color] = true
272
+
color
256
273
257
-
in your test helper or rakefile or wherever and get ready to be **bedazzled**. If you need custom colors, let us know.
274
+
in your `.wrong` file and get ready to be **bedazzled**. If you need custom colors, let us know.
258
275
259
-
## Aliases ##
276
+
###Aliases###
260
277
261
-
An end to the language wars! Name your "assert" and "deny" methods anything you want. Here are some suggestions:
278
+
An end to the language wars! Name your "assert" and "deny" methods anything you want. In your code, use `Wrong.config.alias_assert` and `Wrong.config.alias_deny`, and in your `.wrong` file, use Here are some suggestions:
262
279
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)
280
+
alias_assert:expect
281
+
alias_assert:should # This looks nice in RSpec
282
+
alias_assert:confirm
283
+
alias_assert:be
267
284
268
-
Wrong.config.alias_assert(:is)
269
-
Wrong.config.alias_deny(:aint)
285
+
alias_assert:is
286
+
alias_deny:aint
270
287
271
-
Wrong.config.alias_assert(:assure)
272
-
Wrong.config.alias_deny(:refute)
288
+
alias_assert:assure
289
+
alias_deny:refute
273
290
274
-
Wrong.config.alias_assert(:yep)
275
-
Wrong.config.alias_deny(:nope)
291
+
alias_assert:yep
292
+
alias_deny:nope
276
293
277
-
Wrong.config.alias_assert(:yay!)
278
-
Wrong.config.alias_deny(:boo!)
294
+
alias_assert:yay!
295
+
alias_deny:boo!
279
296
280
297
Just don't use "`aver`" since we took that one for an internal method in `Wrong::Assert`.
281
298
@@ -294,7 +311,8 @@ If you're in Ruby 1.8, you **really** shouldn't do it! But if you do, you can us
* the [Wrong way translation table (from RSpec and Test::Unit)](https://spreadsheets.google.com/pub?key=0AouPn6oLrimWdE0tZDVOWnFGMzVPZy0tWHZwdnhFYkE&hl=en&output=html). (Ask <[email protected]> if you want editing privileges.)
300
318
* the [Wrong slides](http://www.slideshare.net/alexchaffee/wrong-5069976) that Alex presented at Carbon Five and GoGaRuCo
0 commit comments