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

Add anysatisfy for Map #1377

Closed
almondtools opened this issue Dec 29, 2018 · 7 comments
Closed

Add anysatisfy for Map #1377

almondtools opened this issue Dec 29, 2018 · 7 comments
Milestone

Comments

@almondtools
Copy link
Contributor

Summary

Allow to verify a bunch of assertions for any key-value pairs of a Map. This would serve the symmetry of the map assert. See #1030 (allSatisfy for Maps) and #889 (anySatisfy for Iterables).

Example

class Person {
  public String firstName;
  public String lastName;

  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

Person max = new Person("Max","Mustermann");
Person min = new Person("Min","Mustermann");
Map<Person, Person> knows = new HashMap<Person, Person>();
knows.put(max, min);
knows.put(max, max);
assertThat(knows).anySatisfy((key, value) -> {
  assertThat(key.firstName).isEqualTo("Max");
  assertThat(key.lastName).isEqualTo("Mustermann");
  assertThat(value.firstName).isEqualTo("Min");
  assertThat(value.lastName).isEqualTo("Mustermann");
});```
@joel-costigliola
Copy link
Member

I agree, @almondtools would you like to contribute it?

@almondtools
Copy link
Contributor Author

I can try, yet I have a question:

I tried to understand the actual code for assertAllSatisfy in Maps and it diverges from the assertAllSatisfy and assertAnySatisfy in Iterables (the latter aggregates the Information, the former simply fails with the delegated assertion).

actual.entrySet().stream()
          .forEach(entry -> entryRequirements.accept(entry.getKey(), entry.getValue()));

assertAnySatisfy for Maps cannot be implemented similar to assertAllSatisfy in Maps since the method should return succesful if only one assertion is succesful (swallowing all failed assertions). So I would prefer also to touch assertAllSatisfy in Maps such that all and any look similar after the contribution. Is this ok for you?

@joel-costigliola
Copy link
Member

joel-costigliola commented Dec 30, 2018

There was an improvement done for assertAllSatisfy in Iterables to gather all elements not satisfying the requirements. It's an improvement we want to also for maps, I have just created #1379 to track that.

Since #1379 will impact assertAllSatisfy, I would prefer that we don't change it at the moment (one thing at a time). I think you can follow assertAnySatisfy from Iterables for the implementation.

almondtools added a commit to almondtools/assertj-core that referenced this issue Dec 30, 2018
@almondtools
Copy link
Contributor Author

The code for #1379 was already written as your answer arrived me. Fortunately I kept both changes separately. I think you can review this one.

The Javadoc does yet not contain version information, I am not certain whether to add a hard version or whether there is a variable that is updated at the next release time. So if you do not want to adjust it yourself, give me a hint.

@joel-costigliola
Copy link
Member

You are too fast! 😆

Put a @since 3.12 annotation for the version in the javadoc, we have started to do that some times ago that's why you won't find it everywhere.

almondtools added a commit to almondtools/assertj-core that referenced this issue Dec 31, 2018
@almondtools
Copy link
Contributor Author

Notify me if I should package a pull request.

@joel-costigliola
Copy link
Member

You can create a PR when you feel the code is ready to be reviewed or want some feedback even if the implementation is partial.

almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 1, 2019
almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 2, 2019
almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 2, 2019
@almondtools almondtools mentioned this issue Jan 2, 2019
almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 3, 2019
* code style
* new (hopefully more feasible) scenario for anySatisfy example
* new assertion idioms for exceptional cases
almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 3, 2019
almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 3, 2019
almondtools added a commit to almondtools/assertj-core that referenced this issue Jan 3, 2019
* code style
* new (hopefully more feasible) scenario for anySatisfy example
* new assertion idioms for exceptional cases
@joel-costigliola joel-costigliola added this to the 3.12 milestone Jan 5, 2019
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

2 participants