-
-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add description generation for any Has matcher
- Loading branch information
1 parent
e14e87f
commit a7b702d
Showing
4 changed files
with
79 additions
and
59 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,35 +1,39 @@ | |||
module RSpec | module RSpec | ||
module Matchers | module Matchers | ||
|
|||
class Has | class Has | ||
|
|||
def initialize(expected, *args) | def initialize(expected, *args) | ||
@expected, @args = expected, args | @expected, @args = expected, args | ||
end | end | ||
|
|||
def matches?(actual) | def matches?(actual) | ||
actual.__send__(predicate(@expected), *@args) | actual.__send__(predicate(@expected), *@args) | ||
end | end | ||
|
|||
def failure_message_for_should | def failure_message_for_should | ||
"expected ##{predicate(@expected)}(#{@args[0].inspect}) to return true, got false" | "expected ##{predicate(@expected)}(#{@args[0].inspect}) to return true, got false" | ||
end | end | ||
|
|||
def failure_message_for_should_not | def failure_message_for_should_not | ||
"expected ##{predicate(@expected)}(#{@args[0].inspect}) to return false, got true" | "expected ##{predicate(@expected)}(#{@args[0].inspect}) to return false, got true" | ||
end | end | ||
|
|||
def description | def description | ||
"have key #{@args[0].inspect}" | [method_description(@expected), args_description(@args)].compact.join(' ') | ||
end | end | ||
|
|||
private | private | ||
|
|||
def predicate(sym) | def predicate(sym) | ||
"#{sym.to_s.sub("have_","has_")}?".to_sym | "#{sym.to_s.sub("have_","has_")}?".to_sym | ||
end | end | ||
|
|
||
def method_description(method) | |||
method.to_s.gsub('_', ' ') | |||
end | |||
|
|||
def args_description(args) | |||
return nil if args.empty? | |||
args.map { |arg| arg.inspect }.join(', ') | |||
end | |||
end | 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
Oops, something went wrong.