forked from hamcrest/ramcrest
/
has_attribute.rb
38 lines (32 loc) · 1.04 KB
/
has_attribute.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'ramcrest/match'
require 'ramcrest/anything'
module Ramcrest
module HasAttribute
module_function
def has_attribute(attribute_name, value_matcher = Ramcrest::Anything.anything())
Matcher.new(attribute_name, value_matcher)
end
class Matcher
def initialize(attribute_name, value_matcher)
@attribute_name = attribute_name
@value_matcher = value_matcher
end
def matches?(actual)
if actual.respond_to?(@attribute_name)
match = @value_matcher.matches?(actual.send(@attribute_name))
if match.matched?
return success
else
return mismatch("object <#{actual}> attribute '#{@attribute_name}' #{match.description}")
end
else
return mismatch("object <#{actual}> was missing attribute '#{@attribute_name}'")
end
end
def description
"an object with an attribute named '#{@attribute_name}' and value #{@value_matcher.description}"
end
include Ramcrest::Match
end
end
end