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)
Constructor Details
#initialize(json_string) ⇒ Json
Returns a new instance of Json.
25 26 27 |
# File 'lib/assert_json/assert_json.rb', line 25 def initialize(json_string) @decoded_json = ActiveSupport::JSON.decode(json_string) end |
Instance Method Details
#element(*args, &block) ⇒ Object Also known as: has
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 |
# File 'lib/assert_json/assert_json.rb', line 34 def element(*args, &block) arg = args.shift token = if @decoded_json.is_a?(Array) if @index @decoded_json[@index] else @decoded_json.shift end else @decoded_json end case token when Hash raise_error("element #{arg} not found") unless token.keys.include?(arg) unless args.empty? second_arg = args.shift case second_arg when Regexp raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg !~ token[arg] else raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg != token[arg] end end when Array raise_error("element #{arg} not found") if 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 end when NilClass raise_error("no element left") else flunk end if block_given? begin in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token yield ensure @decoded_json = in_scope end end end |
#item(index, &block) ⇒ Object
29 30 31 32 |
# File 'lib/assert_json/assert_json.rb', line 29 def item(index, &block) @index = index yield if block_given? end |
#not_element(*args, &block) ⇒ Object Also known as: has_not
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/assert_json/assert_json.rb', line 84 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) else raise_error("element #{arg} found, but not expected") if token.keys.include?(arg) end end |