Skip to content

Commit

Permalink
Merge pull request #274 from recca0120/fix/pest-with-namedargument
Browse files Browse the repository at this point in the history
fix named argument
  • Loading branch information
recca0120 authored Jan 31, 2025
2 parents 0acb77e + cba90bd commit 3fbbff2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"displayName": "PHPUnit Test Explorer",
"icon": "img/icon.png",
"publisher": "recca0120",
"version": "3.5.20",
"version": "3.5.21",
"private": true,
"license": "MIT",
"repository": {
Expand Down
28 changes: 21 additions & 7 deletions src/PHPUnit/TestParser/Parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Call, Declaration, Identifier, Namespace, Node, Variable } from 'php-parser';
import { Call, Closure, Declaration, Identifier, namedargument, Namespace, Node, String, Variable } from 'php-parser';
import { PHPUnitXML } from '../PHPUnitXML';
import { TransformerFactory } from '../Transformer';
import { TestDefinition, TestType } from '../types';
Expand All @@ -25,26 +25,40 @@ export abstract class Parser {
return this.phpUnitXML?.root() ?? '';
}

protected parseName(declaration?: Namespace | Declaration | Call | Identifier | Variable): string | undefined {
protected parseName(declaration?: Namespace | Declaration | Call | Identifier | namedargument | String | Variable): string | undefined {
if (!declaration) {
return undefined;
return declaration;
}

if ('what' in declaration) {
return this.parseName(declaration.what);
}

if (typeof declaration.name === 'string') {
return declaration.name;
if (declaration.kind === 'namedargument') {
return this.parseName(((declaration as namedargument).value as String));
}

if ('name' in declaration) {
if (typeof declaration.name === 'string') {
return declaration.name;
}

if (declaration.name && 'name' in declaration.name) {
return declaration.name.name;
}
}

if (declaration.name && 'name' in declaration.name) {
return declaration.name.name;
if (declaration.kind === 'string') {
return (declaration as String).value;
}

return undefined;
};

protected parseClosure(argument: Closure | namedargument) {
return (argument.kind === 'namedargument' ? (argument as namedargument).value : argument) as Closure;
}

protected parseAnnotations(declaration: Declaration) {
return { ...annotationParser.parse(declaration), ...attributeParser.parse(declaration) };
}
Expand Down
25 changes: 25 additions & 0 deletions src/PHPUnit/TestParser/PestParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,29 @@ it('asserts true is true', function () {
depth: 3,
});
});

it('test with namedargument', async () => {
const content = `<?php
describe(description: 'something', test: function () {
it(description: 'asserts true is true', test: function () {
expect(true)->toBe(true);
});
});
`;

expect(givenTest(file, content, '`something` → it asserts true is true')).toEqual({
type: TestType.method,
id: 'tests/Fixtures/ExampleTest.php::`something` → it asserts true is true',
classFQN: 'P\\Tests\\Fixtures\\ExampleTest',
namespace: 'P\\Tests\\Fixtures',
className: 'ExampleTest',
methodName: '`something` → it asserts true is true',
label: 'it asserts true is true',
file,
start: { line: expect.any(Number), character: expect.any(Number) },
end: { line: expect.any(Number), character: expect.any(Number) },
depth: 4,
});
});
});
10 changes: 6 additions & 4 deletions src/PHPUnit/TestParser/PestParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { basename, dirname, join, relative } from 'node:path';
import { Block, Call, Closure, Declaration, ExpressionStatement, Node, Program, String } from 'php-parser';
import {
Block, Call, Closure, Declaration, ExpressionStatement, namedargument, Node, Program, String,
} from 'php-parser';
import { Transformer, TransformerFactory } from '../Transformer';
import { TestDefinition, TestType } from '../types';
import { capitalize } from '../utils';
Expand Down Expand Up @@ -51,8 +53,8 @@ export class PestParser extends Parser {
if (['program', 'namespace'].includes(declaration.kind)) {
children = (declaration as Program).children;
} else {
const closure = (declaration as Call).arguments[1] as Closure;
prefixes = [...prefixes, ((declaration as Call).arguments[0] as String).value];
const closure = this.parseClosure((declaration as Call).arguments[1] as Closure | namedargument);
prefixes = [...prefixes, this.parseName((declaration as Call).arguments[0] as namedargument | String)!];
children = closure.kind === 'arrowfunc' ? [{ expression: closure.body! }] : closure.body!.children!;
}

Expand Down Expand Up @@ -85,7 +87,7 @@ export class PestParser extends Parser {
return this.parseTestOrIt(((call.what as any).what) as Call, clazz, prefixes);
}

let methodName = (call.arguments[0] as String).value;
let methodName = this.parseName(call.arguments[0] as (String | namedargument));
if (this.parseName(call) === 'it') {
methodName = 'it ' + methodName;
}
Expand Down

0 comments on commit 3fbbff2

Please sign in to comment.