Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
reitzig opened this issue Aug 17, 2020 · 2 comments
Open

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

reitzig opened this issue Aug 17, 2020 · 2 comments

Comments

@reitzig
Copy link

reitzig commented Aug 17, 2020

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.

@reitzig
Copy link
Author

reitzig commented Aug 17, 2020

Never mind my PS: https://joel-costigliola.github.io/assertj/assertj-assertions-generator-maven-plugin.html#custom-templates

The configuration seems very specific, though; on first glance, I don't see how to tell the generator "if the property has type X, use this template instead/also".

@reitzig
Copy link
Author

reitzig commented Aug 2, 2024

I think #220 would be a neater, more generally applicable alternative. Of course, we couldn't get back to the original Assert once we "descend" into the one of the property, so there may be independent value of unwrapping single-property classes/records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant