Class: Jason::HaveJasonMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/jason/have_jason_matcher.rb

Overview

Provides the have_jason functionality

Instance Method Summary collapse

Constructor Details

#initialize(specs) ⇒ HaveJasonMatcher

Returns a new instance of HaveJasonMatcher.

Examples:

Specs by array

# will check if the actual JSON has a foo and a bar key
HaveJasonMatcher.new([ :foo, :bar ])

Specs by hash

# will check if the actual has an item object with a foo and a bar key
HaveJasonMatcher.new( item: [ :foo, :bar ])

# will check if the actual has an item object with a foo key with a
# bar value
HaveJasonMatcher.new( item: { foo: "bar" } )

Specs by hash with objects

# will check if the actual has a attribute key with the value of
# ruby_object.attribute()
HaveJasonMatcher.new( ruby_object => [ :attribute ] )

Specs by Jason.spec

# will check the actual against the provided Jason::Spec
HaveJasonMatcher.new( item: Jason.spec(type: Hash, fields: [ :foo, :bar ] ))

Parameters:

  • specs (Array, Hash)

    List of specifications



32
33
34
# File 'lib/jason/have_jason_matcher.rb', line 32

def initialize(specs)
  @specs = specs
end

Instance Method Details

#failure_messageString

Returns Message to provide if it should have matched but didn’t.

Returns:

  • (String)

    Message to provide if it should have matched but didn’t



149
150
151
# File 'lib/jason/have_jason_matcher.rb', line 149

def failure_message
  "Jason misses: #{@misses.pretty_inspect}\n\tin #{@actual}"
end

#match_recursively(specs, actual, root = "") ⇒ Hash

recursivly walk over the specs and the actual to find any misses

Parameters:

  • specs (Array, Hash)

    List of specifications

  • actual (Array, Hash)

    Provided data-structure (once JSON)

Returns:

  • (Hash)

    Hash with all the misses



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jason/have_jason_matcher.rb', line 57

def match_recursively(specs, actual, root="")
  actual ||= {}

  misses = {}

  if specs.is_a?(Array)
    specs.map(&:to_s).each do |key|
      res_key = root != "" ? "#{root}.#{key}" : "#{key}"
      if !actual.has_key? key
        misses[res_key] = :missing
      end
    end

    return misses
  end

  if specs.is_a?(Jason::Spec)
    root = "root" if root == ""

    if !actual
      misses[root] = { expected: { key: root }, got: :not_present }
    elsif !specs.fits?(actual)
      misses[root] = { expected: specs.opts, got: specs.misses }
    end

    return misses
  end

  specs.each do |key, value|
    res_key = root != "" ? "#{root}.#{key}" : "#{key}"
    key = key.to_s if key.is_a?(Symbol)

    if key.is_a?(String)
      if value.is_a?(Jason::Spec)
        if !actual[key]
          misses[res_key] = { expected: { key: key }, got: :not_present }
          next
        end

        if !value.fits?(actual[key])
          misses[res_key] = { expected: value.opts, got: value.misses }
        end

      elsif value.is_a?(Array) or value.is_a?(Hash)
        begin
          misses.merge!(match_recursively(value, actual[key], res_key))
        rescue => ex
          $stderr.puts ex.backtrace.join("\n\t")
          misses[res_key] = { error: ex.message }
        end

      elsif actual[key] != value
        misses[res_key] = { expected: value, got: actual[key] }
      end

    else # the key is an object, the value is an array of methods
      value.each do |attr|
        res_key = root != "" ? "#{root}.#{attr}" : "#{attr}"

        if attr.is_a?(Hash)
          misses.merge!(match_recursively(attr, actual[attr], res_key))
          next
        end

        attr = attr.to_s

        expected = key.send(attr) rescue nil
        found    = actual[attr]

        if found
          if expected.is_a?(Time)
            # revert to seconds - microseconds do not compare
            found = Time.at(Time.parse(found).to_i)
            expected = Time.at(expected.to_i)

          elsif expected.is_a?(Date)
            found = Date.parse(found)

          end
        end

        if found != expected
          misses[res_key] = { expected: expected, got: found }
        end
      end
    end
  end

  return misses
end

#matches?(actual) ⇒ Boolean

Check if the given JSON matches the specifications

Parameters:

  • actual (JSON, Hash)

    the value to check

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/jason/have_jason_matcher.rb', line 42

def matches?(actual)
  @actual = actual.is_a?(String) ? Rufus::Json.decode(actual) : actual

  @misses = match_recursively(@specs, @actual)

  @misses.empty?
end

#negative_failure_messageString

Returns Message to provide if it shouldn’t have matched but did.

Returns:

  • (String)

    Message to provide if it shouldn’t have matched but did



154
155
156
# File 'lib/jason/have_jason_matcher.rb', line 154

def negative_failure_message
  "Jason has: #{@actual}"
end