You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2023/01/12 02:02:08 UTC
[groovy] branch master updated: move static typing checks into a separate test plus improve coverage
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new aad0dcf238 move static typing checks into a separate test plus improve coverage
aad0dcf238 is described below
commit aad0dcf2388aaa0deec9410010b68456fea426b9
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Jan 12 12:01:59 2023 +1000
move static typing checks into a separate test plus improve coverage
---
.../groovy/runtime/ArrayGroovyMethods.java | 294 +++++++++------------
.../runtime/ArrayGroovyMethodsSTCTest.groovy | 84 ++++++
.../groovy/runtime/ArrayGroovyMethodsTest.groovy | 89 -------
3 files changed, 202 insertions(+), 265 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index 9147f3248d..c25a47eb45 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -79,16 +79,35 @@ public class ArrayGroovyMethods {
//-------------------------------------------------------------------------
// any
+ /**
+ * Iterates over the contents of a boolean Array, and checks whether
+ * any element is true.
+ * <pre class="groovyTestCase">
+ * boolean[] array1 = [false, true]
+ * assert array1.any()
+ * boolean[] array2 = [false]
+ * assert !array2.any()
+ * </pre>
+ *
+ * @param self the boolean array over which we iterate
+ * @return true if any iteration for the booleans matches the closure predicate
+ * @since 5.0.0
+ */
+ public static boolean any(boolean[] self) {
+ Objects.requireNonNull(self);
+ for (boolean item : self) {
+ if (item) return true;
+ }
+ return false;
+ }
+
/**
* Iterates over the contents of a boolean Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * boolean[] array = [false, true, false]
- * assert array.any{ true == it.booleanValue() }
- * }
- * test()
+ * boolean[] array = [true]
+ * assert array.any{ it }
+ * assert !array.any{ !it }
* </pre>
*
* @param self the boolean array over which we iterate
@@ -109,12 +128,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of a byte Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * byte[] array = [0, 1, 2]
- * assert array.any{ 0 == it.byteValue() }
- * }
- * test()
+ * byte[] array = [0, 1, 2]
+ * assert array.any{ it > 1 }
+ * assert !array.any{ it > 3 }
* </pre>
*
* @param self the byte array over which we iterate
@@ -135,12 +151,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of a char Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * char[] array = ['a' as char, 'b' as char, 'c' as char]
- * assert array.any{ 'a' as char == it.charValue() }
- * }
- * test()
+ * char[] array = ['a', 'b', 'c']
+ * assert array.any{ it <= 'a' }
+ * assert !array.any{ it < 'a' }
* </pre>
*
* @param self the char array over which we iterate
@@ -161,12 +174,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of a short Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * short[] array = [0, 1, 2]
- * assert array.any{ 0 == it.shortValue() }
- * }
- * test()
+ * short[] array = [0, 1, 2]
+ * assert array.any{ it > 1 }
+ * assert !array.any{ it > 3 }
* </pre>
*
* @param self the char array over which we iterate
@@ -187,12 +197,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of an int Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * int[] array = [0, 1, 2]
- * assert array.any{ 0 == it.intValue() }
- * }
- * test()
+ * int[] array = [0, 1, 2]
+ * assert array.any{ it > 1 }
+ * assert !array.any{ it > 3 }
* </pre>
*
* @param self the int array over which we iterate
@@ -213,12 +220,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of a long Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * long[] array = [0, 1, 2]
- * assert array.any{ 0 == it.longValue() }
- * }
- * test()
+ * long[] array = [0L, 1L, 2L]
+ * assert array.any{ it > 1L }
+ * assert !array.any{ it > 3L }
* </pre>
*
* @param self the long array over which we iterate
@@ -239,12 +243,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of a float Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * float[] array = [0, 1, 2]
- * assert array.any{ 0 == it.floatValue() }
- * }
- * test()
+ * float[] array = [0.0f, 1.0f, 2.0f]
+ * assert array.any{ it > 1.5f }
+ * assert !array.any{ it > 2.5f }
* </pre>
*
* @param self the float array over which we iterate
@@ -265,12 +266,9 @@ public class ArrayGroovyMethods {
* Iterates over the contents of a double Array, and checks whether a
* predicate is valid for at least one element.
* <pre class="groovyTestCase">
- * {@code @groovy.transform.TypeChecked}
- * void test(){
- * double[] array = [0, 1, 2]
- * assert array.any{ 0 == it.floatValue() }
- * }
- * test()
+ * double[] array = [0.0d, 1.0d, 2.0d]
+ * assert array.any{ it > 1.5d }
+ * assert !array.any{ it > 2.5d }
* </pre>
*
* @param self the double array over which we iterate
@@ -1464,6 +1462,14 @@ public class ArrayGroovyMethods {
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * boolean[] array1 = [true, false]
+ * boolean[] array2 = [true, false]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a boolean array
* @param right the array being compared
@@ -1471,26 +1477,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(boolean[] left, boolean[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * byte[] array1 = [4, 8]
+ * byte[] array2 = [4, 8]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a byte array
* @param right the array being compared
@@ -1498,26 +1497,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(byte[] left, byte[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * char[] array1 = ['a', 'b']
+ * char[] array2 = ['a', 'b']
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a char array
* @param right the array being compared
@@ -1525,26 +1517,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(char[] left, char[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * short[] array1 = [4, 8]
+ * short[] array2 = [4, 8]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a short array
* @param right the array being compared
@@ -1552,26 +1537,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(short[] left, short[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * int[] array1 = [4, 8]
+ * int[] array2 = [4, 8]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left an int array
* @param right the array being compared
@@ -1579,26 +1557,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(int[] left, int[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * long[] array1 = [4L, 8L]
+ * long[] array2 = [4L, 8L]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a long array
* @param right the array being compared
@@ -1606,26 +1577,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(long[] left, long[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * float[] array1 = [4.0f, 8.0f]
+ * float[] array2 = [4.0f, 8.0f]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a float array
* @param right the array being compared
@@ -1633,26 +1597,19 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(float[] left, float[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
/**
* Compare the contents of this array to the contents of the given array.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * double[] array1 = [4.0d, 8.0d]
+ * double[] array2 = [4.0d, 8.0d]
+ * assert array1 !== array2
+ * assert array1.equals(array2)
+ * </pre>
*
* @param left a double array
* @param right the array being compared
@@ -1660,22 +1617,7 @@ public class ArrayGroovyMethods {
* @since 5.0.0
*/
public static boolean equals(double[] left, double[] right) {
- if (left == null) {
- return right == null;
- }
- if (right == null) {
- return false;
- }
- if (left == right) {
- return true;
- }
- if (left.length != right.length) {
- return false;
- }
- for (int i = 0; i < left.length; i++) {
- if (left[i] != right[i]) return false;
- }
- return true;
+ return Arrays.equals(left, right);
}
//-------------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/ArrayGroovyMethodsSTCTest.groovy b/src/test/org/codehaus/groovy/runtime/ArrayGroovyMethodsSTCTest.groovy
new file mode 100644
index 0000000000..566bcfd139
--- /dev/null
+++ b/src/test/org/codehaus/groovy/runtime/ArrayGroovyMethodsSTCTest.groovy
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.runtime
+
+import groovy.transform.stc.StaticTypeCheckingTestCase
+
+/**
+ * STC Tests for ArrayGroovyMethods
+ */
+class ArrayGroovyMethodsSTCTest extends StaticTypeCheckingTestCase {
+
+ void testAnyForBooleanArray() {
+ assertScript '''
+ boolean[] array = [true]
+ assert array.any{ it.booleanValue() }
+ '''
+ }
+
+ void testAnyForByteArray() {
+ assertScript '''
+ byte[] array = [0, 1, 2]
+ assert array.any{ 1 == it.byteValue() }
+ '''
+ }
+
+ void testAnyForCharArray() {
+ assertScript '''
+ char[] array = ['a', 'b', 'c']
+ assert array.any{ 'c' == it.charValue() }
+ '''
+ }
+
+ void testAnyForShortArray() {
+ assertScript '''
+ short[] array = [0, 1, 2]
+ assert array.any{ 2 == it.shortValue() }
+ '''
+ }
+
+ void testAnyForIntArray() {
+ assertScript '''
+ int[] array = [0, 1, 2]
+ assert array.any{ 1 == it.intValue() }
+ '''
+ }
+
+ void testAnyForLongArray() {
+ assertScript '''
+ long[] array = [0L, 1L, 2L]
+ assert array.any{ 0L == it.longValue() }
+ '''
+ }
+
+ void testAnyForFloatArray() {
+ assertScript '''
+ float[] array = [0.0f, 1.0f, 2.0f]
+ assert array.any{ 0.0f == it.floatValue() }
+ '''
+ }
+
+ void testAnyForDoubleArray() {
+ assertScript '''
+ double[] array = [0.0d, 1.0d, 2.0d]
+ assert array.any{ 0.0d == it.doubleValue() }
+ '''
+ }
+
+}
diff --git a/src/test/org/codehaus/groovy/runtime/ArrayGroovyMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/ArrayGroovyMethodsTest.groovy
deleted file mode 100644
index e25c673b0b..0000000000
--- a/src/test/org/codehaus/groovy/runtime/ArrayGroovyMethodsTest.groovy
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.codehaus.groovy.runtime
-
-import org.junit.Test
-
-import static groovy.test.GroovyAssert.shouldFail
-
-/**
- * Tests for ArrayGroovyMethods
- */
-class ArrayGroovyMethodsTest {
-
- @Test
- void firstErrorCases() {
- assertNoSuchElementForAllPrimitiveEmptyArrays('first')
- }
-
- @Test
- void headErrorCases() {
- assertNoSuchElementForAllPrimitiveEmptyArrays('head')
- }
-
- @Test
- void initErrorCases() {
- assertUnsupportedOperationForAllPrimitiveEmptyArrays('init')
- }
-
- @Test
- void lastErrorCases() {
- assertNoSuchElementForAllPrimitiveEmptyArrays('last')
- }
-
- @Test
- void maxErrorCases() {
- assertUnsupportedOperationForGivenPrimitiveEmptyArrays('max', ['int', 'long', 'double'])
- }
-
- @Test
- void minErrorCases() {
- assertUnsupportedOperationForGivenPrimitiveEmptyArrays('max', ['int', 'long', 'double'])
- }
-
- @Test
- void tailErrorCases() {
- assertUnsupportedOperationForAllPrimitiveEmptyArrays('tail')
- }
-
- private static assertUnsupportedOperationForAllPrimitiveEmptyArrays(String method) {
- assertUnsupportedOperationForGivenPrimitiveEmptyArrays(method,
- ['boolean', 'byte', 'char', 'short', 'int', 'long', 'float', 'double'])
- }
-
-
- private static assertUnsupportedOperationForGivenPrimitiveEmptyArrays(String method, ArrayList<String> types) {
- for (primType in types) {
- def ex = shouldFail(UnsupportedOperationException) {
- Eval.me("new $primType[0]")."$method"()
- }
- assert ex.message == "Accessing $method() is unsupported for an empty array"
- }
- }
-
- private static assertNoSuchElementForAllPrimitiveEmptyArrays(String method) {
- for (primType in ['boolean', 'byte', 'char', 'short', 'int', 'long', 'float', 'double']) {
- def ex = shouldFail(NoSuchElementException) {
- Eval.me("new $primType[0]")."$method"()
- }
- assert ex.message == "Cannot access $method() for an empty array"
- }
- }
-
-}