Skip to content

Clean up ReaderTest's assertion style #380

@tgregg

Description

@tgregg

ReaderTest contains many tests like the following

    @Test
    public void testReadingDecimalAsInteger()
    {
        // decimal value <space> int conversion
        read(
            "0. 0 " +
            "-2147483648. -2147483648 " + // Min int
            "2147483647. 2147483647 " // Max int
        );
        while (in.next() != null)
        {
            int actual = in.intValue();

            in.next();
            int expected = in.intValue();

            assertEquals(expected, actual);
        }
    }

Inside read, every other value is an "actual" or an "expected" value, which is a strange pattern. It would be more readable to structure these tests like the following.

    int readInt(String text) {
        read(text);
        if (in.next() == null) {
            fail("Expected to read an integer");
        }
        return in.intValue();
    }

    @Test
    public void testReadingDecimalAsInteger()
    {
        // decimal value <space> int conversion
        assertEquals(readInt("0."), readInt("0"));
        assertEquals(readInt("-2147483648."), readInt("-2147483648")); // Min int
        assertEquals(readInt("2147483647."), readInt("2147483647")); // Max int
    }

See #355 (comment) for more info.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions