Class: Speq::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/speq/question.rb

Overview

Boolean #match? for a given result and descriptive phrase

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, pass_phrase, fail_phrase) ⇒ Matcher

Returns a new instance of Matcher.



52
53
54
55
56
# File 'lib/speq/question.rb', line 52

def initialize(matcher, pass_phrase, fail_phrase)
  @matcher = matcher
  @pass_phrase = pass_phrase
  @fail_phrase = fail_phrase
end

Class Method Details

.a?(type) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/speq/question.rb', line 116

def self.a?(type)
  matcher_is(->(val) { val.is_a?(type) }, "a #{type}")
end

.eq?(expected_value) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/speq/question.rb', line 96

def self.eq?(expected_value)
  new(
    ->(result) { expected_value == result },
    "equals #{expected_value.inspect}",
    "does not equal #{expected_value.inspect}"
  )
end

.falsy?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/speq/question.rb', line 112

def self.falsy?
  matcher_is(->(actual_value) { actual_value ? false : true }, 'falsey')
end

.for(question_name, *args, &block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/speq/question.rb', line 70

def self.for(question_name, *args, &block)
  if respond_to?(question_name)
    send(question_name, *args, &block)
  else
    result_matcher(question_name, *args, &block)
  end
end

.have?(*symbols, **key_value_pairs) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/speq/question.rb', line 120

def self.have?(*symbols, **key_value_pairs)
  new(
    lambda do |object|
      symbols.each do |symbol|
        return false unless object.respond_to?(symbol)
      end
      key_value_pairs.each do |key, value|
        return false unless object.send(key) == value
      end
    end,
    "has #{symbols.empty? ? nil : symbols}#{key_value_pairs}",
    "doesn't have #{symbols.empty? ? nil : symbols}#{key_value_pairs}"
  )
end

.match?(description, &block) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/speq/question.rb', line 78

def self.match?(description, &block)
  new(block, "matches #{description}", "does not match #{description}")
end

.matcher_is(match, thing) ⇒ Object



66
67
68
# File 'lib/speq/question.rb', line 66

def self.matcher_is(match, thing)
  new(match, "is #{thing}", "is not #{thing}")
end

.raise?(expected_except) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
# File 'lib/speq/question.rb', line 135

def self.raise?(expected_except)
  case expected_except
  when Class
    raise_class(expected_except)
  when String
    raise_message(expected_except)
  else
    raise ArgumentError
  end
end

.raise_class(expected_error) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/speq/question.rb', line 146

def self.raise_class(expected_error)
  Matcher.new(
    ->(actual_except) { actual_except.class <= expected_error },
    "raises #{expected_error}",
    "does not raise #{expected_error}"
  )
end

.raise_message(expected_message) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/speq/question.rb', line 154

def self.raise_message(expected_message)
  Matcher.new(
    ->(actual_except) { actual_except.message == expected_message },
    "raises #{expected_message.inspect}",
    "does not raise #{expected_message.inspect}"
  )
end

.result_matcher(question_name, *args, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/speq/question.rb', line 82

def self.result_matcher(question_name, *args, &block)
  new(
    lambda do |obj|
      if obj.respond_to?(question_name)
        return obj.send(question_name, *args, &block)
      end

      raise "No question called #{question_name.inspect} or existing method on #{obj.inspect}"
    end,
    "is #{question_name[0..-2]}",
    "is not #{question_name[0..-2]}"
  )
end

.true?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/speq/question.rb', line 104

def self.true?
  matcher_is(->(result) { result == true }, 'true')
end

.truthy?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/speq/question.rb', line 108

def self.truthy?
  matcher_is(->(actual_value) { actual_value ? true : false }, 'truthy')
end

Instance Method Details

#match?(actual) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/speq/question.rb', line 58

def match?(actual)
  @matcher[actual]
end

#phrase(actual) ⇒ Object



62
63
64
# File 'lib/speq/question.rb', line 62

def phrase(actual)
  @matcher[actual] ? @pass_phrase : @fail_phrase
end