Any sequence always ends with an empty sequence. That is always true.
While this may seem silly, the current behaviour limits the usefulness of property-based tests (I was playing around with junit-quickcheck when I noticed the error).
...
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>2.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.pholser</groupId>
<artifactId>junit-quickcheck-generators</artifactId>
<version>0.7</version>
<scope>test</scope>
</dependency>
...
public final class Lists {
public static final List<String> prepend(String head, String... tail) {
List<String> result = new ArrayList<>(1 + tail.length);
result.add(head);
result.addAll(asList(tail));
return result;
}
}
@RunWith(JUnitQuickcheck.class)
public class ListsTest {
@Property
public void prepends_head_to_list(String head, ArrayList<String> strings) {
String[] tail = strings.toArray(new String[strings.size()]);
List<String> result = Lists.prepend(head, tail);
assertThat(result)
.hasSize(1 + tail.length)
.startsWith(head)
.endsWith(tail);
}
}
That will yield:
java.lang.AssertionError: Property prepends_head_to_list falsified via shrinking: actual is not empty
Shrunken args: [, []]
Original failure message: [actual is not empty]
Original args: [, []]
Seeds: [-6556694662598446822, -5206529275569188621]
at org.assertj.core.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty(CommonValidations.java:92)
at org.assertj.core.internal.Iterables.commonCheckThatIterableAssertionSucceeds(Iterables.java:598)
at org.assertj.core.internal.Iterables.assertEndsWith(Iterables.java:579)
at org.assertj.core.api.AbstractIterableAssert.endsWith(AbstractIterableAssert.java:354)
at org.assertj.core.api.ListAssert.endsWith(ListAssert.java:112)
at io.xxx.internal.collection.ListsTest.prepends_head_to_list(ListsTest.java:39)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at com.pholser.junit.quickcheck.runner.PropertyVerifier$2.evaluate(PropertyVerifier.java:104)
at com.pholser.junit.quickcheck.runner.PropertyVerifier$1.evaluate(PropertyVerifier.java:76)
at com.pholser.junit.quickcheck.runner.PropertyVerifier.verify(PropertyVerifier.java:68)
at com.pholser.junit.quickcheck.runner.ShrinkNode.verifyProperty(ShrinkNode.java:98)
at com.pholser.junit.quickcheck.runner.Shrinker.shrink(Shrinker.java:82)
at com.pholser.junit.quickcheck.runner.PropertyStatement.shrink(PropertyStatement.java:158)
at com.pholser.junit.quickcheck.runner.PropertyStatement.lambda$property$38(PropertyStatement.java:132)
at com.pholser.junit.quickcheck.runner.PropertyVerifier$1.evaluate(PropertyVerifier.java:86)
at com.pholser.junit.quickcheck.runner.PropertyVerifier.verify(PropertyVerifier.java:68)
at com.pholser.junit.quickcheck.runner.PropertyStatement.verifyProperty(PropertyStatement.java:108)
at com.pholser.junit.quickcheck.runner.PropertyStatement.evaluate(PropertyStatement.java:92)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.AssertionError: actual is not empty
at org.assertj.core.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty(CommonValidations.java:92)
at org.assertj.core.internal.Iterables.commonCheckThatIterableAssertionSucceeds(Iterables.java:598)
at org.assertj.core.internal.Iterables.assertEndsWith(Iterables.java:579)
at org.assertj.core.api.AbstractIterableAssert.endsWith(AbstractIterableAssert.java:354)
at org.assertj.core.api.ListAssert.endsWith(ListAssert.java:112)
at io.xxx.internal.collection.ListsTest.prepends_head_to_list(ListsTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at com.pholser.junit.quickcheck.runner.PropertyVerifier$2.evaluate(PropertyVerifier.java:104)
at com.pholser.junit.quickcheck.runner.PropertyVerifier$1.evaluate(PropertyVerifier.java:76)
... 17 more
Nope.
@fbiville you have a point, but it's a bit weird to allow this assertion:
assertThat(list(1,2,3)).endsWith()
I'm not yet fully convinced to allow that ... I still need to think about it.
I wonder if there is a mathematical definition of endsWith to setlle this, I could not find one but I only spent 5min looking.
I was more thinking of assertThat(list(1,2,3)).endsWith(array) where array.length == 0.
If the initial method supported only varargs, what about introducing instead:
endsWith(first: T, rest: T...)endsWith(T[])The first overload keeps the "comfort" of using varargs while avoiding the weird endsWith() and the second would happily allow endsWith(myEmptyArray).
@joel-costigliola Haskell Data.List#isSuffixOf example (try it with https://repl.it/languages/haskell)
$> :m + Data.List
$> isSuffixOf "c" "abc"
=> True
$> isSuffixOf "a" "abc"
=> False
$> isSuffixOf "" "string is just a sequence of chars"
=> True
$> isSuffixOf "" ""
=> True
Just to make it clear, I would be happy with adding your suggestion to solve the issue:
endsWith(first: T, rest: T...)endsWith(T[])Once https://github.com/joel-costigliola/assertj-core/pull/1074 is merged, I'm thinking of adding the junit-quickcheck example with this assertion in assertj-examples.
WDYT @joel-costigliola? Would this example be too complex?
happy to have some examples with junit-quickcheck - I like property based testing :)