/
include_json_spec.rb
82 lines (69 loc) · 2.47 KB
/
include_json_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require "spec_helper"
describe JsonSpec::Matchers::IncludeJson do
it "matches included array elements" do
json = %(["one",1,1.0,true,false,null])
json.should include_json(%("one"))
json.should include_json(%(1))
json.should include_json(%(1.0))
json.should include_json(%(true))
json.should include_json(%(false))
json.should include_json(%(null))
end
it "matches an array included in an array" do
json = %([[1,2,3],[4,5,6]])
json.should include_json(%([1,2,3]))
json.should include_json(%([4,5,6]))
end
it "matches a hash included in an array" do
json = %([{"one":1},{"two":2}])
json.should include_json(%({"one":1}))
json.should include_json(%({"two":2}))
end
it "matches included hash values" do
json = %({"string":"one","integer":1,"float":1.0,"true":true,"false":false,"null":null})
json.should include_json(%("one"))
json.should include_json(%(1))
json.should include_json(%(1.0))
json.should include_json(%(true))
json.should include_json(%(false))
json.should include_json(%(null))
end
it "matches a hash included in a hash" do
json = %({"one":{"two":3},"four":{"five":6}})
json.should include_json(%({"two":3}))
json.should include_json(%({"five":6}))
end
it "matches an array included in a hash" do
json = %({"one":[2,3],"four":[5,6]})
json.should include_json(%([2,3]))
json.should include_json(%([5,6]))
end
it "matches a substring" do
json = %("json")
json.should include_json(%("js"))
json.should include_json(%("json"))
end
it "matches at a path" do
%({"one":{"two":[3,4]}}).should include_json(%([3,4])).at_path("one")
end
it "ignores excluded keys" do
%([{"id":1,"two":3}]).should include_json(%({"two":3}))
end
it "provides a description message" do
matcher = include_json(%({"json":"spec"}))
matcher.matches?(%({"id":1,"json":"spec"}))
matcher.description.should == "include JSON"
end
it "provides a description message with path" do
matcher = include_json(%("spec")).at_path("json/0")
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(include JSON at path "json/0")
end
it "raises an error when not given expected JSON" do
expect{ %([{"id":1,"two":3}]).should include_json }.to raise_error
end
it "matches file contents" do
JsonSpec.directory = files_path
%({"one":{"value":"from_file"},"four":{"five":6}}).should include_json.from_file("one.json")
end
end