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

"should include_json_expression" matchers #8

Closed
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/json_expressions/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ def wildcard_matcher

Object.infect_an_assertion :assert_json_match, :must_match_json_expression
Object.infect_an_assertion :refute_json_match, :wont_match_json_expression
Object.infect_an_assertion :assert_json_include, :must_include_json_expression
Object.infect_an_assertion :refute_json_include, :wont_include_json_expression
8 changes: 8 additions & 0 deletions lib/json_expressions/minitest/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ def refute_json_match(exp, act, msg = nil)
# Return the matcher
return exp
end

def assert_json_include(exp, act, msg = nil)
assert_json_match exp.forgiving!, act, msg
end

def refute_json_include(exp, act, msg = nil)
refute_json_match exp.forgiving!, act, msg
end
end
end
3 changes: 2 additions & 1 deletion lib/json_expressions/rspec/matchers.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'json_expressions/rspec/matchers/match_json_expression'
require 'json_expressions/rspec/matchers/match_json_expression'
require 'json_expressions/rspec/matchers/include_json_expression'
25 changes: 25 additions & 0 deletions lib/json_expressions/rspec/matchers/include_json_expression.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'json'

module JsonExpressions
module RSpec
module Matchers
class IncludeJsonExpression < MatchJsonExpression
def initialize(expected)
super expected.forgiving!
end

def failure_message_for_should
"expected #{@target.inspect} to include JSON expression #{@expected.inspect}\n" + @expected.last_error
end

def failure_message_for_should_not
"expected #{@target.inspect} not to include JSON expression #{@expected.inspect}"
end
end

def include_json_expression(expected)
IncludeJsonExpression.new(expected)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'
require 'json_expressions/rspec'

module JsonExpressions
module RSpec
describe Matchers, "#include_json_expression" do
before(:each) do
@hash = {
l1_string: 'Hello world!',
l1_regexp: '0xC0FFEE',
l1_object: {
l2_string: 'Hi there!',
}
}
end

it "ignores extra elements" do
@hash.should include_json_expression(l1_string: 'Hello world!')
end

it "still uses matchers" do
@hash.should include_json_expression(l1_regexp: /\A0x[0-9a-f]+\z/i, l1_object: { l2_string: String })
end

it "catches missing elements" do
@hash.should_not include_json_expression(l1_string: 'Hello world!', element_should_be_here: "uh oh!")
end

it "catches nonmatching elements" do
@hash.should_not include_json_expression(l1_string: 'Wrong string!')
end

it "works with JSON strings" do
positive_json_string = '{"l1_string":"Hello world!","l1_regexp":"0xC0FFEE","l1_object":{"l2_string":"Hi there!"}}'
negative_json_string = '{"l1_string":"Wrong","l1_regexp":"0xC0FFEE","l1_object":{"l2_string":"Hi there!"}}'

positive_json_string.should include_json_expression(l1_string: 'Hello world!')
negative_json_string.should_not include_json_expression(l1_string: 'Hello world!')
end
end
end
end
20 changes: 20 additions & 0 deletions test/json_expressions/minitest/test_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ def test_refute_json_match
})
end

def test_assert_json_include
assert_json_include({l1_regexp: /\A0x[0-9a-f]+\z/i},{
l1_string: 'Hello world!',
l1_regexp: '0xC0FFEE',
l1_object: {
l2_string: 'Hi there!',
}
})
end

def test_refute_json_include
refute_json_include({l1_regexp: /\A0x[0-9]+\z/i},{
l1_string: 'Hello world!',
l1_regexp: '0xC0FFEE',
l1_object: {
l2_string: 'Hi there!',
}
})
end

def test_json_match_with_json_string
assert_json_match @pattern, '{"l1_string":"Hello world!","l1_regexp":"0xC0FFEE","l1_boolean":false,"l1_module":1.1,"l1_wildcard":true,"l1_array":["l1: Hello world",1,true,null,false],"l1_object":{"l2_string":"Hi there!","l2_regexp":"1234-5678-1234-5678","l2_boolean":true,"l2_module":[1,2,3,4],"l2_wildcard":"Whatever","l2_array":["l2: Hello world",2,true,null,"Whatever"],"l2_object":{"l3_string":"Good day...","l3_regexp":"","l3_boolean":false,"l3_module":"This is like... inception!","l3_wildcard":null,"l3_array":["l3: Hello world",3,true,null,[]]}}}'
assert_raises(::MiniTest::Assertion) { assert_json_match @pattern, '{"l1_string":"Hello world!","l1_regexp":"0xC0FFEE","l1_boolean":false,"l1_module":1.1,"l1_wildcard":true,"l1_array":["l1: Hello world",1,true,null,false],"l1_object":{"l2_string":"Hi there!","l2_regexp":"1234-5678-1234-5678","l2_boolean":true,"l2_module":[1,2,3,4],"l2_wildcard":"Whatever","l2_array":["l2: Hello world",2,true,null,"Whatever"],"l2_object":{"l3_string":"Good day...","l3_regexp":"","l3_boolean":false,"l3_module":"This is like... inception!","l3_wildcard":null,"l3_array":["***THIS SHOULD BREAK THINGS***",3,true,null,[]]}}}' }
Expand Down
36 changes: 36 additions & 0 deletions test/json_expressions/test_minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,40 @@ def test_wildcard_matcher_defined
@non_matching_json_string.wont_match_json_expression @pattern
assert_raises(::MiniTest::Assertion) { @matching_json.wont_match_json_expression @pattern }
end

describe '#must_include_json_expression' do
before do
@pattern = {
l1_string: 'Hello world!',
}
@non_matching_json[:l1_string] = "Oops!"
end
it 'should pass when the json matches the pattern' do
@matching_json.must_include_json_expression @pattern
end

it 'should raise an exception when the json does not match the pattern' do
assert_raises(::MiniTest::Assertion) do
@non_matching_json.must_include_json_expression @pattern
end
end
end

describe '#wont_include_json_expression' do
before do
@pattern = {
l1_string: 'Hello world!',
}
@non_matching_json[:l1_string] = "Oops!"
end
it 'should pass when the json matches the pattern' do
@non_matching_json.wont_include_json_expression @pattern
end

it 'should raise an exception when the json does not match the pattern' do
assert_raises(::MiniTest::Assertion) do
@matching_json.wont_include_json_expression @pattern
end
end
end
end