Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 75 lines (62 sloc) 1.755 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
b814d03 @laserlemon Refactor parsing/generation helpers
laserlemon authored
16 expect{ parse_json("json_spec") }.to raise_error(JSON::ParserError)
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|
28 expect{ parse_json(json, path) }.to raise_error(JsonSpec::MissingPathError)
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
74 end
Something went wrong with that request. Please try again.