Skip to content

Refactor: isolate status logic & remove magic numbers#2490

Closed
EmanuelAry wants to merge 2 commits into
android:mainfrom
EmanuelAry:replace_Magic_Number
Closed

Refactor: isolate status logic & remove magic numbers#2490
EmanuelAry wants to merge 2 commits into
android:mainfrom
EmanuelAry:replace_Magic_Number

Conversation

@EmanuelAry

Copy link
Copy Markdown

…s code mapping to a constant map (STATUS_CODE_MAP), create dedicated methods statusFromCode() and applyStatus() to handle status code parsing and callback invocation. Also extract content extraction to a reusable extractContent() method. This eliminates the long switch with hardcoded integers and makes the STATUS_CODE handling self-documenting and easier to extend.

Overview

This change isolates the logic for converting the AM instrument's status codes into values ​​for the Status enum, eliminating the magic numbers (1, 0, -1, -2, -4) scattered throughout the processLine method.

Proposed Changes

  • Creation of the constant map STATUS_CODE_MAP associating each integer with its respective Status.
  • Extraction of private methods:
    • statusFromCode(int): converts code -> Status, with validation.
    • applyStatus(int): applies the status to the currentTest and triggers onTestStart when STARTED.
    • extractContent(String, String): removes line prefix using substring (safer than replace).
  • Simplification of the STATUS_CODE block in processLine, which now only extracts the code, calls applyStatus, and maintains the finalization in the finally block.

…s code mapping to a constant map (STATUS_CODE_MAP), create dedicated methods statusFromCode() and applyStatus() to handle status code parsing and callback invocation. Also extract content extraction to a reusable extractContent() method. This eliminates the long switch with hardcoded integers and makes the STATUS_CODE handling self-documenting and easier to extend.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the status code processing logic in InstrumentationTestRunnerProcessor.java by replacing a switch-case block with a static map lookup and extracting helper methods like applyStatus and extractContent. However, calling applyStatus(code) outside of the try block introduces a bug where exceptions bypass the finally block, preventing onTestFinished from executing and failing to reset currentTest. It is recommended to move this call inside the try block and remove the Portuguese comment.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines 130 to 136
applyStatus(code);
try {
switch (statusInt) {
case 1:
currentTest.setStatus(Status.STARTED);
onTestStart(currentTest.build());
break;
case 0:
currentTest.setStatus(Status.PASSED);
break;
case -1:
currentTest.setStatus(Status.ERROR);
break;
case -2:
currentTest.setStatus(Status.FAILED);
break;
case -4:
currentTest.setStatus(Status.ASSUMPTION_FAILURE);
break;
default:
throw new IllegalArgumentException(
String.format("Illegal test instrumentation code: \"%s\"", statusCode));
}
// A chamada onTestStart, se necessária, já foi feita em applyStatus
} finally {
onTestFinished(currentTest.build());
currentTest = null;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

By calling applyStatus(code) outside of the try block, any IllegalArgumentException thrown by statusFromCode (when an invalid status code is encountered) will bypass the finally block. This means onTestFinished won't be called and currentTest won't be reset to null, which changes the original error-handling behavior and can corrupt the state for subsequent lines.

Additionally, the empty try block with a Portuguese comment is awkward and inconsistent with the rest of the English codebase.

We should move applyStatus(code) inside the try block.

      try {
        applyStatus(code);
      } finally {
        onTestFinished(currentTest.build());
        currentTest = null;
      }

…s code mapping to a constant map (STATUS_CODE_MAP), create dedicated methods statusFromCode() and applyStatus() to handle status code parsing and callback invocation. Also extract content extraction to a reusable extractContent() method. This eliminates the long switch with hardcoded integers and makes the STATUS_CODE handling self-documenting and easier to extend.
@brettchabot

Copy link
Copy Markdown
Collaborator

sorry, everything in the tools directory is obsolete code, and we aren't accepting PRs for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants