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

Optionally generate AssertJ assertions for generated Immutable types #1087

Open
KangoV opened this issue Sep 18, 2019 · 6 comments
Open

Optionally generate AssertJ assertions for generated Immutable types #1087

KangoV opened this issue Sep 18, 2019 · 6 comments

Comments

@KangoV
Copy link

KangoV commented Sep 18, 2019

It would be really cool, if at the same time as generating the immutable classes, custom assertions could be generated. AssertJ has an implementation which could be run from the annotation processor. This would mean clean tests when using in the same project or downstream.

Not sure how it would work. Maybe the processor could detect the presence of an assertion generator (AssertJ/Google Truth etc) and then generate.

There could be a an assertion flag on @Style? or a new annotation?

Many ways to do it.

@asereda-gs
Copy link
Member

Can you show some example of assertions you want to be executed ?
Why do they have to be run from annotation processor ?

@cal101
Copy link

cal101 commented Jan 30, 2020

Would https://github.com/assertj/assertj-assertions-generator cover your needs instead?

@asereda-gs
Copy link
Member

While not exactly assertions perhaps you can use Criteria generated DSL which has an expression evaluator already.

One can easily write expressions (evaluators) like:

Person p = ...; 
assertThat(p, person
   .nickName.startsWith("prefix")
   .age.greaterThan(21));

// a nicer option (which is not supported yet) would be
assertThat(person).nickName.startsWith("prefix");

That DSL has already many "assertions" (we call them Matchers)

@cal101
Copy link

cal101 commented Jan 30, 2020

I generated custom assertj assertions myself for AST matcher tests.
What is great about assertj assertions is the detailed error reporting.
You test for existance of some object in collection and on failure the collections and the expectations are printed in understandable form.
Will the DSL-solution explain where something failed?
If nickname does not start with "prefix" will I get a message
"expected nickName to start with 'prefix' but nickname is 'abraxas'" ?

@asereda-gs
Copy link
Member

Will the DSL-solution explain where something failed?

Currently no. But

Probably you can write your own evaluator which would print nice messages (it has all context information).

Or, assuming AssertJ allows creating assertions programmatically, one can convert criteria expression into AssertJ assertion and let it be evaluated by AssertJ.

@cal101
Copy link

cal101 commented Jan 31, 2020

Thanks for the pointer to the evaluator. It should indeed be possible to enhance that for assertJ like expressive messages.

From what I remember from the assertj inner workings it's more comparable to the let's say builder code generated by immutables. There is no "runtime system" or "evaluator" that can be "programmed".
It's static code that allows writing fluent assertions and the error reporting is baked in.

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

3 participants