1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
= Introduction
AE is an assertions framework for Ruby. It's designed
around the concept of an Assertor. The Assertor is an
Assertion Functor, or Higher-Order Function, which
reroutes method calls while monitoring them for failing
conditions.
== What AE Provides
Requiring the AE library.
require 'ae'
Loads two classes, +Assertion+ and +Assertor+, the Kernel
method +assert+ and it's antonyms +assert!+ and +refute+
and a set of core extensions that make writing certain types
of assertions easier.
== Assertion and Assertor Classes
The +Assertion+ class is at the heart of AE. All other AE
methods depend on it. The +Assertion+ class is a subclass
of Exception. When an assertion is made and fails, it is
an instance of Assertion that is raised.
expect Assertion do
msg = "my failure message"
assert false, msg
end
Like any raised exception, the last Assertion message is available
via <tt>$!</tt>.
(FYI, in Test::Unit the equivalent class was called +AssertionFailedError+.)
Assertions themselves are not generally used in creating tests or
behavior specifications. Rather they are used to create additional
types of assertion methods.
As mentioned above the +Assertor+ class is a type of Higher-Order
function, or Functor, which intercedes with a normal message
invocation to monitor for failed conditions, upon which is raises
Assertion exceptions.
== Assertion Methods
The three methods, +assert+, <tt>assert!</tt> and +refute+ all
return an Assertor instance when used fluidly, i.e. magic-dot
notation, higher-order notation, functor notation, whatever you
prefer to call it.
assert(AE::Assertor === assert)
Through the use of +method_missing+, the Assertor allows us to write
statements like:
1.assert == 1
If the operation evaluates to false or nil, then an Assertion error
is raised.
expect Assertion do
1.assert == 2
end
The methods <tt>assert!</tt> and +refute+ are just like +assert+
expect they purport the negative condition. Patterned after Ruby's
own use of "<tt>!</tt>" as meaning +not+, <tt>assert!</tt> should be
read "assert not". While +refute+ exists for the sake of those who
find the use of a bang method for this purpose unsuited to them.
== How It Works
An Assertor essentially sits in wait for a method call (via
method_missing). When that happens it applies the method to the
original receiver, but wrapped in a clause that raises an
Assertion should the statement fail. If we wanted to be
pedantic, we could write our assertions like:
raise Assertion.new("1 != 1") unless 1 == 1
Instead of
1.assert == 1
Obviously using Assertor methods are whole lot more concise.
= Assertion Class
The Assertion class is a subclass of Exception and is the error raised when
and assertion fails.
= Assert Method
== Compatible with Test::Unit
The +assert+ method is designed to be backward compatible
with the same method in <tt>Test::Unit</tt>.
Using an argument, +assert+ will check that an argument evaluates
to true. Optionally one can send along a meaningful message should
the assertion fail.
assert(true, "Not true!")
expect Assertion do
assert(false, "Not true!")
end
== Assert with a Block
In addition +assert+ has been extended to accept a block. Like the case of the
argument, the block is expected to return something that evaluates as true.
assert do
true
end
Assertion.assert.raised? do
assert do
false
end
end
We should also mention that, while probably not very useful, since
the arity of a block can be checked, one can also pass the receiver
into the block as a block argument.
"hi".assert do |s|
/h/ =~ s
end
== Antonyms for Assert
We can state the opposite assertion using <tt>assert!</tt>.
10.assert! == 9
Or, because some people do not like the use of a bang method, +refute+.
10.refute == 9
These terms can be used just as +assert+ is used in all examples,
but with the opposite inference.
Another way to get the opposite inference, is to use +not+.
10.assert.not == 9
== Lambda Assertions
Passing +assert+ a `Proc` object, or any object that responds to `#call`,
will be used as if it were a block. This allows for a simple way to quickly
create reusable assertions.
palindrome = lambda{ |word| word == word.reverse }
"abracarba".assert palindrome
The message for a failed assertion will come from calling `#to_s` on the
object.
== RSpec-style Assertion Matchers
If an object passed to assert responds to `#matches?` then AE will handle
the object as an RSpec-style mather, the receiver will be passed to the
`#matches?` method to determine if the assertion passes and RSpec matcher
message methods will be used if they are defined.
palindrome = Object.new
def palindrome.matches?(word)
word == word.reverse
end
"abracarba".assert palindrome
== Identity Assertions
Rather then the general form.
x = 10
x.assert.object_id == x.object_id
We can use Ruby's own <tt>equal?</tt> method.
x.assert.equal?(x)
AE provides <tt>identical?</tt> method as an alternative
to make it a bit more clear.
x.assert.identical?(x)
== Equality Assertions
The most common assertion is that of value equality (<tt>==</tt>),
as we have seen throughout this document. But other forms of
equality can be verified as easily. We have already mentioned
identity. In addition there is <i>type equality</i>.
17.assert.eql? 17
Assertion.assert.raised? do
17.assert.eql? 17.0
end
And there is <i>case equality</i>.
Numeric.assert === 3
== Checking Equality with a Block
Because operators can not take blocks, and at times blocks can
be convenient means of supplying a value to an assertion,
AE has defined alternate renditions of the equality methods.
For equal? and eql?, the method names are the same, they simply
can take a block in place of an argument if need be.
For <i>value equality</i> (<tt>==</tt>), the method is called <tt>eq?</tt>.
10.assert.eq? do
10.0
end
And should it fail...
Assertion.assert.raised? do
10.assert.eq? do
20
end
end
== Case Equality
For <i>case equality</i> (<tt>===</tt>), it is <tt>case?</tt>.
Numeric.assert.case? do
"3".to_i
end
Assertion.assert.raised? do
Numeric.assert.case? do
"3"
end
end
== Regular Expressions
Regular Expressions can be used to make assertions in much the same way as equality.
/i/.assert =~ "i"
Assertion.assert.raised? do
/i/.assert =~ "g"
end
Conversely the String class recognizes the #=~ method as well.
"i".assert =~ /i/
Assertion.assert.raised? do
"i".assert =~ /g/
end
== Exception Assertions
Validating errors is easy too, as has already been shown
in the document to verify assertion failures.
StandardError.assert.raised? do
unknown_method
end
== Assertions on Object State
While testing or specifying the internal state of an object is
generally considered poor form, there are times when it is
necessary. Assert combined with +instance_eval+ makes it easy too.
class X
attr :a
def initialize(a); @a = a; end
end
x = X.new(1)
x.assert.instance_eval do
@a == 1
end
== Catch/Try Assertions
Catch/Try throws can be tested via <tt>Symbol#thrown?</tt>.
:hookme.assert.thrown? do
throw :hookme
end
Alternatively, a lambda containing the potential throw
can be the receiver using <tt>throws?</tt>.
hook = lambda{ throw :hookme }
hook.assert.throws?(:hookme)
== Assertions on Proc Changes
I have to admit I'm not sure how this is useful,
but I found it in the Bacon API and ported it over
just for sake of thoroughness.
a = 0
l = lambda{ a }
l.assert.change?{ a +=1 }
== Assertion on literal True, False and Nil
Ruby already provides the #nil? method.
nil.assert.nil?
AE adds <tt>true?</tt> and <tt>false?</tt> which acts accordingly.
true.assert.true?
false.assert.false?
== Send Assertions
Assert that a method can be successfully called.
"STRING".assert.send?(:upcase)
== Numeric Delta and Epsilon
You may wish to assert that a numeric value is with some
range.
3.in_delta?(1,5)
Or minimum range.
3.in_epsilon?(3,5)
== Verifying Object State
Not surprisingly if underlying object state needs to be verified, +instance_eval+
can be used in conjunction with +assert+.
class X
attr :a
def initialize(a); @a = a; end
end
x = X.new(4)
x.instance_eval do
@a.assert == 4
end
However #instance_eval is a reserved method for the underlying Assertor class,
so it cannot be used on #assert, e.g.
x.assert.instance_eval do
@a == "obvisouly wrong"
end
AE offers an optional helper method for times when testing underlying private
or protected methods is important, called #pry. See the QED on pry for more
information.
For some testing underlying implementation might be considered poor
form. You will get no argument here. It should be used thoughtfully,
but I would not bet against there being occasions when such validations
might be needed.
= Subjunctives
Okay. I can hear the BDDers rumbling, "where's the *should?*"
AE has nothing against "should", but there are different
approaches for utilizing should nomenclature in specifications,
and AE wants to be open to these techniques. One of which
is how Shoulda (http://shoulda.rubyforge.org) utilizes
+should+ in a way analogous to RSpec's use of +it+.
Even so, AE provides an optional mixin called +Subjunctive+ which
can be used to create assertor methods with English subjunctive
terms, such as +should+, or +must+, +shall+ and +will+.
To load this library use:
require 'ae/subjunctive'
Then all that is required it to define a subjunctive method for all
objects. For example:
def will(*args, &block)
Assertor.new(self, :backtrace=>caller).be(*args,&block)
end
It's that easy. Because of their commonality AE provides two such terms,
+should+ and +must+ as optional add-ons out-of-the-box.
require 'ae/should'
require 'ae/must'
We will use these two methods interchangeable for the rest of this
demonstration, but to be clear they both work exactly the same way,
and almost exactly like +assert+.
Keep in mind, AE "conical" functionality does not entail the subjunctive
forms. These are simply options you can load via your <tt>test_helper.rb</tt>,
or similar script, if you prefer these nomenclatures.
== Fluent Notation and Antonyms
Like +assert+, +should+ and +must+ can be used as higher order functions.
4.should == 4
4.must == 4
Antonyms provided for +should+ as <tt>should!</tt> (read "should not") and +shouldnt+.
For +must+ +must+, they are <tt>must!</tt> and +wont+.
4.should! == 5
4.shouldnt == 5
4.must! == 5
4.wont == 5
== To Be
On occasions where the English readability of a specification is hindered,
+be+ can be used.
StandardError.must.be.raised? do
unknown_method
end
The +be+ method is the same as +assert+ with the single exception
that it will compare a lone argument to the receiver using +equate?+,
unlike +assert+ which simply checks to see that the argument evaluates
as true.
10.should.be 10
10.should.be 10.0
10.should.be Numeric
Assertion.assert.raised? do
10.should.be "40"
end
== Indefinite Articles
Additional English forms are +a+ and +an+, equivalent to +be+ except
that they use <tt>case?</tt> (same as <tt>#===</tt>) instead of
<tt>equate?</tt> when acting on a single argument.
"hi".must.be.a String
Assertion.assert.raised? do
/x/.must.be.a /x/
end
Otherwise they are interchangeable.
"hi".must.be.an.instance_of?(String)
The indefinite articles work well when a noun follows as an arguments.
palindrome = lambda{ |x| x == x.reverse }
"abracarba".must.be.a palindrome
= Expect Method
Expect is another assertion nomenclature available for use in your
tests or specifications. Inspired by Jay Fields' Expectations library,
it provides convenient syntax for creating exception and case equality
assertions.
require 'ae/expect'
== Underlying Comparison
Expect uses #=== for comparison. So providing an argument and a block to
#expect we can test for a somewhat broader range of compassion than #assert.
For example we can test for a subclass.
expect Numeric do
3
end
Assertion.assert.raised? do
expect Numeric do
"3"
end
end
== Exception Expectation
If the comparator is an Exception class or a instance of an Exception class,
then #expect will check to see if the block raises that kind of exception.
expect StandardError do
some_undefined_method
end
expect Assertion do
expect(nil)
end
This is an important distinction to note because it means #expect can not be used
if verify instances of Exception classes.
Assertion.assert.raised? do
expect Exception do
Exception.new
end
end
== Regex Expectations
That #expect entails #=== also means we can check for Regexp matches.
expect /x/ do
"oooxooo"
end
== Expected Method
We can use #expected to make the receiver the object of expectation.
x = "dummy"
/x/.expected do
"x"
end
== Without Block
Without a block, the receiver is compared to the argument.
x.expect String
== Negative Forms
Like #assert, #expect has a negated form called #expect!
expect! /x/ do
"o"
end
The pure word form for those who do not like the clever use of the
explimation mark is #forbid.
forbid /x/ do
"o"
end
== Functor, or Higher Order Function
Like #assert, #expect can be used used as a *fluid* notation.
10.expect == 10
In which case it works just like #assert, including negative forms.
10.expect! == 11
10.forbid == 11
= Assertion Counts
AE tracks the number of assertions made and the number that failed to pass.
We can reset the count using the +recount+ class method.
old_counts = AE::Assertor.recount
For example if we have one assertion pass and another fail.
assert(true)
expect Assertion do
assert(false)
end
We will see that AE counted three assertions and one failure.
counts = AE::Assertor.counts.dup
counts[:total].assert == 3
counts[:pass].assert == 2
counts[:fail].assert == 1
The #expect call is an assertion too, which is why the total count is 3
rather than 2.
Now that we are done checking counts we will restore them so that
any other demos being run with this will tally correctly.
AE::Assertor.recount(old_counts)
AE::Assertor.counts[:total] += 3
AE::Assertor.counts[:pass] += 3
= Matchers
Matchers are simply Procs or objects with the proper interface that can be
passed to #assert or #refute (or other Assertor) as an ecapsulated test.
== Proc or #to_proc
Passing a Proc object or an object that responds to :to_proc, will use it
as if it were a block of the method. This allows for a simple way to quickly
create reusable assertions.
palindrome = lambda{ |word| word == word.reverse }
"abracarba".assert palindrome
== #matches?
Additionally if an object responds to #matches? then the receiver
will be passed to this method to determine if the assertion passes.
palindrome = Object.new
def palindrome.matches?(word)
word == word.reverse
end
"abracarba".assert palindrome
== RSpec, Shoulda and other 3rd-Party Matchers
With tha addition of #matches?, AE supports the same interface for matchers
as RSpec. Any matcher library designed for use with RSpec should therefore
be usable with AE as well. This includes RSpecs own matchers and Shoulda's
excellent Rails matchers.
== Check Ok/No
The Check library is an optional library that can be used
to conveniently speed-up construction of reptitive assertions.
To use it, first require the library, then include the mixin
into the namespace in which you will utilize it.
require 'ae/check'
include AE::Check
Now we can define ok/no check procedures. A one-off procedure is
defined with a block only.
check do |x, y|
x == y
end
ok 1,1
no 1,2
To define reusable check procedures, give the procedure a name.
check :palindrome do |x|
x.reverse == x
end
This will also cause the current check method to be set.
Later in the code, the check procedure can be reset to this
by just passing the name.
check :palindrome
ok 'abracarba'
no 'foolishness'
The Check mixin comes preloaded with a few standard checks.
By default the `:equality` procedure is used.
check :equality
ok 1=>1.0
Notice the use of the hash argument here. This is a useful construct for
many check procedures becuase it it akin to the `#=>` pattern we often
see in code examples and it also allows for multiple assertions in one call.
For instance, in the case of `:equality`, multiple entries convey a
meaning of logical-or.
ok 1=>2, 1=>1
This would pass becuase the second assertion of equality is true.
Another built in check is `:case_equality` which uses `#===` instead of `#==`
to make the comparison.
check :case_equality
ok 1=>Integer
|