Skip to content

Unit test of chain() doesn't verify correct passing on of state #1

@andrewswan

Description

@andrewswan

First off, thanks so much for this repo, it's been really useful to me for getting to grips with how the State monad looks in Java. I'm still struggling with how to use State#chain in practice, given that Java doesn't have for comprehensions like other languages do.

I'm only raising an issue because:

  • I'm not sure if there's a better way of communicating on GitHub, and
  • the unit test for the #chain method doesn't seem to verify that it correctly passes on the updated state (which is the whole purpose of this monad in the first place).

To expand on the latter point, right now the test looks like this:

    @Test
    public void chain() {

        // arrange
        State<String, Integer> homer = State.of("Homer");

        // act
        BiTuple<String, Integer> ran = homer.chain(message -> State.of(format("Hello %s", message))).run(0);

        // assert
        assertThat(ran._1, equalTo("Hello Homer"));
        assertThat(ran._2, equalTo(0));
    }

I see that the type of state is Integer, and that the initial state (the argument to the run method) is 0. The test verifies that the ran._2 field also contains the 0 state. So the state is never modified at any point. You could probably deliberately introduce a bug into the chain method (replace .run(result._2) with .run(s)) and the unit test would still pass.

I'd raise a PR if I knew how to write a better test for chain(). But I'm still in the learning phase of my journey with this monad, so I'm hoping you could please add another test that does demonstrate that the state is correctly passed on? That would help me understand more about how chain is called. Right now it's only called in a context where the state is irrelevant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions