Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #63

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/json_spec.rb
@@ -1,8 +1,9 @@
require "json"
require "rspec"
require "json_spec/errors"
require "json_spec/at_path"
require "json_spec/configuration"
require "json_spec/exclusion"
require "json_spec/errors"
require "json_spec/helpers"
require "json_spec/messages"
require "json_spec/matchers"
Expand Down
10 changes: 10 additions & 0 deletions lib/json_spec/at_path.rb
@@ -0,0 +1,10 @@
module JsonSpec
module AtPath
extend self

def at_path(path)
@path = path
self
end
end
end
62 changes: 17 additions & 45 deletions lib/json_spec/cucumber.rb
Expand Up @@ -2,6 +2,14 @@

World(JsonSpec::Helpers, JsonSpec::Matchers)

def _match(negative, matcher)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method is being defined in the main object space, we should probably name it something less collision-prone like _json_spec_match.

if negative
last_json.should_not matcher
else
last_json.should matcher
end
end

After do
JsonSpec.forget
end
Expand All @@ -11,51 +19,27 @@
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be:$/ do |path, negative, json|
if negative
last_json.should_not be_json_eql(JsonSpec.remember(json)).at_path(path)
else
last_json.should be_json_eql(JsonSpec.remember(json)).at_path(path)
end
_match negative, be_json_eql(JsonSpec.remember(json)).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be file "(.+)"$/ do |path, negative, file_path|
if negative
last_json.should_not be_json_eql.to_file(file_path).at_path(path)
else
last_json.should be_json_eql.to_file(file_path).at_path(path)
end
_match negative, be_json_eql.to_file(file_path).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ do |path, negative, value|
if negative
last_json.should_not be_json_eql(JsonSpec.remember(value)).at_path(path)
else
last_json.should be_json_eql(JsonSpec.remember(value)).at_path(path)
end
_match negative, be_json_eql(JsonSpec.remember(value)).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include:$/ do |path, negative, json|
if negative
last_json.should_not include_json(JsonSpec.remember(json)).at_path(path)
else
last_json.should include_json(JsonSpec.remember(json)).at_path(path)
end
_match negative, include_json(JsonSpec.remember(json)).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include file "(.+)"$/ do |path, negative, file_path|
if negative
last_json.should_not include_json.from_file(file_path).at_path(path)
else
last_json.should include_json.from_file(file_path).at_path(path)
end
_match negative, include_json.from_file(file_path).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? include (".*"|\-?\d+(?:\.\d+)?(?:[eE][\+\-]?\d+)?|\[.*\]|%?\{.*\}|true|false|null)$/ do |path, negative, value|
if negative
last_json.should_not include_json(JsonSpec.remember(value)).at_path(path)
else
last_json.should include_json(JsonSpec.remember(value)).at_path(path)
end
_match negative, include_json(JsonSpec.remember(value)).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should have the following:$/ do |base, table|
Expand All @@ -71,25 +55,13 @@
end

Then /^the (?:JSON|json)(?: response)? should( not)? have "(.*)"$/ do |negative, path|
if negative
last_json.should_not have_json_path(path)
else
last_json.should have_json_path(path)
end
_match negative, have_json_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? be an? (.*)$/ do |path, negative, type|
if negative
last_json.should_not have_json_type(type).at_path(path)
else
last_json.should have_json_type(type).at_path(path)
end
_match negative, have_json_type(type).at_path(path)
end

Then /^the (?:JSON|json)(?: response)?(?: at "(.*)")? should( not)? have (\d+)/ do |path, negative, size|
if negative
last_json.should_not have_json_size(size.to_i).at_path(path)
else
last_json.should have_json_size(size.to_i).at_path(path)
end
_match negative, have_json_size(size.to_i).at_path(path)
end
10 changes: 10 additions & 0 deletions lib/json_spec/exclusion.rb
Expand Up @@ -2,6 +2,16 @@ module JsonSpec
module Exclusion
extend self

def excluding(*keys)
excluded_keys.merge(keys.map(&:to_s))
self
end

def including(*keys)
excluded_keys.subtract(keys.map(&:to_s))
self
end

def exclude_keys(ruby)
case ruby
when Hash
Expand Down
16 changes: 1 addition & 15 deletions lib/json_spec/matchers/be_json_eql.rb
@@ -1,6 +1,7 @@
module JsonSpec
module Matchers
class BeJsonEql
include JsonSpec::AtPath
include JsonSpec::Helpers
include JsonSpec::Exclusion
include JsonSpec::Messages
Expand All @@ -22,26 +23,11 @@ def matches?(actual_json)
@actual == @expected
end

def at_path(path)
@path = path
self
end

def to_file(path)
@expected_json = load_json(path)
self
end

def excluding(*keys)
excluded_keys.merge(keys.map(&:to_s))
self
end

def including(*keys)
excluded_keys.subtract(keys.map(&:to_s))
self
end

def failure_message_for_should
message_with_path("Expected equivalent JSON")
end
Expand Down
6 changes: 1 addition & 5 deletions lib/json_spec/matchers/have_json_size.rb
@@ -1,6 +1,7 @@
module JsonSpec
module Matchers
class HaveJsonSize
include JsonSpec::AtPath
include JsonSpec::Helpers
include JsonSpec::Messages

Expand All @@ -14,11 +15,6 @@ def matches?(json)
@actual == @expected
end

def at_path(path)
@path = path
self
end

def failure_message_for_should
message_with_path("Expected JSON value size to be #{@expected}, got #{@actual}")
end
Expand Down
6 changes: 1 addition & 5 deletions lib/json_spec/matchers/have_json_type.rb
@@ -1,6 +1,7 @@
module JsonSpec
module Matchers
class HaveJsonType
include JsonSpec::AtPath
include JsonSpec::Helpers
include JsonSpec::Messages

Expand All @@ -13,11 +14,6 @@ def matches?(json)
@classes.any?{|c| c === @ruby }
end

def at_path(path)
@path = path
self
end

def failure_message_for_should
message_with_path("Expected JSON value type to be #{@classes.join(", ")}, got #{@ruby.class}")
end
Expand Down
16 changes: 1 addition & 15 deletions lib/json_spec/matchers/include_json.rb
@@ -1,6 +1,7 @@
module JsonSpec
module Matchers
class IncludeJson
include JsonSpec::AtPath
include JsonSpec::Helpers
include JsonSpec::Exclusion
include JsonSpec::Messages
Expand All @@ -22,26 +23,11 @@ def matches?(actual_json)
end
end

def at_path(path)
@path = path
self
end

def from_file(path)
@expected_json = load_json(path)
self
end

def excluding(*keys)
excluded_keys.merge(keys.map(&:to_s))
self
end

def including(*keys)
excluded_keys.subtract(keys.map(&:to_s))
self
end

def failure_message_for_should
message_with_path("Expected included JSON")
end
Expand Down
2 changes: 2 additions & 0 deletions spec/json_spec/configuration_spec.rb
@@ -1,6 +1,8 @@
require "spec_helper"

describe JsonSpec::Configuration do
before { JsonSpec.reset }

it "excludes id and timestamps by default" do
JsonSpec.excluded_keys.should == ["id", "created_at", "updated_at"]
end
Expand Down
2 changes: 2 additions & 0 deletions spec/json_spec/memory_spec.rb
@@ -1,6 +1,8 @@
require "spec_helper"

describe JsonSpec::Memory do
before { JsonSpec.reset }

it "has a memory" do
JsonSpec.memory.should == {}
end
Expand Down
3 changes: 0 additions & 3 deletions spec/spec_helper.rb
@@ -1,9 +1,6 @@
require "json_spec"

RSpec.configure do |config|
config.before do
JsonSpec.reset
end
end

def files_path
Expand Down