Skip to content
This repository
Browse code

Refactor parsing/generation helpers

  • Loading branch information...
commit b814d0389eeab3c1451312dd8ff268a3873e67d8 1 parent aaa103e
Steve Richert laserlemon authored
2  lib/json_spec/cucumber.rb
@@ -7,7 +7,7 @@
7 7 end
8 8
9 9 When /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ do |path, key|
10   - JsonSpec.memorize(key, path ? json_at_path(last_json, path) : last_json)
  10 + JsonSpec.memorize(key, normalize_json(last_json, path))
11 11 end
12 12
13 13 Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be:$/ do |path, negative, json|
48 lib/json_spec/helpers.rb
@@ -2,39 +2,43 @@ module JsonSpec
2 2 module Helpers
3 3 extend self
4 4
5   - def json_at_path(json, path)
6   - pretty_json_value(ruby_at_json_path(json, path))
  5 + def parse_json(json, path = nil)
  6 + ruby = JSON.parse(%([#{json}])).first
  7 + value_at_json_path(ruby, path)
  8 + rescue JSON::ParserError
  9 + JSON.parse(json)
  10 + end
  11 +
  12 + def normalize_json(json, path = nil)
  13 + ruby = parse_json(json, path)
  14 + generate_normalized_json(ruby)
7 15 end
8 16
9   - def pretty_json_value(ruby)
  17 + def generate_normalized_json(ruby)
10 18 case ruby
11 19 when Hash, Array then JSON.pretty_generate(ruby)
12 20 else ruby.to_json
13 21 end
14 22 end
15 23
16   - def ruby_at_json_path(json, path)
17   - json_path_to_keys(path).inject(parse_json_value(json)) do |value, key|
18   - case value
19   - when Hash, Array then value.fetch(key){ missing_json_path!(path) }
20   - else missing_json_path!(path)
  24 + private
  25 + def value_at_json_path(ruby, path)
  26 + return ruby unless path
  27 +
  28 + json_path_to_keys(path).inject(ruby) do |value, key|
  29 + case value
  30 + when Hash, Array then value.fetch(key){ missing_json_path!(path) }
  31 + else missing_json_path!(path)
  32 + end
21 33 end
22 34 end
23   - end
24   -
25   - def json_path_to_keys(path)
26   - path.to_s.gsub(/(?:^\/|\/$)/, "").split("/").map{|k| k =~ /^\d+$/ ? k.to_i : k }
27   - end
28 35
29   - def parse_json_value(json)
30   - JSON.parse(%({"root":#{json}}))["root"]
31   - rescue JSON::ParserError
32   - # Re-raise more appropriate parsing error
33   - JSON.parse(json)
34   - end
  36 + def json_path_to_keys(path)
  37 + path.split("/").map{|k| k =~ /^\d+$/ ? k.to_i : k }
  38 + end
35 39
36   - def missing_json_path!(path)
37   - raise JsonSpec::MissingPathError.new(path)
38   - end
  40 + def missing_json_path!(path)
  41 + raise JsonSpec::MissingPathError.new(path)
  42 + end
39 43 end
40 44 end
10 lib/json_spec/matchers.rb
@@ -36,8 +36,7 @@
36 36 end
37 37
38 38 def scrub(json, path = nil)
39   - ruby = path ? ruby_at_json_path(json, path) : parse_json_value(json)
40   - pretty_json_value(exclude_keys(ruby)).chomp + "\n"
  39 + generate_normalized_json(exclude_keys(parse_json(json, path))).chomp + "\n"
41 40 end
42 41
43 42 def exclude_keys(ruby)
@@ -67,7 +66,7 @@ def excluded_keys
67 66
68 67 match do |json|
69 68 begin
70   - ruby_at_json_path(json, path)
  69 + parse_json(json, path)
71 70 true
72 71 rescue JsonSpec::MissingPathError
73 72 false
@@ -87,8 +86,7 @@ def excluded_keys
87 86 include JsonSpec::Helpers
88 87
89 88 match do |json|
90   - ruby = @path ? ruby_at_json_path(json, @path) : parse_json_value(json)
91   - ruby.is_a?(klass)
  89 + parse_json(json, @path).is_a?(klass)
92 90 end
93 91
94 92 chain :at_path do |path|
@@ -112,7 +110,7 @@ def excluded_keys
112 110 include JsonSpec::Helpers
113 111
114 112 match do |json|
115   - ruby = @path ? ruby_at_json_path(json, @path) : parse_json_value(json)
  113 + ruby = parse_json(json, @path)
116 114 actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
117 115 actual_size == expected_size
118 116 end
111 spec/json_spec/helpers_spec.rb
@@ -3,115 +3,72 @@
3 3 describe JsonSpec::Helpers do
4 4 include described_class
5 5
6   - context "parse_json_value" do
  6 + context "parse_json" do
7 7 it "parses JSON documents" do
8   - parse_json_value(%({"json":["spec"]})).should == {"json" => ["spec"]}
  8 + parse_json(%({"json":["spec"]})).should == {"json" => ["spec"]}
9 9 end
10 10
11 11 it "parses JSON values" do
12   - parse_json_value(%("json_spec")).should == "json_spec"
  12 + parse_json(%("json_spec")).should == "json_spec"
13 13 end
14 14
15 15 it "raises a parser error for invalid JSON" do
16   - expect{ parse_json_value("json_spec") }.to raise_error(JSON::ParserError)
  16 + expect{ parse_json("json_spec") }.to raise_error(JSON::ParserError)
17 17 end
18   - end
19   -
20   - context "json_path_to_keys" do
21   - it "splits on slashes" do
22   - json_path_to_keys("json/spec").should == ["json", "spec"]
23   - end
24   -
25   - it "converts digits to integers" do
26   - json_path_to_keys("json/spec/1/2/3").should == ["json", "spec", 1, 2, 3]
27   - end
28   -
29   - it "ignores leading and trailing slashes" do
30   - json_path_to_keys("/json/spec/").should == ["json", "spec"]
31   - end
32   - end
33   -
34   - context "ruby_at_json_path" do
35   - let(:json){ %({"json":["spec"]}) }
36 18
37   - it "parses JSON at a path" do
38   - ruby_at_json_path(json, "json").should == ["spec"]
39   - ruby_at_json_path(json, "json/0").should == "spec"
  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"
40 23 end
41 24
42 25 it "raises an error for a missing path" do
43   - %w(spec json/1 json/0/0).each do |path|
44   - expect{ ruby_at_json_path(json, path).to raise_error(JsonSpec::MissingPathError) }
  26 + json = %({"json":["spec"]})
  27 + %w(spec json/1).each do |path|
  28 + expect{ parse_json(json, path) }.to raise_error(JsonSpec::MissingPathError)
45 29 end
46 30 end
47 31 end
48 32
49   - context "pretty_json_value" do
50   - it "formats a hash" do
51   - pretty = <<-JSON
52   -{
53   - "json": "spec"
54   -}
55   - JSON
56   - pretty_json_value({"json" => "spec"}).should == pretty.chomp
57   - end
58   -
59   - it "formats an array" do
60   - pretty = <<-JSON
61   -[
62   - "json",
63   - "spec"
64   -]
65   - JSON
66   - pretty_json_value(["json", "spec"]).should == pretty.chomp
67   - end
68   -
69   - it "formats nested structures" do
70   - pretty = <<-JSON
  33 + context "normalize_json" do
  34 + it "normalizes a JSON document" do
  35 + normalized = <<-JSON
71 36 {
72 37 "json": [
73 38 "spec"
74 39 ]
75 40 }
76 41 JSON
77   - pretty_json_value({"json" => ["spec"]}).should == pretty.chomp
78   - end
79   -
80   - it "formats a string" do
81   - pretty_json_value("json_spec").should == %("json_spec")
82   - end
83   -
84   - it "formats an integer" do
85   - pretty_json_value(10).should == %(10)
86   - end
87   -
88   - it "formats a float" do
89   - pretty_json_value(10.0).should == %(10.0)
  42 + normalize_json(%({"json":["spec"]})).should == normalized.chomp
90 43 end
91 44
92   - it "formats true" do
93   - pretty_json_value(true).should == %(true)
  45 + it "normalizes at a path" do
  46 + normalize_json(%({"json":["spec"]}), "json/0").should == %("spec")
94 47 end
95 48
96   - it "formats false" do
97   - pretty_json_value(false).should == %(false)
  49 + it "accepts a JSON value" do
  50 + normalize_json(%("json_spec")).should == %("json_spec")
98 51 end
99 52
100   - it "formats nil" do
101   - pretty_json_value(nil).should == %(null)
  53 + it "normalizes JSON values" do
  54 + normalize_json(%(1e+1)).should == %(10.0)
102 55 end
103 56 end
104 57
105   - context "json_at_path" do
106   - it "formats inline JSON" do
107   - json = %({"json":["spec"]})
108   - array = <<-JSON
109   -[
110   - "spec"
111   -]
  58 + context "generate_normalized_json" do
  59 + it "generates a normalized JSON document" do
  60 + normalized = <<-JSON
  61 +{
  62 + "json": [
  63 + "spec"
  64 + ]
  65 +}
112 66 JSON
113   - json_at_path(json, "json").should == array.chomp
114   - json_at_path(json, "json/0").should == %("spec")
  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)
115 72 end
116 73 end
117 74 end

0 comments on commit b814d03

Please sign in to comment.
Something went wrong with that request. Please try again.