Module: RSpec::Matchers::Pretty

Included in:
BuiltIn::BaseMatcher, DSL::Matcher
Defined in:
lib/rspec/matchers/pretty.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) _pretty_print(array)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rspec/matchers/pretty.rb', line 23

def _pretty_print(array)
  RSpec.deprecate("`RSpec::Matchers::Pretty#_pretty_print`",
                  :replacement => "`RSpec::Matchers::Pretty#to_sentence`")
  result = ""
  array.each_with_index do |item, index|
    if index < (array.length - 2)
      result << "#{item.inspect}, "
    elsif index < (array.length - 1)
      result << "#{item.inspect} and "
    else
      result << "#{item.inspect}"
    end
  end
  result
end

- (Object) expected_to_sentence



47
48
49
50
51
# File 'lib/rspec/matchers/pretty.rb', line 47

def expected_to_sentence
  RSpec.deprecate("`RSpec::Matchers::Pretty#expected_to_sentence`",
                  :replacement => "`RSpec::Matchers::Pretty#to_sentence(expected)`")
  to_sentence(@expected) if defined?(@expected)
end

- (Object) name



53
54
55
# File 'lib/rspec/matchers/pretty.rb', line 53

def name
  defined?(@name) ? @name : underscore(self.class.name.split("::").last)
end

- (Object) name_to_sentence



43
44
45
# File 'lib/rspec/matchers/pretty.rb', line 43

def name_to_sentence
  split_words(name)
end

- (Object) split_words(sym)



4
5
6
# File 'lib/rspec/matchers/pretty.rb', line 4

def split_words(sym)
  sym.to_s.gsub(/_/,' ')
end

- (Object) to_sentence(words)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec/matchers/pretty.rb', line 8

def to_sentence(words)
  return "" unless words
  words = Array(words).map { |w| to_word(w) }
  case words.length
    when 0
      ""
    when 1
      " #{words[0]}"
    when 2
      " #{words[0]} and #{words[1]}"
    else
      " #{words[0...-1].join(', ')}, and #{words[-1]}"
  end
end

- (Object) to_word(item)



39
40
41
# File 'lib/rspec/matchers/pretty.rb', line 39

def to_word(item)
  is_matcher_with_description?(item) ? item.description : item.inspect
end

- (Object) underscore(camel_cased_word)

Borrowed from ActiveSupport



58
59
60
61
62
63
64
65
# File 'lib/rspec/matchers/pretty.rb', line 58

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