Skip to content

Wrong descriptions are generated for switch cases #756

@alisevych

Description

@alisevych

Description

Human readable test names have missing arguments.
Some test names are assigned to wrong tests.

To Reproduce

Steps to reproduce the behavior:

Generate tests with default settings for the following code:

class Solution {
    public int romanToInt(String s) {
        if (s == null) {
            throw new IllegalArgumentException("string is null");
        }
        int length = s.length();
        if (length < 1 || length > 15) {
            throw new IllegalArgumentException("1 <= s.length <= 15, but it is: " + length);
        }
        int result = 0, cursor = 0, previous = 1000, group = 0;
        for (int i = 0; i < length; i++) {
            previous = cursor;
            cursor = symbolToInt(s.charAt(i));
            if (previous == cursor) {
                group += cursor;
            }
            if (previous > cursor) {
                result += group;
                group = cursor;
            }
            if (previous < cursor) {
                group = cursor - group;
            }
        }
        result += group;
        return result;
    }

    private int symbolToInt(char c) {
        switch (c) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
            default: throw new IllegalArgumentException("Unrecognized symbol: " + c);
        }
    }
}

Expected behavior

DisplayName annotations must be correct and consistent.

Actual behavior

The following test is present - where 'M' is described, while 'I' is used.

    /**
     * <pre>
     * Test iterates the loop {@code for(int i = 0; i < length; i++) } once,
     *     inside this loop, the test calls {@link Solution#symbolToInt(char)},
     *     there it activates switch case: {@code 'M' }, returns from: {@code return 1; }
     * Test then returns from: {@code return result; }
     * </pre>
     */
    @Test
    @DisplayName("romanToInt: switch(c) case: 'M' -> return 1")
    public void testRomanToInt_Return1() {
        Solution solution = new Solution();
        String s = "I";

        int actual = solution.romanToInt(s);

        assertEquals(1, actual);
    }

Also some tests have missing argument in description, F.i.:

    /**
     * <pre>
     * Test iterates the loop {@code for(int i = 0; i < length; i++) } once,
     *     inside this loop, the test calls {@link Solution#symbolToInt(char)},
     *     there it returns from: {@code return 1000; }
     * Test afterwards returns from: {@code return result; }
     * </pre>
     */
    @Test
    @DisplayName("romanToInt: switch(c) case:  -> return 1000")
    public void testRomanToInt_Return1000() {
        Solution solution = new Solution();
        String s = "M";

        int actual = solution.romanToInt(s);

        assertEquals(1000, actual);
    }

Environment

IntelliJ IDEA 2022.1.4 (JRE 11)
Gradle project with JDK 8 / 11
New plugin build from main branch

Metadata

Metadata

Labels

comp-summariesSomething related to the method names, code comments and display names generationctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions