Skip to content
This repository
Browse code

Add specs for helper methods

  • Loading branch information...
commit aaa103e647c6e1be3bd6ee8e8153644076499949 1 parent 75dbb3a
Steve Richert laserlemon authored

Showing 1 changed file with 117 additions and 0 deletions. Show diff stats Hide diff stats

  1. +117 0 spec/json_spec/helpers_spec.rb
117 spec/json_spec/helpers_spec.rb
... ... @@ -0,0 +1,117 @@
  1 +require "spec_helper"
  2 +
  3 +describe JsonSpec::Helpers do
  4 + include described_class
  5 +
  6 + context "parse_json_value" do
  7 + it "parses JSON documents" do
  8 + parse_json_value(%({"json":["spec"]})).should == {"json" => ["spec"]}
  9 + end
  10 +
  11 + it "parses JSON values" do
  12 + parse_json_value(%("json_spec")).should == "json_spec"
  13 + end
  14 +
  15 + it "raises a parser error for invalid JSON" do
  16 + expect{ parse_json_value("json_spec") }.to raise_error(JSON::ParserError)
  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 +
  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"
  40 + end
  41 +
  42 + 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) }
  45 + end
  46 + end
  47 + end
  48 +
  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
  71 +{
  72 + "json": [
  73 + "spec"
  74 + ]
  75 +}
  76 + 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)
  90 + end
  91 +
  92 + it "formats true" do
  93 + pretty_json_value(true).should == %(true)
  94 + end
  95 +
  96 + it "formats false" do
  97 + pretty_json_value(false).should == %(false)
  98 + end
  99 +
  100 + it "formats nil" do
  101 + pretty_json_value(nil).should == %(null)
  102 + end
  103 + end
  104 +
  105 + context "json_at_path" do
  106 + it "formats inline JSON" do
  107 + json = %({"json":["spec"]})
  108 + array = <<-JSON
  109 +[
  110 + "spec"
  111 +]
  112 + JSON
  113 + json_at_path(json, "json").should == array.chomp
  114 + json_at_path(json, "json/0").should == %("spec")
  115 + end
  116 + end
  117 +end

0 comments on commit aaa103e

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