packages/sql-faker/tests/Unit/Compiler/Bison/Directive/ExpectDirectiveReaderTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\Compiler\Bison\Directive;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Compiler\Bison\Ast\BisonExpectDeclaration;
11use SqlFaker\Compiler\Bison\Directive\BisonDeclarationBoundary;
12use SqlFaker\Compiler\Bison\Directive\ExpectDirectiveReader;
13use SqlFaker\Compiler\Bison\Lexer\ActionScanner;
14use SqlFaker\Compiler\Bison\Lexer\BisonLexeme;
15use SqlFaker\Compiler\Bison\Lexer\BisonLexer;
16use SqlFaker\Compiler\Bison\Lexer\BisonScannerChain;
17use SqlFaker\Compiler\Bison\Lexer\BisonToken;
18use SqlFaker\Compiler\Bison\Lexer\BisonTokenStream;
19use SqlFaker\Compiler\Bison\Lexer\BisonTrivia;
20use SqlFaker\Compiler\Bison\Lexer\DirectiveScanner;
21use SqlFaker\Compiler\Bison\Lexer\IdentifierScanner;
22use SqlFaker\Compiler\Bison\Lexer\NumberScanner;
23use SqlFaker\Compiler\Bison\Lexer\QuotedLiteralScanner;
24use SqlFaker\Compiler\Bison\Lexer\SourceCursor;
25use SqlFaker\Compiler\Bison\Lexer\TypeTagScanner;
26
27#[CoversClass(ExpectDirectiveReader::class)]
28#[UsesClass(BisonDeclarationBoundary::class)]
29#[UsesClass(BisonLexeme::class)]
30#[UsesClass(BisonLexer::class)]
31#[UsesClass(BisonScannerChain::class)]
32#[UsesClass(BisonToken::class)]
33#[UsesClass(BisonTokenStream::class)]
34#[UsesClass(BisonTrivia::class)]
35#[UsesClass(SourceCursor::class)]
36#[UsesClass(BisonExpectDeclaration::class)]
37#[UsesClass(ActionScanner::class)]
38#[UsesClass(DirectiveScanner::class)]
39#[UsesClass(IdentifierScanner::class)]
40#[UsesClass(NumberScanner::class)]
41#[UsesClass(QuotedLiteralScanner::class)]
42#[UsesClass(TypeTagScanner::class)]
43#[UsesClass(\SqlFaker\Compiler\Bison\Lexer\PunctuationScanner::class)]
44#[UsesClass(\SqlFaker\Compiler\GrammarParseException::class)]
45final class ExpectDirectiveReaderTest extends TestCase
46{
47 public function testHandlesClaimsOnlyTheExpectDirective(): void
48 {
49 $reader = new ExpectDirectiveReader();
50
51 self::assertTrue($reader->handles('%expect'));
52 self::assertFalse($reader->handles('%define'));
53 }
54
55 public function testReadTakesTheConflictCount(): void
56 {
57 $declaration = (new ExpectDirectiveReader())->read(BisonTokenStream::over('42'), '%expect');
58
59 self::assertInstanceOf(BisonExpectDeclaration::class, $declaration);
60 self::assertSame(42, $declaration->count);
61 }
62
63 public function testReadDeclaresNothingWhenNoNumberFollows(): void
64 {
65 self::assertNull((new ExpectDirectiveReader())->read(BisonTokenStream::over('foo'), '%expect'));
66 }
67
68 public function testReadLeavesATokenItDidNotClaim(): void
69 {
70 $stream = BisonTokenStream::over('foo');
71
72 (new ExpectDirectiveReader())->read($stream, '%expect');
73
74 self::assertSame('foo', $stream->next()->value);
75 }
76}
77