json_assert 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE.md +20 -0
- data/README.md +30 -0
- data/Rakefile +21 -0
- data/json_assert.gemspec +24 -0
- data/lib/json_assert/assertions.rb +139 -0
- data/lib/json_assert/config.rb +27 -0
- data/lib/json_assert/errors.rb +13 -0
- data/lib/json_assert/exclusions.rb +26 -0
- data/lib/json_assert/helpers.rb +54 -0
- data/lib/json_assert/version.rb +3 -0
- data/lib/json_assert.rb +11 -0
- data/test/assertions_eql_test.rb +62 -0
- data/test/assertions_include_test.rb +71 -0
- data/test/assertions_part_test.rb +25 -0
- data/test/assertions_path_test.rb +27 -0
- data/test/assertions_size_test.rb +27 -0
- data/test/assertions_type_test.rb +45 -0
- data/test/test_helper.rb +13 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright © 2011 Frazer Horn
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
json_assert
|
2
|
+
========
|
3
|
+
Easily handle JSON in Test::Unit.
|
4
|
+
This is completely copied from the excellent json_spec gem found here https://github.com/collectiveidea/json_spec
|
5
|
+
I just wanted the same features for a project where rspec was not available
|
6
|
+
|
7
|
+
Installation
|
8
|
+
------------
|
9
|
+
gem install json_assert
|
10
|
+
|
11
|
+
or with Bundler:
|
12
|
+
|
13
|
+
gem "json_assert"
|
14
|
+
|
15
|
+
Documentation
|
16
|
+
-------------
|
17
|
+
|
18
|
+
json_assert defines six new assertions:
|
19
|
+
|
20
|
+
* `assert_json_eql`
|
21
|
+
* `assert_part_json`
|
22
|
+
* `assert_include_json`
|
23
|
+
* `assert_json_path`
|
24
|
+
* `assert_json_type`
|
25
|
+
* `assert_json_size`
|
26
|
+
|
27
|
+
Copyright
|
28
|
+
---------
|
29
|
+
Copyright © 2011 Frazer Horn
|
30
|
+
See [LICENSE](https://github.com/frazerh/json_assert/blob/master/LICENSE.md) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
require 'rake/testtask'
|
8
|
+
Rake::TestTask.new(:test) do |test|
|
9
|
+
test.libs << 'lib' << 'test'
|
10
|
+
test.test_files = FileList['test/*_test.rb']
|
11
|
+
test.verbose = true
|
12
|
+
# test.warning = true
|
13
|
+
end
|
14
|
+
|
15
|
+
namespace :test do
|
16
|
+
Rake::TestTask.new(:unit) do |test|
|
17
|
+
test.libs << 'lib' << 'test'
|
18
|
+
test.pattern = 'test/*_test.rb'
|
19
|
+
test.verbose = true
|
20
|
+
end
|
21
|
+
end
|
data/json_assert.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "json_assert/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "json_assert"
|
7
|
+
s.version = JsonAssert::VERSION
|
8
|
+
s.authors = ["Frazer Horn"]
|
9
|
+
s.email = ["frazer.horn@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/frazerh/json_assert"
|
11
|
+
s.summary = %q{Easily handle JSON in Test::Unit}
|
12
|
+
s.description = %q{Easily handle JSON in Test::Unit}
|
13
|
+
|
14
|
+
s.rubyforge_project = "json_assert"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
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) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "multi_json", "~> 1.0.0"
|
22
|
+
s.add_development_dependency "rake", "~> 0.9"
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require "json"
|
2
|
+
require 'active_support/core_ext/hash/diff'
|
3
|
+
|
4
|
+
module JsonAssert
|
5
|
+
module Assertions
|
6
|
+
|
7
|
+
include JsonAssert::Helpers
|
8
|
+
include JsonAssert::Exclusion
|
9
|
+
|
10
|
+
def assert_json_eql(actual_json, expected_json, options={})
|
11
|
+
parse_options(options)
|
12
|
+
actual, expected = scrub(actual_json, @path), [scrub(expected_json)]
|
13
|
+
msg = build_message(@message, "<?> expected to be equal to\n<?>.", expected.inspect, actual.inspect)
|
14
|
+
assert_block(msg) { actual == expected.first }
|
15
|
+
end
|
16
|
+
|
17
|
+
def assert_json_not_eql(actual_json, expected_json, options={})
|
18
|
+
parse_options(options)
|
19
|
+
actual, expected = scrub(actual_json, @path), [scrub(expected_json)]
|
20
|
+
msg = build_message(@message, "<?> expected not to be equal to\n<?>.", expected.to_s, actual.to_s)
|
21
|
+
assert_block(msg) { actual != expected.first }
|
22
|
+
end
|
23
|
+
|
24
|
+
def assert_include_json(actual_json, expected_json, options={})
|
25
|
+
parse_options(options)
|
26
|
+
actual = parse_json(actual_json, @path)
|
27
|
+
expected = exclude_keys(parse_json(expected_json))
|
28
|
+
msg = build_message(@message, "<?> expected to include\n<?>.", expected.to_s, actual.to_s)
|
29
|
+
assert_block(msg) {
|
30
|
+
case actual
|
31
|
+
when Hash then actual.values.map{|v| exclude_keys(v) }.include?(expected)
|
32
|
+
when Array then actual.map{|e| exclude_keys(e) }.include?(expected)
|
33
|
+
else false
|
34
|
+
end
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def assert_json_path(json, path)
|
39
|
+
msg = "path was not found for #{path}"
|
40
|
+
assert_block(msg) do
|
41
|
+
begin
|
42
|
+
parse_json(json, path)
|
43
|
+
true
|
44
|
+
rescue JsonAssert::MissingPathError
|
45
|
+
false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def assert_not_json_path(json, path)
|
51
|
+
msg = "path was unexpectedly found for #{path}"
|
52
|
+
assert_block(msg) do
|
53
|
+
begin
|
54
|
+
parse_json(json, path)
|
55
|
+
false
|
56
|
+
rescue JsonAssert::MissingPathError
|
57
|
+
true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def assert_part_json(json, json_part, options={})
|
63
|
+
parse_options(options)
|
64
|
+
@json = parse_json(json, @path)
|
65
|
+
@json_part = parse_json(json_part)
|
66
|
+
msg = build_message(@message, "<?> was not part of \n<?>.", @json_part.to_s, @json.to_s)
|
67
|
+
assert_block(msg){ find_part_json == true }
|
68
|
+
end
|
69
|
+
|
70
|
+
def assert_not_part_json(json, json_part, options={})
|
71
|
+
parse_options(options)
|
72
|
+
@json = parse_json(json, @path)
|
73
|
+
@json_part = parse_json(json_part)
|
74
|
+
msg = build_message(@message, "<?> was unexpectedly part of \n<?>.", @json_part.to_s, @json.to_s)
|
75
|
+
assert_block(msg){ !find_part_json }
|
76
|
+
end
|
77
|
+
|
78
|
+
def assert_json_type(json, klass, options={})
|
79
|
+
parse_options(options)
|
80
|
+
actual = parse_json(json, @path)
|
81
|
+
msg = build_message(@message, "<?> expected to be a\n<?>.", actual.to_s, klass.to_s)
|
82
|
+
assert_block(msg) { actual.is_a?(klass) }
|
83
|
+
end
|
84
|
+
|
85
|
+
def assert_json_size(json, expected_size, options={})
|
86
|
+
parse_options(options)
|
87
|
+
@json = json
|
88
|
+
size = actual_size
|
89
|
+
msg = build_message(@message, "expected size was <?> but it was <?>.", expected_size.to_s, size.to_s)
|
90
|
+
assert_block(msg) { size == expected_size }
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def parse_options(options)
|
96
|
+
@message = options.delete(:message)
|
97
|
+
options.each{|k,v| self.send(k.to_s, v) }
|
98
|
+
end
|
99
|
+
|
100
|
+
def scrub(json, path = nil)
|
101
|
+
generate_normalized_json(exclude_keys(parse_json(json, path))).chomp + "\n"
|
102
|
+
end
|
103
|
+
|
104
|
+
def excluding(*keys)
|
105
|
+
excluded_keys.merge(keys.flatten.map{|k| k.to_s })
|
106
|
+
end
|
107
|
+
|
108
|
+
def including(*keys)
|
109
|
+
excluded_keys.subtract(keys.map{|k| k.to_s })
|
110
|
+
end
|
111
|
+
|
112
|
+
def at_path(path)
|
113
|
+
@path = path
|
114
|
+
end
|
115
|
+
|
116
|
+
def actual_size
|
117
|
+
ruby = parse_json(@json, @path)
|
118
|
+
ruby.is_a?(Enumerable) ? ruby.size : 1
|
119
|
+
end
|
120
|
+
|
121
|
+
def find_part_json
|
122
|
+
@json_part.each do |k,v|
|
123
|
+
return false unless find_key_value_pair(@json, k,v)
|
124
|
+
end
|
125
|
+
return true
|
126
|
+
end
|
127
|
+
|
128
|
+
def find_key_value_pair(json, key, value)
|
129
|
+
json.each do |k, v|
|
130
|
+
return true if k == key && v==value
|
131
|
+
if v.is_a?(Hash)
|
132
|
+
return true if find_key_value_pair(v, key, value)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
return false
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "set"
|
2
|
+
|
3
|
+
module JsonAssert
|
4
|
+
module Config
|
5
|
+
DEFAULT_EXCLUDED_KEYS = %w(id created_at updated_at)
|
6
|
+
|
7
|
+
def configure(&block)
|
8
|
+
instance_eval(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def excluded_keys
|
12
|
+
@excluded_keys ||= DEFAULT_EXCLUDED_KEYS
|
13
|
+
end
|
14
|
+
|
15
|
+
def excluded_keys=(keys)
|
16
|
+
@excluded_keys = keys.map{|k| k.to_s }.uniq
|
17
|
+
end
|
18
|
+
|
19
|
+
def exclude_keys(*keys)
|
20
|
+
self.excluded_keys = keys
|
21
|
+
end
|
22
|
+
|
23
|
+
def reset
|
24
|
+
instance_variables.each{|iv| remove_instance_variable(iv) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module JsonAssert
|
2
|
+
module Exclusion
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def exclude_keys(ruby)
|
6
|
+
case ruby
|
7
|
+
when Hash
|
8
|
+
ruby.sort.inject({}) do |hash, (key, value)|
|
9
|
+
hash[key] = exclude_keys(value) unless exclude_key?(key)
|
10
|
+
hash
|
11
|
+
end
|
12
|
+
when Array
|
13
|
+
ruby.map{|v| exclude_keys(v) }
|
14
|
+
else ruby
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def exclude_key?(key)
|
19
|
+
excluded_keys.include?(key)
|
20
|
+
end
|
21
|
+
|
22
|
+
def excluded_keys
|
23
|
+
@excluded_keys ||= Set.new(JsonAssert.excluded_keys)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module JsonAssert
|
4
|
+
module Helpers
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def parse_json(json, path = nil)
|
9
|
+
ruby = MultiJson.decode(%([#{json}])).first
|
10
|
+
value_at_json_path(ruby, path)
|
11
|
+
rescue MultiJson::DecodeError
|
12
|
+
MultiJson.decode(json)
|
13
|
+
end
|
14
|
+
|
15
|
+
def normalize_json(json, path = nil)
|
16
|
+
ruby = parse_json(json, path)
|
17
|
+
generate_normalized_json(ruby)
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate_normalized_json(ruby)
|
21
|
+
case ruby
|
22
|
+
when Hash, Array then JSON.pretty_generate(ruby)
|
23
|
+
else ruby.to_json
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def value_at_json_path(ruby, path)
|
30
|
+
return ruby unless path
|
31
|
+
|
32
|
+
json_path_to_keys(path).inject(ruby) do |value, key|
|
33
|
+
case value
|
34
|
+
when Hash, Array then value.fetch(key){ missing_json_path!(path) }
|
35
|
+
else missing_json_path!(path)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def json_path_to_keys(path)
|
41
|
+
path.split("/").map{|k| k =~ /^\d+$/ ? k.to_i : k }
|
42
|
+
end
|
43
|
+
|
44
|
+
def missing_json_path!(path)
|
45
|
+
raise JsonAssert::MissingPathError.new(path)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
data/lib/json_assert.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "json_assert/version"
|
2
|
+
require "json_assert/errors"
|
3
|
+
require "json_assert/config"
|
4
|
+
require "json_assert/exclusions"
|
5
|
+
require "json_assert/helpers"
|
6
|
+
require "json_assert/assertions"
|
7
|
+
|
8
|
+
module JsonAssert
|
9
|
+
# Your code goes here...
|
10
|
+
extend Config
|
11
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssertionsEqlTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include JsonAssert::Assertions
|
6
|
+
|
7
|
+
def test_identical_json
|
8
|
+
assert_json_eql %({"json":"spec"}), %({"json":"spec"})
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_differently_formatted_json
|
12
|
+
assert_json_eql %({"json": "spec"}), %({"json":"spec"})
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_out_of_order_hashes
|
16
|
+
assert_json_eql %({"laser":"lemon","json":"spec"}), %({"json":"spec","laser":"lemon"})
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_does_not_match_out_of_order_arrays
|
20
|
+
assert_json_not_eql %(["json","spec"]), %(["spec","json"])
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_valid_json_values_yet_invalid_json_documents
|
24
|
+
assert_json_eql %("json_spec"), %("json_spec")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_ignores_custom_excluded_hash_keys
|
28
|
+
JsonAssert.exclude_keys("ignore")
|
29
|
+
assert_json_eql %({"json":"spec","ignore":"please"}), %({"json":"spec"})
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_excludes_extra_hash_keys_per_matcher
|
33
|
+
JsonAssert.excluded_keys = %w(ignore)
|
34
|
+
assert_json_eql %({"id":1,"json":"spec","ignore":"please"}), %({"id":2,"json":"spec","ignore":"this"}), :excluding => "id"
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_excludes_extra_hash_keys_given_as_symbols
|
38
|
+
JsonAssert.excluded_keys = []
|
39
|
+
assert_json_eql %({"id":1,"json":"spec"}), %({"id":2,"json":"spec"}), :excluding => :id
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_excludes_multiple_keys
|
43
|
+
JsonAssert.excluded_keys = []
|
44
|
+
assert_json_eql %({"id":1,"json":"spec"}), %({"id":2,"json":"different"}), :excluding => [:id, :json]
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_includes_globally_excluded_hash_keys_per_matcher
|
48
|
+
JsonAssert.excluded_keys = %w(id ignore)
|
49
|
+
assert_json_not_eql %({"id":1,"json":"spec","ignore":"please"}), %({"id":2,"json":"spec","ignore":"this"}), :including => "id"
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_includes_globally_included_hash_keys_given_as_symbols
|
53
|
+
JsonAssert.excluded_keys = %w(id)
|
54
|
+
assert_json_not_eql %({"id":1,"json":"spec"}), %({"id":2,"json":"spec"}), :including => :id
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_includes_multiple_keys
|
58
|
+
JsonAssert.excluded_keys = %w(id json)
|
59
|
+
assert_json_eql %({"id":1,"json":"spec"}), %({"id":2,"json":"different"}), :including => [:id, :json]
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssertionsIncludeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include JsonAssert::Assertions
|
6
|
+
|
7
|
+
def test_included_array_elements
|
8
|
+
json = %(["one",1,1.0,true,false,null])
|
9
|
+
assert_include_json json, %("one")
|
10
|
+
assert_include_json json, %(1)
|
11
|
+
assert_include_json json, %(1.0)
|
12
|
+
assert_include_json json, %(true)
|
13
|
+
assert_include_json json, %(false)
|
14
|
+
assert_include_json json, %(null)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_an_array_included_in_an_array
|
18
|
+
json = %([[1,2,3],[4,5,6]])
|
19
|
+
assert_include_json(json, %([1,2,3]))
|
20
|
+
assert_include_json(json, %([4,5,6]))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_test_a_hash_included_in_an_array
|
24
|
+
json = %([{"one":1},{"two":2}])
|
25
|
+
assert_include_json json, %({"one":1})
|
26
|
+
assert_include_json json, %({"two":2})
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_an_array_included_in_an_array
|
30
|
+
json = %([[1,2,3],[4,5,6]])
|
31
|
+
assert_include_json json, %([1,2,3])
|
32
|
+
assert_include_json json, %([4,5,6])
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_a_hash_included_in_an_array
|
36
|
+
json = %([{"one":1},{"two":2}])
|
37
|
+
assert_include_json json, %({"one":1})
|
38
|
+
assert_include_json json, %({"two":2})
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_include_hash_values
|
42
|
+
json = %({"string":"one","integer":1,"float":1.0,"true":true,"false":false,"null":null})
|
43
|
+
assert_include_json json, %("one")
|
44
|
+
assert_include_json json, %(1)
|
45
|
+
assert_include_json json, %(1.0)
|
46
|
+
assert_include_json json, %(true)
|
47
|
+
assert_include_json json, %(false)
|
48
|
+
assert_include_json json, %(null)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_a_hash_included_in_a_hash
|
52
|
+
json = %({"one":{"two":3},"four":{"five":6}})
|
53
|
+
assert_include_json json, %({"two":3})
|
54
|
+
assert_include_json json, %({"five":6})
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_an_array_included_in_a_hash
|
58
|
+
json = %({"one":[2,3],"four":[5,6]})
|
59
|
+
assert_include_json json, %([2,3])
|
60
|
+
assert_include_json json, %([5,6])
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_at_a_path
|
64
|
+
assert_include_json %({"one":{"two":[3,4]}}), %([3,4]), :at_path => "one"
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_ignores_excluded_keys
|
68
|
+
assert_include_json %([{"id":1,"two":3}]), %({"two":3})
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssertionsEqlTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include JsonAssert::Assertions
|
6
|
+
|
7
|
+
def test_simple_json
|
8
|
+
assert_part_json %({"json":"assert","some":"other"}), %({"json":"assert"})
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_nested_json
|
12
|
+
assert_part_json %({"test":{"assert":"this"},"some":"other"}), %({"assert":"this"})
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_multiple_json
|
16
|
+
assert_part_json %({"test":"x", "assert":"this","some":"other","beta":7}), %({"assert":"this", "beta":7})
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_not_part_of_json
|
20
|
+
assert_not_part_json %({"json":"assert","some":"other"}), %({"sdsdsd":"sdsd"})
|
21
|
+
end
|
22
|
+
|
23
|
+
# TODO: What about arrays
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssertionsPathTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include JsonAssert::Assertions
|
6
|
+
|
7
|
+
def test_hash_keys
|
8
|
+
assert_json_path %({"one":{"two":{"three":4}}}), "one/two/three"
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_doesnt_match_values
|
12
|
+
assert_not_json_path %({"one":{"two":{"three":4}}}), "one/two/three/4"
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_array_indexes
|
16
|
+
assert_json_path %([1,[1,2,[1,2,3,4]]]), "1/2/3"
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_respects_null_array_values
|
20
|
+
assert_json_path %([null,[null,null,[null,null,null,null]]]), "1/2/3"
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_hash_keys_and_array_indexes
|
24
|
+
assert_json_path %({"one":[1,2,{"three":4}]}), "one/2/three"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssertionsSizeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include JsonAssert::Assertions
|
6
|
+
|
7
|
+
def test_counts_array_entries
|
8
|
+
assert_json_size %([1,2,3]), 3
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_counts_null_array_entries
|
12
|
+
assert_json_size %([1,null,3]), 3
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_counts_hash_key_value_pairs
|
16
|
+
assert_json_size %({"one":1,"two":2,"three":3}), 3
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_counts_null_hash_values
|
20
|
+
assert_json_size %({"one":1,"two":null,"three":3}), 3
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_at_a_path
|
24
|
+
assert_json_size %({"one":[1,2,3]}), 3, :at_path => "one"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class AssertionsTypeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include JsonAssert::Assertions
|
6
|
+
|
7
|
+
def test_hashes
|
8
|
+
assert_json_type %({}), Hash
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_arrays
|
12
|
+
assert_json_type %([]), Array
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_type_at_a_path
|
16
|
+
assert_json_type %({"root":[]}), Array, :at_path => "root"
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_strings
|
20
|
+
assert_json_type %(["json_spec"]), String, :at_path => "0"
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_a_valid_json_value_yet_invalid_json_document
|
24
|
+
assert_json_type %("json_spec"), String
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_empty_strings
|
28
|
+
assert_json_type %(""), String
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_integers
|
32
|
+
assert_json_type %(10), Integer
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_floats
|
36
|
+
assert_json_type %(10.0), Float
|
37
|
+
assert_json_type %(1e+1), Float
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_ancestor_classes
|
41
|
+
assert_json_type %(10), Numeric
|
42
|
+
assert_json_type %(10.0), Numeric
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
puts(if Object.const_defined? :RUBY_DESCRIPTION
|
2
|
+
RUBY_DESCRIPTION
|
3
|
+
else
|
4
|
+
"ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE} patchlevel #{RUBY_PATCHLEVEL}) [#{RUBY_PLATFORM}]"
|
5
|
+
end)
|
6
|
+
|
7
|
+
dir = File.dirname(__FILE__)
|
8
|
+
$LOAD_PATH.unshift "#{dir}/../lib"
|
9
|
+
|
10
|
+
require 'rubygems'
|
11
|
+
require 'test/unit'
|
12
|
+
require "pp"
|
13
|
+
require "json_assert"
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_assert
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Frazer Horn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-24 00:00:00.000000000 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: multi_json
|
17
|
+
requirement: &2168527320 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2168527320
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &2168526700 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2168526700
|
37
|
+
description: Easily handle JSON in Test::Unit
|
38
|
+
email:
|
39
|
+
- frazer.horn@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE.md
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- json_assert.gemspec
|
50
|
+
- lib/json_assert.rb
|
51
|
+
- lib/json_assert/assertions.rb
|
52
|
+
- lib/json_assert/config.rb
|
53
|
+
- lib/json_assert/errors.rb
|
54
|
+
- lib/json_assert/exclusions.rb
|
55
|
+
- lib/json_assert/helpers.rb
|
56
|
+
- lib/json_assert/version.rb
|
57
|
+
- test/assertions_eql_test.rb
|
58
|
+
- test/assertions_include_test.rb
|
59
|
+
- test/assertions_part_test.rb
|
60
|
+
- test/assertions_path_test.rb
|
61
|
+
- test/assertions_size_test.rb
|
62
|
+
- test/assertions_type_test.rb
|
63
|
+
- test/test_helper.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: https://github.com/frazerh/json_assert
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project: json_assert
|
85
|
+
rubygems_version: 1.6.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Easily handle JSON in Test::Unit
|
89
|
+
test_files:
|
90
|
+
- test/assertions_eql_test.rb
|
91
|
+
- test/assertions_include_test.rb
|
92
|
+
- test/assertions_part_test.rb
|
93
|
+
- test/assertions_path_test.rb
|
94
|
+
- test/assertions_size_test.rb
|
95
|
+
- test/assertions_type_test.rb
|
96
|
+
- test/test_helper.rb
|