PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/README.rdoc

https://github.com/Flameeyes/matchy
Unknown | 162 lines | 125 code | 37 blank | 0 comment | 0 complexity | c83ee45b2dafcc8e52331e0c6c6f26aa MD5 | raw file
  1. = matchy
  2. * http://github.com/jeremymcanally/matchy
  3. == DESCRIPTION:
  4. Hate writing assertions? Need a little behavior-driven love in your tests? Then matchy is for you.
  5. == FEATURES/PROBLEMS:
  6. * Get the beauty of RSpec without all the overhead
  7. * Create your own matchers with ease
  8. == SYNOPSIS:
  9. * Get BDD on your objects
  10. x = 13 * 4
  11. x.should == 42
  12. y = "hello"
  13. y.length.should_not be(4)
  14. * Use familiar syntax to specify things
  15. # RSpec
  16. "my string".should =~ /string/
  17. lambda { raise "FAIL" }.should raise_error
  18. # matchy
  19. "my string".should =~ /string/
  20. lambda { raise "FAIL" }.should raise_error
  21. * Most of familiar RSpec Matchers are built in
  22. # raise_error matcher
  23. lambda {raise}.should raise_error #pass
  24. lambda {raise MyCustomError.new}.should raise_error(MyCustomError) #pass
  25. lambda {raise "message"}.should raise_error("message") #pass
  26. lambda {raise "message"}.should raise_error(/essa/) #pass
  27. # change matcher
  28. lambda {@var+=1}.should change {@var}
  29. # passes
  30. lambda { }.should change {@var}
  31. # fails
  32. @var = 1
  33. lambda {@var+=1}.should change {@var}.from(1).to(2)
  34. # passes
  35. # be_something matcher
  36. @obj.should be_something
  37. # passes if @obj.something? is true
  38. * a lot more ...
  39. * Create your own custom matchers
  40. # maybe in your test helper
  41. class Test::Unit::TestCase
  42. custom_matcher :be_nil do |receiver, matcher, args|
  43. receiver.nil?
  44. end
  45. # also you can set positive (should) and negative (should not) failure messages
  46. custom_matcher :be_nil do |receiver, matcher, args|
  47. matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
  48. matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
  49. receiver.nil?
  50. end
  51. end
  52. # your actual test
  53. class NilTest < Test::Unit::TestCase
  54. def test_nil_stuff
  55. nil.should be_nil # pass
  56. nil.should_not be_nil # fail
  57. 'foo'.should_not be_nil # pass
  58. 'foo'.should be_nil # fail
  59. end
  60. end
  61. # Matchers can accept arguments
  62. class Test::Unit::TestCase
  63. custom_matcher :have_error_on do |receiver, matcher, args|
  64. attribute = args[0]
  65. receiver.valid?
  66. receiver.errors.on(attribute).should_not == nil
  67. end
  68. end
  69. class ArgumentTest < Test::Unit::TestCase
  70. class Item < ActiveRecord::Base
  71. validate_presence_of :title
  72. end
  73. def test_arguments
  74. item = Item.new
  75. item.should have_error_on(:title) # pass
  76. item.title = 'Foo'
  77. item.should_not have_error_on(:title) # pass
  78. end
  79. end
  80. # Even more advanced, you can have messages on matchers
  81. class Test::Unit::TestCase
  82. custom_matcher :have do |receiver, matcher, args|
  83. count = args[0]
  84. something = matcher.chained_messages[0].name
  85. actual = receiver.send(something).size
  86. matcher.positive_failure_message = "Expected #{receiver} to have #{actual} #{something}, but found #{count} "
  87. actual == count
  88. end
  89. end
  90. class MoreAdvancedTest < Test::Unit::TestCase
  91. class Item
  92. def tags
  93. %w(foo bar baz)
  94. end
  95. end
  96. def test_item_has_tags
  97. item = Item.new
  98. item.should have(3).tags # pass
  99. item.should have(2).tags # fail
  100. end
  101. end
  102. == REQUIREMENTS:
  103. * Test::Unit (you got it)
  104. == INSTALL:
  105. $ gem sources -a http://gems.github.com
  106. $ sudo gem install jnunemaker-matchy
  107. == LICENSE:
  108. (The MIT License)
  109. Copyright (c) 2008 Jeremy McAnally
  110. Permission is hereby granted, free of charge, to any person obtaining
  111. a copy of this software and associated documentation files (the
  112. 'Software'), to deal in the Software without restriction, including
  113. without limitation the rights to use, copy, modify, merge, publish,
  114. distribute, sublicense, and/or sell copies of the Software, and to
  115. permit persons to whom the Software is furnished to do so, subject to
  116. the following conditions:
  117. The above copyright notice and this permission notice shall be
  118. included in all copies or substantial portions of the Software.
  119. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  120. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  121. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  122. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  123. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  124. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  125. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.