Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Identifier#match? #1305

Merged
merged 1 commit into from Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 { Regexp.last_match } }
end

context 'non-matching regex' do
let(:pat) { %r{\A/qux/monkey} }
it { is_expected.to be(false) }
example { expect { subject }.not_to change { Regexp.last_match } }
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 { Regexp.last_match } }
end

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

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

Expand Down