|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Remorhaz\UniLex\Example\Brainfuck\Test; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Remorhaz\UniLex\Example\Brainfuck\Command\OutputCommand; |
|
9
|
|
|
use Remorhaz\UniLex\Example\Brainfuck\Exception as BrainfuckException; |
|
10
|
|
|
use Remorhaz\UniLex\Example\Brainfuck\Runtime; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @covers \Remorhaz\UniLex\Example\Brainfuck\Runtime |
|
14
|
|
|
* @covers \Remorhaz\UniLex\Example\Brainfuck\Command\OutputCommand |
|
15
|
|
|
*/ |
|
16
|
|
|
class RuntimeTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param int $memory |
|
20
|
|
|
* @dataProvider providerNonPositiveMemory |
|
21
|
|
|
* @throws BrainfuckException |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testConstruct_NonPositiveMemory_ThrowsException(int $memory): void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->expectException(BrainfuckException::class); |
|
26
|
|
|
$this->expectExceptionMessage('Memory amount must be positive'); |
|
27
|
|
|
new Runtime($memory); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function providerNonPositiveMemory(): array |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
'Zero memory' => [0], |
|
34
|
|
|
'Negative memory' => [-1], |
|
35
|
|
|
]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws BrainfuckException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testGetOutput_NoOutputCommandsExecuted_ReturnsEmptyString(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$actualValue = (new Runtime())->getOutput(); |
|
44
|
|
|
self::assertSame('', $actualValue); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @throws BrainfuckException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testGetOutput_OutputCommandExecuted_ReturnsMatchingString(): void |
|
51
|
|
|
{ |
|
52
|
|
|
$runtime = new Runtime(); |
|
53
|
|
|
$command = new OutputCommand($runtime); |
|
54
|
|
|
$runtime->addCommand($command); |
|
55
|
|
|
$command->exec(); |
|
56
|
|
|
$actualValue = $runtime->getOutput(); |
|
57
|
|
|
self::assertSame("\0", $actualValue); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|