assert_json 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjFmZDllNzY1MjM4YTJkZjg3ZjNkZmVmMGFhYTBhYTNkZGNiYzUwZg==
5
+ data.tar.gz: !binary |-
6
+ NDRmZTM5YzYzMTEyNzYzODBiNjE2ZDEyN2M3MzE3MjEwMGRhM2I5NQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MGVlNDRmNmU2MTlmNWIwNGViYmJkNjY5YTE5MjNiZTc3MjFkYzBhZWUyZGRl
10
+ ZmM5YzQ1YjY1ZTZiNzJlNTZkNWQ3ZjMyZDQ0MjYxMjE0NDcxYmZmZjNlZjM1
11
+ Yzg4NTJmNzM3N2MyNzQzNmNjMTE3MjU5Mjk3NGVkMjFhNWMwMzg=
12
+ data.tar.gz: !binary |-
13
+ YTJhYTZkOTEzNTlmNjFlOThkZGI4NWFhN2JiNjQ4YWFkMjBkZjVlMmI1OTZh
14
+ NDhiMjNkNmQxYjIyMDc1MTEwODAwYjZiOWIyYzY5ZmRlMGJhMmE4YWQyMTFl
15
+ NzM4NGU3M2QyMDYxYzE2NGY3ZDYxYzVlZjRmNWUyOGE2NDQwMjM=
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
1
  pkg
2
2
  Gemfile.lock
3
3
 
4
+ .ruby-version
5
+ .ruby-gemset
6
+ .rvmrc
data/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.2.0
4
+
5
+ * support selecting (and skipping) single items of an array (see [issue 2](https://github.com/alto/assert_json/issues/2))
6
+
7
+ ## 0.1.1
8
+
9
+ * bugfix for a block problem
10
+
11
+ ## 0.1.0
12
+
13
+ * introduced new (cleaner and nicer) DSL
14
+ * supporting regular expressions for hash values and strings
15
+
16
+ ## 0.0.3
17
+
18
+ * supporting pure JSON arrays
19
+
20
+ ## 0.0.2
21
+
22
+ * added support for Ruby 1.9
23
+
24
+ ## 0.0.1
25
+
26
+ * initial version
data/MIT-LICENSE CHANGED
@@ -1,15 +1,15 @@
1
- Copyright (c) 2009-2011 XING AG (http://www.xing.com/)
2
-
1
+ Copyright (c) 2009-2014 Thorsten Böttger (http://mt7.de)
2
+
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
5
5
  in the Software without restriction, including without limitation the rights
6
6
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
7
  copies of the Software, and to permit persons to whom the Software is
8
8
  furnished to do so, subject to the following conditions:
9
-
9
+
10
10
  The above copyright notice and this permission notice shall be included in
11
11
  all copies or substantial portions of the Software.
12
-
12
+
13
13
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
14
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
15
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
data/README.md CHANGED
@@ -6,7 +6,7 @@ Test your JSON strings.
6
6
 
7
7
  ```sh
8
8
  cd path/to/your/rails-project
9
- ./script/plugin install git://github.com/xing/assert_json.git
9
+ ./script/plugin install git://github.com/alto/assert_json.git
10
10
  ```
11
11
 
12
12
  Or if you prefer [bundler](http://getbundler.com)
@@ -26,8 +26,8 @@ class MyActionTest < ActionController::TestCase
26
26
  def test_my_action
27
27
  get :my_action, :format => 'json'
28
28
  # => @response.body= '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}'
29
-
30
- assert_json(@response.body) do |json|
29
+
30
+ assert_json(@response.body) do
31
31
  has 'key' do
32
32
  has 'inner_key1', 'value1'
33
33
  has 'inner_key2', /lue2/
@@ -39,31 +39,48 @@ class MyActionTest < ActionController::TestCase
39
39
  end
40
40
  ```
41
41
 
42
- ## Authors ##
42
+ Arrays are little bit special. If you want to test single items (or skip others) you
43
+ use `item(i)` to select the item to test, like this
44
+
45
+ ```ruby
46
+ assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
47
+ item 0 do
48
+ has 'id', 1
49
+ has 'key', 'test'
50
+ has 'name', 'test'
51
+ end
52
+ item 2 do
53
+ has 'id', 3
54
+ end
55
+ end
56
+ ```
43
57
 
44
- [Thorsten Böttger](http://github.com/alto),
45
- [Ralph von der Heyden](http://github.com/ralph)
46
58
 
47
- Please find out more about our work in our
48
- [Xing Dev Blog](http://devblog.xing.com/).
59
+ ## Changelog ##
60
+
61
+ Look at the [CHANGELOG](https://github.com/alto/assert_json/blob/master/CHANGELOG.md) for details.
62
+
63
+ ## Authors ##
64
+
65
+ [Thorsten Böttger](http://github.com/alto)
49
66
 
50
67
 
51
68
  ## License ##
52
69
 
53
70
  The MIT License
54
-
55
- Copyright (c) 2009-2011 [XING AG](http://www.xing.com/)
56
-
71
+
72
+ Copyright (c) 2009-2014 [Thorsten Böttger](http://mt7.de/)
73
+
57
74
  Permission is hereby granted, free of charge, to any person obtaining a copy
58
75
  of this software and associated documentation files (the "Software"), to deal
59
76
  in the Software without restriction, including without limitation the rights
60
77
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61
78
  copies of the Software, and to permit persons to whom the Software is
62
79
  furnished to do so, subject to the following conditions:
63
-
80
+
64
81
  The above copyright notice and this permission notice shall be included in
65
82
  all copies or substantial portions of the Software.
66
-
83
+
67
84
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68
85
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69
86
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
data/Rakefile CHANGED
@@ -3,11 +3,13 @@ require 'rake'
3
3
  require 'bundler/gem_tasks'
4
4
 
5
5
  require 'rake/testtask'
6
+
6
7
  desc 'Test the assert_json plugin.'
7
8
  Rake::TestTask.new(:test) do |t|
8
9
  t.libs << 'lib'
9
10
  t.pattern = 'test/**/*_test.rb'
10
11
  t.verbose = true
11
12
  end
13
+
12
14
  desc 'Default: run unit tests.'
13
15
  task :default => :test
data/assert_json.gemspec CHANGED
@@ -1,3 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
1
2
  $:.push File.expand_path("../lib", __FILE__)
2
3
  require "assert_json/version"
3
4
 
@@ -6,11 +7,11 @@ Gem::Specification.new do |s|
6
7
  s.version = AssertJson::VERSION
7
8
  s.summary = "A gem to test JSON strings."
8
9
  s.description = "A gem to test JSON strings."
9
- s.files = Dir["{app,lib,config}/**/*"] + ["MIT-LICENSE", "Rakefile", "Gemfile", "README.md"]
10
10
 
11
- s.authors = ["Thorsten Böttger", "Ralph von der Heyden"]
11
+ s.authors = ["Thorsten Böttger"]
12
12
  s.email = %q{boettger@mt7.de}
13
- s.homepage = %q{https://github.com/xing/assert_json}
13
+ s.homepage = %q{https://github.com/alto/assert_json}
14
+ s.licenses = ["MIT"]
14
15
 
15
16
  s.files = `git ls-files`.split("\n")
16
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -18,5 +19,6 @@ Gem::Specification.new do |s|
18
19
  s.require_paths = ["lib"]
19
20
 
20
21
  s.add_dependency 'activesupport'
22
+
21
23
  s.add_development_dependency 'minitest'
22
24
  end
@@ -1,5 +1,5 @@
1
1
  module AssertJson
2
-
2
+
3
3
  def assert_json(json_string, &block)
4
4
  if block_given?
5
5
  @json = AssertJson::Json.new(json_string)
@@ -8,6 +8,10 @@ module AssertJson
8
8
  end
9
9
  end
10
10
 
11
+ def item(index, &block)
12
+ @json.item(index, &block)
13
+ end
14
+
11
15
  def has(*args, &block)
12
16
  @json.has(*args, &block)
13
17
  end
@@ -17,14 +21,27 @@ module AssertJson
17
21
  end
18
22
 
19
23
  class Json
20
-
24
+
21
25
  def initialize(json_string)
22
26
  @decoded_json = ActiveSupport::JSON.decode(json_string)
23
27
  end
24
-
28
+
29
+ def item(index, &block)
30
+ @index = index
31
+ yield if block_given?
32
+ end
33
+
25
34
  def element(*args, &block)
26
35
  arg = args.shift
27
- token = @decoded_json.is_a?(Array) ? @decoded_json.shift : @decoded_json
36
+ token = if @decoded_json.is_a?(Array)
37
+ if @index
38
+ @decoded_json[@index]
39
+ else
40
+ @decoded_json.shift
41
+ end
42
+ else
43
+ @decoded_json
44
+ end
28
45
  case token
29
46
  when Hash
30
47
  raise_error("element #{arg} not found") unless token.keys.include?(arg)
@@ -50,7 +67,7 @@ module AssertJson
50
67
  else
51
68
  flunk
52
69
  end
53
-
70
+
54
71
  if block_given?
55
72
  begin
56
73
  in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token
@@ -59,10 +76,10 @@ module AssertJson
59
76
  @decoded_json = in_scope
60
77
  end
61
78
  end
62
-
79
+
63
80
  end
64
81
  alias has element
65
-
82
+
66
83
  def not_element(*args, &block)
67
84
  arg = args.shift
68
85
  token = @decoded_json
@@ -74,9 +91,9 @@ module AssertJson
74
91
  end
75
92
  end
76
93
  alias has_not not_element
77
-
94
+
78
95
  private
79
-
96
+
80
97
  def raise_error(message)
81
98
  if Object.const_defined?(:MiniTest)
82
99
  raise MiniTest::Assertion.new(message)
@@ -84,6 +101,6 @@ module AssertJson
84
101
  raise Test::Unit::AssertionFailedError.new(message)
85
102
  end
86
103
  end
87
-
104
+
88
105
  end
89
106
  end
@@ -1,3 +1,3 @@
1
1
  module AssertJson
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -25,7 +25,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
25
25
  has 'key', /alu/
26
26
  end
27
27
  end
28
-
28
+
29
29
  def test_single_hash
30
30
  assert_json '{"key":"value"}' do
31
31
  has 'key', 'value'
@@ -51,7 +51,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
51
51
  end
52
52
  end
53
53
  end
54
-
54
+
55
55
  def test_has_not
56
56
  assert_json '{"key":"value"}' do
57
57
  has 'key', 'value'
@@ -65,7 +65,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
65
65
  end
66
66
  end
67
67
  end
68
-
68
+
69
69
  def test_array
70
70
  assert_json '["value1","value2","value3"]' do
71
71
  has 'value1'
@@ -102,7 +102,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
102
102
  end
103
103
  end
104
104
  end
105
-
105
+
106
106
  def test_nested_arrays
107
107
  assert_json '[[["deep","another_depp"],["second_deep"]]]' do
108
108
  has [["deep","another_depp"],["second_deep"]]
@@ -120,7 +120,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
120
120
  end
121
121
  end
122
122
  end
123
-
123
+
124
124
  def test_hash_with_value_array
125
125
  assert_json '{"key":["value1","value2"]}' do
126
126
  has 'key', ['value1', 'value2']
@@ -147,7 +147,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
147
147
  end
148
148
  end
149
149
  end
150
-
150
+
151
151
  def test_hash_with_array_of_hashes
152
152
  assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
153
153
  has 'key' do
@@ -174,7 +174,36 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
174
174
  end
175
175
  end
176
176
  end
177
-
177
+
178
+
179
+ def test_array_with_multi_item_hashes
180
+ assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
181
+ item 0 do
182
+ has 'id', 1
183
+ has 'key', 'test'
184
+ has 'name', 'test'
185
+ end
186
+ item 2 do
187
+ has 'id', 3
188
+ end
189
+ end
190
+ end
191
+ def test_array_with_multi_item_hashes_crosscheck
192
+ assert_raises(MiniTest::Assertion) do
193
+ assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
194
+ item 0 do
195
+ has 'id', 1
196
+ has 'key', 'test'
197
+ has 'name', 'test'
198
+ end
199
+ item 2 do
200
+ has 'id', 2
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+
178
207
  def test_array_with_two_hashes
179
208
  assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
180
209
  has 'key1', 'value1'
@@ -202,7 +231,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
202
231
  end
203
232
  end
204
233
  end
205
-
234
+
206
235
  def test_nested_hashes
207
236
  assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
208
237
  has 'outer_key' do
@@ -228,7 +257,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
228
257
  end
229
258
  end
230
259
  end
231
-
260
+
232
261
  def test_real_xws
233
262
  assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do
234
263
  has 'contact_request' do
@@ -237,7 +266,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
237
266
  has 'id', 1
238
267
  end
239
268
  end
240
-
269
+
241
270
  assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do
242
271
  has 'private_message' do
243
272
  has 'sender' do
@@ -248,7 +277,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
248
277
  end
249
278
  end
250
279
  end
251
-
280
+
252
281
  def test_not_enough_hass_in_array
253
282
  assert_raises(MiniTest::Assertion) do
254
283
  assert_json '["one","two"]' do
@@ -258,7 +287,7 @@ class AssertJsonNewDslTest < Test::Unit::TestCase
258
287
  end
259
288
  end
260
289
  end
261
-
290
+
262
291
  def test_not_enough_hass_in_hash_array
263
292
  assert_raises(MiniTest::Assertion) do
264
293
  assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do
@@ -25,7 +25,7 @@ class AssertJsonTest < Test::Unit::TestCase
25
25
  json.element 'key', /alu/
26
26
  end
27
27
  end
28
-
28
+
29
29
  def test_single_hash
30
30
  assert_json '{"key":"value"}' do |json|
31
31
  json.element 'key', 'value'
@@ -45,7 +45,7 @@ class AssertJsonTest < Test::Unit::TestCase
45
45
  end
46
46
  end
47
47
  end
48
-
48
+
49
49
  def test_not_element
50
50
  assert_json '{"key":"value"}' do |json|
51
51
  json.element 'key', 'value'
@@ -59,7 +59,7 @@ class AssertJsonTest < Test::Unit::TestCase
59
59
  end
60
60
  end
61
61
  end
62
-
62
+
63
63
  def test_array
64
64
  assert_json '["value1","value2","value3"]' do |json|
65
65
  json.element 'value1'
@@ -96,7 +96,7 @@ class AssertJsonTest < Test::Unit::TestCase
96
96
  end
97
97
  end
98
98
  end
99
-
99
+
100
100
  def test_nested_arrays
101
101
  assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
102
102
  json.element [["deep","another_depp"],["second_deep"]]
@@ -114,7 +114,7 @@ class AssertJsonTest < Test::Unit::TestCase
114
114
  end
115
115
  end
116
116
  end
117
-
117
+
118
118
  def test_hash_with_value_array
119
119
  assert_json '{"key":["value1","value2"]}' do |json|
120
120
  json.element 'key', ['value1', 'value2']
@@ -168,7 +168,7 @@ class AssertJsonTest < Test::Unit::TestCase
168
168
  end
169
169
  end
170
170
  end
171
-
171
+
172
172
  def test_array_with_two_hashes
173
173
  assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
174
174
  json.element 'key1', 'value1'
@@ -196,7 +196,7 @@ class AssertJsonTest < Test::Unit::TestCase
196
196
  end
197
197
  end
198
198
  end
199
-
199
+
200
200
  def test_nested_hashes
201
201
  assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
202
202
  json.element 'outer_key' do
metadata CHANGED
@@ -1,54 +1,52 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: assert_json
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
6
5
  platform: ruby
7
- authors:
8
- - "Thorsten B\xC3\xB6ttger"
9
- - Ralph von der Heyden
6
+ authors:
7
+ - Thorsten Böttger
10
8
  autorequire:
11
9
  bindir: bin
12
10
  cert_chain: []
13
-
14
- date: 2011-10-27 00:00:00 +02:00
15
- default_executable:
16
- dependencies:
17
- - !ruby/object:Gem::Dependency
11
+ date: 2014-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
18
14
  name: activesupport
19
- prerelease: false
20
- requirement: &id001 !ruby/object:Gem::Requirement
21
- none: false
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
26
20
  type: :runtime
27
- version_requirements: *id001
28
- - !ruby/object:Gem::Dependency
29
- name: minitest
30
21
  prerelease: false
31
- requirement: &id002 !ruby/object:Gem::Requirement
32
- none: false
33
- requirements:
34
- - - ">="
35
- - !ruby/object:Gem::Version
36
- version: "0"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
37
34
  type: :development
38
- version_requirements: *id002
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
39
41
  description: A gem to test JSON strings.
40
42
  email: boettger@mt7.de
41
43
  executables: []
42
-
43
44
  extensions: []
44
-
45
45
  extra_rdoc_files: []
46
-
47
- files:
46
+ files:
48
47
  - .gitignore
49
- - .rvmrc
48
+ - CHANGELOG.md
50
49
  - Gemfile
51
- - Gemfile.lock
52
50
  - MIT-LICENSE
53
51
  - README.md
54
52
  - Rakefile
@@ -59,35 +57,31 @@ files:
59
57
  - test/assert_json_new_dsl_test.rb
60
58
  - test/assert_json_test.rb
61
59
  - test/test_helper.rb
62
- has_rdoc: true
63
- homepage: https://github.com/xing/assert_json
64
- licenses: []
65
-
60
+ homepage: https://github.com/alto/assert_json
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
66
64
  post_install_message:
67
65
  rdoc_options: []
68
-
69
- require_paths:
66
+ require_paths:
70
67
  - lib
71
- required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: "0"
77
- required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: "0"
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
83
78
  requirements: []
84
-
85
79
  rubyforge_project:
86
- rubygems_version: 1.6.2
80
+ rubygems_version: 2.1.11
87
81
  signing_key:
88
- specification_version: 3
82
+ specification_version: 4
89
83
  summary: A gem to test JSON strings.
90
- test_files:
84
+ test_files:
91
85
  - test/assert_json_new_dsl_test.rb
92
86
  - test/assert_json_test.rb
93
87
  - test/test_helper.rb
data/.rvmrc DELETED
@@ -1,2 +0,0 @@
1
- rvm use --create 1.9.2@assert_json
2
-
data/Gemfile.lock DELETED
@@ -1,18 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- assert_json (0.0.3)
5
- activesupport
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- activesupport (3.0.7)
11
- minitest (2.1.0)
12
-
13
- PLATFORMS
14
- ruby
15
-
16
- DEPENDENCIES
17
- assert_json!
18
- minitest