-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCompilerTest.php
131 lines (106 loc) · 3.48 KB
/
CompilerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* This file is part of PhpAidc LabelPrinter package.
*
* © Appwilio (https://appwilio.com)
* © JhaoDa (https://github.com/jhaoda)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpAidc\LabelPrinter\Tests\Unit;
use PhpAidc\LabelPrinter\Compiler;
use PhpAidc\LabelPrinter\Label\Batch;
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\Contract\Command;
use PhpAidc\LabelPrinter\Contract\Language;
use PhpAidc\LabelPrinter\Contract\Label as LabelContract;
use PHPUnit\Framework\TestCase;
class CompilerTest extends TestCase
{
public function testCompileLabel(): void
{
$compiler = Compiler::create(new LanguageA());
$label = Label::create()->add(Element::raw(''));
$this->assertEquals('ASIZE|ACMD|APRINT1|', $compiler->compile($label));
$label->copies(2);
$this->assertEquals('ASIZE|ACMD|APRINT2|', $compiler->compile($label));
}
public function testCompileBatch(): void
{
$compiler = Compiler::create(new LanguageA());
$label = Label::create()->add(Element::raw(''));
$batch = new Batch();
$batch->add(clone $label);
$batch->add((clone $label)->copies(2));
$this->assertEquals('ASIZE|ACMD|APRINT1|ASIZE|ACMD|APRINT2|', $compiler->compile($batch));
}
public function testLanguageCondition(): void
{
$label = Label::create()
->for(LanguageA::class, static function (Label $label) {
$label->add(Element::raw(''));
})
->for(LanguageB::class, static function (Label $label) {
$label->add(Element::raw(''));
});
$this->assertEquals('ASIZE|ACMD|APRINT1|', Compiler::create(new LanguageA())->compile($label));
$this->assertEquals('BSIZE|BCMD|BPRINT1|', Compiler::create(new LanguageB())->compile($label));
}
public function testBooleanConditionTruthy(): void
{
$label = Label::create()
->when(1 > 0, static function (Label $label) {
$label->add(Element::raw(''));
});
$this->assertEquals('ASIZE|ACMD|APRINT1|', Compiler::create(new LanguageA())->compile($label));
}
public function testBooleanConditionFalsy(): void
{
$label = Label::create()
->when(0 > 1, static function (Label $label) {
$label->add(Element::raw(''));
});
$this->assertEquals('ASIZE|APRINT1|', Compiler::create(new LanguageA())->compile($label));
}
}
class LanguageA implements Language
{
public function compileDeclaration(LabelContract $label): iterable
{
yield 'ASIZE|';
}
public function isSupport(Command $command): bool
{
return true;
}
public function compileCommand(Command $command): iterable
{
yield 'ACMD|';
}
public function compilePrint(int $copies): iterable
{
yield "APRINT{$copies}|";
}
}
class LanguageB implements Language
{
public function compileDeclaration(LabelContract $label): iterable
{
yield 'BSIZE|';
}
public function isSupport(Command $command): bool
{
return true;
}
public function compileCommand(Command $command): iterable
{
yield 'BCMD|';
}
public function compilePrint(int $copies): iterable
{
yield "BPRINT{$copies}|";
}
}