Class: AssertJson::Json

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

Overview

nodoc

Instance Method Summary collapse

Constructor Details

#initialize(json_string) ⇒ Json

Returns a new instance of Json.



36
37
38
39
40
# File 'lib/assert_json/assert_json.rb', line 36

def initialize(json_string)
  @decoded_json = ActiveSupport::JSON.decode(json_string)
  @expected_keys = []
  @only = false
end

Instance Method Details

#element(*args) ⇒ Object Also known as: has



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
# File 'lib/assert_json/assert_json.rb', line 64

def element(*args)
  arg = args.shift

  token = case @decoded_json
          when Array
            @decoded_json.shift
          else
            @decoded_json
          end

  case token
  when Hash
    arg = arg.to_s
    raise_error("element #{arg} not found") unless token.keys.include?(arg)
    unless args.empty?
      second_arg = args.shift
      gen_error = -> { raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") }
      case second_arg
      when Regexp
        gen_error.call if second_arg !~ token[arg].to_s
      when Symbol
        gen_error.call if second_arg.to_s != token[arg]
      else
        gen_error.call if second_arg != token[arg]
      end
    end
  when Array
    raise_error("element #{arg} not found") if !block_given? && token != arg
  when String
    case arg
    when Regexp
      raise_error("element #{arg.inspect} not found") if token !~ arg
    else
      raise_error("element #{arg.inspect} not found") if token != arg.to_s
    end
  when NilClass
    raise_error("no element left")
  else
    flunk
  end

  @expected_keys.push arg

  return unless block_given?
  begin
    only_in_scope = @only
    expected_keys_in_scope = @expected_keys
    @expected_keys = []
    decoded_json_in_scope = @decoded_json
    @decoded_json = case token
                    when Hash
                      token[arg]
                    else
                      token
                    end
    yield
    test_for_unexpected_keys(arg)
  ensure
    @expected_keys = expected_keys_in_scope
    @only = only_in_scope
    @decoded_json = decoded_json_in_scope
  end
end

#has_length_of(expected) ⇒ Object



42
43
44
45
46
# File 'lib/assert_json/assert_json.rb', line 42

def has_length_of(expected)
  raise_error("element #{@decoded_json.inspect} is not sizable") unless @decoded_json.respond_to?(:size)
  return if @decoded_json.size == expected
  raise_error("element #{@decoded_json.inspect} has #{@decoded_json.size} items, expected #{expected}")
end

#item(index) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/assert_json/assert_json.rb', line 48

def item(index)
  only_in_scope = @only
  expected_keys_in_scope = @expected_keys
  @expected_keys = []
  decoded_json_in_scope = @decoded_json
  @decoded_json = @decoded_json[index]
  begin
    yield if block_given?
    test_for_unexpected_keys(index)
  ensure
    @decoded_json = decoded_json_in_scope
    @expected_keys = expected_keys_in_scope
    @only = only_in_scope
  end
end

#not_element(*args) ⇒ Object Also known as: has_not



129
130
131
132
133
134
135
136
137
138
# File 'lib/assert_json/assert_json.rb', line 129

def not_element(*args)
  arg = args.shift
  token = @decoded_json
  case token
  when Array
    raise_error("element #{arg} found, but not expected") if token.include?(arg.to_s)
  else
    raise_error("element #{arg} found, but not expected") if token.keys.include?(arg.to_s)
  end
end

#onlyObject Also known as: has_only



141
142
143
# File 'lib/assert_json/assert_json.rb', line 141

def only
  @only = true
end

#test_for_unexpected_keys(name = 'root') ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/assert_json/assert_json.rb', line 146

def test_for_unexpected_keys(name = 'root')
  return unless @only
  return unless @decoded_json.is_a?(Hash)

  unexpected_keys = @decoded_json.keys - @expected_keys
  return if unexpected_keys.count <= 0
  raise_error("element #{name} has unexpected keys: #{unexpected_keys.join(', ')}")
end