forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
481631c
commit fd16caa
Showing
9 changed files
with
257 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Shoulda # :nodoc: | ||
module Matchers | ||
module ActiveModel # :nodoc: | ||
class BlankValue | ||
|
||
def initialize(instance, attribute) | ||
@instance = instance | ||
@attribute = attribute | ||
end | ||
|
||
def value | ||
if collection? | ||
[] | ||
else | ||
nil | ||
end | ||
end | ||
|
||
private | ||
def collection? | ||
if reflection | ||
[:has_many, :has_and_belongs_to_many].include?(reflection.macro) | ||
else | ||
false | ||
end | ||
end | ||
|
||
def reflection | ||
@instance.class.respond_to?(:reflect_on_association) && | ||
@instance.class.reflect_on_association(@attribute) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Shoulda # :nodoc: | ||
module Matchers | ||
module ActiveModel # :nodoc: | ||
class CompositeMatcher < ValidationMatcher | ||
def initialize(attribute) | ||
@sub_matchers = [] | ||
super | ||
end | ||
|
||
def add_matcher(matcher) | ||
@sub_matchers << matcher | ||
self | ||
end | ||
|
||
def matches?(subject) | ||
sub_matchers_match?(subject) | ||
end | ||
|
||
def description | ||
"No description" | ||
end | ||
|
||
def sub_matchers_match?(subject) | ||
if @sub_matchers.empty? | ||
true | ||
else | ||
@sub_matchers.all? { |matcher| matcher.matches?(subject) } | ||
end | ||
end | ||
|
||
def sub_matcher_descriptions | ||
@sub_matchers.map(&:description) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require 'spec_helper' | ||
|
||
|
||
describe Shoulda::Matchers::ActiveModel::BlankValue do | ||
it "returns an array for collections" do | ||
model = define_model :parent do | ||
has_many :children | ||
end.new | ||
BlankValue.new(model, :children).value.should == [] | ||
end | ||
|
||
it "returns nil for non collections" do | ||
model = define_active_model_class("Example", :accessors => [:attr]).new | ||
BlankValue.new(model, :attr).value.should be nil | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
describe Shoulda::Matchers::ActiveModel::CompositeMatcher do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'spec_helper' | ||
|
||
describe Shoulda::Matchers::ActiveModel::ValidationMatcher do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
require 'spec_helper' | ||
|
||
describe Shoulda::Matchers::ActiveModel::WithMessageMatcher do | ||
context 'when attribute is invalid' do | ||
it 'matches when the error message matches' do | ||
attribute = :age | ||
non_numeric_value = 'a string' | ||
expected_message = "oh no" | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => expected_message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, non_numeric_value, expected_message) | ||
matcher.matches?(model).should be_true | ||
end | ||
|
||
it 'does not match when the error message does not match' do | ||
attribute = :age | ||
non_numeric_value = 'a string' | ||
actual_message = 'for real' | ||
expected_message = 'not matching' | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => actual_message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, non_numeric_value, expected_message) | ||
matcher.matches?(model).should be_false | ||
end | ||
|
||
it 'does not match when the expected error message is a substring of the actual one' do | ||
attribute = :age | ||
non_numeric_value = 'a string' | ||
actual_message = 'substring' | ||
expected_message = 'sub' | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => actual_message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, non_numeric_value, expected_message) | ||
matcher.matches?(model).should be_false | ||
end | ||
end | ||
|
||
context 'when attribute is valid' do | ||
it 'does not match even when error message matches' do | ||
attribute = :age | ||
good_value = 1 | ||
message = "oh no" | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, good_value, message) | ||
matcher.matches?(model).should be_false | ||
end | ||
|
||
it 'does not match when error message does not match' do | ||
attribute = :age | ||
good_value = 1 | ||
actual_message = 'for real' | ||
expected_message = 'not matching' | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => actual_message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, good_value, expected_message) | ||
matcher.matches?(model).should be_false | ||
end | ||
end | ||
|
||
context 'given a regex to match against' do | ||
it 'matches when error message matches regex' do | ||
attribute = :age | ||
non_numeric_value = 'a string' | ||
message = 'foo bar' | ||
regex_matching_message = /foo/ | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, non_numeric_value, regex_matching_message) | ||
matcher.matches?(model).should be_true | ||
end | ||
end | ||
|
||
context '#failure_message' do | ||
it 'provides a failure message' do | ||
attribute = :age | ||
non_numeric_value = 'a string' | ||
actual_message = 'for real' | ||
expected_message = 'not matching' | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute]) do | ||
validates attribute, :numericality => { :message => actual_message } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, non_numeric_value, expected_message) | ||
matcher.matches?(model) | ||
matcher.failure_message.should == "Expected #{expected_message} got #{actual_message}" | ||
end | ||
|
||
it 'is correct when model has more than one error' do | ||
attribute = :age | ||
other_attribute = :name | ||
non_numeric_value = 'a string' | ||
actual_message = 'for real' | ||
expected_message = 'not matching' | ||
|
||
model = define_active_model_class(:example, :accessors => [attribute, other_attribute]) do | ||
validates attribute, :numericality => { :message => actual_message } | ||
validates other_attribute, :presence => { :message => 'other message' } | ||
end.new | ||
|
||
matcher = WithMessageMatcher.new(attribute, non_numeric_value, expected_message) | ||
matcher.matches?(model) | ||
matcher.failure_message.should == "Expected #{expected_message} got #{actual_message}, other message" | ||
end | ||
end | ||
end |