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
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/assert_json/assert_json.rb', line 39 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 if block_given? begin decoded_json_in_scope = @decoded_json @decoded_json = case token when Hash token[arg] else token end yield ensure @decoded_json = decoded_json_in_scope end end end |
#item(index, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/assert_json/assert_json.rb', line 29 def item(index, &block) decoded_json_in_scope = @decoded_json @decoded_json = @decoded_json[index] begin yield if block_given? ensure @decoded_json = decoded_json_in_scope end end |
#not_element(*args, &block) ⇒ Object Also known as: has_not
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/assert_json/assert_json.rb', line 100 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 |