[
https://issues.apache.org/jira/browse/GROOVY-12083?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18088131#comment-18088131
]
ASF GitHub Bot commented on GROOVY-12083:
-----------------------------------------
Copilot commented on code in PR #2604:
URL: https://github.com/apache/groovy/pull/2604#discussion_r3392404055
##########
subprojects/groovy-contracts/src/test/groovy/org/apache/groovy/contracts/tests/post/SimplePostconditionTests.groovy:
##########
@@ -323,4 +323,89 @@ class Account {
a.m(null)
}
}
+
+ // GROOVY-12083: with multiple @Ensures only the first was wired in; the
rest were silently dropped
+ @Test
+ void multiple_postconditions_second_is_violated() {
+
+ def source = """
+ import groovy.contracts.*
+
+ class A {
+ @Ensures({ result > 0 })
+ @Ensures({ result < 10 })
+ int m(int n) { n }
+ }
+ """
+
+ def a = create_instance_of(source)
+ assert a.m(5) == 5
+ shouldFail(PostconditionViolation) {
+ a.m(42)
+ }
+ }
+
+ // GROOVY-12083
+ @Test
+ void multiple_postconditions_first_is_violated() {
+
+ def source = """
+ import groovy.contracts.*
+
+ class A {
+ @Ensures({ result < 10 })
+ @Ensures({ result > 0 })
+ int m(int n) { n }
+ }
+ """
+
+ def a = create_instance_of(source)
+ assert a.m(5) == 5
+ shouldFail(PostconditionViolation) {
+ a.m(-3)
+ }
+ }
+
+ // GROOVY-12083: all three postconditions must be evaluated (AND-combined)
+ @Test
+ void multiple_postconditions_all_evaluated() {
+
+ def source = """
+ import groovy.contracts.*
+
+ class A {
+ @Ensures({ result != null })
+ @Ensures({ result.size() == 2 })
+ @Ensures({ result.contains(s) })
+ List<String> toList(String s) { [s, s] }
+ }
+ """
+
+ def a = create_instance_of(source)
+ assert a.toList('x') == ['x', 'x']
+ }
Review Comment:
`multiple_postconditions_all_evaluated` currently only asserts the happy
path where all three `@Ensures` pass. If the third `@Ensures` were accidentally
ignored again, this test would still pass because `toList` returns `[s, s]` and
always satisfies `result.contains(s)`. Adjust the method/test to include a
failing call that only the third postcondition can catch.
##########
subprojects/groovy-contracts/src/test/groovy/org/apache/groovy/contracts/tests/post/SimplePostconditionTests.groovy:
##########
@@ -323,4 +323,89 @@ class Account {
a.m(null)
}
}
+
+ // GROOVY-12083: with multiple @Ensures only the first was wired in; the
rest were silently dropped
+ @Test
+ void multiple_postconditions_second_is_violated() {
+
+ def source = """
+ import groovy.contracts.*
+
+ class A {
+ @Ensures({ result > 0 })
+ @Ensures({ result < 10 })
+ int m(int n) { n }
+ }
+ """
+
+ def a = create_instance_of(source)
+ assert a.m(5) == 5
+ shouldFail(PostconditionViolation) {
+ a.m(42)
+ }
+ }
+
+ // GROOVY-12083
+ @Test
+ void multiple_postconditions_first_is_violated() {
+
+ def source = """
+ import groovy.contracts.*
+
+ class A {
+ @Ensures({ result < 10 })
+ @Ensures({ result > 0 })
+ int m(int n) { n }
+ }
+ """
+
+ def a = create_instance_of(source)
+ assert a.m(5) == 5
+ shouldFail(PostconditionViolation) {
+ a.m(-3)
+ }
Review Comment:
This test is named "multiple_postconditions_first_is_violated" but the
current failing input `-3` violates the *second* `@Ensures` (`result > 0`), not
the first (`result < 10`). Using a value like `42` will specifically violate
the first postcondition and better cover the intended regression.
> groovy-contracts: only the first @Ensures on a method fires
> -----------------------------------------------------------
>
> Key: GROOVY-12083
> URL: https://issues.apache.org/jira/browse/GROOVY-12083
> Project: Groovy
> Issue Type: Bug
> Reporter: Paul King
> Priority: Major
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)