assert_json 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b01b0ad73c49c672ad096d20bc0c01989f47bd2f
4
- data.tar.gz: 89aefc5f150b7955103a3099cca8a82ade7fa4a7
3
+ metadata.gz: 84984fa4a3256eeeb0fd478682aaac73edf8ce6e
4
+ data.tar.gz: 735eb857f7c81ee55bfe0d24bc7283713768396c
5
5
  SHA512:
6
- metadata.gz: d25fdba2cfa9776c19bb5ddc62cdae83b1fd3d603f8440c331b56c9f7849ae261c833e30bfaaecad0a06d00bc8ee069c58402508978829de381146df84e3a8ba
7
- data.tar.gz: 39e7799b0c82bf725a396b187ef4f2596da1888d583dfeeb99e960d4dfb956e2b9c74aec6fc3546f23b4b30186fd881777533c4ec1c64e21b5052262e7ecd916
6
+ metadata.gz: 065d1b029be8512f5f6676e91b4596aa2e41f1ff220d8d35c9b2e45e11ed0ddfe3b61988c7afeebf8484ff3f12eabb9b579d895fdd75cda8063762810f178597
7
+ data.tar.gz: ee97759d3a65474fce2ce15f43cd3bf2b10025437136167c07d638a0a18b319e47095559825561089dc42a8e09a02f1ec0b1ff606d569593786414963d8bbf54
data/CHANGELOG.md CHANGED
@@ -1,11 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
- ## 0.3.0 (not yet released)
3
+ ## 0.2.3
4
4
 
5
- * allow Ruby symbols as keys and values (thanks to [@pgaertig](https://github.com/pgaertig), see [issue 4](https://github.com/alto/assert_json/issues/4))
5
+ * bugfix for itemised nested arrays (see [issue 7](https://github.com/alto/assert_json/issues/7))
6
6
 
7
7
  ## 0.2.2
8
8
 
9
+ * allow Ruby symbols as keys and values (thanks to [@pgaertig](https://github.com/pgaertig), see [issue 4](https://github.com/alto/assert_json/issues/4))
9
10
  * allowing to validate real numbers (thanks to [@plu](https://github.com/plu), see [issue 6](https://github.com/alto/assert_json/pull/6))
10
11
 
11
12
  ## 0.2.1
@@ -27,21 +27,25 @@ module AssertJson
27
27
  end
28
28
 
29
29
  def item(index, &block)
30
- @index = index
31
- yield if block_given?
30
+ decoded_json_in_scope = @decoded_json
31
+ @decoded_json = @decoded_json[index]
32
+ begin
33
+ yield if block_given?
34
+ ensure
35
+ @decoded_json = decoded_json_in_scope
36
+ end
32
37
  end
33
38
 
34
39
  def element(*args, &block)
35
40
  arg = args.shift
36
- token = if @decoded_json.is_a?(Array)
37
- if @index
38
- @decoded_json[@index]
39
- else
40
- @decoded_json.shift
41
- end
41
+
42
+ token = case @decoded_json
43
+ when Array
44
+ @decoded_json.shift
42
45
  else
43
46
  @decoded_json
44
47
  end
48
+
45
49
  case token
46
50
  when Hash
47
51
  arg = arg.to_s
@@ -59,7 +63,9 @@ module AssertJson
59
63
  end
60
64
  end
61
65
  when Array
62
- raise_error("element #{arg} not found") if token != arg
66
+ if !block_given? && token != arg
67
+ raise_error("element #{arg} not found")
68
+ end
63
69
  when String
64
70
  case arg
65
71
  when Regexp
@@ -75,10 +81,16 @@ module AssertJson
75
81
 
76
82
  if block_given?
77
83
  begin
78
- in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token
84
+ decoded_json_in_scope = @decoded_json
85
+ @decoded_json = case token
86
+ when Hash
87
+ token[arg]
88
+ else
89
+ token
90
+ end
79
91
  yield
80
92
  ensure
81
- @decoded_json = in_scope
93
+ @decoded_json = decoded_json_in_scope
82
94
  end
83
95
  end
84
96
 
@@ -1,3 +1,3 @@
1
1
  module AssertJson
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -121,6 +121,43 @@ class AssertJsonNewDslTest < Minitest::Test
121
121
  end
122
122
  end
123
123
 
124
+ def test_itemised_nested_array
125
+ json = '[["deep","another_depp"],["second_deep"]]'
126
+ assert_json json do
127
+ item 0 do
128
+ item 0 do
129
+ has 'deep'
130
+ end
131
+ item 1 do
132
+ has 'another_depp'
133
+ end
134
+ end
135
+ item 1 do
136
+ has 'second_deep'
137
+ end
138
+ end
139
+ end
140
+ def test_itemised_nested_array_crosscheck
141
+ json = '[["deep","another_depp"],["second_deep"]]'
142
+ assert_json json do
143
+ item 0 do
144
+ item 0 do
145
+ has 'deep'
146
+ end
147
+ item 1 do
148
+ assert_raises(MiniTest::Assertion) do
149
+ has 'wrong_item_value'
150
+ end
151
+ end
152
+ end
153
+ item 1 do
154
+ assert_raises(MiniTest::Assertion) do
155
+ has 'unknown_item_value'
156
+ end
157
+ end
158
+ end
159
+ end
160
+
124
161
  def test_hash_with_value_array
125
162
  assert_json '{"key":["value1","value2"]}' do
126
163
  has 'key', ['value1', 'value2']
@@ -412,4 +449,29 @@ class AssertJsonNewDslTest < Minitest::Test
412
449
  end
413
450
  end
414
451
 
452
+ # for issue #/
453
+ def test_complex_example
454
+ json = '{"count":2,"total_count":3,"results":[{"id":14,"tags":["tag1","tag2"],"created_at":"2014-10-14T00:50:39Z","updated_at":"2014-10-14T00:51:39Z"},{"id":15,"tags":["tag3","tag4"],"created_at":"2014-10-15T00:50:39Z","updated_at":"2014-10-15T00:51:39Z"}]}'
455
+
456
+ assert_json json do
457
+ has 'count', 2
458
+ has 'total_count', 3
459
+ has 'results' do
460
+ item 0 do
461
+ has 'id', 14
462
+ has 'tags' do
463
+ item 0 do
464
+ has 'tag1'
465
+ end
466
+ item 1 do
467
+ has 'tag2'
468
+ end
469
+ end
470
+ has 'created_at', '2014-10-14T00:50:39Z'
471
+ has 'updated_at', '2014-10-14T00:51:39Z'
472
+ end
473
+ end
474
+ end
475
+ end
476
+
415
477
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thorsten Böttger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-17 00:00:00.000000000 Z
11
+ date: 2014-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport