rspec_structure_matcher 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/have_structure_matcher.rb +7 -11
- data/lib/version.rb +1 -1
- data/rspec_structure_matcher.gemspec +1 -1
- data/spec/lib/have_structure_matcher_spec.rb +54 -0
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97089c3a4fd2b002689a6a2a3e09de64aa3ee1eb
|
4
|
+
data.tar.gz: 5ad3e4fddee55e825f44b509991fef636743d8c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a68a9bc2ab6232520560706094bad4694babf7ec0e2f9097f1648e74f210333101cd082a281aa04400fbc63e3dd602e8837c1fc2a67d7220bad967db3b64f530
|
7
|
+
data.tar.gz: 01b45d272f2e2069bfd914aa067ab20bbe76c0a130e807cbb32e6724761f25a254e94145be616ef301e5279331b7da4e276bf5f8a4000b8864a5a0442cddd9e0
|
data/README.md
CHANGED
@@ -61,12 +61,12 @@ This will pass if `video['episode_number']` is either `null` or `is_a?(Integer)`
|
|
61
61
|
Similar to optional values, testing deep strucutres has been kept as simple as possible. Simply define a new expected structure:
|
62
62
|
|
63
63
|
tv_show_expected_structure = {
|
64
|
-
title:
|
64
|
+
title: String
|
65
65
|
}
|
66
66
|
|
67
67
|
And then compare the structure as normal:
|
68
68
|
|
69
|
-
expect(video['tv_show'])
|
69
|
+
expect(video['tv_show']).to have_structure(tv_show_expected_structure)
|
70
70
|
|
71
71
|
## Contributing
|
72
72
|
|
@@ -1,23 +1,19 @@
|
|
1
1
|
RSpec::Matchers.define :have_structure do |expected|
|
2
2
|
match do |actual|
|
3
|
-
|
3
|
+
invalid_items(actual, expected).empty?
|
4
4
|
end
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
"missing or invalid keys: #{
|
6
|
+
failure_message do |actual|
|
7
|
+
invalid = invalid_items(actual, expected)
|
8
|
+
"missing or invalid keys: #{invalid.keys.join(', ')}\n\n#{invalid}"
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
expected.each do |key, value|
|
11
|
+
def invalid_items(actual, expected)
|
12
|
+
expected.each_with_object({}) do |(key, value), memo|
|
15
13
|
if !actual.has_key?(key.to_s) || !value_match(actual[key.to_s], value)
|
16
|
-
|
14
|
+
memo[key] = value
|
17
15
|
end
|
18
16
|
end
|
19
|
-
|
20
|
-
missing_items
|
21
17
|
end
|
22
18
|
|
23
19
|
def value_match(actual_value, expected_value)
|
data/lib/version.rb
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "rspec", "~>
|
21
|
+
spec.add_dependency "rspec", "~> 3.0"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rspec_structure_matcher'
|
2
|
+
|
3
|
+
describe 'have_structure' do
|
4
|
+
let(:expected_structure) {
|
5
|
+
{
|
6
|
+
'foo' => String,
|
7
|
+
'bar' => String
|
8
|
+
}
|
9
|
+
}
|
10
|
+
|
11
|
+
context 'when there are missing keys' do
|
12
|
+
let(:structure) {
|
13
|
+
{
|
14
|
+
'foo' => 'baz'
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
it 'raises the correct error' do
|
19
|
+
expect {
|
20
|
+
expect(structure).to have_structure(expected_structure)
|
21
|
+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /missing or invalid keys: bar/)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when there are invalid keys' do
|
26
|
+
let(:structure) {
|
27
|
+
{
|
28
|
+
'foo' => 1,
|
29
|
+
'bar' => 'baz'
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
it 'raises the correct error' do
|
34
|
+
expect {
|
35
|
+
expect(structure).to have_structure(expected_structure)
|
36
|
+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, /missing or invalid keys: foo/)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when there are no missing keys' do
|
41
|
+
let(:structure) {
|
42
|
+
{
|
43
|
+
'foo' => 'baz',
|
44
|
+
'bar' => 'baz'
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
it 'raise no error' do
|
49
|
+
expect {
|
50
|
+
expect(structure).to have_structure(expected_structure)
|
51
|
+
}.not_to raise_error
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_structure_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Marklove
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '3.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- lib/rspec_structure_matcher.rb
|
71
71
|
- lib/version.rb
|
72
72
|
- rspec_structure_matcher.gemspec
|
73
|
+
- spec/lib/have_structure_matcher_spec.rb
|
73
74
|
homepage: https://github.com/jjbananas/rspec_structure_matcher
|
74
75
|
licenses:
|
75
76
|
- MIT
|
@@ -94,4 +95,5 @@ rubygems_version: 2.1.11
|
|
94
95
|
signing_key:
|
95
96
|
specification_version: 4
|
96
97
|
summary: Rspec matchers for structured JSON responses.
|
97
|
-
test_files:
|
98
|
+
test_files:
|
99
|
+
- spec/lib/have_structure_matcher_spec.rb
|