Class: JsonSpec::Matchers::BeJsonEql

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JsonSpec::Messages

#message_with_path

Methods included from Exclusion

#exclude_key?, #exclude_keys, #excluded_keys

Methods included from Helpers

#generate_normalized_json, #load_json, #normalize_json, #parse_json

Constructor Details

#initialize(expected_json = nil) ⇒ BeJsonEql

Returns a new instance of BeJsonEql.



14
15
16
# File 'lib/json_spec/matchers/be_json_eql.rb', line 14

def initialize(expected_json = nil)
  @expected_json = expected_json
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



8
9
10
# File 'lib/json_spec/matchers/be_json_eql.rb', line 8

def actual
  @actual
end

#expectedObject (readonly)

Returns the value of attribute expected.



8
9
10
# File 'lib/json_spec/matchers/be_json_eql.rb', line 8

def expected
  @expected
end

Instance Method Details

#at_path(path) ⇒ Object



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

def at_path(path)
  @path = path
  self
end

#descriptionObject



55
56
57
# File 'lib/json_spec/matchers/be_json_eql.rb', line 55

def description
  message_with_path("equal JSON")
end

#diffable?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/json_spec/matchers/be_json_eql.rb', line 10

def diffable?
  true
end

#excluding(*keys) ⇒ Object



35
36
37
38
# File 'lib/json_spec/matchers/be_json_eql.rb', line 35

def excluding(*keys)
  excluded_keys.merge(keys.map(&:to_s))
  self
end

#failure_messageObject Also known as: failure_message_for_should



45
46
47
# File 'lib/json_spec/matchers/be_json_eql.rb', line 45

def failure_message
  message_with_path("Expected equivalent JSON")
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



50
51
52
# File 'lib/json_spec/matchers/be_json_eql.rb', line 50

def failure_message_when_negated
  message_with_path("Expected inequivalent JSON")
end

#including(*keys) ⇒ Object



40
41
42
43
# File 'lib/json_spec/matchers/be_json_eql.rb', line 40

def including(*keys)
  excluded_keys.subtract(keys.map(&:to_s))
  self
end

#matches?(actual_json) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/json_spec/matchers/be_json_eql.rb', line 18

def matches?(actual_json)
  raise "Expected equivalent JSON not provided" if @expected_json.nil?

  @actual, @expected = scrub(actual_json, @path), scrub(@expected_json)
  @actual == @expected
end

#to_file(path) ⇒ Object



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

def to_file(path)
  @expected_json = load_json(path)
  self
end