Skip to content

Commit

Permalink
add test on glob and GLOB_BRACE flag
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Gitton <gary.gitton@gmail.com>
  • Loading branch information
garygitton committed Dec 15, 2021
1 parent ca01d74 commit 93353e5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected static function systemGlob($pattern, $flags)
*/
protected static function fallbackGlob($pattern, $flags)
{
if (! $flags & self::GLOB_BRACE) {
if (self::flagsIsEqualTo($flags, self::GLOB_BRACE)) {
return static::systemGlob($pattern, $flags);
}

Expand Down Expand Up @@ -195,14 +195,19 @@ protected static function nextBraceSub($pattern, $begin, $flags)
$current = $begin;

while ($current < $length) {
if (! $flags & self::GLOB_NOESCAPE && $pattern[$current] === '\\') {
$flagsEqualsNoEscape = self::flagsIsEqualTo($flags, self::GLOB_NOESCAPE);

if ($flagsEqualsNoEscape && $pattern[$current] === '\\') {
if (++$current === $length) {
break;
}

$current++;
} else {
if (($pattern[$current] === '}' && $depth-- === 0) || ($pattern[$current] === ',' && $depth === 0)) {
if (
($pattern[$current] === '}' && $depth-- === 0)
|| ($pattern[$current] === ',' && $depth === 0)
) {
break;
} elseif ($pattern[$current++] === '{') {
$depth++;
Expand All @@ -212,4 +217,9 @@ protected static function nextBraceSub($pattern, $begin, $flags)

return $current < $length ? $current : null;
}

public static function flagsIsEqualTo(int $flags, int $otherFlags): bool
{
return (bool) ($flags & $otherFlags);
}
}
71 changes: 67 additions & 4 deletions test/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function count;
use function defined;
use function glob;
use function realpath;
use function str_repeat;

use const GLOB_BRACE;
Expand All @@ -23,15 +24,29 @@ public function testFallback(): void
$this->markTestSkipped('GLOB_BRACE not available');
}

self::assertEquals(
glob(__DIR__ . '/_files/{alph,bet}a', GLOB_BRACE),
Glob::glob(__DIR__ . '/_files/{alph,bet}a', Glob::GLOB_BRACE, true)
$expected = glob(__DIR__ . '/_files/{alph,bet}a', GLOB_BRACE);
$actual = Glob::glob(
__DIR__ . '/_files/{alph,bet}a',
Glob::GLOB_BRACE,
true
);

self::assertEquals($actual, $expected);

$notExpectedPath = realpath(__DIR__ . '/_files/{alph,bet}a');

self::assertNotContains(
$notExpectedPath,
$actual
);
}

public function testNonMatchingGlobReturnsArray(): void
{
$result = Glob::glob('/some/path/{,*.}{this,orthis}.php', Glob::GLOB_BRACE);
$result = Glob::glob(
'/some/path/{,*.}{this,orthis}.php',
Glob::GLOB_BRACE
);
self::assertIsArray($result);
}

Expand Down Expand Up @@ -80,4 +95,52 @@ public function patternsProvider(): array
],
];
}

public function testGlobWithoutGlobBraceFlag(): void
{
$expected = [
realpath(__DIR__ . '/_files/{alph,bet}a'),
];

self::assertEquals(
glob(__DIR__ . '/_files/{alph,bet}a', 0),
$expected
);
}

/**
* @psalm-return array<array-key, array{
* int,
* int,
* bool
* }>
*/
public function flagsIsEqualsToMethodDataProvider(): array
{
return [
[
Glob::GLOB_BRACE,
Glob::GLOB_BRACE,
true,
],
[
Glob::GLOB_BRACE,
Glob::GLOB_NOSORT,
false,
],
];
}

/**
* @dataProvider flagsIsEqualsToMethodDataProvider
*/
public function testFlagsIsEqualsToMethod(
int $flags,
int $otherFlags,
bool $expected
): void {
$actual = Glob::flagsIsEqualTo($flags, $otherFlags);

$this->assertEquals($expected, $actual);
}
}
Empty file added test/_files/{alph,bet}a
Empty file.

0 comments on commit 93353e5

Please sign in to comment.