Feature: Generate IterableAsserts for all generated Asserts

Open
#219 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
java
Domain
tooling

Research direction

Start by tracing the assertion generator that creates FooAssert, then inspect how ListAssert and the assertThat entry points are produced. Compare the requested FooIterableAssert shape with the current generated output and decide how generated iterable assertions should be exposed. Done means generated assertions support the requested singleElement().hasName("Fool") usage without manual wrapper classes.

Written by the indexing model from the issue text.

Description

We would like to write something like this:

List<Foo> result;
assertThat(result).singleElement().hasName("Fool")

Now, we can generate FooAssert with the generator, but the ListAssert we get out of assertThat in this case does, of course, not known about the generated assertion; we get a regular ObjectAssert<Foo>.

We can work around this by creating something like this here:

public class FooIterableAssert
  extends AbstractIterableAssert<FooIterableAssert, Iterable<? extends Foo>, Foo, FooIterableAssert> {

  protected FooIterableAssert(Iterable<? extends Foo> foos, Class<?> selfType) {
    super(foos, selfType);
  }

  @Override
  protected FooAssert toAssert(Foo value, String description) {
    return new FooAssert(value);
  }

  @Override
  protected FooIterableAssert newAbstractIterableAssert(Iterable<? extends Foo> iterable) {
    return new FooIterableAssert(iterable, FooIterableAssert.class);
  }

  public static FooIterableAssert assertThat(List<Foo> foos) {
    return new FooIterableAssert(foos, FooIterableAssert.class);
  }
}

This has the feel of "should be a template" and "surely not only we want this", so I'm raising this issue. :)
I'm also not sure whether one would have/want to fiddle with the entrypoint methods in support of this.

PS: I was considering to use Lombok's @ExtensionMethod on ListAssert to add something like FooAssert singleFoo() to it. Unfortunately, it does not seem possible to get the actual value back from an Assert, so I was stuck there.

Dominant language
Java
Stars
72
Forks
47
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from assertj/assertj-generator

All issues in assertj/assertj-generator

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.