Feature: Create "unwrapped" overload for `hasX` if X is a value type

Open
#159 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
java
Domain
tooling

Research direction

No source file or test is named. Start with the generator's Maven plugin path and the existing template customization mechanism mentioned in the issue, then review how generated hasX methods are defined. Before implementation, agree on supported value-type construction patterns, parameter limits, and whether annotation-based aliases are in scope; done means the chosen behavior is specified and covered by generated output tests.

Written by the indexing model from the issue text.

Description

This is potentially a little vague in its broadness, but here goes.

Say we have a value class like e.g.

@Value
class AccountNumber {
    String rawValue;
}

If another class (for which we generate assertions) has a property of this type, we can assert:

assertThat(someObject).hasAccountNumber(new AccountNumber("abc"))

Nicer would be:

assertThat(someObject).hasAccountNumber("abc")

The generator could look for constructors, static _.of methods, and other popular patterns. It's not clear to me if there should be a limit on the length of the parameter list. Anyway, the desired implementation seems straight-forward:

public S hasAccountNumber(String accountNumberRawValue) {
   return hasAccountNumber(new AccountNumber(fooRawValue));
}

As an alternative, there could be an annotation like e.g.

@AssertionAlias
static Foo of(String rawValue) { ... }

On a property Foo bar, this would cause additional generation of something like

public S hasBar(String fooRawValue) {
   return hasBar(Foo.of(fooRawValue));
}

PS: This is probably easily done for specific use cases in any given project using templates, but I don't see documentation for a way to inject custom templates when using the generator through the Maven plugin.

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.