-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
358 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { expect, test } from 'vitest'; | ||
|
||
import * as pestCommon from '../common/pest'; | ||
import * as phpParser from '../parsers/php/parser'; | ||
import * as testUtils from './testUtils'; | ||
|
||
test('Get pest test items', async () => { | ||
const code = testUtils.stripInitialNewline(` | ||
<?php | ||
describe('dummy test', function () { | ||
it('dummy of test 1', function () { | ||
// ... | ||
}); | ||
test('dummy of test 2', function () { | ||
// ... | ||
}); | ||
}); | ||
test('dummy of test 3', function () { | ||
// ... | ||
}); | ||
`); | ||
|
||
const ast = phpParser.getAstByParseCode(code)!; | ||
|
||
const testItems = pestCommon.getPestTestItems(ast); | ||
|
||
const expected = [ | ||
{ | ||
description: 'dummy of test 1', | ||
callName: 'it', | ||
startOffset: 47, | ||
endOffset: 103, | ||
}, | ||
{ | ||
description: 'dummy of test 2', | ||
callName: 'test', | ||
startOffset: 109, | ||
endOffset: 167, | ||
}, | ||
{ | ||
description: 'dummy of test 3', | ||
callName: 'test', | ||
startOffset: 173, | ||
endOffset: 223, | ||
}, | ||
]; | ||
|
||
expect(testItems).toMatchObject(expected); | ||
}); | ||
|
||
test('Get pest test description at editor offset', async () => { | ||
const editorOffset = 125; | ||
|
||
const testItems: pestCommon.PestTestItemType[] = [ | ||
{ | ||
description: 'dummy of test 1', | ||
callName: 'it', | ||
startOffset: 47, | ||
endOffset: 103, | ||
}, | ||
{ | ||
description: 'dummy of test 2', | ||
callName: 'test', | ||
startOffset: 109, | ||
endOffset: 167, | ||
}, | ||
]; | ||
|
||
const pestTestDecription = pestCommon.getPestTestDescriptionAtEditorOffset(testItems, editorOffset); | ||
|
||
const expected = 'dummy of test 2'; | ||
expect(pestTestDecription).toBe(expected); | ||
}); | ||
|
||
test('Get phpunit style test items', async () => { | ||
// escape \ -> \\ | ||
const code = testUtils.stripInitialNewline(` | ||
<?php | ||
use PHPUnit\\Framework\\TestCase; | ||
class PhpUnitStyleTest extends TestCase | ||
{ | ||
public function testDummy1(): void | ||
{ | ||
$expected = 'Dummy1'; | ||
$this->assertSame('Dummy2', $expected); | ||
} | ||
public function testDummy2(): void | ||
{ | ||
$expected = 'Dummy2'; | ||
$this->assertSame('Dummy2', $expected); | ||
} | ||
} | ||
`); | ||
|
||
const ast = phpParser.getAstByParseCode(code)!; | ||
|
||
const testItems = pestCommon.getPhpUnitStyleTestItems(ast); | ||
|
||
const expected = [ | ||
{ | ||
methodName: 'testDummy1', | ||
startOffset: 86, | ||
endOffset: 210, | ||
}, | ||
{ | ||
methodName: 'testDummy2', | ||
startOffset: 216, | ||
endOffset: 340, | ||
}, | ||
]; | ||
|
||
expect(testItems).toMatchObject(expected); | ||
}); | ||
|
||
test('Get phpunit style test name at editor offset', async () => { | ||
const editorOffset = 220; | ||
|
||
const testItems: pestCommon.PhpUnitTestItemType[] = [ | ||
{ | ||
methodName: 'testDummy1', | ||
startOffset: 86, | ||
endOffset: 210, | ||
}, | ||
{ | ||
methodName: 'testDummy2', | ||
startOffset: 216, | ||
endOffset: 340, | ||
}, | ||
]; | ||
|
||
const testName = pestCommon.getPhpUnitTestNameAtEditorOffset(testItems, editorOffset); | ||
|
||
const expected = 'testDummy2'; | ||
expect(testName).toBe(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function printDebug(data: any) { | ||
console.log(`==DEBUG==: ${JSON.stringify(data, null, 2)}`); | ||
} | ||
|
||
export function stripInitialNewline(text: string) { | ||
return text.replace(/^\n/, ''); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import * as phpParser from '../parsers/php/parser'; | ||
|
||
import { Call, Identifier, Method, Name as NameNode, Node, String as StringNode } from 'php-parser'; | ||
|
||
export type PestTestItemType = { | ||
description: string; | ||
callName: string; | ||
startOffset: number; | ||
endOffset: number; | ||
}; | ||
|
||
export type PhpUnitTestItemType = { | ||
methodName: string; | ||
startOffset: number; | ||
endOffset: number; | ||
}; | ||
|
||
export function getPestTestItems(ast: Node) { | ||
const items: PestTestItemType[] = []; | ||
|
||
phpParser.walk((node, parent) => { | ||
if (!parent) return; | ||
if (parent.kind !== 'call') return; | ||
if (node.kind !== 'name') return; | ||
const parentCallNode = parent as Call; | ||
const nameNode = node as NameNode; | ||
|
||
if (!parentCallNode.loc) return; | ||
|
||
if (!isPestTestName(nameNode.name)) return; | ||
if (parentCallNode.arguments.length === 0) return; | ||
if (parentCallNode.arguments[0].kind !== 'string') return; | ||
const stringNode = parentCallNode.arguments[0] as StringNode; | ||
|
||
items.push({ | ||
description: stringNode.value, | ||
callName: nameNode.name, | ||
startOffset: parentCallNode.loc.start.offset, | ||
endOffset: parentCallNode.loc.end.offset, | ||
}); | ||
}, ast); | ||
|
||
return items; | ||
} | ||
|
||
export function isPestTestName(name: string) { | ||
return ['test', 'it'].includes(name); | ||
} | ||
|
||
export function getPestTestDescriptionAtEditorOffset(testItems: PestTestItemType[], editorOffset: number) { | ||
const testDescriptions: string[] = []; | ||
|
||
for (const t of testItems) { | ||
if (t.startOffset <= editorOffset && t.endOffset >= editorOffset) { | ||
testDescriptions.push(t.description); | ||
} | ||
} | ||
|
||
if (testDescriptions.length === 0) return undefined; | ||
return testDescriptions[0]; | ||
} | ||
|
||
export function getPhpUnitStyleTestItems(ast: Node) { | ||
const items: PhpUnitTestItemType[] = []; | ||
|
||
phpParser.walk((node, parent) => { | ||
if (!parent) return; | ||
if (parent.kind !== 'method') return; | ||
if (node.kind !== 'identifier') return; | ||
const parentMethodNode = parent as Method; | ||
const identifierNode = node as Identifier; | ||
|
||
if (!parentMethodNode.loc) return; | ||
|
||
if (!isPhpUnitTestName(identifierNode.name)) return; | ||
|
||
items.push({ | ||
methodName: identifierNode.name, | ||
startOffset: parentMethodNode.loc.start.offset, | ||
endOffset: parentMethodNode.loc.end.offset, | ||
}); | ||
}, ast); | ||
|
||
return items; | ||
} | ||
|
||
export function isPhpUnitTestName(name: string) { | ||
return name.startsWith('test'); | ||
} | ||
|
||
export function getPhpUnitTestNameAtEditorOffset(testItems: PhpUnitTestItemType[], editorOffset: number) { | ||
const testNames: string[] = []; | ||
|
||
for (const t of testItems) { | ||
if (t.startOffset <= editorOffset && t.endOffset >= editorOffset) { | ||
testNames.push(t.methodName); | ||
} | ||
} | ||
|
||
if (testNames.length === 0) return undefined; | ||
return testNames[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.