Created
August 8, 2009 20:20
-
-
Save btelles/164491 to your computer and use it in GitHub Desktop.
Rspec matcher that tests whether strings exist in a file
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
class HaveALineWithMatcher | |
# HaveALineWithMatcher is a simple rspec matcher that tests whether strings exist in a file | |
# | |
# = Usage | |
# | |
# 1. place this file in your spec/support directory. | |
# | |
# 2. use it in your specs: | |
# | |
# "/path/to/file".should have_a_line_with "some string" | |
# "/path/to/file".should have_a_line_with /some regular expression/ | |
# | |
def initialize(reg_exp) | |
if reg_exp.instance_of? String | |
@reg_exp = Regexp.new(Regexp.escape(reg_exp)) | |
else | |
@reg_exp = reg_exp | |
end | |
end | |
def matches?(target_file) | |
@target_file = target_file | |
File.open(@target_file).each do |line| | |
return true if line =~ @reg_exp | |
end | |
false | |
end | |
def failure_message | |
"expected to find #{@reg_exp.source} in #{@target_file}, but didn't" | |
end | |
def negative_failure_message | |
"expected not to find #{@reg_exp.source} in #{@target_file}, but it did" | |
end | |
end | |
def have_a_line_with(expected) | |
HaveALineWithMatcher.new(expected) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment