private async findTestCasesRegexFallback()

in src/language-tools/java.ts [94:148]


  private async findTestCasesRegexFallback(
    uri: vscode.Uri
  ): Promise<TestFileContents> {
    // Get document text.
    const fileContents = await vscode.workspace.fs.readFile(uri)
    const text = fileContents.toString()
    const lines = text.split('\n')

    const testCases: DocumentTestItem[] = []
    let classTestCase: DocumentTestItem | undefined

    // Match the package name
    const packageMatch = PACKAGE_NAME_REGEX.exec(text)
    const packageName = packageMatch?.groups?.packageName ?? ''

    let match
    const regex = new RegExp(JAVA_TEST_REGEX, 'g')

    let lineStartIndex = 0
    let currentLine = 0
    while ((match = regex.exec(text)) !== null) {
      // Advance the current line each time there is a match.
      while (lineStartIndex + lines[currentLine].length < match.index) {
        lineStartIndex += lines[currentLine].length + 1 // +1 for the newline character.
        currentLine++
      }
      const position = new vscode.Position(currentLine, 0)

      // Each match will contain either the full Class, or an individual test method.
      if (match.groups.className) {
        // Class information will be used to run the whole file.
        classTestCase = {
          name: match.groups.className,
          range: new vscode.Range(position, position),
          uri: uri,
          testFilter: `${packageName}.${match.groups.className}`,
        }
      } else if (match.groups.methodName && classTestCase) {
        // Method information with @Test decorator will be used to run an individual test case.
        const newItem: DocumentTestItem = {
          name: match.groups.methodName,
          range: new vscode.Range(position, position),
          uri: uri,
          testFilter: `${classTestCase?.testFilter}.${match.groups.methodName}`,
        }
        testCases.push(newItem)
      }
    }

    return {
      isTestFile: true,
      documentTest: classTestCase,
      testCases: testCases,
    }
  }