Skip to content
This repository
Browse code

be_true matcher from dsl to class

  • Loading branch information...
commit 747c3648e3f290b92df69e5e45992a24232b6dde 1 parent 175873e
David Chelimsky dchelimsky authored
6 lib/rspec/matchers/base_matcher.rb
@@ -13,7 +13,7 @@ module BaseMatcher
13 13
14 14 attr_reader :actual, :expected
15 15
16   - def initialize(expected)
  16 + def initialize(expected=nil)
17 17 @expected = expected
18 18 end
19 19
@@ -24,6 +24,10 @@ def failure_message_for_should
24 24 def failure_message_for_should_not
25 25 "expected #{actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
26 26 end
  27 +
  28 + def description
  29 + split_words(name)
  30 + end
27 31 end
28 32 end
29 33 end
11 lib/rspec/matchers/be.rb
@@ -2,14 +2,19 @@
2 2
3 3 module RSpec
4 4 module Matchers
  5 + class BeTrue
  6 + include BaseMatcher
5 7
6   - # @method be_true
7   - RSpec::Matchers.define :be_true do
8   - match do |actual|
  8 + def matches?(actual)
  9 + @actual = actual
9 10 actual
10 11 end
11 12 end
12 13
  14 + def be_true
  15 + BeTrue.new
  16 + end
  17 +
13 18 RSpec::Matchers.define :be_false do
14 19 match do |actual|
15 20 !actual
14 lib/rspec/matchers/pretty.rb
@@ -6,6 +6,7 @@ def split_words(sym)
6 6 end
7 7
8 8 def to_sentence(words)
  9 + return "" unless words
9 10 words = words.map{|w| w.inspect}
10 11 case words.length
11 12 when 0
@@ -42,8 +43,19 @@ def expected_to_sentence
42 43 end
43 44
44 45 def name
45   - defined?(@name) ? @name : self.class.name.split("::").last.downcase
  46 + defined?(@name) ? @name : underscore(self.class.name.split("::").last)
46 47 end
  48 +
  49 + # Borrowed from ActiveSupport
  50 + def underscore(camel_cased_word)
  51 + word = camel_cased_word.to_s.dup
  52 + word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  53 + word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  54 + word.tr!("-", "_")
  55 + word.downcase!
  56 + word
  57 + end
  58 +
47 59 end
48 60 end
49 61 end

0 comments on commit 747c364

Please sign in to comment.
Something went wrong with that request. Please try again.