-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from RobertWesner/development
Release v0.3.0
- Loading branch information
Showing
15 changed files
with
420 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace RobertWesner\AWDY\Tests; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use RobertWesner\AWDY\AWDY; | ||
use RobertWesner\AWDY\Template\Area; | ||
use RobertWesner\AWDY\Template\Border; | ||
use RobertWesner\AWDY\Template\Buffer; | ||
use RobertWesner\AWDY\Template\BufferLogger; | ||
use RobertWesner\AWDY\Template\Connection; | ||
use RobertWesner\AWDY\Template\Templates\SimpleTemplate; | ||
|
||
#[CoversClass(AWDY::class)] | ||
#[UsesClass(Area::class)] | ||
#[UsesClass(Border::class)] | ||
#[UsesClass(Buffer::class)] | ||
#[UsesClass(BufferLogger::class)] | ||
#[UsesClass(Connection::class)] | ||
final class AWDYTest extends BaseTest | ||
{ | ||
public function test(): void | ||
{ | ||
ob_start(); | ||
AWDY::setUp(new SimpleTemplate(), 56, 18); | ||
self::assertSame(<<<EOF | ||
.------------------------------------------------------. | ||
| [> ] | | ||
+------------------------------------------------------+ | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| | | ||
'------------------------------------------------------' | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
|
||
// this does not render the entire thing, only the changed area | ||
ob_start(); | ||
AWDY::printf('test'); | ||
self::assertSame(<<<EOF | ||
test | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
|
||
ob_start(); | ||
AWDY::progress(1 / 100, 1, 100); | ||
self::assertSame(<<<EOF | ||
1/100 [> ] | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
|
||
ob_start(); | ||
AWDY::progress(33 / 100, 33, 100); | ||
self::assertSame(<<<EOF | ||
33/100 [==============> ] | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
|
||
ob_start(); | ||
AWDY::progress(75 / 100, 75, 100); | ||
self::assertSame(<<<EOF | ||
75/100 [================================> ] | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
|
||
ob_start(); | ||
AWDY::progress(99 / 100, 99, 100); | ||
self::assertSame(<<<EOF | ||
99/100 [==========================================] | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
|
||
ob_start(); | ||
AWDY::progress(1, 100, 100); | ||
self::assertSame(<<<EOF | ||
100/100 [==========================================] | ||
EOF, $this->renderAwdyOutput(ob_get_clean())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RobertWesner\AWDY\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class BaseTest extends TestCase | ||
{ | ||
protected function stripAnsiEscapes(string $value): string | ||
{ | ||
return preg_replace('/\\033\[[^A-Za-z]*[A-Za-z]/', '', $value); | ||
} | ||
|
||
protected function renderAwdyOutput(string $value): string | ||
{ | ||
$segments = preg_split('/(?=\\x1b\[\d+;\d+f)/', $value); | ||
|
||
$result = ''; | ||
foreach ($segments as &$segment) { | ||
if ( | ||
str_starts_with($segment, "\033") | ||
&& preg_match( | ||
'/\\x1b\[(?<y>\d+);(?<x>\d+)f(?<text>.*)/', | ||
$segment, | ||
$matches, | ||
) | ||
) { | ||
$segment = [ | ||
'x' => (int)$matches['x'] - 1, | ||
'y' => (int)$matches['y'] - 1, | ||
'text' => $this->stripAnsiEscapes($matches['text']), | ||
]; | ||
} else { | ||
$segment = [ | ||
'x' => 0, | ||
'y' => 0, | ||
'text' => $this->stripAnsiEscapes($segment), | ||
]; | ||
} | ||
} | ||
|
||
foreach ($segments as ['x' => $x, 'y' => $y, 'text' => $text]) { | ||
$explodedResult = explode(PHP_EOL, $result); | ||
|
||
foreach (explode(PHP_EOL, $text) as $i => $line) { | ||
$explodedResult[$y + $i] = substr_replace( | ||
$explodedResult[$y + $i] ?? str_repeat(' ', $x), | ||
$line, | ||
$x, | ||
strlen($line), | ||
); | ||
} | ||
|
||
$result = implode(PHP_EOL, $explodedResult); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace RobertWesner\AWDY\Tests\Template; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use RobertWesner\AWDY\AnsiEscape; | ||
use RobertWesner\AWDY\Template\Area; | ||
use RobertWesner\AWDY\Template\Buffer; | ||
use RobertWesner\AWDY\Tests\BaseTest; | ||
|
||
#[CoversClass(Area::class)] | ||
#[UsesClass(Buffer::class)] | ||
final class AreaTest extends BaseTest | ||
{ | ||
private string $changingValue = 'Hello'; | ||
|
||
public function test(): void | ||
{ | ||
$area = Area::create(5, 2, -6, -3, function (Buffer $buffer) { | ||
$buffer->draw(0, 0, 'A'); | ||
$buffer->draw(1, 1, $this->changingValue); | ||
$buffer->draw(-1, 0, 'B'); | ||
$buffer->draw(0, -1, 'C'); | ||
$buffer->draw(-1, -1, 'D'); | ||
}); | ||
|
||
// dirty by default | ||
ob_start(); | ||
$area->render(20, 10); | ||
self::assertSame( | ||
sprintf( | ||
'%sA B%s Hello %s %s %s %sC D', | ||
AnsiEscape::moveTo(6, 3), | ||
AnsiEscape::moveTo(6, 4), | ||
AnsiEscape::moveTo(6, 5), | ||
AnsiEscape::moveTo(6, 6), | ||
AnsiEscape::moveTo(6, 7), | ||
AnsiEscape::moveTo(6, 8), | ||
), | ||
ob_get_clean(), | ||
); | ||
|
||
// should not render since it is not dirty | ||
ob_start(); | ||
$area->render(80, 32); | ||
self::assertSame('', ob_get_clean()); | ||
|
||
// mark area as dirty and re-render it | ||
$this->changingValue = 'World'; | ||
$area->dirty(); | ||
ob_start(); | ||
$area->render(20, 10); | ||
self::assertSame( | ||
sprintf( | ||
'%sA B%s World %s %s %s %sC D', | ||
AnsiEscape::moveTo(6, 3), | ||
AnsiEscape::moveTo(6, 4), | ||
AnsiEscape::moveTo(6, 5), | ||
AnsiEscape::moveTo(6, 6), | ||
AnsiEscape::moveTo(6, 7), | ||
AnsiEscape::moveTo(6, 8), | ||
), | ||
ob_get_clean(), | ||
); | ||
} | ||
} |
Oops, something went wrong.