Created
October 9, 2008 15:29
-
-
Save Peeja/15799 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module HaveAMatcher | |
class HaveA | |
def method_missing(attribute) | |
@attribute ||= attribute | |
self | |
end | |
def matches?(target) | |
@target = target | |
raise ArgumentError, "No attribute specified for have(:a)" unless @attribute | |
@value = @target.__send__(@attribute) | |
!(@value.nil? || @value.empty?) | |
end | |
def failure_message | |
"expected #{@target} to have #{an} #{@attribute}, but its #{@attribute} was #{@value.inspect}." | |
end | |
def negative_failure_message | |
"expected #{@target} not to have #{an} #{@attribute}, but its #{@attribute} was #{@value.inspect}." | |
end | |
def an | |
vowels = [?a, ?e, ?i, ?o, ?u] | |
vowels += [?A, ?E, ?I, ?O, ?U] | |
if vowels.include? @attribute.to_s[0] | |
"an" | |
else | |
"a" | |
end | |
end | |
end | |
def have(n=nil) | |
if n.nil? | |
HaveA.new | |
else | |
super | |
end | |
end | |
alias :have_a :have | |
alias :have_an :have | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment