Skip to content

Commit

Permalink
Add Identifier#match?
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jan 26, 2018
1 parent 4630125 commit 05a4abc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nanoc/lib/nanoc/base/entities/identifier.rb
Expand Up @@ -101,6 +101,11 @@ def =~(other)
Nanoc::Int::Pattern.from(other).match?(to_s) ? 0 : nil
end

contract C::Any => C::Bool
def match?(other)
Nanoc::Int::Pattern.from(other).match?(to_s)
end

contract C::Any => C::Num
def <=>(other)
to_s <=> other.to_s
Expand Down
34 changes: 34 additions & 0 deletions nanoc/spec/nanoc/base/entities/identifier_spec.rb
Expand Up @@ -249,6 +249,40 @@
end
end

describe '#match?' do
let(:identifier) { described_class.new('/foo/bar') }

subject { identifier.match?(pat) }

context 'given a regex' do
context 'matching regex' do
let(:pat) { %r{\A/foo/bar} }
it { is_expected.to be(true) }
example { expect { subject }.not_to change { $1 } }
end

context 'non-matching regex' do
let(:pat) { %r{\A/qux/monkey} }
it { is_expected.to be(false) }
example { expect { subject }.not_to change { $1 } }
end
end

context 'given a string' do
context 'matching string' do
let(:pat) { '/foo/*' }
it { is_expected.to be(true) }
example { expect { subject }.not_to change { $1 } }
end

context 'non-matching string' do
let(:pat) { '/qux/*' }
it { is_expected.to be(false) }
example { expect { subject }.not_to change { $1 } }
end
end
end

describe '#<=>' do
let(:identifier) { described_class.new('/foo/bar') }

Expand Down

0 comments on commit 05a4abc

Please sign in to comment.