packages/deriver/tests/Unit/Deriver/Evaluator/Language/CatchesTest.php
1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Language;
6
7use Deriver\Deriver\Contributions;
8use Deriver\Deriver\DeriverConfig;
9use Deriver\Deriver\Evaluator\Call\Evaluators;
10use Deriver\Deriver\Evaluator\Call\Registrations;
11use Deriver\Deriver\Evaluator\Enumeration\Choices;
12use Deriver\Deriver\Evaluator\Evaluator;
13use Deriver\Deriver\Evaluator\EvaluatorConfig;
14use Deriver\Deriver\Evaluator\Index\DefinitionIndex;
15use Deriver\Deriver\Evaluator\Language\Catches;
16use Deriver\Deriver\Evaluator\Request\Budget;
17use Deriver\Deriver\Evaluator\Scalar\Identity;
18use Deriver\Deriver\Evaluator\Scope\Context;
19use Deriver\Deriver\Evaluator\Scope\Frame;
20use Deriver\Deriver\Evaluator\TargetProfile;
21use Deriver\Deriver\Evaluator\Term;
22use Deriver\Deriver\Lowerer\Declaration\Index;
23use Deriver\Deriver\MicroEvaluatorConversion;
24use Deriver\Deriver\Parser\Node\File;
25use Deriver\Deriver\Parser\Parser;
26use Deriver\Deriver\Parser\ParserConfig;
27use Deriver\Deriver\Providers\ProviderExpansion;
28use Deriver\Deriver\Resolver\Resolver;
29use Deriver\Deriver\Sources\Path;
30use Deriver\Deriver\Sources\Source;
31use Deriver\Deriver\Sources\Sources;
32use PHPUnit\Framework\Attributes\CoversNothing;
33use PHPUnit\Framework\TestCase;
34
35#[CoversNothing]
36final class CatchesTest extends TestCase
37{
38 public function testResolveFollowsTheCatchSelectedByADemandedValue(): void
39 {
40 $source = 'function number(int $x){return $x;}function target(){try{return number([]);}catch(TypeError $e){return "typed";}}';
41 $configuration = new DeriverConfig();
42 $budget = new Budget();
43 $contributions = new Contributions();
44 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
45 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
46 $contributions1 = $providers->contributions;
47 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
48 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
49 $snapshotId = 'test';
50 $input = $providers->input;
51 $parser = new Parser(new ParserConfig($target->version));
52 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
53 $program = new Index($snapshotId, $trees);
54 $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
55 $symbol = 'target';
56 $graph = $e->context->index->graph($symbol);
57 self::assertNotNull($graph);
58 $alternatives = (new Choices())->alternatives($e->returns(new Frame($graph, 'source:' . $symbol), 64));
59 $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
60 [$value, $_] = $alternative;
61 $values[(new Identity())->key($value)] = $value;
62 return $values;
63 }, []);
64 self::assertCount(1, $values);
65 $resolvedValue = array_values($values)[0];
66 self::assertInstanceOf(Term::class, $resolvedValue);
67 self::assertSame('typed', $resolvedValue->native());
68 }
69 public function testRegionsIndexesTheLexicalHandler(): void
70 {
71 $source = 'function target(){try{return missing();}catch(Error $e){return 1;}}';
72 $configuration = new DeriverConfig();
73 $budget = new Budget();
74 $contributions = new Contributions();
75 $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
76 $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
77 $contributions1 = $providers->contributions;
78 $evaluators = new Evaluators(new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision), new Registrations($contributions1->evaluators, $contributions1->providers, $contributions1->domains, $contributions1->intrinsics, $contributions1->stateSlots), $configuration->standardEvaluators);
79 $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
80 $snapshotId = 'test';
81 $input = $providers->input;
82 $parser = new Parser(new ParserConfig($target->version));
83 $trees = array_map(static fn (Source $source1): File => (new Resolver())->resolve($parser->parse(new Source(Path::normalize($source1->path), $source1->contents, $source1->declarationsOnly))), iterator_to_array($input, false));
84 $program = new Index($snapshotId, $trees);
85 $e = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
86 $symbol = 'target';
87 $graph = $e->context->index->graph($symbol);
88 self::assertNotNull($graph);
89 self::assertSame([0], (new Catches())->regions(new Frame($graph, 'source:' . $symbol), 0));
90 }
91}
92