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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to