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 flatExtracting for map #1034

Closed
joel-costigliola opened this issue Jul 14, 2017 · 5 comments
Closed

add flatExtracting for map #1034

joel-costigliola opened this issue Jul 14, 2017 · 5 comments
Milestone

Comments

@joel-costigliola
Copy link
Member

joel-costigliola commented Jul 14, 2017

Summary

Iterable assertions provide flatMap extraction but map assertions don't, it was suggested in:
https://stackoverflow.com/questions/45081542/assertj-flatmap-list-of-lists-after-calling-extracting

Example

Map<String, List<String>> testMap  = new HashMap<>();
List<String> nameList = newArrayList("Dave", "Jeff");
List<String> jobList = newArrayList("Plumber", "Builder");
List<String> cityList = newArrayList("Dover", "Boston");

testMap.put("name", nameList);
testMap.put("job", jobList);
testMap.put("city", cityList);

assertThat(testMap)
        .flatExtracting("name", "city", "job")
        .contains("Dave", "Jeff", "Plumber", "Builder", "Dover", "Boston");

Java 8 specific ?

  • YES : to lambda part (PR on master)
  • NO : when extracting by keys (PR on 2.x)
@dafenix
Copy link
Contributor

dafenix commented Oct 14, 2017

Hello there,

i am looking at this.

dafenix added a commit to dafenix/assertj-core that referenced this issue Oct 14, 2017
@joel-costigliola joel-costigliola added this to the 2.9.0 milestone Oct 21, 2017
@Vidit-Gupta
Copy link

@joel-costigliola This looks pretty sweet. However this would not let you check all unique elements if I understand correctly, no?
For example if I have

class A {
 List<Name> names()
}
A[] = {new A("foo", "bar"), new A("foo")}

then applying flatExtracting on A::names I cannot check that it contains exactly ["foo", "bar"] can I?

@PascalSchumacher
Copy link
Member

flatExtracting returns an AbstractObjectArrayAssert. You can use all methods provided by this assertion, e.g. containsExactly, see: https://joel-costigliola.github.io/assertj/core-8/api/org/assertj/core/api/AbstractObjectArrayAssert.html#containsExactly-ELEMENT...-

@Vidit-Gupta
Copy link

@PascalSchumacher assertThat(ImmutableList.of("a", "a", "b")).containsExactly("a", "b") will fail.
What I'm saying is flatExtracting gives me back that list with duplicates (["a", "a", "b"]) hence I cannot use it

@PascalSchumacher
Copy link
Member

PascalSchumacher commented Nov 9, 2017

Not sure if I understand you correctly, but if you want to check all unique elements you can use:

String[] abc = {"a", "a", "b"};
assertThat(abc).containsOnly("a", "b")

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

5 participants