Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 107 lines (88 sloc) 2.848 kB
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
1 require "spec_helper"
2
3 describe JsonSpec::Helpers do
4 include described_class
5
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
6 context "parse_json" do
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
7 it "parses JSON documents" do
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
8 parse_json(%({"json":["spec"]})).should == {"json" => ["spec"]}
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
9 end
10
11 it "parses JSON values" do
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
12 parse_json(%("json_spec")).should == "json_spec"
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
13 end
14
15 it "raises a parser error for invalid JSON" do
53baed9 @jeroenvandijk Allow the user to decide which json library to use by using MultiJson
jeroenvandijk authored
16 expect{ parse_json("json_spec") }.to raise_error(MultiJson::DecodeError)
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
17 end
18
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
19 it "parses at a path if given" do
20 json = %({"json":["spec"]})
21 parse_json(json, "json").should == ["spec"]
22 parse_json(json, "json/0").should == "spec"
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
23 end
24
25 it "raises an error for a missing path" do
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
26 json = %({"json":["spec"]})
27 %w(spec json/1).each do |path|
ac6d63f @laserlemon Clean up errors and make them inherit from a common error class
laserlemon authored
28 expect{ parse_json(json, path) }.to raise_error(JsonSpec::MissingPath)
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
29 end
30 end
31 end
32
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
33 context "normalize_json" do
34 it "normalizes a JSON document" do
35 normalized = <<-JSON
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
36 {
37 "json": [
38 "spec"
39 ]
40 }
41 JSON
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
42 normalize_json(%({"json":["spec"]})).should == normalized.chomp
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
43 end
44
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
45 it "normalizes at a path" do
46 normalize_json(%({"json":["spec"]}), "json/0").should == %("spec")
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
47 end
48
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
49 it "accepts a JSON value" do
50 normalize_json(%("json_spec")).should == %("json_spec")
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
51 end
52
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
53 it "normalizes JSON values" do
54 normalize_json(%(1e+1)).should == %(10.0)
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
55 end
56 end
57
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
58 context "generate_normalized_json" do
59 it "generates a normalized JSON document" do
60 normalized = <<-JSON
61 {
62 "json": [
63 "spec"
64 ]
65 }
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
66 JSON
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
67 generate_normalized_json({"json" => ["spec"]}).should == normalized.chomp
68 end
69
70 it "generates a normalized JSON value" do
71 generate_normalized_json(nil).should == %(null)
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
72 end
73 end
c47e143 @bleonard Add RSpec helpers and matchers that load JSON from files
bleonard authored
74
75 context "load_json_file" do
76 it "raises an error when no directory is set" do
ac6d63f @laserlemon Clean up errors and make them inherit from a common error class
laserlemon authored
77 expect{ load_json("one.json") }.to raise_error(JsonSpec::MissingDirectory)
c47e143 @bleonard Add RSpec helpers and matchers that load JSON from files
bleonard authored
78 end
79
80 it "returns JSON when the file exists" do
81 JsonSpec.directory = files_path
82 load_json("one.json").should == %({"value":"from_file"})
83 end
84
85 it "ignores extra slashes" do
86 JsonSpec.directory = "/#{files_path}/"
87 load_json("one.json").should == %({"value":"from_file"})
88 end
89
90 it "raises an error when the file doesn't exist" do
91 JsonSpec.directory = files_path
ac6d63f @laserlemon Clean up errors and make them inherit from a common error class
laserlemon authored
92 expect{ load_json("bogus.json") }.to raise_error(JsonSpec::MissingFile)
c47e143 @bleonard Add RSpec helpers and matchers that load JSON from files
bleonard authored
93 end
94
95 it "raises an error when the directory doesn't exist" do
96 JsonSpec.directory = "#{files_path}_bogus"
ac6d63f @laserlemon Clean up errors and make them inherit from a common error class
laserlemon authored
97 expect{ load_json("one.json") }.to raise_error(JsonSpec::MissingFile)
c47e143 @bleonard Add RSpec helpers and matchers that load JSON from files
bleonard authored
98 end
99
100 it "finds nested files" do
101 JsonSpec.directory = files_path
102 load_json("project/one.json").should == %({"nested":"inside_folder"})
103 load_json("project/version/one.json").should == %({"nested":"deeply"})
104 end
105 end
aaa103e @laserlemon Add specs for helper methods
laserlemon authored
106 end
Something went wrong with that request. Please try again.