Class: RSpec::Hal::Matchers::TemplatedRelationMatcher

Inherits:
Object
  • Object
show all
Includes:
HalMatcherHelpers
Defined in:
lib/rspec/hal/matchers/templated_relation_matcher.rb

Overview

Example

expect(doc).to have_templated_relation("search")
expect(doc).to have_templated_relation("search", matching("{?q}"))

Constant Summary

Constants included from HalMatcherHelpers

HalMatcherHelpers::NullMatcher

Instance Method Summary collapse

Methods included from HalMatcherHelpers

#matcher?, #matcherize, #parse, #sentencize

Constructor Details

#initialize(link_rel, expected = NullMatcher) ⇒ TemplatedRelationMatcher

Returns a new instance of TemplatedRelationMatcher.



13
14
15
16
# File 'lib/rspec/hal/matchers/templated_relation_matcher.rb', line 13

def initialize(link_rel, expected=NullMatcher)
  @link_rel = link_rel
  @expected = expected
end

Instance Method Details

#descriptionObject



52
53
54
# File 'lib/rspec/hal/matchers/templated_relation_matcher.rb', line 52

def description
  "have templated #{link_rel} link"
end

#failure_messageObject Also known as: failure_message_for_should



36
37
38
39
40
41
42
43
44
# File 'lib/rspec/hal/matchers/templated_relation_matcher.rb', line 36

def failure_message
  but_clause = if outcome == :no_templates
                 "found only non-templated links"
               else
                 "found none"
               end

  sentencize "Expected templated `#{link_rel}` link", expected.description, "but #{but_clause}"
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



47
48
49
# File 'lib/rspec/hal/matchers/templated_relation_matcher.rb', line 47

def failure_message_when_negated
  "Expected `#{link_rel}` link to be absent or not templated"
end

#matches?(jsonish) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rspec/hal/matchers/templated_relation_matcher.rb', line 18

def matches?(jsonish)
  repr = parse(jsonish)

  repr.raw_related_hrefs(link_rel){[]}
    .select{|it| it.respond_to? :expand}
    .select{|it| expected === it }
    .any?
    .tap do |outcome|
      @outcome = if outcome
                   :pass
                 elsif repr.raw_related_hrefs(link_rel){[]}.any?
                   :no_templates
                 else
                   :no_relations
                 end
    end
end