Class: JsonSpec::Matchers::HaveJsonType

Inherits:
Object
  • Object
show all
Includes:
Helpers, JsonSpec::Messages
Defined in:
lib/json_spec/matchers/have_json_type.rb

Instance Method Summary collapse

Methods included from JsonSpec::Messages

#message_with_path

Methods included from Helpers

#generate_normalized_json, #load_json, #normalize_json, #parse_json

Constructor Details

#initialize(type) ⇒ HaveJsonType

Returns a new instance of HaveJsonType.



7
8
9
# File 'lib/json_spec/matchers/have_json_type.rb', line 7

def initialize(type)
  @classes = type_to_classes(type)
end

Instance Method Details

#at_path(path) ⇒ Object



16
17
18
19
# File 'lib/json_spec/matchers/have_json_type.rb', line 16

def at_path(path)
  @path = path
  self
end

#descriptionObject



31
32
33
# File 'lib/json_spec/matchers/have_json_type.rb', line 31

def description
  message_with_path(%(have JSON type "#{@classes.join(", ")}"))
end

#failure_messageObject Also known as: failure_message_for_should



21
22
23
# File 'lib/json_spec/matchers/have_json_type.rb', line 21

def failure_message
  message_with_path("Expected JSON value type to be #{@classes.join(", ")}, got #{@ruby.class}")
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



26
27
28
# File 'lib/json_spec/matchers/have_json_type.rb', line 26

def failure_message_when_negated
  message_with_path("Expected JSON value type to not be #{@classes.join(", ")}, got #{@ruby.class}")
end

#matches?(json) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/json_spec/matchers/have_json_type.rb', line 11

def matches?(json)
  @ruby = parse_json(json, @path)
  @classes.any? { |c| c === @ruby }
end