Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoah1 committed Jul 2, 2024
1 parent adeaab3 commit ef38c73
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 79 deletions.
12 changes: 9 additions & 3 deletions src/language-tools/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const JAVA_TEST_REGEX =
/@Test\s+.*\s+public void (?<methodName>\w+)|public class (?<className>(Test\w*|\w+Test))\s+extends/
const PACKAGE_NAME_REGEX =
/package\s+(?<packageName>([a-zA-Z_][a-zA-Z0-9_]*)(\.[a-zA-Z_][a-zA-Z0-9_]*)*);/
const PARAMETERIZED_TEST_REGEX = /^(?<lookupKey>.*)\[(?<subTestName>.*)\]$/
const PARAMETERIZED_TEST_REGEX = /^(?<lookupKey>.*?)(?=\[.*?\])(.*)$/

export class JavaLanguageTools implements LanguageTools {
/**
Expand All @@ -20,7 +20,10 @@ export class JavaLanguageTools implements LanguageTools {
* @returns Lookup key to find this test case in the TestRunTracker.
*/
mapTestFinishDataToLookupKey(testFinishData: TestFinish): string | undefined {
if (testFinishData.dataKind === TestFinishDataKind.JUnitStyleTestCaseData) {
if (
testFinishData.dataKind === TestFinishDataKind.JUnitStyleTestCaseData &&
testFinishData.data
) {
const testCaseData = testFinishData.data as JUnitStyleTestCaseData
if (testCaseData.className !== undefined) {
let testCaseName = testFinishData.displayName
Expand All @@ -31,7 +34,10 @@ export class JavaLanguageTools implements LanguageTools {
testCaseName = match.groups.lookupKey
}

return `${testCaseData.className}.${testCaseName}`
// Use the class name as the base, and append the test case name if available.
let result = testCaseData.className
if (testCaseName.length > 0) result += `.${testCaseName}`
return result
} else {
return testFinishData.displayName
}
Expand Down
12 changes: 9 additions & 3 deletions src/language-tools/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {BaseLanguageTools} from './base'
import {JUnitStyleTestCaseData, TestFinishDataKind} from '../bsp/bsp-ext'

const TEST_FILE_REGEX = /^(test_.+\.py|.+_test\.py)$/
const PARAMETERIZED_TEST_REGEX = /^(?<lookupKey>.*)\[(?<subTestName>.*)\]$/
const PARAMETERIZED_TEST_REGEX = /^(?<lookupKey>.*?)(?=\[.*?\])(.*)$/

export class PythonLanguageTools
extends BaseLanguageTools
Expand All @@ -22,7 +22,10 @@ export class PythonLanguageTools
* @returns Lookup key to find this test case in the TestRunTracker.
*/
mapTestFinishDataToLookupKey(testFinishData: TestFinish): string | undefined {
if (testFinishData.dataKind === TestFinishDataKind.JUnitStyleTestCaseData) {
if (
testFinishData.dataKind === TestFinishDataKind.JUnitStyleTestCaseData &&
testFinishData.data
) {
const testCaseData = testFinishData.data as JUnitStyleTestCaseData
let testCaseName = testFinishData.displayName

Expand All @@ -32,7 +35,10 @@ export class PythonLanguageTools
testCaseName = match.groups.lookupKey
}

return `${testCaseData.className}.${testCaseName}`
// Use the class name as the base, and append the test case name if available.
let result = testCaseData.className
if (testCaseName.length > 0) result += `.${testCaseName}`
return result
}
return undefined
}
Expand Down
277 changes: 242 additions & 35 deletions src/test/suite/language-tools/java.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,45 +99,252 @@ suite('Java Language Tools', () => {
assert.strictEqual(result.testCases.length, 0)
})

test('map test finish data to lookup key', async () => {
let result = languageTools.mapTestFinishDataToLookupKey({
displayName: 'myTest',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
const testCases = [
{
description: 'test method within a class',
input: {
displayName: 'myTest',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
},
},
})
assert.strictEqual(result, 'com.example.ClassName.myTest')

result = languageTools.mapTestFinishDataToLookupKey({
displayName: 'com.example.MySuite',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
expected: 'com.example.ClassName.myTest',
},
{
description: 'suite level test case',
input: {
displayName: 'com.example.MySuite',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
},
},
})
assert.strictEqual(result, 'com.example.MySuite')

result = languageTools.mapTestFinishDataToLookupKey({
displayName: 'com.example.MySuite',
status: TestStatus.Failed,
})
assert.strictEqual(result, undefined)

result = languageTools.mapTestFinishDataToLookupKey({
displayName: 'myTest[example1]',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
expected: 'com.example.MySuite',
},
{
description: 'no dataKind provided',
input: {
displayName: 'com.example.MySuite',
status: TestStatus.Failed,
},
expected: undefined,
},
{
description: 'parameterized test cases',
input: {
displayName: 'myTest[example1]',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
},
},
expected: 'com.example.ClassName.myTest',
},
{
description: 'parameterized test with special characters',
input: {
displayName: 'testMethod[example1!@#]',
status: TestStatus.Passed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 1,
className: 'com.example.SpecialCharsExample',
},
},
expected: 'com.example.SpecialCharsExample.testMethod',
},
{
description: 'parameterized test with spaces',
input: {
displayName: 'testMethod[example with spaces]',
status: TestStatus.Skipped,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0.5,
className: 'com.example.SpaceTestExample',
},
},
expected: 'com.example.SpaceTestExample.testMethod',
},
{
description: 'parameterized test with multiple brackets',
input: {
displayName: 'testMethod[example[inner]]',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 2,
className: 'com.example.MultiBracketTestExample',
},
},
expected: 'com.example.MultiBracketTestExample.testMethod',
},
{
description: 'parameterized test with numbers',
input: {
displayName: 'testMethod[12345]',
status: TestStatus.Passed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0.1,
className: 'com.example.NumericTestExample',
},
},
expected: 'com.example.NumericTestExample.testMethod',
},
{
description: 'parameterized test with empty brackets',
input: {
displayName: 'testMethod[]',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 1.5,
className: 'com.example.EmptyBracketTestExample',
},
},
expected: 'com.example.EmptyBracketTestExample.testMethod',
},
{
description: 'parameterized test with special symbols',
input: {
displayName: 'testMethod[!@#$%^&*()]',
status: TestStatus.Skipped,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 3,
className: 'com.example.SymbolsTestExample',
},
},
expected: 'com.example.SymbolsTestExample.testMethod',
},
{
description: 'parameterized test with long name',
input: {
displayName: 'testMethod[averylongsubtestnamethatisunusuallylong]',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 2.5,
className: 'com.example.LongNameTestExample',
},
},
expected: 'com.example.LongNameTestExample.testMethod',
},
{
description: 'parameterized test with nested brackets',
input: {
displayName: 'testMethod[example[nested[brackets]]]',
status: TestStatus.Passed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0.2,
className: 'com.example.NestedBracketsTestExample',
},
},
expected: 'com.example.NestedBracketsTestExample.testMethod',
},
{
description: 'successful tests with data',
input: {
displayName: 'mySuccessfulTest',
status: TestStatus.Passed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 1,
className: 'com.example.SuccessClass',
},
},
expected: 'com.example.SuccessClass.mySuccessfulTest',
},
{
description: 'tests with no className',
input: {
displayName: 'myTestWithoutClass',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 2,
},
},
expected: 'myTestWithoutClass',
},
{
description: 'unknown dataKind',
input: {
displayName: 'unknownTest',
status: TestStatus.Failed,
dataKind: 'UnknownDataKind',
data: {
time: 0,
className: 'com.example.UnknownClass',
},
},
expected: undefined,
},
{
description: 'null data gracefully',
input: {
displayName: 'nullDataTest',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: null,
},
expected: undefined,
},
{
description: 'numeric displayName',
input: {
displayName: '123456',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
},
},
expected: 'com.example.ClassName.123456',
},
{
description: 'special characters in displayName',
input: {
displayName: '!@#$%^&*()',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
},
},
expected: 'com.example.ClassName.!@#$%^&*()',
},
{
description: 'empty string as displayName',
input: {
displayName: '',
status: TestStatus.Failed,
dataKind: TestFinishDataKind.JUnitStyleTestCaseData,
data: {
time: 0,
className: 'com.example.ClassName',
},
},
expected: 'com.example.ClassName',
},
]

for (const testCase of testCases) {
test(`map test finish data to lookup key: ${testCase.description}`, async () => {
const result = languageTools.mapTestFinishDataToLookupKey(testCase.input)
assert.strictEqual(result, testCase.expected)
})
assert.strictEqual(result, 'com.example.ClassName.myTest')
})
}

test('map test case info to lookup key', async () => {
let testInfo = testController.createTestItem('test1', 'test1')
Expand Down
Loading

0 comments on commit ef38c73

Please sign in to comment.