assert_json 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +860 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/HOWTO.md +3 -0
- data/README.md +8 -0
- data/Rakefile +1 -1
- data/assert_json.gemspec +6 -5
- data/lib/assert_json/assert_json.rb +58 -52
- data/lib/assert_json/version.rb +2 -1
- data/test/assert_json_has_only_test.rb +85 -49
- data/test/assert_json_new_dsl_test.rb +422 -321
- data/test/assert_json_test.rb +200 -177
- data/test/test_helper.rb +3 -2
- metadata +21 -4
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 0.4.0
|
4
|
+
|
5
|
+
* add support for `size` (to test the size of arrays and hashes) (thanks to [@thomasjachmann](https://github.com/thomasjachmann), see [pull request 9](https://github.com/alto/assert_json/pull/9))
|
6
|
+
|
3
7
|
## 0.3.0
|
4
8
|
|
5
9
|
* 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))
|
data/Gemfile
CHANGED
data/HOWTO.md
ADDED
data/README.md
CHANGED
@@ -58,6 +58,14 @@ assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "nam
|
|
58
58
|
end
|
59
59
|
```
|
60
60
|
|
61
|
+
You can also check the size of arrays like this
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
assert_json '["value1", "value2"]' do
|
65
|
+
size 2
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
61
69
|
To test that objects have the declared set of properties and nothing more,
|
62
70
|
include `has_only` at any level, like this
|
63
71
|
|
data/Rakefile
CHANGED
data/assert_json.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
$LOAD_PATH.push File.expand_path("../lib", __FILE__)
|
3
3
|
require "assert_json/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
@@ -9,17 +9,18 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.description = "A gem to test JSON strings."
|
10
10
|
|
11
11
|
s.authors = ["Thorsten Böttger"]
|
12
|
-
s.email = %
|
13
|
-
s.homepage =
|
12
|
+
s.email = %w(boettger@mt7.de)
|
13
|
+
s.homepage = 'https://github.com/alto/assert_json'
|
14
14
|
s.licenses = ["MIT"]
|
15
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
21
|
s.add_dependency 'activesupport'
|
22
22
|
|
23
23
|
s.add_development_dependency 'rake'
|
24
|
-
s.add_development_dependency '
|
24
|
+
s.add_development_dependency 'maxitest'
|
25
|
+
s.add_development_dependency 'shoulda-context'
|
25
26
|
end
|
@@ -1,12 +1,16 @@
|
|
1
|
+
# nodoc
|
1
2
|
module AssertJson
|
3
|
+
def assert_json(json_string)
|
4
|
+
return unless block_given?
|
2
5
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
@json = AssertJson::Json.new(json_string)
|
7
|
+
# json.instance_exec(json, &block)
|
8
|
+
yield @json
|
9
|
+
@json.test_for_unexpected_keys('root')
|
10
|
+
end
|
11
|
+
|
12
|
+
def size(expected)
|
13
|
+
@json.size(expected)
|
10
14
|
end
|
11
15
|
|
12
16
|
def item(index, &block)
|
@@ -17,23 +21,31 @@ module AssertJson
|
|
17
21
|
@json.has(*args, &block)
|
18
22
|
end
|
19
23
|
|
20
|
-
def
|
24
|
+
def not?(*args, &block)
|
21
25
|
@json.has_not(*args, &block)
|
22
26
|
end
|
27
|
+
alias_method :has_not, :not?
|
23
28
|
|
24
|
-
def
|
29
|
+
def only?
|
25
30
|
@json.has_only
|
26
31
|
end
|
32
|
+
alias_method :has_only, :only?
|
27
33
|
|
34
|
+
# nodoc
|
28
35
|
class Json
|
29
|
-
|
30
36
|
def initialize(json_string)
|
31
37
|
@decoded_json = ActiveSupport::JSON.decode(json_string)
|
32
38
|
@expected_keys = []
|
33
39
|
@only = false
|
34
40
|
end
|
35
41
|
|
36
|
-
def
|
42
|
+
def size(expected)
|
43
|
+
raise_error("element #{@decoded_json.inspect} is not sizable") unless @decoded_json.respond_to?(:size)
|
44
|
+
return if @decoded_json.size == expected
|
45
|
+
raise_error("element #{@decoded_json.inspect} has #{@decoded_json.size} items, expected #{expected}")
|
46
|
+
end
|
47
|
+
|
48
|
+
def item(index)
|
37
49
|
only_in_scope = @only
|
38
50
|
expected_keys_in_scope = @expected_keys
|
39
51
|
@expected_keys = []
|
@@ -49,7 +61,7 @@ module AssertJson
|
|
49
61
|
end
|
50
62
|
end
|
51
63
|
|
52
|
-
def element(*args
|
64
|
+
def element(*args)
|
53
65
|
arg = args.shift
|
54
66
|
|
55
67
|
token = case @decoded_json
|
@@ -65,7 +77,7 @@ module AssertJson
|
|
65
77
|
raise_error("element #{arg} not found") unless token.keys.include?(arg)
|
66
78
|
unless args.empty?
|
67
79
|
second_arg = args.shift
|
68
|
-
gen_error =
|
80
|
+
gen_error = -> { raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}") }
|
69
81
|
case second_arg
|
70
82
|
when Regexp
|
71
83
|
gen_error.call if second_arg !~ token[arg].to_s
|
@@ -76,9 +88,7 @@ module AssertJson
|
|
76
88
|
end
|
77
89
|
end
|
78
90
|
when Array
|
79
|
-
if !block_given? && token != arg
|
80
|
-
raise_error("element #{arg} not found")
|
81
|
-
end
|
91
|
+
raise_error("element #{arg} not found") if !block_given? && token != arg
|
82
92
|
when String
|
83
93
|
case arg
|
84
94
|
when Regexp
|
@@ -94,30 +104,29 @@ module AssertJson
|
|
94
104
|
|
95
105
|
@expected_keys.push arg
|
96
106
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
end
|
107
|
+
return unless block_given?
|
108
|
+
begin
|
109
|
+
only_in_scope = @only
|
110
|
+
expected_keys_in_scope = @expected_keys
|
111
|
+
@expected_keys = []
|
112
|
+
decoded_json_in_scope = @decoded_json
|
113
|
+
@decoded_json = case token
|
114
|
+
when Hash
|
115
|
+
token[arg]
|
116
|
+
else
|
117
|
+
token
|
118
|
+
end
|
119
|
+
yield
|
120
|
+
test_for_unexpected_keys(arg)
|
121
|
+
ensure
|
122
|
+
@expected_keys = expected_keys_in_scope
|
123
|
+
@only = only_in_scope
|
124
|
+
@decoded_json = decoded_json_in_scope
|
116
125
|
end
|
117
126
|
end
|
118
|
-
|
127
|
+
alias_method :has, :element
|
119
128
|
|
120
|
-
def not_element(*args
|
129
|
+
def not_element(*args)
|
121
130
|
arg = args.shift
|
122
131
|
token = @decoded_json
|
123
132
|
case token
|
@@ -127,33 +136,30 @@ module AssertJson
|
|
127
136
|
raise_error("element #{arg} found, but not expected") if token.keys.include?(arg.to_s)
|
128
137
|
end
|
129
138
|
end
|
130
|
-
|
139
|
+
alias_method :has_not, :not_element
|
131
140
|
|
132
141
|
def only
|
133
142
|
@only = true
|
134
143
|
end
|
135
|
-
|
144
|
+
alias_method :has_only, :only
|
136
145
|
|
137
146
|
def test_for_unexpected_keys(name = 'root')
|
138
147
|
return unless @only
|
148
|
+
return unless @decoded_json.is_a?(Hash)
|
139
149
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
raise_error("element #{name} has unexpected keys: #{unexpected_keys.join(', ')}")
|
144
|
-
end
|
145
|
-
end
|
150
|
+
unexpected_keys = @decoded_json.keys - @expected_keys
|
151
|
+
return if unexpected_keys.count <= 0
|
152
|
+
raise_error("element #{name} has unexpected keys: #{unexpected_keys.join(', ')}")
|
146
153
|
end
|
147
154
|
|
148
155
|
private
|
149
156
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
157
|
+
def raise_error(message)
|
158
|
+
if Object.const_defined?(:MiniTest)
|
159
|
+
fail MiniTest::Assertion, message
|
160
|
+
else
|
161
|
+
fail Test::Unit::AssertionFailedError, message
|
156
162
|
end
|
157
|
-
|
163
|
+
end
|
158
164
|
end
|
159
165
|
end
|
data/lib/assert_json/version.rb
CHANGED
@@ -1,94 +1,130 @@
|
|
1
1
|
require_relative './test_helper'
|
2
2
|
|
3
|
+
# nodoc
|
3
4
|
class AssertJsonHasNoUnexpectedKeysTest < Minitest::Test
|
4
5
|
include AssertJson
|
5
6
|
|
6
|
-
|
7
|
-
|
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
|
7
|
+
context "has_only" do
|
8
|
+
should "test_on_root_object" do
|
16
9
|
assert_json '{"keyA":"value","keyB":"value"}' do
|
17
10
|
has_only
|
18
|
-
has 'keyA'
|
11
|
+
has 'keyA'
|
12
|
+
has 'keyB'
|
19
13
|
end
|
20
14
|
end
|
21
|
-
assert_equal 'element root has unexpected keys: keyB', err.message
|
22
|
-
end
|
23
15
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
16
|
+
should "test_on_root_object_failure" do
|
17
|
+
err = assert_raises(MiniTest::Assertion) do
|
18
|
+
assert_json '{"keyA":"value","keyB":"value"}' do
|
19
|
+
has_only
|
20
|
+
has 'keyA', 'value'
|
21
|
+
end
|
30
22
|
end
|
31
|
-
has
|
23
|
+
assert_equal 'element root has unexpected keys: keyB', err.message
|
32
24
|
end
|
33
|
-
end
|
34
25
|
|
35
|
-
|
36
|
-
err = assert_raises(MiniTest::Assertion) do
|
26
|
+
should "test_on_root_object_with_sub_object" do
|
37
27
|
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
38
28
|
has_only
|
39
29
|
has 'keyA' do
|
40
30
|
has 'subKeyA'
|
41
31
|
has 'subKeyB'
|
42
32
|
end
|
33
|
+
has 'keyB'
|
43
34
|
end
|
44
35
|
end
|
45
|
-
assert_equal 'element root has unexpected keys: keyB', err.message
|
46
|
-
end
|
47
36
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
37
|
+
should "test_on_root_object_with_sub_object_failure" do
|
38
|
+
err = assert_raises(MiniTest::Assertion) do
|
39
|
+
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
40
|
+
has_only
|
41
|
+
has 'keyA' do
|
42
|
+
has 'subKeyA'
|
43
|
+
has 'subKeyB'
|
44
|
+
end
|
45
|
+
end
|
54
46
|
end
|
47
|
+
assert_equal 'element root has unexpected keys: keyB', err.message
|
55
48
|
end
|
56
|
-
end
|
57
49
|
|
58
|
-
|
59
|
-
err = assert_raises(MiniTest::Assertion) do
|
50
|
+
should "test_on_sub_object" do
|
60
51
|
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
61
52
|
has 'keyA' do
|
62
53
|
has_only
|
63
54
|
has 'subKeyA'
|
55
|
+
has 'subKeyB'
|
64
56
|
end
|
65
57
|
end
|
66
58
|
end
|
67
|
-
assert_equal 'element keyA has unexpected keys: subKeyB', err.message
|
68
|
-
end
|
69
59
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
60
|
+
should "test_on_sub_object_failure" do
|
61
|
+
err = assert_raises(MiniTest::Assertion) do
|
62
|
+
assert_json '{"keyA":{"subKeyA":"value","subKeyB":"value"},"keyB":"value"}' do
|
63
|
+
has 'keyA' do
|
64
|
+
has_only
|
65
|
+
has 'subKeyA'
|
66
|
+
end
|
67
|
+
end
|
77
68
|
end
|
69
|
+
assert_equal 'element keyA has unexpected keys: subKeyB', err.message
|
78
70
|
end
|
79
|
-
end
|
80
71
|
|
81
|
-
|
82
|
-
|
83
|
-
|
72
|
+
should "test_on_root_array_of_objects" do
|
73
|
+
json = <<JSON
|
74
|
+
[
|
75
|
+
{
|
76
|
+
"id":1,
|
77
|
+
"key":"test",
|
78
|
+
"name":"test"
|
79
|
+
},{
|
80
|
+
"id":2,
|
81
|
+
"key":"test",
|
82
|
+
"name":"test"
|
83
|
+
},{
|
84
|
+
"id":3,
|
85
|
+
"key":"test",
|
86
|
+
"name":"test"
|
87
|
+
}
|
88
|
+
]
|
89
|
+
JSON
|
90
|
+
assert_json json do
|
84
91
|
has_only
|
85
92
|
item 0 do
|
86
93
|
has :id, 1
|
87
94
|
has :key, 'test'
|
95
|
+
has :name, 'test'
|
88
96
|
end
|
89
97
|
end
|
90
98
|
end
|
91
|
-
assert_equal 'element 0 has unexpected keys: name', err.message
|
92
|
-
end
|
93
99
|
|
100
|
+
should "test_on_root_array_of_objects_failure" do
|
101
|
+
err = assert_raises(MiniTest::Assertion) do
|
102
|
+
json = <<JSON
|
103
|
+
[
|
104
|
+
{
|
105
|
+
"id":1,
|
106
|
+
"key":"test",
|
107
|
+
"name":"test"
|
108
|
+
},{
|
109
|
+
"id":2,
|
110
|
+
"key":"test",
|
111
|
+
"name":"test"
|
112
|
+
},{
|
113
|
+
"id":3,
|
114
|
+
"key":"test",
|
115
|
+
"name":"test"
|
116
|
+
}
|
117
|
+
]
|
118
|
+
JSON
|
119
|
+
assert_json json do
|
120
|
+
has_only
|
121
|
+
item 0 do
|
122
|
+
has :id, 1
|
123
|
+
has :key, 'test'
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
assert_equal 'element 0 has unexpected keys: name', err.message
|
128
|
+
end
|
129
|
+
end
|
94
130
|
end
|
@@ -1,477 +1,578 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
+
# nodoc
|
3
4
|
class AssertJsonNewDslTest < Minitest::Test
|
4
5
|
include AssertJson
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
has 'key'
|
9
|
-
end
|
10
|
-
end
|
11
|
-
def test_string_crosscheck
|
12
|
-
assert_raises(MiniTest::Assertion) do
|
7
|
+
context "strings" do
|
8
|
+
should "test_string" do
|
13
9
|
assert_json '"key"' do
|
14
|
-
has '
|
10
|
+
has 'key'
|
15
11
|
end
|
16
12
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
def test_regular_expression_for_hash_values
|
24
|
-
assert_json '{"key":"value"}' do
|
25
|
-
has 'key', /alu/
|
13
|
+
should "test_string_crosscheck" do
|
14
|
+
assert_raises(MiniTest::Assertion) do
|
15
|
+
assert_json '"key"' do
|
16
|
+
has 'wrong_key'
|
17
|
+
end
|
18
|
+
end
|
26
19
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
has 'key', 'value'
|
20
|
+
should "test_regular_expression_for_strings" do
|
21
|
+
assert_json '"string"' do
|
22
|
+
has(/tri/)
|
23
|
+
end
|
32
24
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
has 'key', @values['value']
|
25
|
+
should "test_regular_expression_for_hash_values" do
|
26
|
+
assert_json '{"key":"value"}' do
|
27
|
+
has 'key', /alu/
|
28
|
+
end
|
38
29
|
end
|
39
|
-
end
|
40
|
-
|
41
|
-
|
30
|
+
end # strings
|
31
|
+
|
32
|
+
context "hashes" do
|
33
|
+
should "test_single_hash" do
|
42
34
|
assert_json '{"key":"value"}' do
|
43
|
-
has '
|
35
|
+
has 'key', 'value'
|
44
36
|
end
|
45
37
|
end
|
46
|
-
|
47
|
-
|
48
|
-
assert_raises(MiniTest::Assertion) do
|
38
|
+
should "test_single_hash_with_outer_variable" do
|
39
|
+
@values = { 'value' => 'value' }
|
49
40
|
assert_json '{"key":"value"}' do
|
50
|
-
has 'key', '
|
41
|
+
has 'key', @values['value']
|
51
42
|
end
|
52
43
|
end
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
44
|
+
should "test_single_hash_crosscheck_for_key" do
|
45
|
+
assert_raises(MiniTest::Assertion) do
|
46
|
+
assert_json '{"key":"value"}' do
|
47
|
+
has 'wrong_key', 'value'
|
48
|
+
end
|
49
|
+
end
|
59
50
|
end
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
51
|
+
should "test_single_hash_crosscheck_for_value" do
|
52
|
+
assert_raises(MiniTest::Assertion) do
|
53
|
+
assert_json '{"key":"value"}' do
|
54
|
+
has 'key', 'wrong_value'
|
55
|
+
end
|
65
56
|
end
|
66
57
|
end
|
67
|
-
end
|
68
58
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
59
|
+
should "test_has_not" do
|
60
|
+
assert_json '{"key":"value"}' do
|
61
|
+
has 'key', 'value'
|
62
|
+
has_not 'key_not_included'
|
63
|
+
end
|
74
64
|
end
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
65
|
+
should "test_has_not_crosscheck" do
|
66
|
+
assert_raises(MiniTest::Assertion) do
|
67
|
+
assert_json '{"key":"value"}' do
|
68
|
+
has_not 'key'
|
69
|
+
end
|
70
|
+
end
|
81
71
|
end
|
82
|
-
end
|
83
|
-
|
84
|
-
|
72
|
+
end # hashes
|
73
|
+
|
74
|
+
context "arrays" do
|
75
|
+
should "test_array" do
|
85
76
|
assert_json '["value1","value2","value3"]' do
|
77
|
+
has 'value1'
|
86
78
|
has 'value2'
|
79
|
+
has 'value3'
|
87
80
|
end
|
88
81
|
end
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
82
|
+
should "test_has_not_array" do
|
83
|
+
assert_json '["value1","value2"]' do
|
84
|
+
has 'value1'
|
85
|
+
has 'value2'
|
86
|
+
has_not 'value3'
|
94
87
|
end
|
95
88
|
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
has 'wrong_value2'
|
89
|
+
should "test_array_crosscheck_order" do
|
90
|
+
assert_raises(MiniTest::Assertion) do
|
91
|
+
assert_json '["value1","value2","value3"]' do
|
92
|
+
has 'value2'
|
93
|
+
end
|
102
94
|
end
|
103
95
|
end
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
96
|
+
should "test_array_crosscheck_for_first_item" do
|
97
|
+
assert_raises(MiniTest::Assertion) do
|
98
|
+
assert_json '["value1","value2","value3"]' do
|
99
|
+
has 'wrong_value1'
|
100
|
+
end
|
101
|
+
end
|
109
102
|
end
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
103
|
+
should "test_array_crosscheck_for_second_item" do
|
104
|
+
assert_raises(MiniTest::Assertion) do
|
105
|
+
assert_json '["value1","value2","value3"]' do
|
106
|
+
has 'value1'
|
107
|
+
has 'wrong_value2'
|
108
|
+
end
|
115
109
|
end
|
116
110
|
end
|
117
|
-
|
111
|
+
|
112
|
+
should "test_nested_arrays" do
|
118
113
|
assert_json '[[["deep","another_depp"],["second_deep"]]]' do
|
119
|
-
has [
|
114
|
+
has [%w(deep another_depp), %w(second_deep)]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
should "test_nested_arrays_crosscheck" do
|
118
|
+
assert_raises(MiniTest::Assertion) do
|
119
|
+
assert_json '[[["deep","another_depp"],["second_deep"]]]' do
|
120
|
+
has [%w(deep wrong_another_depp), %w(second_deep)]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
assert_raises(MiniTest::Assertion) do
|
124
|
+
assert_json '[[["deep","another_depp"],["second_deep"]]]' do
|
125
|
+
has [%w(deep another_depp), %w(wrong_second_deep)]
|
126
|
+
end
|
120
127
|
end
|
121
128
|
end
|
122
|
-
end
|
123
129
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
+
should "test_array_size" do
|
131
|
+
assert_json '["value1","value2"]' do
|
132
|
+
size 2
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
should "test_array_size_error" do
|
137
|
+
assert_raises(MiniTest::Assertion) do
|
138
|
+
assert_json '["single value"]' do
|
139
|
+
size 2
|
130
140
|
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
should "test_nested_array_size" do
|
145
|
+
assert_json '["value1",["value2.1","value2.2","value3.3"]]' do
|
146
|
+
size 2
|
131
147
|
item 1 do
|
132
|
-
|
148
|
+
size 3
|
133
149
|
end
|
134
150
|
end
|
135
|
-
|
136
|
-
|
151
|
+
end
|
152
|
+
|
153
|
+
should "test_itemised_nested_array" do
|
154
|
+
json = '[["deep","another_depp"],["second_deep"]]'
|
155
|
+
assert_json json do
|
156
|
+
item 0 do
|
157
|
+
item 0 do
|
158
|
+
has 'deep'
|
159
|
+
end
|
160
|
+
item 1 do
|
161
|
+
has 'another_depp'
|
162
|
+
end
|
163
|
+
end
|
164
|
+
item 1 do
|
165
|
+
has 'second_deep'
|
166
|
+
end
|
137
167
|
end
|
138
168
|
end
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
assert_json json do
|
143
|
-
item 0 do
|
169
|
+
should "test_itemised_nested_array_crosscheck" do
|
170
|
+
json = '[["deep","another_depp"],["second_deep"]]'
|
171
|
+
assert_json json do
|
144
172
|
item 0 do
|
145
|
-
|
173
|
+
item 0 do
|
174
|
+
has 'deep'
|
175
|
+
end
|
176
|
+
item 1 do
|
177
|
+
assert_raises(MiniTest::Assertion) do
|
178
|
+
has 'wrong_item_value'
|
179
|
+
end
|
180
|
+
end
|
146
181
|
end
|
147
182
|
item 1 do
|
148
183
|
assert_raises(MiniTest::Assertion) do
|
149
|
-
has '
|
184
|
+
has 'unknown_item_value'
|
150
185
|
end
|
151
186
|
end
|
152
187
|
end
|
153
|
-
item 1 do
|
154
|
-
assert_raises(MiniTest::Assertion) do
|
155
|
-
has 'unknown_item_value'
|
156
|
-
end
|
157
|
-
end
|
158
188
|
end
|
159
|
-
end
|
189
|
+
end # arrays
|
160
190
|
|
161
|
-
|
162
|
-
|
163
|
-
has 'key', ['value1', 'value2']
|
164
|
-
end
|
165
|
-
end
|
166
|
-
def test_hash_with_value_array_crosscheck_wrong_key
|
167
|
-
assert_raises(MiniTest::Assertion) do
|
191
|
+
context "hashes with arrays" do
|
192
|
+
should "test_hash_with_value_array" do
|
168
193
|
assert_json '{"key":["value1","value2"]}' do
|
169
|
-
has '
|
194
|
+
has 'key', %w(value1 value2)
|
170
195
|
end
|
171
196
|
end
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
197
|
+
should "test_hash_with_value_array_crosscheck_wrong_key" do
|
198
|
+
assert_raises(MiniTest::Assertion) do
|
199
|
+
assert_json '{"key":["value1","value2"]}' do
|
200
|
+
has 'wrong_key', %w(value1 value2)
|
201
|
+
end
|
177
202
|
end
|
178
203
|
end
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
204
|
+
should "test_hash_with_value_array_crosscheck_wrong_value1" do
|
205
|
+
assert_raises(MiniTest::Assertion) do
|
206
|
+
assert_json '{"key":["value1","value2"]}' do
|
207
|
+
has 'key', %w(wrong_value1 value2)
|
208
|
+
end
|
184
209
|
end
|
185
210
|
end
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
has 'inner_key1', 'value1'
|
192
|
-
has 'inner_key2', 'value2'
|
211
|
+
should "test_hash_with_value_array_crosscheck_wrong_value2" do
|
212
|
+
assert_raises(MiniTest::Assertion) do
|
213
|
+
assert_json '{"key":["value1","value2"]}' do
|
214
|
+
has 'key', %w(value1 wrong_value2)
|
215
|
+
end
|
193
216
|
end
|
194
217
|
end
|
195
|
-
|
196
|
-
|
197
|
-
assert_raises(MiniTest::Assertion) do
|
218
|
+
|
219
|
+
should "test_hash_with_array_of_hashes" do
|
198
220
|
assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
|
199
221
|
has 'key' do
|
200
|
-
has '
|
222
|
+
has 'inner_key1', 'value1'
|
223
|
+
has 'inner_key2', 'value2'
|
201
224
|
end
|
202
225
|
end
|
203
226
|
end
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
227
|
+
should "test_hash_with_array_of_hashes_crosscheck_inner_key" do
|
228
|
+
assert_raises(MiniTest::Assertion) do
|
229
|
+
assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
|
230
|
+
has 'key' do
|
231
|
+
has 'wrong_inner_key1', 'value1'
|
232
|
+
end
|
210
233
|
end
|
211
234
|
end
|
212
235
|
end
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
has 'key', 'test'
|
221
|
-
has 'name', 'test'
|
222
|
-
end
|
223
|
-
item 2 do
|
224
|
-
has 'id', 3
|
236
|
+
should "test_hash_with_array_of_hashes_crosscheck_inner_value" do
|
237
|
+
assert_raises(MiniTest::Assertion) do
|
238
|
+
assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
|
239
|
+
has 'key' do
|
240
|
+
has 'inner_key1', 'wrong_value1'
|
241
|
+
end
|
242
|
+
end
|
225
243
|
end
|
226
244
|
end
|
227
|
-
end
|
228
|
-
|
229
|
-
|
230
|
-
|
245
|
+
end # hashes with arrays
|
246
|
+
|
247
|
+
context "arrays with hashes" do
|
248
|
+
should "test_array_with_multi_item_hashes" do
|
249
|
+
json = <<JSON
|
250
|
+
[
|
251
|
+
{
|
252
|
+
"id":1,
|
253
|
+
"key":"test",
|
254
|
+
"name":"test"
|
255
|
+
},{
|
256
|
+
"id":2,
|
257
|
+
"key":"test",
|
258
|
+
"name":"test"
|
259
|
+
},{
|
260
|
+
"id":3,
|
261
|
+
"key":"test",
|
262
|
+
"name":"test"
|
263
|
+
}
|
264
|
+
]
|
265
|
+
JSON
|
266
|
+
assert_json json do
|
231
267
|
item 0 do
|
232
268
|
has 'id', 1
|
233
269
|
has 'key', 'test'
|
234
270
|
has 'name', 'test'
|
235
271
|
end
|
236
272
|
item 2 do
|
237
|
-
has 'id',
|
273
|
+
has 'id', 3
|
238
274
|
end
|
239
275
|
end
|
240
276
|
end
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
277
|
+
should "test_array_with_multi_item_hashes_crosscheck" do
|
278
|
+
assert_raises(MiniTest::Assertion) do
|
279
|
+
json = <<JSON
|
280
|
+
[
|
281
|
+
{
|
282
|
+
"id":1,
|
283
|
+
"key":"test",
|
284
|
+
"name":"test"
|
285
|
+
},{
|
286
|
+
"id":2,
|
287
|
+
"key":"test",
|
288
|
+
"name":"test"
|
289
|
+
},{
|
290
|
+
"id":3,
|
291
|
+
"key":"test",
|
292
|
+
"name":"test"
|
293
|
+
}
|
294
|
+
]
|
295
|
+
JSON
|
296
|
+
assert_json json do
|
297
|
+
item 0 do
|
298
|
+
has 'id', 1
|
299
|
+
has 'key', 'test'
|
300
|
+
has 'name', 'test'
|
301
|
+
end
|
302
|
+
item 2 do
|
303
|
+
has 'id', 2
|
304
|
+
end
|
305
|
+
end
|
254
306
|
end
|
255
307
|
end
|
256
|
-
|
257
|
-
|
258
|
-
assert_raises(MiniTest::Assertion) do
|
308
|
+
|
309
|
+
should "test_array_with_two_hashes" do
|
259
310
|
assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
|
260
|
-
has '
|
311
|
+
has 'key1', 'value1'
|
261
312
|
has 'key2', 'value2'
|
262
313
|
end
|
263
314
|
end
|
264
|
-
|
265
|
-
assert_json '[{"key1":
|
266
|
-
has 'key1'
|
267
|
-
|
315
|
+
should "test_array_with_nested_hashes" do
|
316
|
+
assert_json '[{"key1":{"key2":"value2"}}]' do
|
317
|
+
has 'key1' do
|
318
|
+
has 'key2', 'value2'
|
319
|
+
end
|
268
320
|
end
|
269
321
|
end
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
has 'key' do
|
276
|
-
has 'inner_key', 'value'
|
322
|
+
should "test_array_with_two_hashes_crosscheck" do
|
323
|
+
assert_raises(MiniTest::Assertion) do
|
324
|
+
assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
|
325
|
+
has 'wrong_key1', 'value1'
|
326
|
+
has 'key2', 'value2'
|
277
327
|
end
|
278
328
|
end
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
has 'wrong_outer_key'
|
329
|
+
assert_raises(MiniTest::Assertion) do
|
330
|
+
assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
|
331
|
+
has 'key1', 'value1'
|
332
|
+
has 'key2', 'wrong_value2'
|
333
|
+
end
|
285
334
|
end
|
286
335
|
end
|
287
|
-
|
336
|
+
end # arrays with hashes
|
337
|
+
|
338
|
+
context "nested hashes" do
|
339
|
+
should "test_nested_hashes" do
|
288
340
|
assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
|
289
341
|
has 'outer_key' do
|
290
342
|
has 'key' do
|
291
|
-
has 'inner_key', '
|
343
|
+
has 'inner_key', 'value'
|
292
344
|
end
|
293
345
|
end
|
294
346
|
end
|
295
347
|
end
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
348
|
+
should "test_nested_hashes_crosscheck" do
|
349
|
+
assert_raises(MiniTest::Assertion) do
|
350
|
+
assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
|
351
|
+
has 'wrong_outer_key'
|
352
|
+
end
|
353
|
+
end
|
354
|
+
assert_raises(MiniTest::Assertion) do
|
355
|
+
assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
|
356
|
+
has 'outer_key' do
|
357
|
+
has 'key' do
|
358
|
+
has 'inner_key', 'wrong_value'
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
304
362
|
end
|
305
363
|
end
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
364
|
+
end # nested hashes
|
365
|
+
|
366
|
+
context "real life examples" do
|
367
|
+
should "test_complex_example" do
|
368
|
+
json = <<JSON
|
369
|
+
{
|
370
|
+
"count":2,
|
371
|
+
"total_count":3,
|
372
|
+
"results":[
|
373
|
+
{
|
374
|
+
"id":14,
|
375
|
+
"tags":[
|
376
|
+
"tag1",
|
377
|
+
"tag2"
|
378
|
+
],
|
379
|
+
"created_at":"2014-10-14T00:50:39Z",
|
380
|
+
"updated_at":"2014-10-14T00:51:39Z"
|
381
|
+
},{
|
382
|
+
"id":15,
|
383
|
+
"tags":[
|
384
|
+
"tag3",
|
385
|
+
"tag4"
|
386
|
+
],
|
387
|
+
"created_at":"2014-10-15T00:50:39Z",
|
388
|
+
"updated_at":"2014-10-15T00:51:39Z"
|
389
|
+
}
|
390
|
+
]
|
391
|
+
}
|
392
|
+
JSON
|
393
|
+
|
394
|
+
assert_json json do
|
395
|
+
has 'count', 2
|
396
|
+
has 'total_count', 3
|
397
|
+
has 'results' do
|
398
|
+
item 0 do
|
399
|
+
has 'id', 14
|
400
|
+
has 'tags' do
|
401
|
+
item 0 do
|
402
|
+
has 'tag1'
|
403
|
+
end
|
404
|
+
item 1 do
|
405
|
+
has 'tag2'
|
406
|
+
end
|
407
|
+
end
|
408
|
+
has 'created_at', '2014-10-14T00:50:39Z'
|
409
|
+
has 'updated_at', '2014-10-14T00:51:39Z'
|
410
|
+
end
|
311
411
|
end
|
312
|
-
has 'receiver_id', '2'
|
313
|
-
has 'body', 'body'
|
314
412
|
end
|
315
413
|
end
|
316
|
-
end
|
317
414
|
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
415
|
+
should "test_real_xws" do
|
416
|
+
assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do
|
417
|
+
has 'contact_request' do
|
418
|
+
has 'sender_id', '3'
|
419
|
+
has 'receiver_id', '2'
|
420
|
+
has 'id', 1
|
421
|
+
end
|
324
422
|
end
|
325
|
-
end
|
326
|
-
end
|
327
423
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
has '
|
334
|
-
has '
|
424
|
+
assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do
|
425
|
+
has 'private_message' do
|
426
|
+
has 'sender' do
|
427
|
+
has 'display_name', 'first last'
|
428
|
+
end
|
429
|
+
has 'receiver_id', '2'
|
430
|
+
has 'body', 'body'
|
335
431
|
end
|
336
432
|
end
|
337
433
|
end
|
338
|
-
end
|
434
|
+
end # real life examples
|
339
435
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
436
|
+
context "not enough elements" do
|
437
|
+
should "test_not_enough_hass_in_array" do
|
438
|
+
assert_raises(MiniTest::Assertion) do
|
439
|
+
assert_json '["one","two"]' do
|
440
|
+
has "one"
|
441
|
+
has "two"
|
442
|
+
has "three"
|
443
|
+
end
|
444
|
+
end
|
346
445
|
end
|
347
|
-
end
|
348
446
|
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
447
|
+
should "test_not_enough_hass_in_hash_array" do
|
448
|
+
assert_raises(MiniTest::Assertion) do
|
449
|
+
assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do
|
450
|
+
has 'key' do
|
451
|
+
has 'key1', 'value1'
|
452
|
+
has 'key2', 'value2'
|
453
|
+
has 'key3'
|
454
|
+
end
|
455
|
+
end
|
353
456
|
end
|
354
457
|
end
|
355
|
-
|
458
|
+
end # not enough elements
|
459
|
+
|
460
|
+
context "boolean" do
|
461
|
+
should "test_boolean" do
|
356
462
|
assert_json '{"key": true}' do
|
463
|
+
has 'key', true
|
464
|
+
end
|
465
|
+
assert_json '{"key": false}' do
|
357
466
|
has 'key', false
|
358
467
|
end
|
359
468
|
end
|
360
|
-
end
|
361
|
-
|
362
|
-
def test_null
|
363
|
-
assert_json '{"key": null}' do
|
364
|
-
has 'key', nil
|
365
|
-
end
|
366
|
-
end
|
367
469
|
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
470
|
+
should "test_boolean_crosscheck" do
|
471
|
+
assert_raises(MiniTest::Assertion) do
|
472
|
+
assert_json '{"key": false}' do
|
473
|
+
has 'key', true
|
474
|
+
end
|
475
|
+
end
|
476
|
+
assert_raises(MiniTest::Assertion) do
|
477
|
+
assert_json '{"key": true}' do
|
478
|
+
has 'key', false
|
479
|
+
end
|
372
480
|
end
|
373
481
|
end
|
374
|
-
end
|
482
|
+
end # boolean
|
375
483
|
|
376
|
-
|
377
|
-
|
484
|
+
context "null" do
|
485
|
+
should "test_null" do
|
378
486
|
assert_json '{"key": null}' do
|
379
|
-
|
487
|
+
has 'key', nil
|
380
488
|
end
|
381
489
|
end
|
382
|
-
end
|
383
490
|
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
assert_json '{"key": "badtext"}' do
|
390
|
-
has :key, :text
|
491
|
+
should "test_not_null" do
|
492
|
+
assert_raises(MiniTest::Assertion) do
|
493
|
+
assert_json '{"key": 1}' do
|
494
|
+
has 'key', nil
|
495
|
+
end
|
391
496
|
end
|
392
497
|
end
|
393
|
-
end
|
394
498
|
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
end
|
401
|
-
assert_json '{"sym": false, "text": "2", "topkey": {"subkey": "value1"}}' do
|
402
|
-
has :sym, false
|
403
|
-
has :text, /\d+/
|
404
|
-
has_not :bad_sym
|
405
|
-
has :topkey do
|
406
|
-
has :subkey, :value1
|
499
|
+
should "test_null_crosscheck" do
|
500
|
+
assert_raises(MiniTest::Assertion) do
|
501
|
+
assert_json '{"key": null}' do
|
502
|
+
has_not 'key'
|
503
|
+
end
|
407
504
|
end
|
408
505
|
end
|
409
|
-
end
|
506
|
+
end # null
|
410
507
|
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
has :
|
415
|
-
|
508
|
+
context "symbols" do
|
509
|
+
should "test_symbol_as_a_value" do
|
510
|
+
assert_json '{"key": "text"}' do
|
511
|
+
has :key, :text
|
512
|
+
end
|
513
|
+
assert_raises(MiniTest::Assertion) do
|
514
|
+
assert_json '{"key": "badtext"}' do
|
515
|
+
has :key, :text
|
416
516
|
end
|
417
517
|
end
|
418
518
|
end
|
419
|
-
end
|
420
|
-
|
421
519
|
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
has :sym, true #this should fail
|
520
|
+
should "test_symbol_as_a_key" do
|
521
|
+
assert_json '{"sym": true, "text": "1"}' do
|
522
|
+
has :sym, true
|
426
523
|
has :text, /\d+/
|
427
524
|
has_not :bad_sym
|
428
525
|
end
|
429
|
-
|
430
|
-
|
431
|
-
assert_raises(MiniTest::Assertion) do
|
432
|
-
assert_json '{"sym": false, "text": "abc"}' do
|
526
|
+
assert_json '{"sym": false, "text": "2", "topkey": {"subkey": "value1"}}' do
|
433
527
|
has :sym, false
|
434
|
-
has :text, /\d+/
|
528
|
+
has :text, /\d+/
|
435
529
|
has_not :bad_sym
|
530
|
+
has :topkey do
|
531
|
+
has :subkey, :value1
|
532
|
+
end
|
436
533
|
end
|
437
534
|
end
|
438
535
|
|
439
|
-
|
440
|
-
assert_json '{"
|
441
|
-
|
536
|
+
should "test_symbol_as_string_value" do
|
537
|
+
assert_json '{"topkey": {"subkey": "value1"}}' do
|
538
|
+
has :topkey do
|
539
|
+
has :subkey do
|
540
|
+
has :value1
|
541
|
+
end
|
542
|
+
end
|
442
543
|
end
|
443
544
|
end
|
444
|
-
end
|
445
545
|
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
546
|
+
should "test_symbol_as_a_key_crossheck" do
|
547
|
+
assert_raises(MiniTest::Assertion) do
|
548
|
+
assert_json '{"text": "1"}' do
|
549
|
+
has :sym, true # this should fail
|
550
|
+
has :text, /\d+/
|
551
|
+
has_not :bad_sym
|
552
|
+
end
|
553
|
+
end
|
451
554
|
|
452
|
-
|
453
|
-
|
454
|
-
|
555
|
+
assert_raises(MiniTest::Assertion) do
|
556
|
+
assert_json '{"sym": false, "text": "abc"}' do
|
557
|
+
has :sym, false
|
558
|
+
has :text, /\d+/ # this should fail
|
559
|
+
has_not :bad_sym
|
560
|
+
end
|
561
|
+
end
|
455
562
|
|
456
|
-
|
457
|
-
|
458
|
-
|
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'
|
563
|
+
assert_raises(MiniTest::Assertion) do
|
564
|
+
assert_json '{"sym": false}' do
|
565
|
+
has_not :sym
|
472
566
|
end
|
473
567
|
end
|
474
568
|
end
|
475
|
-
end
|
569
|
+
end # symbols
|
476
570
|
|
571
|
+
context "regular expressions for numbers" do
|
572
|
+
should "test_regex_with_number" do
|
573
|
+
assert_json '{"number": 1}' do
|
574
|
+
has :number, /^\d+$/
|
575
|
+
end
|
576
|
+
end
|
577
|
+
end
|
477
578
|
end
|