Permalink
Browse files

Refactor parsing/generation helpers

  • Loading branch information...
1 parent aaa103e commit b814d0389eeab3c1451312dd8ff268a3873e67d8 @laserlemon laserlemon committed Jul 9, 2011
Showing with 65 additions and 106 deletions.
  1. +1 −1 lib/json_spec/cucumber.rb
  2. +26 −22 lib/json_spec/helpers.rb
  3. +4 −6 lib/json_spec/matchers.rb
  4. +34 −77 spec/json_spec/helpers_spec.rb
@@ -7,7 +7,7 @@
end
When /^(?:I )?keep the (?:JSON|json)(?: response)?(?: at "(.*)")? as "(.*)"$/ do |path, key|
- JsonSpec.memorize(key, path ? json_at_path(last_json, path) : last_json)
+ JsonSpec.memorize(key, normalize_json(last_json, path))
end
Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be:$/ do |path, negative, json|
View
@@ -2,39 +2,43 @@ module JsonSpec
module Helpers
extend self
- def json_at_path(json, path)
- pretty_json_value(ruby_at_json_path(json, path))
+ def parse_json(json, path = nil)
+ ruby = JSON.parse(%([#{json}])).first
+ value_at_json_path(ruby, path)
+ rescue JSON::ParserError
+ JSON.parse(json)
+ end
+
+ def normalize_json(json, path = nil)
+ ruby = parse_json(json, path)
+ generate_normalized_json(ruby)
end
- def pretty_json_value(ruby)
+ def generate_normalized_json(ruby)
case ruby
when Hash, Array then JSON.pretty_generate(ruby)
else ruby.to_json
end
end
- def ruby_at_json_path(json, path)
- json_path_to_keys(path).inject(parse_json_value(json)) do |value, key|
- case value
- when Hash, Array then value.fetch(key){ missing_json_path!(path) }
- else missing_json_path!(path)
+ private
+ def value_at_json_path(ruby, path)
+ return ruby unless path
+
+ json_path_to_keys(path).inject(ruby) do |value, key|
+ case value
+ when Hash, Array then value.fetch(key){ missing_json_path!(path) }
+ else missing_json_path!(path)
+ end
end
end
- end
-
- def json_path_to_keys(path)
- path.to_s.gsub(/(?:^\/|\/$)/, "").split("/").map{|k| k =~ /^\d+$/ ? k.to_i : k }
- end
- def parse_json_value(json)
- JSON.parse(%({"root":#{json}}))["root"]
- rescue JSON::ParserError
- # Re-raise more appropriate parsing error
- JSON.parse(json)
- end
+ def json_path_to_keys(path)
+ path.split("/").map{|k| k =~ /^\d+$/ ? k.to_i : k }
+ end
- def missing_json_path!(path)
- raise JsonSpec::MissingPathError.new(path)
- end
+ def missing_json_path!(path)
+ raise JsonSpec::MissingPathError.new(path)
+ end
end
end
View
@@ -36,8 +36,7 @@
end
def scrub(json, path = nil)
- ruby = path ? ruby_at_json_path(json, path) : parse_json_value(json)
- pretty_json_value(exclude_keys(ruby)).chomp + "\n"
+ generate_normalized_json(exclude_keys(parse_json(json, path))).chomp + "\n"
end
def exclude_keys(ruby)
@@ -67,7 +66,7 @@ def excluded_keys
match do |json|
begin
- ruby_at_json_path(json, path)
+ parse_json(json, path)
true
rescue JsonSpec::MissingPathError
false
@@ -87,8 +86,7 @@ def excluded_keys
include JsonSpec::Helpers
match do |json|
- ruby = @path ? ruby_at_json_path(json, @path) : parse_json_value(json)
- ruby.is_a?(klass)
+ parse_json(json, @path).is_a?(klass)
end
chain :at_path do |path|
@@ -112,7 +110,7 @@ def excluded_keys
include JsonSpec::Helpers
match do |json|
- ruby = @path ? ruby_at_json_path(json, @path) : parse_json_value(json)
+ ruby = parse_json(json, @path)
actual_size = ruby.is_a?(Enumerable) ? ruby.size : 1
actual_size == expected_size
end
@@ -3,115 +3,72 @@
describe JsonSpec::Helpers do
include described_class
- context "parse_json_value" do
+ context "parse_json" do
it "parses JSON documents" do
- parse_json_value(%({"json":["spec"]})).should == {"json" => ["spec"]}
+ parse_json(%({"json":["spec"]})).should == {"json" => ["spec"]}
end
it "parses JSON values" do
- parse_json_value(%("json_spec")).should == "json_spec"
+ parse_json(%("json_spec")).should == "json_spec"
end
it "raises a parser error for invalid JSON" do
- expect{ parse_json_value("json_spec") }.to raise_error(JSON::ParserError)
+ expect{ parse_json("json_spec") }.to raise_error(JSON::ParserError)
end
- end
-
- context "json_path_to_keys" do
- it "splits on slashes" do
- json_path_to_keys("json/spec").should == ["json", "spec"]
- end
-
- it "converts digits to integers" do
- json_path_to_keys("json/spec/1/2/3").should == ["json", "spec", 1, 2, 3]
- end
-
- it "ignores leading and trailing slashes" do
- json_path_to_keys("/json/spec/").should == ["json", "spec"]
- end
- end
-
- context "ruby_at_json_path" do
- let(:json){ %({"json":["spec"]}) }
- it "parses JSON at a path" do
- ruby_at_json_path(json, "json").should == ["spec"]
- ruby_at_json_path(json, "json/0").should == "spec"
+ it "parses at a path if given" do
+ json = %({"json":["spec"]})
+ parse_json(json, "json").should == ["spec"]
+ parse_json(json, "json/0").should == "spec"
end
it "raises an error for a missing path" do
- %w(spec json/1 json/0/0).each do |path|
- expect{ ruby_at_json_path(json, path).to raise_error(JsonSpec::MissingPathError) }
+ json = %({"json":["spec"]})
+ %w(spec json/1).each do |path|
+ expect{ parse_json(json, path) }.to raise_error(JsonSpec::MissingPathError)
end
end
end
- context "pretty_json_value" do
- it "formats a hash" do
- pretty = <<-JSON
-{
- "json": "spec"
-}
- JSON
- pretty_json_value({"json" => "spec"}).should == pretty.chomp
- end
-
- it "formats an array" do
- pretty = <<-JSON
-[
- "json",
- "spec"
-]
- JSON
- pretty_json_value(["json", "spec"]).should == pretty.chomp
- end
-
- it "formats nested structures" do
- pretty = <<-JSON
+ context "normalize_json" do
+ it "normalizes a JSON document" do
+ normalized = <<-JSON
{
"json": [
"spec"
]
}
JSON
- pretty_json_value({"json" => ["spec"]}).should == pretty.chomp
- end
-
- it "formats a string" do
- pretty_json_value("json_spec").should == %("json_spec")
- end
-
- it "formats an integer" do
- pretty_json_value(10).should == %(10)
- end
-
- it "formats a float" do
- pretty_json_value(10.0).should == %(10.0)
+ normalize_json(%({"json":["spec"]})).should == normalized.chomp
end
- it "formats true" do
- pretty_json_value(true).should == %(true)
+ it "normalizes at a path" do
+ normalize_json(%({"json":["spec"]}), "json/0").should == %("spec")
end
- it "formats false" do
- pretty_json_value(false).should == %(false)
+ it "accepts a JSON value" do
+ normalize_json(%("json_spec")).should == %("json_spec")
end
- it "formats nil" do
- pretty_json_value(nil).should == %(null)
+ it "normalizes JSON values" do
+ normalize_json(%(1e+1)).should == %(10.0)
end
end
- context "json_at_path" do
- it "formats inline JSON" do
- json = %({"json":["spec"]})
- array = <<-JSON
-[
- "spec"
-]
+ context "generate_normalized_json" do
+ it "generates a normalized JSON document" do
+ normalized = <<-JSON
+{
+ "json": [
+ "spec"
+ ]
+}
JSON
- json_at_path(json, "json").should == array.chomp
- json_at_path(json, "json/0").should == %("spec")
+ generate_normalized_json({"json" => ["spec"]}).should == normalized.chomp
+ end
+
+ it "generates a normalized JSON value" do
+ generate_normalized_json(nil).should == %(null)
end
end
end

0 comments on commit b814d03

Please sign in to comment.