Class: JsonSpec::Matchers::HaveJsonSize

Inherits:
Object
  • Object
show all
Includes:
Helpers, JsonSpec::Messages
Defined in:
lib/json_spec/matchers/have_json_size.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(size) ⇒ HaveJsonSize

Returns a new instance of HaveJsonSize.



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

def initialize(size)
  @expected = size
end

Instance Method Details

#at_path(path) ⇒ Object



18
19
20
21
# File 'lib/json_spec/matchers/have_json_size.rb', line 18

def at_path(path)
  @path = path
  self
end

#descriptionObject



33
34
35
# File 'lib/json_spec/matchers/have_json_size.rb', line 33

def description
  message_with_path(%(have JSON size "#{@expected}"))
end

#failure_messageObject Also known as: failure_message_for_should



23
24
25
# File 'lib/json_spec/matchers/have_json_size.rb', line 23

def failure_message
  message_with_path("Expected JSON value size to be #{@expected}, got #{@actual}")
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



28
29
30
# File 'lib/json_spec/matchers/have_json_size.rb', line 28

def failure_message_when_negated
  message_with_path("Expected JSON value size to not be #{@expected}, got #{@actual}")
end

#matches?(json) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



11
12
13
14
15
16
# File 'lib/json_spec/matchers/have_json_size.rb', line 11

def matches?(json)
  ruby = parse_json(json, @path)
  raise EnumerableExpected.new(ruby) unless Enumerable === ruby
  @actual = ruby.size
  @actual == @expected
end