Class: AssertJson::Json
- Inherits:
-
Object
- Object
- AssertJson::Json
- Defined in:
- lib/assert_json/assert_json.rb
Instance Method Summary collapse
- #element(*args, &block) ⇒ Object (also: #has)
-
#initialize(json_string) ⇒ Json
constructor
A new instance of Json.
- #item(index, &block) ⇒ Object
- #not_element(*args, &block) ⇒ Object (also: #has_not)
- #only ⇒ Object (also: #has_only)
- #test_for_unexpected_keys(name = 'root') ⇒ Object
Constructor Details
#initialize(json_string) ⇒ Json
Returns a new instance of Json.
30 31 32 33 34 |
# File 'lib/assert_json/assert_json.rb', line 30 def initialize(json_string) @decoded_json = ActiveSupport::JSON.decode(json_string) @expected_keys = [] @only = false end |
Instance Method Details
#element(*args, &block) ⇒ Object Also known as: has
52 53 54 55 56 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 |
# File 'lib/assert_json/assert_json.rb', line 52 def element(*args, &block) 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 = lambda {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 if !block_given? && token != arg raise_error("element #{arg} not found") end 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 if 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 end |
#item(index, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/assert_json/assert_json.rb', line 36 def item(index, &block) 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, &block) ⇒ Object Also known as: has_not
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/assert_json/assert_json.rb', line 120 def not_element(*args, &block) 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 |
#only ⇒ Object Also known as: has_only
132 133 134 |
# File 'lib/assert_json/assert_json.rb', line 132 def only @only = true end |
#test_for_unexpected_keys(name = 'root') ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/assert_json/assert_json.rb', line 137 def test_for_unexpected_keys(name = 'root') return unless @only if @decoded_json.is_a?(Hash) unexpected_keys = @decoded_json.keys - @expected_keys if unexpected_keys.count > 0 raise_error("element #{name} has unexpected keys: #{unexpected_keys.join(', ')}") end end end |