Skip to content

Idea: a bit more readable unit tests #27

@vhotspur

Description

@vhotspur

The current unit tests force the developer to inherit from JUnitTest and then uses an overloaded method to actually execute the test.

Following snippet builds the test a bit more slowly but it clearly states what are the individual strings (note that newlines are added automatically to individual arguments of the setSource to decrease occurrences of the escape sequences). The first test uses fluent interface to make the code more compact.

@Test
public void copy() {
    TruppleTest
        .create()
        .setSource(
            "program copy;",
            "var i : integer;",
            "begin readln(i); writeln(i); end."
        )
        .setInput("42");
        .expectsOutput("42");
        .run();
}
@Test
public void simpleUnit() {
    TruppleTest test = new TruppleTest();
    test.addImport(
        "UNIT math;",
        "",
        "INTERFACE",
        "",
        "function add(a,b:integer): integer;",
        /* etc. */
    );
    test.setStandard("tp6");
    test.setSource(
        "uses math;",
        "begin",
        "    write(add(3,5));",
        "end."
    );
    test.expectsOutput("8");
    test.run();
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions