Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Enum values are needlessly overwritten #306

Closed
dtim opened this issue Jun 27, 2022 · 0 comments · Fixed by #1416
Closed

Static Enum values are needlessly overwritten #306

dtim opened this issue Jun 27, 2022 · 0 comments · Fixed by #1416
Assignees
Labels
comp-codegen Issue is related to code generator ctg-enhancement New feature, improvement or change request priority-top-focus Top priority chosen by dev team

Comments

@dtim
Copy link
Collaborator

dtim commented Jun 27, 2022

Description

The code generator overwrites static Enum values with themselves in tests where an Enum constant is required.

To Reproduce

Generate a test suite for the EnumExamples.isRedOrGreen method below.

// Color.java
public enum Color {
    UNDEFINED,
    RED,
    YELLOW,
    GREEN
}

// EnumExamples.java
public class EnumExamples {
    public boolean isRedOrGreen(@NotNull Color color) {
        return color.equals(Color.YELLOW) || color.equals(Color.GREEN);
    }
}

Expected behavior

No static field assignment should take place in any of generated test cases.

Test cases could look like the following example:

    @Test
    @DisplayName("isRedOrGreen: return color.equals(Color.YELLOW) || color.equals(Color.GREEN) : False -> return color.equals(Color.YELLOW) || color.equals(Color.GREEN)")
    public void testIsRedOrGreen_ColorEqualsOrColorEquals() throws ClassNotFoundException, Exception {
        EnumExamples enumExamples = new EnumExamples();
        boolean actual = enumExamples.isRedOrGreen(Color.YELLOW);
        assertTrue(actual);
    }

Actual behavior

Each test case involves static field assignments with their original values.

Visual proofs (screenshots, logs, images)

    ///region SUCCESSFUL EXECUTIONS for method isRedOrGreen(enums.Color)

    /**
     * <pre>
     * Test returns from: {@code return color.equals(Color.YELLOW) || color.equals(Color.GREEN); }
     * </pre>
     */
    @Test
    @DisplayName("isRedOrGreen: return color.equals(Color.YELLOW) || color.equals(Color.GREEN) : False -> return color.equals(Color.YELLOW) || color.equals(Color.GREEN)")
    public void testIsRedOrGreen_ColorEqualsOrColorEquals() throws ClassNotFoundException, Exception {
        Color prevYELLOW = Color.YELLOW;
        try {
            Color yellow = Color.YELLOW;
            Class colorClazz = Class.forName("enums.Color");
            setStaticField(colorClazz, "YELLOW", yellow);
            EnumExamples enumExamples = new EnumExamples();

            boolean actual = enumExamples.isRedOrGreen(yellow);

            assertTrue(actual);
        } finally {
            setStaticField(Color.class, "YELLOW", prevYELLOW);
        }
    }

    /**
     * <pre>
     * Test returns from: {@code return color.equals(Color.YELLOW) || color.equals(Color.GREEN); }
     * </pre>
     */
    @Test
    @DisplayName("isRedOrGreen: return color.equals(Color.YELLOW) || color.equals(Color.GREEN) : False -> return color.equals(Color.YELLOW) || color.equals(Color.GREEN)")
    public void testIsRedOrGreen_ColorEqualsOrColorEquals_1() throws ClassNotFoundException, Exception {
        Color prevYELLOW = Color.YELLOW;
        Color prevGREEN = Color.GREEN;
        try {
            Color yellow = Color.YELLOW;
            Class colorClazz = Class.forName("enums.Color");
            setStaticField(colorClazz, "YELLOW", yellow);
            Color green = Color.GREEN;
            setStaticField(colorClazz, "GREEN", green);
            EnumExamples enumExamples = new EnumExamples();
            Color color = Color.UNDEFINED;

            boolean actual = enumExamples.isRedOrGreen(color);

            assertFalse(actual);
        } finally {
            setStaticField(Color.class, "YELLOW", prevYELLOW);
            setStaticField(Color.class, "GREEN", prevGREEN);
        }
    }

    /**
     * <pre>
     * Test returns from: {@code return color.equals(Color.YELLOW) || color.equals(Color.GREEN); }
     * </pre>
     */
    @Test
    @DisplayName("isRedOrGreen: return color.equals(Color.YELLOW) || color.equals(Color.GREEN) : True -> return color.equals(Color.YELLOW) || color.equals(Color.GREEN)")
    public void testIsRedOrGreen_ColorEqualsOrColorEquals_2() throws ClassNotFoundException, Exception {
        Color prevYELLOW = Color.YELLOW;
        Color prevGREEN = Color.GREEN;
        try {
            Color yellow = Color.YELLOW;
            Class colorClazz = Class.forName("enums.Color");
            setStaticField(colorClazz, "YELLOW", yellow);
            Color green = Color.GREEN;
            setStaticField(colorClazz, "GREEN", green);
            EnumExamples enumExamples = new EnumExamples();

            boolean actual = enumExamples.isRedOrGreen(green);

            assertTrue(actual);
        } finally {
            setStaticField(Color.class, "YELLOW", prevYELLOW);
            setStaticField(Color.class, "GREEN", prevGREEN);
        }
    }
    ///endregion

Environment

Java: 1.8.
Test framework: JUnit5.
Mock strategy: No mocks.
Mock static: No static mocking.
Timeout for class: 60 sec.
Parametrized test: Not parametrized.

Additional context

This issue is not critical, as tests are successfully generated and working. In a way, it may be more a feature request than a bug report.

@dtim dtim added ctg-bug Issue is a bug comp-codegen Issue is related to code generator labels Jun 27, 2022
@korifey korifey moved this to Todo in UTBot Java Jun 27, 2022
@EgorkaKulikov EgorkaKulikov added ctg-enhancement New feature, improvement or change request and removed ctg-bug Issue is a bug labels Jun 30, 2022
@EgorkaKulikov EgorkaKulikov linked a pull request Nov 23, 2022 that will close this issue
Repository owner moved this from Todo to Done in UTBot Java Nov 25, 2022
@alisevych alisevych added this to the 2022.12 Release milestone Nov 30, 2022
@alisevych alisevych added the priority-top-focus Top priority chosen by dev team label Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator ctg-enhancement New feature, improvement or change request priority-top-focus Top priority chosen by dev team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants