Skip to content

Array element assignment incorrectly transformed during test extraction #18

@guan-kevin

Description

@guan-kevin

The inline test framework incorrectly modifies the target statement when extracting tests for array element assignments. Instead of preserving the array indexing operation, it transforms array elements into flattened variable names, causing compilation issues.

For example, given the following code with inline tests:

private static final int[] MAX_SIZES

MAX_SIZES[i] = MAX_SIZES[i - 1] << 1;  // target statement
itest("", 1).given(i, 2).given(MAX_SIZES, new int[]{1, 10, 2}).checkEq(MAX_SIZES[0], 1).checkEq(MAX_SIZES[1], 10).checkEq(MAX_SIZES[2], 20);  
itest("", 2).given(MAX_SIZES[i - 1], 10).checkEq(MAX_SIZES[i], 20);

The framework generates:

@Test
void testLine1() throws Exception {
        int i = 2;  
        int[] MAX_SIZES = new int[] { 1, 10, 2 };
        int MAX_SIZES__i = MAX_SIZES__i << 1;  // Wrong variable, should be: MAX_SIZES[i] = MAX_SIZES[i - 1] << 1
        assertEquals(1, MAX_SIZES__i); // Wrong variable, should be assertEquals(1, MAX_SIZES[0])
        assertEquals(10, MAX_SIZES__i); // Wrong variable, should be assertEquals(10, MAX_SIZES[1])
        assertEquals(20, MAX_SIZES__i); // Wrong variable, should be assertEquals(20, MAX_SIZES[2])
}

@Test
void testLine2() throws Exception {
        int MAX_SIZES__iminus1 = 10;
        int MAX_SIZES__i = MAX_SIZES__i << 1; // Wrong variable, should be: int MAX_SIZES__i  = MAX_SIZES__iminus1 << 1;
        assertEquals(20, MAX_SIZES__i);
}

and the test code will not compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions