ShouldContainOnly is used in a few places where we know what kind of elements we are dealing with, in this case instead of using the term element we could use a more descriptive name.
Spliterator<?> actual = createSpliterator(SORTED | ORDERED);
spliterators.assertHasOnlyCharacteristics(INFO, actual, DISTINCT, SORTED);
fails with this error
Expecting:
<["ORDERED", "SORTED"]>
to contain only:
<["DISTINCT", "SORTED"]>
elements not found:
<["DISTINCT"]>
and elements not expected:
<["ORDERED"]>
but we could have
Expecting spliterator characteristics:
<["ORDERED", "SORTED"]>
to contain only:
<["DISTINCT", "SORTED"]>
characteristics not found:
<["DISTINCT"]>
and characteristics not expected:
<["ORDERED"]>
Same thing for ShouldContain.
Hi @joel-costigliola ,
I would like to work on this issue.
Go for it @WuYff!
Hi @joel-costigliola ,
I tried to implement this improvement.
My idea is: If the method is called through the class of Arrays/Maps/Spliterators, it will print the corresponding name. If not, I will use the class name of the parameter Object actual as the name.
The problem is that I find some existing tests would fail, because they check for the output in a hard code way. For example:
@Test
public void should_be_able_to_catch_exceptions_thrown_by_map_assertions() {
// GIVEN
Map<String, String> map = mapOf(MapEntry.entry("54", "55"));
// WHEN
softly.assertThat(map).contains(MapEntry.entry("1", "2")).isEmpty();
// THEN
List<Throwable> errors = softly.errorsCollected();
assertThat(errors).hasSize(2);
assertThat(errors.get(0)).hasMessageStartingWith(format("%nExpecting:%n"
+ " <{\"54\"=\"55\"}>%n"
+ "to contain:%n"
+ " <[MapEntry[key=\"1\", value=\"2\"]]>%n"
+ "but could not find:%n"
+ " <[MapEntry[key=\"1\", value=\"2\"]]>%n"));
assertThat(errors.get(1)).hasMessageStartingWith(format("%nExpecting empty but was:<{\"54\"=\"55\"}>"));
}
with test failed message:
java.lang.AssertionError:
Expecting:
<"
Expecting map:
<{"54"="55"}>
to contain:
<[MapEntry[key="1", value="2"]]>
but could not find:
<[MapEntry[key="1", value="2"]]>
">
to start with:
<"
Expecting:
<{"54"="55"}>
to contain:
<[MapEntry[key="1", value="2"]]>
but could not find:
<[MapEntry[key="1", value="2"]]>
">
I also find that similarly hard code tests in ShouldContain_create_Test.java and ShouldContainOnly_create_Test.java
Should I modify the existing tests? Do you have any suggestions or ideas?
The failing tests should be refactored by using shoudContain to build the error message.
For the given example the message assertions can be replaced by:
assertThat(errors.get(0)).hasMessage(shouldContain(map,
newLinkedHashSet(MapEntry.entry("1", "2")),
newLinkedHashSet(MapEntry.entry("1", "2"))).create());
Let me know if you have other questions.
Don't hesitate to raise a pull request even if it is not complete if you want to discuss a specific implementation point.
Done in the the above PRs.
Most helpful comment
Hi @joel-costigliola ,
I would like to work on this issue.