Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/test_glob.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: Copyright (c) 2014-2026 Yegor Bugayenko
# SPDX-License-Identifier: MIT

require_relative '../utils/glob'
require_relative 'test__helper'

# PDD::Glob test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2014-2026 Yegor Bugayenko
# License:: MIT
class TestGlob < Minitest::Test
def test_converts_plain_pattern
assert_equal('foo/.*\\.rb', PDD::Glob.new('foo/*.rb').to_regexp)
end

def test_expands_braces
assert_equal('.*\\.(rb|js)', PDD::Glob.new('**/*.{rb,js}').to_regexp)
end

def test_keeps_unmatched_closing_brace
assert_equal('a}b', PDD::Glob.new('a}b').to_regexp)
end

def test_keeps_escaped_char_literal
assert_equal('a\\*b', PDD::Glob.new('a\\*b').to_regexp)
end
end
9 changes: 5 additions & 4 deletions utils/glob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ def initialize(string)
# rubocop:disable Metrics/CyclomaticComplexity
def to_regexp
chars = @string.gsub(%r{(\*\*/\*)|(\*\*)}, '*').chars
curlies = 0, escaping = false
curlies = 0
escaping = false
chars.map do |char|
if escaping
escaping = false
return char
next char
end
case char
when '*'
Expand All @@ -32,9 +33,9 @@ def to_regexp
when '}'
if curlies.positive?
curlies -= 1
return ')'
next ')'
end
return char
next char
when ','
curlies.positive? ? '|' : char
when '\\'
Expand Down
Loading