Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add RSpec helpers and matchers that load JSON from files

  • Loading branch information...
commit c47e1439fbfca3aea968ddf2b7299283b13f6c21 1 parent 25f6a64
@bleonard bleonard authored laserlemon committed
View
1  features/support/files/one.json
@@ -0,0 +1 @@
+{"value":"from_file"}
View
1  features/support/files/project/one.json
@@ -0,0 +1 @@
+{"nested":"inside_folder"}
View
18 features/support/files/project/two.json
@@ -0,0 +1,18 @@
+{
+ "id": null,
+ "one": [
+ 1
+ ],
+ "three": [
+ 1,
+ 2,
+ 3
+ ],
+ "two": [
+ 1,
+ 2
+ ],
+ "zero": [
+
+ ]
+}
View
1  features/support/files/project/version/one.json
@@ -0,0 +1 @@
+{"nested":"deeply"}
View
13 features/support/files/project/version/two.json
@@ -0,0 +1,13 @@
+{
+ "array": [
+
+ ],
+ "false": false,
+ "float": 10.0,
+ "hash": {
+ },
+ "integer": 10,
+ "null": null,
+ "string": "json_spec",
+ "true": true
+}
View
24 features/support/files/two.json
@@ -0,0 +1,24 @@
+{
+ "array": [
+ "json",
+ "spec"
+ ],
+ "created_at": "2011-07-08 02:27:34",
+ "empty_array": [
+
+ ],
+ "empty_hash": {
+ },
+ "false": false,
+ "float": 10.0,
+ "hash": {
+ "json": "spec"
+ },
+ "id": 1,
+ "integer": 10,
+ "negative": -10,
+ "null": null,
+ "string": "json_spec",
+ "true": true,
+ "updated_at": "2011-07-08 02:28:50"
+}
View
8 lib/json_spec/configuration.rb
@@ -20,6 +20,14 @@ def exclude_keys(*keys)
self.excluded_keys = keys
end
+ def directory
+ @directory
+ end
+
+ def directory=(directory)
+ @directory = directory
+ end
+
def reset
instance_variables.each{|iv| remove_instance_variable(iv) }
end
View
18 lib/json_spec/errors.rb
@@ -10,4 +10,22 @@ def to_s
%(Missing JSON path "#{path}")
end
end
+
+ class MissingDirectoryError < StandardError
+ def to_s
+ "No JsonSpec.directory set"
+ end
+ end
+
+ class MissingFileError < StandardError
+ attr_reader :path
+
+ def initialize(path)
+ @path = path
+ end
+
+ def to_s
+ "No JSON file at #{path}"
+ end
+ end
end
View
15 lib/json_spec/helpers.rb
@@ -23,6 +23,13 @@ def generate_normalized_json(ruby)
end
end
+ def load_json(relative_path)
+ missing_json_directory! if JsonSpec.directory.nil?
+ path = File.join(JsonSpec.directory, relative_path)
+ missing_json_file!(path) unless File.exist?(path)
+ File.read(path)
+ end
+
private
def value_at_json_path(ruby, path)
return ruby unless path
@@ -42,5 +49,13 @@ def json_path_to_keys(path)
def missing_json_path!(path)
raise JsonSpec::MissingPathError.new(path)
end
+
+ def missing_json_directory!
+ raise JsonSpec::MissingDirectoryError
+ end
+
+ def missing_json_file!(path)
+ raise JsonSpec::MissingFileError.new(path)
+ end
end
end
View
22 lib/json_spec/matchers.rb
@@ -13,11 +13,13 @@ def diffable?
true
end
- def initialize(expected_json)
+ def initialize(expected_json = nil)
@expected_json = expected_json
end
def matches?(actual_json)
+ raise "Expected equivalent JSON not provided" if @expected_json.nil?
+
@actual, @expected = scrub(actual_json, @path), scrub(@expected_json)
@actual == @expected
end
@@ -27,6 +29,11 @@ def at_path(path)
self
end
+ def to_file(path)
+ @expected_json = load_json(path)
+ self
+ end
+
def excluding(*keys)
excluded_keys.merge(keys.map{|k| k.to_s })
self
@@ -65,11 +72,13 @@ class IncludeJson
include JsonSpec::Helpers
include JsonSpec::Exclusion
- def initialize(expected_json)
+ def initialize(expected_json = nil)
@expected_json = expected_json
end
def matches?(actual_json)
+ raise "Expected included JSON not provided" if @expected_json.nil?
+
actual = parse_json(actual_json, @path)
expected = exclude_keys(parse_json(@expected_json))
case actual
@@ -84,6 +93,11 @@ def at_path(path)
self
end
+ def from_file(path)
+ @expected_json = load_json(path)
+ self
+ end
+
def excluding(*keys)
excluded_keys.merge(keys.map{|k| k.to_s })
self
@@ -215,11 +229,11 @@ def description
end
end
- def be_json_eql(json)
+ def be_json_eql(json = nil)
JsonSpec::Matchers::BeJsonEql.new(json)
end
- def include_json(json)
+ def include_json(json = nil)
JsonSpec::Matchers::IncludeJson.new(json)
end
View
10 spec/json_spec/configuration_spec.rb
@@ -49,4 +49,14 @@
JsonSpec.reset
JsonSpec.excluded_keys.should == original
end
+
+ it "resets its directory" do
+ JsonSpec.directory.should be_nil
+
+ JsonSpec.directory = "/"
+ JsonSpec.directory.should_not be_nil
+
+ JsonSpec.reset
+ JsonSpec.directory.should be_nil
+ end
end
View
34 spec/json_spec/helpers_spec.rb
@@ -71,4 +71,38 @@
generate_normalized_json(nil).should == %(null)
end
end
+
+ context "load_json_file" do
+ let(:files_path){ File.expand_path("../../../features/support/files", __FILE__) }
+
+ it "raises an error when no directory is set" do
+ expect{ load_json("one.json") }.to raise_error(JsonSpec::MissingDirectoryError)
+ end
+
+ it "returns JSON when the file exists" do
+ JsonSpec.directory = files_path
+ load_json("one.json").should == %({"value":"from_file"})
+ end
+
+ it "ignores extra slashes" do
+ JsonSpec.directory = "/#{files_path}/"
+ load_json("one.json").should == %({"value":"from_file"})
+ end
+
+ it "raises an error when the file doesn't exist" do
+ JsonSpec.directory = files_path
+ expect{ load_json("bogus.json") }.to raise_error(JsonSpec::MissingFileError)
+ end
+
+ it "raises an error when the directory doesn't exist" do
+ JsonSpec.directory = "#{files_path}_bogus"
+ expect{ load_json("one.json") }.to raise_error(JsonSpec::MissingFileError)
+ end
+
+ it "finds nested files" do
+ JsonSpec.directory = files_path
+ load_json("project/one.json").should == %({"nested":"inside_folder"})
+ load_json("project/version/one.json").should == %({"nested":"deeply"})
+ end
+ end
end
View
20 spec/json_spec/matchers_spec.rb
@@ -1,6 +1,8 @@
require "spec_helper"
describe "Matchers:" do
+ let(:files_path){ File.expand_path("../../../features/support/files", __FILE__) }
+
context "be_json_eql" do
it "matches identical JSON" do
%({"json":"spec"}).should be_json_eql(%({"json":"spec"}))
@@ -98,6 +100,15 @@
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(equal JSON at path "json/0")
end
+
+ it "raises an error when not given expected JSON" do
+ expect{ %({"id":1,"json":"spec"}).should be_json_eql }.to raise_error
+ end
+
+ it "matches file contents" do
+ JsonSpec.directory = files_path
+ %({ "value" : "from_file" }).should be_json_eql.to_file("one.json")
+ end
end
context "include_json" do
@@ -164,6 +175,15 @@
matcher.matches?(%({"id":1,"json":["spec"]}))
matcher.description.should == %(include JSON at path "json/0")
end
+
+ it "raises an error when not given expected JSON" do
+ expect{ %([{"id":1,"two":3}]).should include_json }.to raise_error
+ end
+
+ it "matches file contents" do
+ JsonSpec.directory = files_path
+ %({"one":{"value":"from_file"},"four":{"five":6}}).should include_json.from_file("one.json")
+ end
end
context "have_json_size" do
Please sign in to comment.
Something went wrong with that request. Please try again.