class JsonTest::Matchers::HaveJsonType

Public Class Methods

new(type) click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 7
def initialize(type)
  @classes = type_to_classes(type)
  @path = nil
end

Public Instance Methods

at_path(path) click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 17
def at_path(path)
  @path = path
  self
end
description() click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 32
def description
  message_with_path(%(have JSON type "#{@classes.join(", ")}"))
end
failure_message() click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 22
def failure_message
  message_with_path("Expected JSON value type to be #{@classes.join(", ")}, got #{@ruby.class}")
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 27
def failure_message_when_negated
  message_with_path("Expected JSON value type to not be #{@classes.join(", ")}, got #{@ruby.class}")
end
matches?(json) click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 12
def matches?(json)
  @ruby = parse_json(json, @path)
  @classes.any? { |c| c === @ruby }
end

Private Instance Methods

type_to_classes(type) click to toggle source
# File lib/jsontest/matchers/have_json_type.rb, line 37
def type_to_classes(type)
  case type
  when Class then [type]
  when Array then type.map { |t| type_to_classes(t) }.flatten
  else
    case type.to_s.downcase
    when "boolean"     then [TrueClass, FalseClass]
    when "object"      then [Hash]
    when "nil", "null" then [NilClass]
    else [Module.const_get(type.to_s.capitalize)]
    end
  end
end