assert_json 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,2 +1,3 @@
1
1
  source "http://rubygems.org"
2
2
  gemspec
3
+ gem 'rubocop', require: false
data/HOWTO.md ADDED
@@ -0,0 +1,3 @@
1
+ ## How to run rubocop
2
+
3
+ rubocop -c .rubocop.yml
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
@@ -11,4 +11,4 @@ Rake::TestTask.new(:test) do |t|
11
11
  end
12
12
 
13
13
  desc 'Default: run unit tests.'
14
- task :default => :test
14
+ task default: :test
data/assert_json.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
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 = %q{boettger@mt7.de}
13
- s.homepage = %q{https://github.com/alto/assert_json}
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 'minitest'
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
- def assert_json(json_string, &block)
4
- if block_given?
5
- @json = AssertJson::Json.new(json_string)
6
- # json.instance_exec(json, &block)
7
- yield @json
8
- @json.test_for_unexpected_keys('root')
9
- end
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 has_not(*args, &block)
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 has_only
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 item(index, &block)
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, &block)
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 = lambda {raise_error("element #{token[arg].inspect} does not match #{second_arg.inspect}")}
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
- if block_given?
98
- begin
99
- only_in_scope = @only
100
- expected_keys_in_scope = @expected_keys
101
- @expected_keys = []
102
- decoded_json_in_scope = @decoded_json
103
- @decoded_json = case token
104
- when Hash
105
- token[arg]
106
- else
107
- token
108
- end
109
- yield
110
- test_for_unexpected_keys(arg)
111
- ensure
112
- @expected_keys = expected_keys_in_scope
113
- @only = only_in_scope
114
- @decoded_json = decoded_json_in_scope
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
- alias has element
127
+ alias_method :has, :element
119
128
 
120
- def not_element(*args, &block)
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
- alias has_not not_element
139
+ alias_method :has_not, :not_element
131
140
 
132
141
  def only
133
142
  @only = true
134
143
  end
135
- alias has_only only
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
- 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
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
- def raise_error(message)
151
- if Object.const_defined?(:MiniTest)
152
- raise MiniTest::Assertion.new(message)
153
- else
154
- raise Test::Unit::AssertionFailedError.new(message)
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
@@ -1,3 +1,4 @@
1
+ # nodoc
1
2
  module AssertJson
2
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
3
4
  end
@@ -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
- 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
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', 'value'
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
- 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'
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 'keyB'
23
+ assert_equal 'element root has unexpected keys: keyB', err.message
32
24
  end
33
- end
34
25
 
35
- def test_on_root_object_with_sub_object_failure
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
- 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'
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
- def test_on_sub_object_failure
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
- 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'
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
- 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
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
- 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
7
+ context "strings" do
8
+ should "test_string" do
13
9
  assert_json '"key"' do
14
- has 'wrong_key'
10
+ has 'key'
15
11
  end
16
12
  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/
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
- end
28
-
29
- def test_single_hash
30
- assert_json '{"key":"value"}' do
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
- end
34
- def test_single_hash_with_outer_variable
35
- @values = {'value' => 'value'}
36
- assert_json '{"key":"value"}' do
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
- def test_single_hash_crosscheck_for_key
41
- assert_raises(MiniTest::Assertion) do
30
+ end # strings
31
+
32
+ context "hashes" do
33
+ should "test_single_hash" do
42
34
  assert_json '{"key":"value"}' do
43
- has 'wrong_key', 'value'
35
+ has 'key', 'value'
44
36
  end
45
37
  end
46
- end
47
- def test_single_hash_crosscheck_for_value
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', 'wrong_value'
41
+ has 'key', @values['value']
51
42
  end
52
43
  end
53
- end
54
-
55
- def test_has_not
56
- assert_json '{"key":"value"}' do
57
- has 'key', 'value'
58
- has_not 'key_not_included'
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
- end
61
- def test_has_not_crosscheck
62
- assert_raises(MiniTest::Assertion) do
63
- assert_json '{"key":"value"}' do
64
- has_not 'key'
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
- def test_array
70
- assert_json '["value1","value2","value3"]' do
71
- has 'value1'
72
- has 'value2'
73
- has 'value3'
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
- end
76
- def test_has_not_array
77
- assert_json '["value1","value2"]' do
78
- has 'value1'
79
- has 'value2'
80
- has_not 'value3'
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
- def test_array_crosscheck_order
84
- assert_raises(MiniTest::Assertion) do
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
- end
90
- def test_array_crosscheck_for_first_item
91
- assert_raises(MiniTest::Assertion) do
92
- assert_json '["value1","value2","value3"]' do
93
- has 'wrong_value1'
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
- end
97
- def test_array_crosscheck_for_second_item
98
- assert_raises(MiniTest::Assertion) do
99
- assert_json '["value1","value2","value3"]' do
100
- has 'value1'
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
- end
105
-
106
- def test_nested_arrays
107
- assert_json '[[["deep","another_depp"],["second_deep"]]]' do
108
- has [["deep","another_depp"],["second_deep"]]
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
- end
111
- def test_nested_arrays_crosscheck
112
- assert_raises(MiniTest::Assertion) do
113
- assert_json '[[["deep","another_depp"],["second_deep"]]]' do
114
- has [["deep","wrong_another_depp"],["second_deep"]]
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
- assert_raises(MiniTest::Assertion) do
111
+
112
+ should "test_nested_arrays" do
118
113
  assert_json '[[["deep","another_depp"],["second_deep"]]]' do
119
- has [["deep","another_depp"],["wrong_second_deep"]]
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
- 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
+ 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
- has 'another_depp'
148
+ size 3
133
149
  end
134
150
  end
135
- item 1 do
136
- has 'second_deep'
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
- end
140
- def test_itemised_nested_array_crosscheck
141
- json = '[["deep","another_depp"],["second_deep"]]'
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
- has 'deep'
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 'wrong_item_value'
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
- def test_hash_with_value_array
162
- assert_json '{"key":["value1","value2"]}' do
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 'wrong_key', ['value1', 'value2']
194
+ has 'key', %w(value1 value2)
170
195
  end
171
196
  end
172
- end
173
- def test_hash_with_value_array_crosscheck_wrong_value1
174
- assert_raises(MiniTest::Assertion) do
175
- assert_json '{"key":["value1","value2"]}' do
176
- has 'key', ['wrong_value1', 'value2']
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
- end
180
- def test_hash_with_value_array_crosscheck_wrong_value2
181
- assert_raises(MiniTest::Assertion) do
182
- assert_json '{"key":["value1","value2"]}' do
183
- has 'key', ['value1', 'wrong_value2']
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
- end
187
-
188
- def test_hash_with_array_of_hashes
189
- assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
190
- has 'key' do
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
- end
196
- def test_hash_with_array_of_hashes_crosscheck_inner_key
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 'wrong_inner_key1', 'value1'
222
+ has 'inner_key1', 'value1'
223
+ has 'inner_key2', 'value2'
201
224
  end
202
225
  end
203
226
  end
204
- end
205
- def test_hash_with_array_of_hashes_crosscheck_inner_value
206
- assert_raises(MiniTest::Assertion) do
207
- assert_json '{"key":[{"inner_key1":"value1"},{"inner_key2":"value2"}]}' do
208
- has 'key' do
209
- has 'inner_key1', 'wrong_value1'
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
- end
214
-
215
-
216
- def test_array_with_multi_item_hashes
217
- assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
218
- item 0 do
219
- has 'id', 1
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
- def test_array_with_multi_item_hashes_crosscheck
229
- assert_raises(MiniTest::Assertion) do
230
- assert_json '[{"id":1, "key":"test", "name":"test"}, {"id":2, "key":"test", "name":"test"}, {"id":3, "key":"test", "name":"test"}]' do
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', 2
273
+ has 'id', 3
238
274
  end
239
275
  end
240
276
  end
241
- end
242
-
243
-
244
- def test_array_with_two_hashes
245
- assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
246
- has 'key1', 'value1'
247
- has 'key2', 'value2'
248
- end
249
- end
250
- def test_array_with_nested_hashes
251
- assert_json '[{"key1":{"key2":"value2"}}]' do
252
- has 'key1' do
253
- has 'key2', 'value2'
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
- end
257
- def test_array_with_two_hashes_crosscheck
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 'wrong_key1', 'value1'
311
+ has 'key1', 'value1'
261
312
  has 'key2', 'value2'
262
313
  end
263
314
  end
264
- assert_raises(MiniTest::Assertion) do
265
- assert_json '[{"key1":"value1"}, {"key2":"value2"}]' do
266
- has 'key1', 'value1'
267
- has 'key2', 'wrong_value2'
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
- end
271
-
272
- def test_nested_hashes
273
- assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
274
- has 'outer_key' do
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
- end
280
- end
281
- def test_nested_hashes_crosscheck
282
- assert_raises(MiniTest::Assertion) do
283
- assert_json '{"outer_key":{"key":{"inner_key":"value"}}}' do
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
- assert_raises(MiniTest::Assertion) do
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', 'wrong_value'
343
+ has 'inner_key', 'value'
292
344
  end
293
345
  end
294
346
  end
295
347
  end
296
- end
297
-
298
- def test_real_xws
299
- assert_json '[{"contact_request":{"sender_id":"3","receiver_id":"2","id":1}}]' do
300
- has 'contact_request' do
301
- has 'sender_id', '3'
302
- has 'receiver_id', '2'
303
- has 'id', 1
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
- assert_json '[{"private_message":{"sender":{"display_name":"first last"},"receiver_id":"2","body":"body"}}]' do
308
- has 'private_message' do
309
- has 'sender' do
310
- has 'display_name', 'first last'
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
- def test_not_enough_hass_in_array
319
- assert_raises(MiniTest::Assertion) do
320
- assert_json '["one","two"]' do
321
- has "one"
322
- has "two"
323
- has "three"
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
- def test_not_enough_hass_in_hash_array
329
- assert_raises(MiniTest::Assertion) do
330
- assert_json '{"key":[{"key1":"value1"}, {"key2":"value2"}]}' do
331
- has 'key' do
332
- has 'key1', 'value1'
333
- has 'key2', 'value2'
334
- has 'key3'
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
- def test_boolean
341
- assert_json '{"key": true}' do
342
- has 'key', true
343
- end
344
- assert_json '{"key": false}' do
345
- has 'key', false
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
- def test_boolean_crosscheck
350
- assert_raises(MiniTest::Assertion) do
351
- assert_json '{"key": false}' do
352
- has 'key', true
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
- assert_raises(MiniTest::Assertion) do
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
- def test_not_null
369
- assert_raises(MiniTest::Assertion) do
370
- assert_json '{"key": 1}' do
371
- has 'key', nil
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
- def test_null_crosscheck
377
- assert_raises(MiniTest::Assertion) do
484
+ context "null" do
485
+ should "test_null" do
378
486
  assert_json '{"key": null}' do
379
- has_not 'key'
487
+ has 'key', nil
380
488
  end
381
489
  end
382
- end
383
490
 
384
- def test_symbol_as_a_value
385
- assert_json '{"key": "text"}' do
386
- has :key, :text
387
- end
388
- assert_raises(MiniTest::Assertion) do
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
- def test_symbol_as_a_key
396
- assert_json '{"sym": true, "text": "1"}' do
397
- has :sym, true
398
- has :text, /\d+/
399
- has_not :bad_sym
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
- def test_symbol_as_string_value
412
- assert_json '{"topkey": {"subkey": "value1"}}' do
413
- has :topkey do
414
- has :subkey do
415
- has :value1
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
- def test_symbol_as_a_key_crossheck
423
- assert_raises(MiniTest::Assertion) do
424
- assert_json '{"text": "1"}' do
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
- end
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+/ #this should fail
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
- assert_raises(MiniTest::Assertion) do
440
- assert_json '{"sym": false}' do
441
- has_not :sym
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
- def test_regex_with_number
447
- assert_json '{"number": 1}' do
448
- has :number, /^\d+$/
449
- end
450
- end
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
- # 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"}]}'
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
- 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'
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