packages/deriver/tests/Unit/Deriver/Evaluator/Invocation/ArgumentsTest.php

1<?php
2
3declare (strict_types=1);
4
5namespace Tests\Unit\Deriver\Evaluator\Invocation;
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\Request\Budget;
16use Deriver\Deriver\Evaluator\Scalar\Identity;
17use Deriver\Deriver\Evaluator\Scope\Context;
18use Deriver\Deriver\Evaluator\Scope\Frame;
19use Deriver\Deriver\Evaluator\TargetProfile;
20use Deriver\Deriver\Evaluator\Term;
21use Deriver\Deriver\Lowerer\Declaration\Index;
22use Deriver\Deriver\MicroEvaluatorConversion;
23use Deriver\Deriver\Parser\Node\File;
24use Deriver\Deriver\Parser\Parser;
25use Deriver\Deriver\Parser\ParserConfig;
26use Deriver\Deriver\Providers\ProviderExpansion;
27use Deriver\Deriver\Resolver\Resolver;
28use Deriver\Deriver\Sources\Path;
29use Deriver\Deriver\Sources\Source;
30use Deriver\Deriver\Sources\Sources;
31use PHPUnit\Framework\Attributes\CoversNothing;
32use PHPUnit\Framework\TestCase;
33
34#[CoversNothing]
35final class ArgumentsTest extends TestCase
36{
37    public function testActualsExpandsNamedUnpack(): void
38    {
39        $source = 'function callee($a,$b){return [$a,$b];}function target(){return callee(...["b"=>2,"a"=>1]);}';
40        $configuration = new DeriverConfig();
41        $budget = new Budget();
42        $contributions = new Contributions();
43        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
44        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
45        $contributions1 = $providers->contributions;
46        $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);
47        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
48        $snapshotId = 'test';
49        $input = $providers->input;
50        $parser = new Parser(new ParserConfig($target->version));
51        $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));
52        $program = new Index($snapshotId, $trees);
53        $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
54        $symbol = 'target';
55        $graph = $engine->context->index->graph($symbol);
56        self::assertNotNull($graph);
57        $alternatives = (new Choices())->alternatives($engine->returns(new Frame($graph, 'source:' . $symbol), 64));
58        $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
59            [$value, $_] = $alternative;
60            $values[(new Identity())->key($value)] = $value;
61            return $values;
62        }, []);
63        self::assertCount(1, $values);
64        $resolvedValue = array_values($values)[0];
65        self::assertInstanceOf(Term::class, $resolvedValue);
66        self::assertSame([1, 2], $resolvedValue->native());
67    }
68    public function testBindPreservesVariadicNames(): void
69    {
70        $source = 'function callee($a,...$more){return $more;}function target(){return callee(1,x:2,y:3);}';
71        $configuration = new DeriverConfig();
72        $budget = new Budget();
73        $contributions = new Contributions();
74        $target = new TargetProfile($configuration->phpVersion, $configuration->integerBits, $configuration->floatPrecision);
75        $providers = new ProviderExpansion(new Sources([new Source('candidate.php', '<?php ' . $source)]), array_map(Term::fromNative(...), $configuration->environment), $contributions);
76        $contributions1 = $providers->contributions;
77        $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);
78        $settings = new EvaluatorConfig($target, $providers->environment, $configuration->standardEvaluators, array_map((new MicroEvaluatorConversion())->convert(...), $configuration->evaluators));
79        $snapshotId = 'test';
80        $input = $providers->input;
81        $parser = new Parser(new ParserConfig($target->version));
82        $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));
83        $program = new Index($snapshotId, $trees);
84        $engine = new Evaluator(new Context(new DefinitionIndex($program), $settings, $evaluators, $budget));
85        $symbol = 'target';
86        $graph = $engine->context->index->graph($symbol);
87        self::assertNotNull($graph);
88        $alternatives = (new Choices())->alternatives($engine->returns(new Frame($graph, 'source:' . $symbol), 64));
89        $values = array_reduce(iterator_to_array($alternatives, false), static function (array $values, array $alternative): array {
90            [$value, $_] = $alternative;
91            $values[(new Identity())->key($value)] = $value;
92            return $values;
93        }, []);
94        self::assertCount(1, $values);
95        $resolvedValue = array_values($values)[0];
96        self::assertInstanceOf(Term::class, $resolvedValue);
97        self::assertSame(['x' => 2, 'y' => 3], $resolvedValue->native());
98    }
99}
100