Skip to content

Commit

Permalink
Fix RegexpFilter, notably used for mspec run -p/-P and test it
Browse files Browse the repository at this point in the history
* Avoid inheritance, it just makes things more complex.
  • Loading branch information
eregon committed Jul 22, 2018
1 parent cef236f commit 072849e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
24 changes: 20 additions & 4 deletions lib/mspec/runner/filters/regexp.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
require 'mspec/runner/filters/match'
class RegexpFilter
def initialize(what, *regexps)
@what = what
@regexps = to_regexp(*regexps)
end

def ===(string)
@regexps.any? { |regexp| regexp === string }
end

def register
MSpec.register @what, self
end

def unregister
MSpec.unregister @what, self
end

class RegexpFilter < MatchFilter
def to_regexp(*strings)
strings.map { |str| Regexp.new str }
def to_regexp(*regexps)
regexps.map { |str| Regexp.new str }
end
private :to_regexp
end
20 changes: 19 additions & 1 deletion spec/runner/filters/regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@
require 'mspec/runner/mspec'
require 'mspec/runner/filters/regexp'

describe MatchFilter, "#===" do
before :each do
@filter = RegexpFilter.new nil, 'a(b|c)', 'b[^ab]', 'cc?'
end

it "returns true if the argument matches any of the #initialize strings" do
@filter.===('ab').should == true
@filter.===('bc suffix').should == true
@filter.===('prefix cc').should == true
end

it "returns false if the argument matches none of the #initialize strings" do
@filter.===('aa').should == false
@filter.===('ba').should == false
@filter.===('prefix d suffix').should == false
end
end

describe RegexpFilter, "#to_regexp" do
before :each do
@filter = RegexpFilter.new nil
end

it "converts its arguments to Regexp instances" do
@filter.to_regexp('a(b|c)', 'b[^ab]', 'cc?').should == [/a(b|c)/, /b[^ab]/, /cc?/]
@filter.send(:to_regexp, 'a(b|c)', 'b[^ab]', 'cc?').should == [/a(b|c)/, /b[^ab]/, /cc?/]
end
end

0 comments on commit 072849e

Please sign in to comment.