Permalink
Browse files

be_true matcher from dsl to class

  • Loading branch information...
1 parent 175873e commit 747c3648e3f290b92df69e5e45992a24232b6dde @dchelimsky dchelimsky committed Oct 20, 2011
Showing with 26 additions and 5 deletions.
  1. +5 −1 lib/rspec/matchers/base_matcher.rb
  2. +8 −3 lib/rspec/matchers/be.rb
  3. +13 −1 lib/rspec/matchers/pretty.rb
@@ -13,7 +13,7 @@ module BaseMatcher
attr_reader :actual, :expected
- def initialize(expected)
+ def initialize(expected=nil)
@expected = expected
end
@@ -24,6 +24,10 @@ def failure_message_for_should
def failure_message_for_should_not
"expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
end
+
+ def description
+ split_words(name)
+ end
end
end
end
View
@@ -2,14 +2,19 @@
module RSpec
module Matchers
+ class BeTrue
+ include BaseMatcher
- # @method be_true
- RSpec::Matchers.define :be_true do
- match do |actual|
+ def matches?(actual)
+ @actual = actual
actual
end
end
+ def be_true
+ BeTrue.new
+ end
+
RSpec::Matchers.define :be_false do
match do |actual|
!actual
@@ -6,6 +6,7 @@ def split_words(sym)
end
def to_sentence(words)
+ return "" unless words
words = words.map{|w| w.inspect}
case words.length
when 0
@@ -42,8 +43,19 @@ def expected_to_sentence
end
def name
- defined?(@name) ? @name : self.class.name.split("::").last.downcase
+ defined?(@name) ? @name : underscore(self.class.name.split("::").last)
end
+
+ # Borrowed from ActiveSupport
+ def underscore(camel_cased_word)
+ word = camel_cased_word.to_s.dup
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
+ word.tr!("-", "_")
+ word.downcase!
+ word
+ end
+
end
end
end

0 comments on commit 747c364

Please sign in to comment.