json_spec 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/json_spec.gemspec +1 -1
- data/lib/json_spec/helpers.rb +5 -3
- data/lib/json_spec/matchers.rb +18 -9
- data/lib/json_spec/version.rb +1 -1
- data/spec/json_spec/helpers_spec.rb +1 -1
- data/spec/json_spec/matchers_spec.rb +34 -0
- metadata +13 -15
data/json_spec.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_dependency "
|
21
|
+
s.add_dependency "multi_json", "~> 1.0.0"
|
22
22
|
s.add_dependency "rspec", "~> 2.0"
|
23
23
|
|
24
24
|
s.add_development_dependency "rake", "~> 0.9"
|
data/lib/json_spec/helpers.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
1
3
|
module JsonSpec
|
2
4
|
module Helpers
|
3
5
|
extend self
|
4
6
|
|
5
7
|
def parse_json(json, path = nil)
|
6
|
-
ruby =
|
8
|
+
ruby = MultiJson.decode(%([#{json}])).first
|
7
9
|
value_at_json_path(ruby, path)
|
8
|
-
rescue
|
9
|
-
|
10
|
+
rescue MultiJson::DecodeError
|
11
|
+
MultiJson.decode(json)
|
10
12
|
end
|
11
13
|
|
12
14
|
def normalize_json(json, path = nil)
|
data/lib/json_spec/matchers.rb
CHANGED
@@ -17,7 +17,7 @@ RSpec::Matchers.define :be_json_eql do |expected_json|
|
|
17
17
|
end
|
18
18
|
|
19
19
|
chain :excluding do |*keys|
|
20
|
-
excluded_keys.
|
20
|
+
excluded_keys.merge(keys.map{|k| k.to_s })
|
21
21
|
end
|
22
22
|
|
23
23
|
chain :including do |*keys|
|
@@ -60,7 +60,7 @@ RSpec::Matchers.define :include_json do |expected_json|
|
|
60
60
|
end
|
61
61
|
|
62
62
|
chain :excluding do |*keys|
|
63
|
-
excluded_keys.
|
63
|
+
excluded_keys.merge(*keys.map{|k| k.to_s })
|
64
64
|
end
|
65
65
|
|
66
66
|
chain :including do |*keys|
|
@@ -105,7 +105,8 @@ RSpec::Matchers.define :have_json_type do |klass|
|
|
105
105
|
include JsonSpec::Helpers
|
106
106
|
|
107
107
|
match do |json|
|
108
|
-
|
108
|
+
@json = json
|
109
|
+
actual.is_a?(klass)
|
109
110
|
end
|
110
111
|
|
111
112
|
chain :at_path do |path|
|
@@ -113,24 +114,27 @@ RSpec::Matchers.define :have_json_type do |klass|
|
|
113
114
|
end
|
114
115
|
|
115
116
|
failure_message_for_should do
|
116
|
-
message = "Expected JSON value type to be #{klass}"
|
117
|
+
message = "Expected JSON value type to be #{klass}, got #{actual.class}"
|
117
118
|
message << %( at path "#{@path}") if @path
|
118
119
|
message
|
119
120
|
end
|
120
121
|
|
121
122
|
failure_message_for_should_not do
|
122
|
-
message = "Expected JSON value type to not be #{klass}"
|
123
|
+
message = "Expected JSON value type to not be #{klass}, got #{actual.class}"
|
123
124
|
message << %( at path "#{@path}") if @path
|
124
125
|
message
|
125
126
|
end
|
127
|
+
|
128
|
+
def actual
|
129
|
+
parse_json(@json, @path)
|
130
|
+
end
|
126
131
|
end
|
127
132
|
|
128
133
|
RSpec::Matchers.define :have_json_size do |expected_size|
|
129
134
|
include JsonSpec::Helpers
|
130
135
|
|
131
136
|
match do |json|
|
132
|
-
|
133
|
-
actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
|
137
|
+
@json = json
|
134
138
|
actual_size == expected_size
|
135
139
|
end
|
136
140
|
|
@@ -139,14 +143,19 @@ RSpec::Matchers.define :have_json_size do |expected_size|
|
|
139
143
|
end
|
140
144
|
|
141
145
|
failure_message_for_should do
|
142
|
-
message = "Expected JSON value size to be #{expected_size}"
|
146
|
+
message = "Expected JSON value size to be #{expected_size}, got #{actual_size}"
|
143
147
|
message << %( at path "#{@path}") if @path
|
144
148
|
message
|
145
149
|
end
|
146
150
|
|
147
151
|
failure_message_for_should_not do
|
148
|
-
message = "Expected JSON value size to not be #{expected_size}"
|
152
|
+
message = "Expected JSON value size to not be #{expected_size}, got #{actual_size}"
|
149
153
|
message << %( at path "#{@path}") if @path
|
150
154
|
message
|
151
155
|
end
|
156
|
+
|
157
|
+
def actual_size
|
158
|
+
ruby = parse_json(@json, @path)
|
159
|
+
ruby.is_a?(Enumerable) ? ruby.size : 1
|
160
|
+
end
|
152
161
|
end
|
data/lib/json_spec/version.rb
CHANGED
@@ -13,7 +13,7 @@ describe JsonSpec::Helpers do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "raises a parser error for invalid JSON" do
|
16
|
-
expect{ parse_json("json_spec") }.to raise_error(
|
16
|
+
expect{ parse_json("json_spec") }.to raise_error(MultiJson::DecodeError)
|
17
17
|
end
|
18
18
|
|
19
19
|
it "parses at a path if given" do
|
@@ -67,6 +67,11 @@ describe "Matchers:" do
|
|
67
67
|
%({"id":1,"json":"spec"}).should be_json_eql(%({"id":2,"json":"spec"})).excluding(:id)
|
68
68
|
end
|
69
69
|
|
70
|
+
it "excludes multiple keys" do
|
71
|
+
JsonSpec.excluded_keys = []
|
72
|
+
%({"id":1,"json":"spec"}).should be_json_eql(%({"id":2,"json":"different"})).excluding(:id, :json)
|
73
|
+
end
|
74
|
+
|
70
75
|
it "includes globally-excluded hash keys per matcher" do
|
71
76
|
JsonSpec.excluded_keys = %w(id ignore)
|
72
77
|
%({"id":1,"json":"spec","ignore":"please"}).should_not be_json_eql(%({"id":2,"json":"spec","ignore":"this"})).including("id")
|
@@ -76,6 +81,11 @@ describe "Matchers:" do
|
|
76
81
|
JsonSpec.excluded_keys = %w(id)
|
77
82
|
%({"id":1,"json":"spec"}).should_not be_json_eql(%({"id":2,"json":"spec"})).including(:id)
|
78
83
|
end
|
84
|
+
|
85
|
+
it "includes multiple keys" do
|
86
|
+
JsonSpec.excluded_keys = %w(id json)
|
87
|
+
%({"id":1,"json":"spec"}).should_not be_json_eql(%({"id":2,"json":"different"})).including(:id, :json)
|
88
|
+
end
|
79
89
|
end
|
80
90
|
|
81
91
|
context "include_json" do
|
@@ -152,6 +162,18 @@ describe "Matchers:" do
|
|
152
162
|
it "matches at a path" do
|
153
163
|
%({"one":[1,2,3]}).should have_json_size(3).at_path("one")
|
154
164
|
end
|
165
|
+
|
166
|
+
it "provides a failure message for should" do
|
167
|
+
matcher = have_json_size(3)
|
168
|
+
matcher.matches?(%([1,2]))
|
169
|
+
matcher.failure_message_for_should.should == "Expected JSON value size to be 3, got 2"
|
170
|
+
end
|
171
|
+
|
172
|
+
it "provides a failure message for should not" do
|
173
|
+
matcher = have_json_size(3)
|
174
|
+
matcher.matches?(%([1,2,3]))
|
175
|
+
matcher.failure_message_for_should_not.should == "Expected JSON value size to not be 3, got 3"
|
176
|
+
end
|
155
177
|
end
|
156
178
|
|
157
179
|
context "have_json_path" do
|
@@ -215,6 +237,18 @@ describe "Matchers:" do
|
|
215
237
|
%(10.0).should have_json_type(Numeric)
|
216
238
|
end
|
217
239
|
|
240
|
+
it "provides a failure message for should" do
|
241
|
+
matcher = have_json_type(Numeric)
|
242
|
+
matcher.matches?(%("foo"))
|
243
|
+
matcher.failure_message_for_should.should == "Expected JSON value type to be Numeric, got String"
|
244
|
+
end
|
245
|
+
|
246
|
+
it "provides a failure message for should not" do
|
247
|
+
matcher = have_json_type(Numeric)
|
248
|
+
matcher.matches?(%(10))
|
249
|
+
matcher.failure_message_for_should_not.should == "Expected JSON value type to not be Numeric, got Fixnum"
|
250
|
+
end
|
251
|
+
|
218
252
|
context "somewhat uselessly" do
|
219
253
|
it "matches true" do
|
220
254
|
%(true).should have_json_type(TrueClass)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,23 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
13
|
-
default_executable:
|
12
|
+
date: 2011-08-19 00:00:00.000000000Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
17
|
-
requirement: &
|
15
|
+
name: multi_json
|
16
|
+
requirement: &70258292680600 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ~>
|
21
20
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
21
|
+
version: 1.0.0
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *70258292680600
|
26
25
|
- !ruby/object:Gem::Dependency
|
27
26
|
name: rspec
|
28
|
-
requirement: &
|
27
|
+
requirement: &70258292680100 !ruby/object:Gem::Requirement
|
29
28
|
none: false
|
30
29
|
requirements:
|
31
30
|
- - ~>
|
@@ -33,10 +32,10 @@ dependencies:
|
|
33
32
|
version: '2.0'
|
34
33
|
type: :runtime
|
35
34
|
prerelease: false
|
36
|
-
version_requirements: *
|
35
|
+
version_requirements: *70258292680100
|
37
36
|
- !ruby/object:Gem::Dependency
|
38
37
|
name: rake
|
39
|
-
requirement: &
|
38
|
+
requirement: &70258292710600 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
41
40
|
requirements:
|
42
41
|
- - ~>
|
@@ -44,10 +43,10 @@ dependencies:
|
|
44
43
|
version: '0.9'
|
45
44
|
type: :development
|
46
45
|
prerelease: false
|
47
|
-
version_requirements: *
|
46
|
+
version_requirements: *70258292710600
|
48
47
|
- !ruby/object:Gem::Dependency
|
49
48
|
name: cucumber
|
50
|
-
requirement: &
|
49
|
+
requirement: &70258292710140 !ruby/object:Gem::Requirement
|
51
50
|
none: false
|
52
51
|
requirements:
|
53
52
|
- - ~>
|
@@ -55,7 +54,7 @@ dependencies:
|
|
55
54
|
version: '1.0'
|
56
55
|
type: :development
|
57
56
|
prerelease: false
|
58
|
-
version_requirements: *
|
57
|
+
version_requirements: *70258292710140
|
59
58
|
description: Easily handle JSON in RSpec and Cucumber
|
60
59
|
email:
|
61
60
|
- steve.richert@gmail.com
|
@@ -92,7 +91,6 @@ files:
|
|
92
91
|
- spec/json_spec/matchers_spec.rb
|
93
92
|
- spec/json_spec/memory_spec.rb
|
94
93
|
- spec/spec_helper.rb
|
95
|
-
has_rdoc: true
|
96
94
|
homepage: https://github.com/collectiveidea/json_spec
|
97
95
|
licenses: []
|
98
96
|
post_install_message:
|
@@ -113,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
111
|
version: '0'
|
114
112
|
requirements: []
|
115
113
|
rubyforge_project: json_spec
|
116
|
-
rubygems_version: 1.
|
114
|
+
rubygems_version: 1.8.8
|
117
115
|
signing_key:
|
118
116
|
specification_version: 3
|
119
117
|
summary: Easily handle JSON in RSpec and Cucumber
|