assert_json 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +20 -0
- data/lib/assert_json/assert_json.rb +37 -1
- data/lib/assert_json/version.rb +1 -1
- data/test/assert_json_has_only_test.rb +94 -0
- metadata +15 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c2af8b07b97f74fe288a7a3aebe27d2686e62f3
|
4
|
+
data.tar.gz: 66167cec3df9aa3c630735e9c0c8cda0780d341b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c05feb7f9aeb5302113627b02123ba240438143e7f38b4ffa48b84db5ff2a083214b7d40fada36841819ec39c3cc16acf63bdb15b9a1fda11632ec280436a135
|
7
|
+
data.tar.gz: a88d567254d842c0778c789609cbc65a7b3f08e90bd64dcc357d44bbf9520e7491eb24f73d284578d122a4ad11204282713aee5f7b0811aa8454c219d90ca402
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.3.0
|
4
|
+
|
5
|
+
* add support for `has_only` (to test that objects have the declared set of properties and nothing more) (thanks to [@derek-watson](https://github.com/derek-watson), see [issue 8](https://github.com/alto/assert_json/pull/8))
|
6
|
+
|
3
7
|
## 0.2.3
|
4
8
|
|
5
9
|
* bugfix for itemised nested arrays (see [issue 7](https://github.com/alto/assert_json/issues/7))
|
data/README.md
CHANGED
@@ -58,6 +58,26 @@ assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "nam
|
|
58
58
|
end
|
59
59
|
```
|
60
60
|
|
61
|
+
To test that objects have the declared set of properties and nothing more,
|
62
|
+
include `has_only` at any level, like this
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
|
66
|
+
has_only
|
67
|
+
item 0 do
|
68
|
+
has :id, 1
|
69
|
+
has :key, 'test'
|
70
|
+
has :name, 'test'
|
71
|
+
end
|
72
|
+
item 1 do
|
73
|
+
has 'id', 2
|
74
|
+
has 'key', 'test'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Failure: element 1 has unexpected keys: name
|
79
|
+
```
|
80
|
+
|
61
81
|
|
62
82
|
## Changelog ##
|
63
83
|
|
@@ -5,6 +5,7 @@ module AssertJson
|
|
5
5
|
@json = AssertJson::Json.new(json_string)
|
6
6
|
# json.instance_exec(json, &block)
|
7
7
|
yield @json
|
8
|
+
@json.test_for_unexpected_keys('root')
|
8
9
|
end
|
9
10
|
end
|
10
11
|
|
@@ -20,19 +21,31 @@ module AssertJson
|
|
20
21
|
@json.has_not(*args, &block)
|
21
22
|
end
|
22
23
|
|
24
|
+
def has_only
|
25
|
+
@json.has_only
|
26
|
+
end
|
27
|
+
|
23
28
|
class Json
|
24
29
|
|
25
30
|
def initialize(json_string)
|
26
31
|
@decoded_json = ActiveSupport::JSON.decode(json_string)
|
32
|
+
@expected_keys = []
|
33
|
+
@only = false
|
27
34
|
end
|
28
35
|
|
29
36
|
def item(index, &block)
|
37
|
+
only_in_scope = @only
|
38
|
+
expected_keys_in_scope = @expected_keys
|
39
|
+
@expected_keys = []
|
30
40
|
decoded_json_in_scope = @decoded_json
|
31
41
|
@decoded_json = @decoded_json[index]
|
32
42
|
begin
|
33
43
|
yield if block_given?
|
44
|
+
test_for_unexpected_keys(index)
|
34
45
|
ensure
|
35
46
|
@decoded_json = decoded_json_in_scope
|
47
|
+
@expected_keys = expected_keys_in_scope
|
48
|
+
@only = only_in_scope
|
36
49
|
end
|
37
50
|
end
|
38
51
|
|
@@ -79,8 +92,13 @@ module AssertJson
|
|
79
92
|
flunk
|
80
93
|
end
|
81
94
|
|
95
|
+
@expected_keys.push arg
|
96
|
+
|
82
97
|
if block_given?
|
83
98
|
begin
|
99
|
+
only_in_scope = @only
|
100
|
+
expected_keys_in_scope = @expected_keys
|
101
|
+
@expected_keys = []
|
84
102
|
decoded_json_in_scope = @decoded_json
|
85
103
|
@decoded_json = case token
|
86
104
|
when Hash
|
@@ -89,11 +107,13 @@ module AssertJson
|
|
89
107
|
token
|
90
108
|
end
|
91
109
|
yield
|
110
|
+
test_for_unexpected_keys(arg)
|
92
111
|
ensure
|
112
|
+
@expected_keys = expected_keys_in_scope
|
113
|
+
@only = only_in_scope
|
93
114
|
@decoded_json = decoded_json_in_scope
|
94
115
|
end
|
95
116
|
end
|
96
|
-
|
97
117
|
end
|
98
118
|
alias has element
|
99
119
|
|
@@ -109,6 +129,22 @@ module AssertJson
|
|
109
129
|
end
|
110
130
|
alias has_not not_element
|
111
131
|
|
132
|
+
def only
|
133
|
+
@only = true
|
134
|
+
end
|
135
|
+
alias has_only only
|
136
|
+
|
137
|
+
def test_for_unexpected_keys(name = 'root')
|
138
|
+
return unless @only
|
139
|
+
|
140
|
+
if @decoded_json.is_a?(Hash)
|
141
|
+
unexpected_keys = @decoded_json.keys - @expected_keys
|
142
|
+
if unexpected_keys.count > 0
|
143
|
+
raise_error("element #{name} has unexpected keys: #{unexpected_keys.join(', ')}")
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
112
148
|
private
|
113
149
|
|
114
150
|
def raise_error(message)
|
data/lib/assert_json/version.rb
CHANGED
@@ -0,0 +1,94 @@
|
|
1
|
+
require_relative './test_helper'
|
2
|
+
|
3
|
+
class AssertJsonHasNoUnexpectedKeysTest < Minitest::Test
|
4
|
+
include AssertJson
|
5
|
+
|
6
|
+
def test_on_root_object
|
7
|
+
assert_json '{"keyA":"value","keyB":"value"}' do
|
8
|
+
has_only
|
9
|
+
has 'keyA'
|
10
|
+
has 'keyB'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_on_root_object_failure
|
15
|
+
err = assert_raises(MiniTest::Assertion) do
|
16
|
+
assert_json '{"keyA":"value","keyB":"value"}' do
|
17
|
+
has_only
|
18
|
+
has 'keyA', 'value'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
assert_equal 'element root has unexpected keys: keyB', err.message
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_on_root_object_with_sub_object
|
25
|
+
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
26
|
+
has_only
|
27
|
+
has 'keyA' do
|
28
|
+
has 'subKeyA'
|
29
|
+
has 'subKeyB'
|
30
|
+
end
|
31
|
+
has 'keyB'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_on_root_object_with_sub_object_failure
|
36
|
+
err = assert_raises(MiniTest::Assertion) do
|
37
|
+
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
38
|
+
has_only
|
39
|
+
has 'keyA' do
|
40
|
+
has 'subKeyA'
|
41
|
+
has 'subKeyB'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
assert_equal 'element root has unexpected keys: keyB', err.message
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_on_sub_object
|
49
|
+
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
50
|
+
has 'keyA' do
|
51
|
+
has_only
|
52
|
+
has 'subKeyA'
|
53
|
+
has 'subKeyB'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_on_sub_object_failure
|
59
|
+
err = assert_raises(MiniTest::Assertion) do
|
60
|
+
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
61
|
+
has 'keyA' do
|
62
|
+
has_only
|
63
|
+
has 'subKeyA'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
assert_equal 'element keyA has unexpected keys: subKeyB', err.message
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_on_root_array_of_objects
|
71
|
+
assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
|
72
|
+
has_only
|
73
|
+
item 0 do
|
74
|
+
has :id, 1
|
75
|
+
has :key, 'test'
|
76
|
+
has :name, 'test'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_on_root_array_of_objects_failure
|
82
|
+
err = assert_raises(MiniTest::Assertion) do
|
83
|
+
assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
|
84
|
+
has_only
|
85
|
+
item 0 do
|
86
|
+
has :id, 1
|
87
|
+
has :key, 'test'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
assert_equal 'element 0 has unexpected keys: name', err.message
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assert_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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:
|
11
|
+
date: 2015-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: A gem to test JSON strings.
|
@@ -58,8 +58,8 @@ executables: []
|
|
58
58
|
extensions: []
|
59
59
|
extra_rdoc_files: []
|
60
60
|
files:
|
61
|
-
- .gitignore
|
62
|
-
- .travis.yml
|
61
|
+
- ".gitignore"
|
62
|
+
- ".travis.yml"
|
63
63
|
- CHANGELOG.md
|
64
64
|
- Gemfile
|
65
65
|
- MIT-LICENSE
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- lib/assert_json.rb
|
70
70
|
- lib/assert_json/assert_json.rb
|
71
71
|
- lib/assert_json/version.rb
|
72
|
+
- test/assert_json_has_only_test.rb
|
72
73
|
- test/assert_json_new_dsl_test.rb
|
73
74
|
- test/assert_json_test.rb
|
74
75
|
- test/test_helper.rb
|
@@ -82,21 +83,22 @@ require_paths:
|
|
82
83
|
- lib
|
83
84
|
required_ruby_version: !ruby/object:Gem::Requirement
|
84
85
|
requirements:
|
85
|
-
- -
|
86
|
+
- - ">="
|
86
87
|
- !ruby/object:Gem::Version
|
87
88
|
version: '0'
|
88
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
90
|
requirements:
|
90
|
-
- -
|
91
|
+
- - ">="
|
91
92
|
- !ruby/object:Gem::Version
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
95
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.4.6
|
96
97
|
signing_key:
|
97
98
|
specification_version: 4
|
98
99
|
summary: A gem to test JSON strings.
|
99
100
|
test_files:
|
101
|
+
- test/assert_json_has_only_test.rb
|
100
102
|
- test/assert_json_new_dsl_test.rb
|
101
103
|
- test/assert_json_test.rb
|
102
104
|
- test/test_helper.rb
|