assert_json 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ Gemfile.lock
3
+
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm use --create 1.9.2@assert_json
2
+
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
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
data/README.md CHANGED
@@ -1,19 +1,25 @@
1
- AssertJson
2
- ==========
1
+ # AssertJson #
3
2
 
4
- A gem to test JSON strings.
3
+ Test your JSON strings.
5
4
 
6
- Installation
7
- ------------
5
+ ## Installation ##
8
6
 
9
- cd path/to/your/rails-project
10
- ./script/plugin install git://github.com/xing/assert_json.git
7
+ ```sh
8
+ cd path/to/your/rails-project
9
+ ./script/plugin install git://github.com/xing/assert_json.git
10
+ ```
11
+
12
+ Or if you prefer [bundler](http://getbundler.com)
13
+
14
+ ```sh
15
+ # in your Gemfile
16
+ gem 'assert_json'
17
+ ```
11
18
 
12
19
 
13
- Usage
14
- -----
20
+ ## Usage ##
15
21
 
16
- ``` ruby
22
+ ```ruby
17
23
  class MyActionTest < ActionController::TestCase
18
24
  include AssertJson
19
25
 
@@ -22,30 +28,27 @@ class MyActionTest < ActionController::TestCase
22
28
  # => @response.body= '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}'
23
29
 
24
30
  assert_json(@response.body) do |json|
25
- json.element 'key' do
26
- json.element 'inner_key1', 'value1'
27
- json.element 'inner_key2', 'value2'
31
+ has 'key' do
32
+ has 'inner_key1', 'value1'
33
+ has 'inner_key2', /lue2/
28
34
  end
29
- json.not_element 'key_not_included'
35
+ has_not 'key_not_included'
30
36
  end
31
37
  end
32
38
 
33
39
  end
34
40
  ```
35
41
 
36
- Authors
37
- -------
42
+ ## Authors ##
38
43
 
39
44
  [Thorsten Böttger](http://github.com/alto),
40
- [Andree Wille](http://github.com/dreewill),
41
45
  [Ralph von der Heyden](http://github.com/ralph)
42
46
 
43
47
  Please find out more about our work in our
44
48
  [Xing Dev Blog](http://devblog.xing.com/).
45
49
 
46
50
 
47
- License
48
- -------
51
+ ## License ##
49
52
 
50
53
  The MIT License
51
54
 
data/Rakefile CHANGED
@@ -1,13 +1,13 @@
1
+ require 'rubygems'
1
2
  require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- desc 'Default: run unit tests.'
6
- task :default => :test
3
+ require 'bundler/gem_tasks'
7
4
 
5
+ require 'rake/testtask'
8
6
  desc 'Test the assert_json plugin.'
9
7
  Rake::TestTask.new(:test) do |t|
10
8
  t.libs << 'lib'
11
9
  t.pattern = 'test/**/*_test.rb'
12
10
  t.verbose = true
13
11
  end
12
+ desc 'Default: run unit tests.'
13
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "assert_json/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "assert_json"
6
+ s.version = AssertJson::VERSION
7
+ s.summary = "A gem to test JSON strings."
8
+ s.description = "A gem to test JSON strings."
9
+ s.files = Dir["{app,lib,config}/**/*"] + ["MIT-LICENSE", "Rakefile", "Gemfile", "README.md"]
10
+
11
+ s.authors = ["Thorsten Böttger", "Ralph von der Heyden"]
12
+ s.email = %q{boettger@mt7.de}
13
+ s.homepage = %q{https://github.com/xing/assert_json}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activesupport'
21
+ s.add_development_dependency 'minitest'
22
+ end
@@ -0,0 +1,80 @@
1
+ module AssertJson
2
+
3
+ def assert_json(json_string, &block)
4
+ if block_given?
5
+ json = AssertJson::Json.new(json_string)
6
+ json.instance_exec(json, &block)
7
+ end
8
+ end
9
+
10
+ class Json
11
+
12
+ def initialize(json_string)
13
+ @decoded_json = ActiveSupport::JSON.decode(json_string)
14
+ end
15
+
16
+ def element(*args, &block)
17
+ arg = args.shift
18
+ token = @decoded_json.is_a?(Array) ? @decoded_json.shift : @decoded_json
19
+ case token
20
+ when Hash
21
+ raise_error("element #{arg} not found") unless token.keys.include?(arg)
22
+ if second_arg = args.shift
23
+ case second_arg
24
+ when Regexp
25
+ raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg !~ token[arg]
26
+ else
27
+ raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg != token[arg]
28
+ end
29
+ end
30
+ when Array
31
+ raise_error("element #{arg} not found") if token != arg
32
+ when String
33
+ case arg
34
+ when Regexp
35
+ raise_error("element #{arg.inspect} not found") if token !~ arg
36
+ else
37
+ raise_error("element #{arg.inspect} not found") if token != arg
38
+ end
39
+ when NilClass
40
+ raise_error("no element left")
41
+ else
42
+ flunk
43
+ end
44
+
45
+ if block_given?
46
+ begin
47
+ in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token
48
+ yield
49
+ ensure
50
+ @decoded_json = in_scope
51
+ end
52
+ end
53
+
54
+ end
55
+ alias has element
56
+
57
+ def not_element(*args, &block)
58
+ arg = args.shift
59
+ token = @decoded_json
60
+ case token
61
+ when Array
62
+ raise_error("element #{arg} found, but not expected") if token.include?(arg)
63
+ else
64
+ raise_error("element #{arg} found, but not expected") if token.keys.include?(arg)
65
+ end
66
+ end
67
+ alias has_not not_element
68
+
69
+ private
70
+
71
+ def raise_error(message)
72
+ if Object.const_defined?(:MiniTest)
73
+ raise MiniTest::Assertion.new(message)
74
+ else
75
+ raise Test::Unit::AssertionFailedError.new(message)
76
+ end
77
+ end
78
+
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module AssertJson
2
+ VERSION = "0.1.0"
3
+ end
data/lib/assert_json.rb CHANGED
@@ -1,65 +1,2 @@
1
- module AssertJson
2
-
3
- def assert_json(json_string, &block)
4
- if block_given?
5
- yield AssertJson::Json.new(json_string)
6
- end
7
- end
8
-
9
- class Json
10
-
11
- def initialize(json_string)
12
- @decoded_json = ActiveSupport::JSON.decode(json_string)
13
- end
14
-
15
- def element(*args, &block)
16
- arg = args.shift
17
- token = @decoded_json.is_a?(Array) ? @decoded_json.shift : @decoded_json
18
- case token
19
- when Hash
20
- raise_error("element #{arg} not found") unless token.keys.include?(arg)
21
- if second_arg = args.shift
22
- raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") if second_arg != token[arg]
23
- end
24
- when String, Array
25
- raise_error("element #{arg} not found") if token != arg
26
- when NilClass
27
- raise_error("no element left")
28
- else
29
- flunk
30
- end
31
-
32
- if block_given?
33
- begin
34
- in_scope, @decoded_json = @decoded_json, token.is_a?(Hash) ? token[arg] : token
35
- yield
36
- ensure
37
- @decoded_json = in_scope
38
- end
39
- end
40
-
41
- end
42
-
43
- def not_element(*args, &block)
44
- arg = args.shift
45
- token = @decoded_json
46
- case token
47
- when Array
48
- raise_error("element #{arg} found, but not expected") if token.include?(arg)
49
- else
50
- raise_error("element #{arg} found, but not expected") if token.keys.include?(arg)
51
- end
52
- end
53
-
54
- private
55
-
56
- def raise_error(message)
57
- if Object.const_defined?(:MiniTest)
58
- raise MiniTest::Assertion.new(message)
59
- else
60
- raise Test::Unit::AssertionFailedError.new(message)
61
- end
62
- end
63
-
64
- end
65
- end
1
+ require 'assert_json/version'
2
+ require 'assert_json/assert_json'
@@ -0,0 +1,268 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class AssertJsonNewDslTest < Test::Unit::TestCase
4
+ include AssertJson
5
+
6
+ def test_string
7
+ assert_json '"key"' do
8
+ has 'key'
9
+ end
10
+ end
11
+ def test_string_crosscheck
12
+ assert_raises(MiniTest::Assertion) do
13
+ assert_json '"key"' do
14
+ has 'wrong_key'
15
+ end
16
+ end
17
+ end
18
+ def test_regular_expression_for_strings
19
+ assert_json '"string"' do
20
+ has /tri/
21
+ end
22
+ end
23
+ def test_regular_expression_for_hash_values
24
+ assert_json '{"key":"value"}' do
25
+ has 'key', /alu/
26
+ end
27
+ end
28
+
29
+ def test_single_hash
30
+ assert_json '{"key":"value"}' do
31
+ has 'key', 'value'
32
+ end
33
+ end
34
+ def test_single_hash_crosscheck_for_key
35
+ assert_raises(MiniTest::Assertion) do
36
+ assert_json '{"key":"value"}' do
37
+ has 'wrong_key', 'value'
38
+ end
39
+ end
40
+ end
41
+ def test_single_hash_crosscheck_for_value
42
+ assert_raises(MiniTest::Assertion) do
43
+ assert_json '{"key":"value"}' do
44
+ has 'key', 'wrong_value'
45
+ end
46
+ end
47
+ end
48
+
49
+ def test_has_not
50
+ assert_json '{"key":"value"}' do
51
+ has 'key', 'value'
52
+ has_not 'key_not_included'
53
+ end
54
+ end
55
+ def test_has_not_crosscheck
56
+ assert_raises(MiniTest::Assertion) do
57
+ assert_json '{"key":"value"}' do
58
+ has_not 'key'
59
+ end
60
+ end
61
+ end
62
+
63
+ def test_array
64
+ assert_json '["value1","value2","value3"]' do
65
+ has 'value1'
66
+ has 'value2'
67
+ has 'value3'
68
+ end
69
+ end
70
+ def test_has_not_array
71
+ assert_json '["value1","value2"]' do
72
+ has 'value1'
73
+ has 'value2'
74
+ has_not 'value3'
75
+ end
76
+ end
77
+ def test_array_crosscheck_order
78
+ assert_raises(MiniTest::Assertion) do
79
+ assert_json '["value1","value2","value3"]' do
80
+ has 'value2'
81
+ end
82
+ end
83
+ end
84
+ def test_array_crosscheck_for_first_item
85
+ assert_raises(MiniTest::Assertion) do
86
+ assert_json '["value1","value2","value3"]' do
87
+ has 'wrong_value1'
88
+ end
89
+ end
90
+ end
91
+ def test_array_crosscheck_for_second_item
92
+ assert_raises(MiniTest::Assertion) do
93
+ assert_json '["value1","value2","value3"]' do
94
+ has 'value1'
95
+ has 'wrong_value2'
96
+ end
97
+ end
98
+ end
99
+
100
+ def test_nested_arrays
101
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do
102
+ has [["deep","another_depp"],["second_deep"]]
103
+ end
104
+ end
105
+ def test_nested_arrays_crosscheck
106
+ assert_raises(MiniTest::Assertion) do
107
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do
108
+ has [["deep","wrong_another_depp"],["second_deep"]]
109
+ end
110
+ end
111
+ assert_raises(MiniTest::Assertion) do
112
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do
113
+ has [["deep","another_depp"],["wrong_second_deep"]]
114
+ end
115
+ end
116
+ end
117
+
118
+ def test_hash_with_value_array
119
+ assert_json '{"key":["value1","value2"]}' do
120
+ has 'key', ['value1', 'value2']
121
+ end
122
+ end
123
+ def test_hash_with_value_array_crosscheck_wrong_key
124
+ assert_raises(MiniTest::Assertion) do
125
+ assert_json '{"key":["value1","value2"]}' do
126
+ has 'wrong_key', ['value1', 'value2']
127
+ end
128
+ end
129
+ end
130
+ def test_hash_with_value_array_crosscheck_wrong_value1
131
+ assert_raises(MiniTest::Assertion) do
132
+ assert_json '{"key":["value1","value2"]}' do
133
+ has 'key', ['wrong_value1', 'value2']
134
+ end
135
+ end
136
+ end
137
+ def test_hash_with_value_array_crosscheck_wrong_value2
138
+ assert_raises(MiniTest::Assertion) do
139
+ assert_json '{"key":["value1","value2"]}' do
140
+ has 'key', ['value1', 'wrong_value2']
141
+ end
142
+ end
143
+ end
144
+
145
+ def test_hash_with_array_of_hashes
146
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
147
+ has 'key' do
148
+ has 'inner_key1', 'value1'
149
+ has 'inner_key2', 'value2'
150
+ end
151
+ end
152
+ end
153
+ def test_hash_with_array_of_hashes_crosscheck_inner_key
154
+ assert_raises(MiniTest::Assertion) do
155
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
156
+ has 'key' do
157
+ has 'wrong_inner_key1', 'value1'
158
+ end
159
+ end
160
+ end
161
+ end
162
+ def test_hash_with_array_of_hashes_crosscheck_inner_value
163
+ assert_raises(MiniTest::Assertion) do
164
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
165
+ has 'key' do
166
+ has 'inner_key1', 'wrong_value1'
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ def test_array_with_two_hashes
173
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
174
+ has 'key1', 'value1'
175
+ has 'key2', 'value2'
176
+ end
177
+ end
178
+ def test_array_with_nested_hashes
179
+ assert_json '[{"key1":{"key2":"value2"}}]' do
180
+ has 'key1' do
181
+ has 'key2', 'value2'
182
+ end
183
+ end
184
+ end
185
+ def test_array_with_two_hashes_crosscheck
186
+ assert_raises(MiniTest::Assertion) do
187
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
188
+ has 'wrong_key1', 'value1'
189
+ has 'key2', 'value2'
190
+ end
191
+ end
192
+ assert_raises(MiniTest::Assertion) do
193
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
194
+ has 'key1', 'value1'
195
+ has 'key2', 'wrong_value2'
196
+ end
197
+ end
198
+ end
199
+
200
+ def test_nested_hashes
201
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
202
+ has 'outer_key' do
203
+ has 'key' do
204
+ has 'inner_key', 'value'
205
+ end
206
+ end
207
+ end
208
+ end
209
+ def test_nested_hashes_crosscheck
210
+ assert_raises(MiniTest::Assertion) do
211
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
212
+ has 'wrong_outer_key'
213
+ end
214
+ end
215
+ assert_raises(MiniTest::Assertion) do
216
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
217
+ has 'outer_key' do
218
+ has 'key' do
219
+ has 'inner_key', 'wrong_value'
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
225
+
226
+ def test_real_xws
227
+ assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do
228
+ has 'contact_request' do
229
+ has 'sender_id', '3'
230
+ has 'receiver_id', '2'
231
+ has 'id', 1
232
+ end
233
+ end
234
+
235
+ assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do
236
+ has 'private_message' do
237
+ has 'sender' do
238
+ has 'display_name', 'first last'
239
+ end
240
+ has 'receiver_id', '2'
241
+ has 'body', 'body'
242
+ end
243
+ end
244
+ end
245
+
246
+ def test_not_enough_hass_in_array
247
+ assert_raises(MiniTest::Assertion) do
248
+ assert_json '["one","two"]' do
249
+ has "one"
250
+ has "two"
251
+ has "three"
252
+ end
253
+ end
254
+ end
255
+
256
+ def test_not_enough_hass_in_hash_array
257
+ assert_raises(MiniTest::Assertion) do
258
+ assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do
259
+ has 'key' do
260
+ has 'key1', 'value1'
261
+ has 'key2', 'value2'
262
+ has 'key3'
263
+ end
264
+ end
265
+ end
266
+ end
267
+
268
+ end
@@ -0,0 +1,268 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ class AssertJsonTest < Test::Unit::TestCase
4
+ include AssertJson
5
+
6
+ def test_string
7
+ assert_json '"key"' do |json|
8
+ json.element 'key'
9
+ end
10
+ end
11
+ def test_string_crosscheck
12
+ assert_raises(MiniTest::Assertion) do
13
+ assert_json '"key"' do |json|
14
+ json.element 'wrong_key'
15
+ end
16
+ end
17
+ end
18
+ def test_regular_expression_for_strings
19
+ assert_json '"string"' do |json|
20
+ json.element /tri/
21
+ end
22
+ end
23
+ def test_regular_expression_for_hash_values
24
+ assert_json '{"key":"value"}' do |json|
25
+ json.element 'key', /alu/
26
+ end
27
+ end
28
+
29
+ def test_single_hash
30
+ assert_json '{"key":"value"}' do |json|
31
+ json.element 'key', 'value'
32
+ end
33
+ end
34
+ def test_single_hash_crosscheck_for_key
35
+ assert_raises(MiniTest::Assertion) do
36
+ assert_json '{"key":"value"}' do |json|
37
+ json.element 'wrong_key', 'value'
38
+ end
39
+ end
40
+ end
41
+ def test_single_hash_crosscheck_for_value
42
+ assert_raises(MiniTest::Assertion) do
43
+ assert_json '{"key":"value"}' do |json|
44
+ json.element 'key', 'wrong_value'
45
+ end
46
+ end
47
+ end
48
+
49
+ def test_not_element
50
+ assert_json '{"key":"value"}' do |json|
51
+ json.element 'key', 'value'
52
+ json.not_element 'key_not_included'
53
+ end
54
+ end
55
+ def test_not_element_crosscheck
56
+ assert_raises(MiniTest::Assertion) do
57
+ assert_json '{"key":"value"}' do |json|
58
+ json.not_element 'key'
59
+ end
60
+ end
61
+ end
62
+
63
+ def test_array
64
+ assert_json '["value1","value2","value3"]' do |json|
65
+ json.element 'value1'
66
+ json.element 'value2'
67
+ json.element 'value3'
68
+ end
69
+ end
70
+ def test_not_element_array
71
+ assert_json '["value1","value2"]' do |json|
72
+ json.element 'value1'
73
+ json.element 'value2'
74
+ json.not_element 'value3'
75
+ end
76
+ end
77
+ def test_array_crosscheck_order
78
+ assert_raises(MiniTest::Assertion) do
79
+ assert_json '["value1","value2","value3"]' do |json|
80
+ json.element 'value2'
81
+ end
82
+ end
83
+ end
84
+ def test_array_crosscheck_for_first_item
85
+ assert_raises(MiniTest::Assertion) do
86
+ assert_json '["value1","value2","value3"]' do |json|
87
+ json.element 'wrong_value1'
88
+ end
89
+ end
90
+ end
91
+ def test_array_crosscheck_for_second_item
92
+ assert_raises(MiniTest::Assertion) do
93
+ assert_json '["value1","value2","value3"]' do |json|
94
+ json.element 'value1'
95
+ json.element 'wrong_value2'
96
+ end
97
+ end
98
+ end
99
+
100
+ def test_nested_arrays
101
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
102
+ json.element [["deep","another_depp"],["second_deep"]]
103
+ end
104
+ end
105
+ def test_nested_arrays_crosscheck
106
+ assert_raises(MiniTest::Assertion) do
107
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
108
+ json.element [["deep","wrong_another_depp"],["second_deep"]]
109
+ end
110
+ end
111
+ assert_raises(MiniTest::Assertion) do
112
+ assert_json '[[["deep","another_depp"],["second_deep"]]]' do |json|
113
+ json.element [["deep","another_depp"],["wrong_second_deep"]]
114
+ end
115
+ end
116
+ end
117
+
118
+ def test_hash_with_value_array
119
+ assert_json '{"key":["value1","value2"]}' do |json|
120
+ json.element 'key', ['value1', 'value2']
121
+ end
122
+ end
123
+ def test_hash_with_value_array_crosscheck_wrong_key
124
+ assert_raises(MiniTest::Assertion) do
125
+ assert_json '{"key":["value1","value2"]}' do |json|
126
+ json.element 'wrong_key', ['value1', 'value2']
127
+ end
128
+ end
129
+ end
130
+ def test_hash_with_value_array_crosscheck_wrong_value1
131
+ assert_raises(MiniTest::Assertion) do
132
+ assert_json '{"key":["value1","value2"]}' do |json|
133
+ json.element 'key', ['wrong_value1', 'value2']
134
+ end
135
+ end
136
+ end
137
+ def test_hash_with_value_array_crosscheck_wrong_value2
138
+ assert_raises(MiniTest::Assertion) do
139
+ assert_json '{"key":["value1","value2"]}' do |json|
140
+ json.element 'key', ['value1', 'wrong_value2']
141
+ end
142
+ end
143
+ end
144
+
145
+ def test_hash_with_array_of_hashes
146
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
147
+ json.element 'key' do
148
+ json.element 'inner_key1', 'value1'
149
+ json.element 'inner_key2', 'value2'
150
+ end
151
+ end
152
+ end
153
+ def test_hash_with_array_of_hashes_crosscheck_inner_key
154
+ assert_raises(MiniTest::Assertion) do
155
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
156
+ json.element 'key' do
157
+ json.element 'wrong_inner_key1', 'value1'
158
+ end
159
+ end
160
+ end
161
+ end
162
+ def test_hash_with_array_of_hashes_crosscheck_inner_value
163
+ assert_raises(MiniTest::Assertion) do
164
+ assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do |json|
165
+ json.element 'key' do
166
+ json.element 'inner_key1', 'wrong_value1'
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ def test_array_with_two_hashes
173
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
174
+ json.element 'key1', 'value1'
175
+ json.element 'key2', 'value2'
176
+ end
177
+ end
178
+ def test_array_with_nested_hashes
179
+ assert_json '[{"key1":{"key2":"value2"}}]' do |json|
180
+ json.element 'key1' do
181
+ json.element 'key2', 'value2'
182
+ end
183
+ end
184
+ end
185
+ def test_array_with_two_hashes_crosscheck
186
+ assert_raises(MiniTest::Assertion) do
187
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
188
+ json.element 'wrong_key1', 'value1'
189
+ json.element 'key2', 'value2'
190
+ end
191
+ end
192
+ assert_raises(MiniTest::Assertion) do
193
+ assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do |json|
194
+ json.element 'key1', 'value1'
195
+ json.element 'key2', 'wrong_value2'
196
+ end
197
+ end
198
+ end
199
+
200
+ def test_nested_hashes
201
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
202
+ json.element 'outer_key' do
203
+ json.element 'key' do
204
+ json.element 'inner_key', 'value'
205
+ end
206
+ end
207
+ end
208
+ end
209
+ def test_nested_hashes_crosscheck
210
+ assert_raises(MiniTest::Assertion) do
211
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
212
+ json.element 'wrong_outer_key'
213
+ end
214
+ end
215
+ assert_raises(MiniTest::Assertion) do
216
+ assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do |json|
217
+ json.element 'outer_key' do
218
+ json.element 'key' do
219
+ json.element 'inner_key', 'wrong_value'
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
225
+
226
+ def test_real_xws
227
+ assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do |json|
228
+ json.element 'contact_request' do
229
+ json.element 'sender_id', '3'
230
+ json.element 'receiver_id', '2'
231
+ json.element 'id', 1
232
+ end
233
+ end
234
+
235
+ assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do |json|
236
+ json.element 'private_message' do
237
+ json.element 'sender' do
238
+ json.element 'display_name', 'first last'
239
+ end
240
+ json.element 'receiver_id', '2'
241
+ json.element 'body', 'body'
242
+ end
243
+ end
244
+ end
245
+
246
+ def test_not_enough_elements_in_array
247
+ assert_raises(MiniTest::Assertion) do
248
+ assert_json '["one","two"]' do |json|
249
+ json.element "one"
250
+ json.element "two"
251
+ json.element "three"
252
+ end
253
+ end
254
+ end
255
+
256
+ def test_not_enough_elements_in_hash_array
257
+ assert_raises(MiniTest::Assertion) do
258
+ assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do |json|
259
+ json.element 'key' do
260
+ json.element 'key1', 'value1'
261
+ json.element 'key2', 'value2'
262
+ json.element 'key3'
263
+ end
264
+ end
265
+ end
266
+ end
267
+
268
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+
5
+ $:.push File.expand_path("../../lib", __FILE__)
6
+ require File.dirname(__FILE__) + '/../lib/assert_json.rb'
metadata CHANGED
@@ -2,17 +2,16 @@
2
2
  name: assert_json
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Thorsten B\xC3\xB6ttger"
9
- - Andree Wille
10
9
  - Ralph von der Heyden
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
13
 
15
- date: 2011-05-05 00:00:00 +02:00
14
+ date: 2011-10-27 00:00:00 +02:00
16
15
  default_executable:
17
16
  dependencies:
18
17
  - !ruby/object:Gem::Dependency
@@ -46,11 +45,20 @@ extensions: []
46
45
  extra_rdoc_files: []
47
46
 
48
47
  files:
49
- - lib/assert_json.rb
50
- - MIT-LICENSE
51
- - Rakefile
48
+ - .gitignore
49
+ - .rvmrc
52
50
  - Gemfile
51
+ - Gemfile.lock
52
+ - MIT-LICENSE
53
53
  - README.md
54
+ - Rakefile
55
+ - assert_json.gemspec
56
+ - lib/assert_json.rb
57
+ - lib/assert_json/assert_json.rb
58
+ - lib/assert_json/version.rb
59
+ - test/assert_json_new_dsl_test.rb
60
+ - test/assert_json_test.rb
61
+ - test/test_helper.rb
54
62
  has_rdoc: true
55
63
  homepage: https://github.com/xing/assert_json
56
64
  licenses: []
@@ -79,5 +87,7 @@ rubygems_version: 1.6.2
79
87
  signing_key:
80
88
  specification_version: 3
81
89
  summary: A gem to test JSON strings.
82
- test_files: []
83
-
90
+ test_files:
91
+ - test/assert_json_new_dsl_test.rb
92
+ - test/assert_json_test.rb
93
+ - test/test_helper.rb