PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/rspec/lib/spec/matchers/be.rb

http://github.com/robbyrussell/rubyurl
Ruby | 224 lines | 159 code | 26 blank | 39 comment | 31 complexity | 51667f637e51626aaa10e84f36d297ee MD5 | raw file
  1. module Spec
  2. module Matchers
  3. class Be #:nodoc:
  4. def initialize(*args)
  5. if args.empty?
  6. @expected = :satisfy_if
  7. else
  8. @expected = parse_expected(args.shift)
  9. end
  10. @args = args
  11. @comparison = ""
  12. end
  13. def matches?(actual)
  14. @actual = actual
  15. if handling_predicate?
  16. begin
  17. return @result = actual.__send__(predicate, *@args)
  18. rescue => predicate_error
  19. # This clause should be empty, but rcov will not report it as covered
  20. # unless something (anything) is executed within the clause
  21. rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
  22. end
  23. # This supports should_exist > target.exists? in the old world.
  24. # We should consider deprecating that ability as in the new world
  25. # you can't write "should exist" unless you have your own custom matcher.
  26. begin
  27. return @result = actual.__send__(present_tense_predicate, *@args)
  28. rescue
  29. raise predicate_error
  30. end
  31. else
  32. return match_or_compare
  33. end
  34. end
  35. def failure_message
  36. return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
  37. return "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
  38. end
  39. def negative_failure_message
  40. return "expected not #{expected}, got #{@actual.inspect}" unless handling_predicate?
  41. return "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
  42. end
  43. def expected
  44. return "if to be satisfied" if @expected == :satisfy_if
  45. return true if @expected == :true
  46. return false if @expected == :false
  47. return "nil" if @expected == :nil
  48. return @expected.inspect
  49. end
  50. def match_or_compare
  51. return @actual ? true : false if @expected == :satisfy_if
  52. return @actual == true if @expected == :true
  53. return @actual == false if @expected == :false
  54. return @actual.nil? if @expected == :nil
  55. return @actual < @expected if @less_than
  56. return @actual <= @expected if @less_than_or_equal
  57. return @actual >= @expected if @greater_than_or_equal
  58. return @actual > @expected if @greater_than
  59. return @actual == @expected if @double_equal
  60. return @actual === @expected if @triple_equal
  61. return @actual.equal?(@expected)
  62. end
  63. def ==(expected)
  64. @prefix = "be "
  65. @double_equal = true
  66. @comparison = "== "
  67. @expected = expected
  68. self
  69. end
  70. def ===(expected)
  71. @prefix = "be "
  72. @triple_equal = true
  73. @comparison = "=== "
  74. @expected = expected
  75. self
  76. end
  77. def <(expected)
  78. @prefix = "be "
  79. @less_than = true
  80. @comparison = "< "
  81. @expected = expected
  82. self
  83. end
  84. def <=(expected)
  85. @prefix = "be "
  86. @less_than_or_equal = true
  87. @comparison = "<= "
  88. @expected = expected
  89. self
  90. end
  91. def >=(expected)
  92. @prefix = "be "
  93. @greater_than_or_equal = true
  94. @comparison = ">= "
  95. @expected = expected
  96. self
  97. end
  98. def >(expected)
  99. @prefix = "be "
  100. @greater_than = true
  101. @comparison = "> "
  102. @expected = expected
  103. self
  104. end
  105. def description
  106. "#{prefix_to_sentence}#{comparison}#{expected_to_sentence}#{args_to_sentence}"
  107. end
  108. private
  109. def parse_expected(expected)
  110. if Symbol === expected
  111. @handling_predicate = true
  112. ["be_an_","be_a_","be_"].each do |prefix|
  113. if expected.starts_with?(prefix)
  114. @prefix = prefix
  115. return "#{expected.to_s.sub(@prefix,"")}".to_sym
  116. end
  117. end
  118. end
  119. @prefix = ""
  120. return expected
  121. end
  122. def handling_predicate?
  123. return false if [:true, :false, :nil].include?(@expected)
  124. return @handling_predicate
  125. end
  126. def predicate
  127. "#{@expected.to_s}?".to_sym
  128. end
  129. def present_tense_predicate
  130. "#{@expected.to_s}s?".to_sym
  131. end
  132. def args_to_s
  133. return "" if @args.empty?
  134. inspected_args = @args.collect{|a| a.inspect}
  135. return "(#{inspected_args.join(', ')})"
  136. end
  137. def comparison
  138. @comparison
  139. end
  140. def expected_to_sentence
  141. split_words(@expected)
  142. end
  143. def prefix_to_sentence
  144. split_words(@prefix)
  145. end
  146. def split_words(sym)
  147. sym.to_s.gsub(/_/,' ')
  148. end
  149. def args_to_sentence
  150. case @args.length
  151. when 0
  152. ""
  153. when 1
  154. " #{@args[0]}"
  155. else
  156. " #{@args[0...-1].join(', ')} and #{@args[-1]}"
  157. end
  158. end
  159. end
  160. # :call-seq:
  161. # should be
  162. # should be_true
  163. # should be_false
  164. # should be_nil
  165. # should be_arbitrary_predicate(*args)
  166. # should_not be_nil
  167. # should_not be_arbitrary_predicate(*args)
  168. #
  169. # Given true, false, or nil, will pass if actual is
  170. # true, false or nil (respectively). Given no args means
  171. # the caller should satisfy an if condition (to be or not to be).
  172. #
  173. # Predicates are any Ruby method that ends in a "?" and returns true or false.
  174. # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
  175. # convert that into a query against the target object.
  176. #
  177. # The arbitrary_predicate feature will handle any predicate
  178. # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
  179. # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
  180. #
  181. # == Examples
  182. #
  183. # target.should be
  184. # target.should be_true
  185. # target.should be_false
  186. # target.should be_nil
  187. # target.should_not be_nil
  188. #
  189. # collection.should be_empty #passes if target.empty?
  190. # "this string".should be_an_intance_of(String)
  191. #
  192. # target.should_not be_empty #passes unless target.empty?
  193. # target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
  194. def be(*args)
  195. Matchers::Be.new(*args)
  196. end
  197. end
  198. end