Class: Speq::Question

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

Overview

Records the question name, result, and constructs the corresponding matcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(question_name, result, matcher) ⇒ Question

Returns a new instance of Question.



21
22
23
24
25
# File 'lib/speq/question.rb', line 21

def initialize(question_name, result, matcher)
  @question_name = question_name
  @result = result
  @matcher = matcher
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



19
20
21
# File 'lib/speq/question.rb', line 19

def matcher
  @matcher
end

#question_nameObject (readonly)

Returns the value of attribute question_name.



19
20
21
# File 'lib/speq/question.rb', line 19

def question_name
  @question_name
end

#resultObject (readonly)

Returns the value of attribute result.



19
20
21
# File 'lib/speq/question.rb', line 19

def result
  @result
end

Class Method Details

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



39
40
41
42
43
44
45
46
47
# File 'lib/speq/question.rb', line 39

def self.for(result, question_name, *args, &block)
  matcher = Matcher.for(question_name, *args, &block)

  Question.new(
    question_name,
    result,
    matcher
  )
end

.question?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/speq/question.rb', line 15

def self.question?(method_name)
  method_name.to_s.end_with?('?')
end

Instance Method Details

#outcomeObject



33
34
35
36
37
# File 'lib/speq/question.rb', line 33

def outcome
  matcher.match?(result) ? Outcome.pass : Outcome.fail
rescue StandardError
  Outcome.error
end

#phraseObject



27
28
29
30
31
# File 'lib/speq/question.rb', line 27

def phrase
  matcher.phrase(result)
rescue StandardError => e
  QuestionError.new(@question_name, e.inspect).to_s
end

#to_sObject



47
48
49
50
# File 'lib/speq/string_fmt.rb', line 47

def to_s
  "#{phrase}#{outcome.fail? ? " (result = #{result.inspect})" : ''}"
    .send(outcome.pass? ? :green : :red)
end