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

Add haveJsonValue matcher #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json_spec
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.1.4
Binary file added json_spec-1.1.4.gem
Binary file not shown.
5 changes: 5 additions & 0 deletions lib/json_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "json_spec/matchers/include_json"
require "json_spec/matchers/have_json_path"
require "json_spec/matchers/have_json_type"
require "json_spec/matchers/have_json_value"
require "json_spec/matchers/have_json_size"

module JsonSpec
Expand All @@ -22,6 +23,10 @@ def have_json_type(type)
JsonSpec::Matchers::HaveJsonType.new(type)
end

def have_json_value(value)
JsonSpec::Matchers::HaveJsonValue.new(value)
end

def have_json_size(size)
JsonSpec::Matchers::HaveJsonSize.new(size)
end
Expand Down
36 changes: 36 additions & 0 deletions lib/json_spec/matchers/have_json_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module JsonSpec
module Matchers
class HaveJsonValue
include JsonSpec::Helpers
include JsonSpec::Messages

def initialize(value)
@value = value
end

def matches?(json)
@ruby = parse_json(json, @path)
@value == @ruby
end

def at_path(path)
@path = path
self
end

def failure_message
message_with_path("Expected JSON value to be \"#{@value}\", got \"#{@ruby}\"")
end
alias :failure_message_for_should :failure_message

def failure_message_when_negated
message_with_path("Expected JSON value to not be \"#{@value}\", got \"#{@ruby}\"")
end
alias :failure_message_for_should_not :failure_message_when_negated

def description
message_with_path(%(have JSON value "#{@value}"))
end
end
end
end
41 changes: 41 additions & 0 deletions spec/json_spec/matchers/have_json_value_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "spec_helper"

describe JsonSpec::Matchers::HaveJsonValue do
it "matches value at root path" do
%("bar").should have_json_value("bar")
end

it "matches value at path" do
%({"foo": "bar"}).should have_json_value("bar").at_path("foo")
end

it "matches value at deep path" do
%({"foo": {"pipe": "bar"}}).should have_json_value("bar").at_path("foo/pipe")
end

it "provides a failure message" do
matcher = have_json_value("pipe")
matcher.matches?(%("bar"))
matcher.failure_message.should == "Expected JSON value to be \"pipe\", got \"bar\""
matcher.failure_message_for_should.should == "Expected JSON value to be \"pipe\", got \"bar\"" # RSpec 2 interface
end

it "provides a failure message for negation" do
matcher = have_json_value("pipe")
matcher.matches?(%("bar"))
matcher.failure_message_when_negated.should == "Expected JSON value to not be \"pipe\", got \"bar\""
matcher.failure_message_for_should_not.should == "Expected JSON value to not be \"pipe\", got \"bar\"" # RSpec 2 interface
end

it "provides a description message" do
matcher = have_json_value("pipe")
matcher.matches?(%("pipe"))
matcher.description.should == %(have JSON value "pipe")
end

it "provides a description message with path" do
matcher = have_json_value("pipe").at_path("json")
matcher.matches?(%({"id":1,"json":"pipe"}))
matcher.description.should == %(have JSON value "pipe" at path "json")
end
end