summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChalard Jean <jchalard@google.com>2019-06-27 13:04:22 +0000
committerChalard Jean <jchalard@google.com>2019-06-27 13:05:10 +0000
commit023e00b0848c5d6091209548a4cd4756fc41a20f (patch)
tree8f22b2a06c4e9c648184f70a84d552a656ef6f5d
parent4e9dbbfa5d557aa74d2f51b428cef7fadcd27141 (diff)
Revert "Upgrade and reinforce ThrowingConsumer"
This reverts commit 4e9dbbfa5d557aa74d2f51b428cef7fadcd27141. Reason for revert: Was submitted in a topic with a change that had to be reverted because it broke the build Bug: 136143444 Change-Id: I44224f96118d4a64a18443a03a2728670b8d34e2
-rw-r--r--tests/lib/src/com/android/testutils/ExceptionUtils.java67
-rw-r--r--tests/lib/src/com/android/testutils/MiscAsserts.kt12
-rw-r--r--tests/lib/src/com/android/testutils/ThrowingConsumer.java26
3 files changed, 32 insertions, 73 deletions
diff --git a/tests/lib/src/com/android/testutils/ExceptionUtils.java b/tests/lib/src/com/android/testutils/ExceptionUtils.java
deleted file mode 100644
index e7dbed5..0000000
--- a/tests/lib/src/com/android/testutils/ExceptionUtils.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2019 The Android Open Source Project
- *
- * Licensed 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 com.android.testutils;
-
-import java.util.function.Supplier;
-
-public class ExceptionUtils {
- /**
- * Like a Consumer, but declared to throw an exception.
- * @param <T>
- */
- @FunctionalInterface
- public interface ThrowingConsumer<T> {
- void accept(T t) throws Exception;
- }
-
- /**
- * Like a Supplier, but declared to throw an exception.
- * @param <T>
- */
- @FunctionalInterface
- public interface ThrowingSupplier<T> {
- T get() throws Exception;
- }
-
- /**
- * Like a Runnable, but declared to throw an exception.
- */
- @FunctionalInterface
- public interface ThrowingRunnable {
- void run() throws Exception;
- }
-
-
- public static <T> Supplier<T> ignoreExceptions(ThrowingSupplier<T> func) {
- return () -> {
- try {
- return func.get();
- } catch (Exception e) {
- return null;
- }
- };
- }
-
- public static Runnable ignoreExceptions(ThrowingRunnable r) {
- return () -> {
- try {
- r.run();
- } catch (Exception e) {
- }
- };
- }
-}
diff --git a/tests/lib/src/com/android/testutils/MiscAsserts.kt b/tests/lib/src/com/android/testutils/MiscAsserts.kt
index 63aedd6..f1800b2 100644
--- a/tests/lib/src/com/android/testutils/MiscAsserts.kt
+++ b/tests/lib/src/com/android/testutils/MiscAsserts.kt
@@ -27,16 +27,16 @@ import kotlin.test.assertTrue
private const val TAG = "Connectivity unit test"
fun <T> assertEmpty(ts: Array<T>) = ts.size.let { len ->
- assertEquals(0, len, "Expected empty array, but length was $len")
+ assertEquals(0, len, "Expected empty array, but length was ${len}")
}
fun <T> assertLength(expected: Int, got: Array<T>) = got.size.let { len ->
- assertEquals(expected, len, "Expected array of length $expected, but was $len for $got")
+ assertEquals(expected, len, "Expected array of length ${expected}, but was ${len} for ${got}")
}
// Bridge method to help write this in Java. If you're writing Kotlin, consider using native
// kotlin.test.assertFailsWith instead, as that method is reified and inlined.
-fun <T : Exception> assertThrows(expected: Class<T>, block: ExceptionUtils.ThrowingRunnable): T {
+fun <T: Exception> assertThrows(expected: Class<T>, block: Runnable): T {
return assertFailsWith(expected.kotlin) { block.run() }
}
@@ -51,13 +51,13 @@ fun <T> assertNotEqualEitherWay(o1: T, o2: T) {
}
fun assertStringContains(got: String, want: String) {
- assertTrue(got.contains(want), "$got did not contain \"${want}\"")
+ assertTrue(got.contains(want), "${got} did not contain \"${want}\"")
}
fun assertContainsExactly(actual: IntArray, vararg expected: Int) {
// IntArray#sorted() returns a list, so it's fine to test with equals()
assertEquals(actual.sorted(), expected.sorted(),
- "$actual does not contain exactly $expected")
+ "${actual} does not contain exactly ${expected}")
}
fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) {
@@ -65,7 +65,7 @@ fun <T> assertContainsAll(list: Collection<T>, vararg elems: T) {
}
fun <T> assertContainsAll(list: Collection<T>, elems: Collection<T>) {
- elems.forEach { assertTrue(list.contains(it), "$it not in list") }
+ elems.forEach { assertTrue(list.contains(it), "${it} not in list") }
}
fun assertRunsInAtMost(descr: String, timeLimit: Long, fn: Runnable) {
diff --git a/tests/lib/src/com/android/testutils/ThrowingConsumer.java b/tests/lib/src/com/android/testutils/ThrowingConsumer.java
new file mode 100644
index 0000000..9046652
--- /dev/null
+++ b/tests/lib/src/com/android/testutils/ThrowingConsumer.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed 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 com.android.testutils;
+
+/**
+ * Like a consumer, but throws an exception.
+ * @param <T>
+ */
+@FunctionalInterface
+public interface ThrowingConsumer<T> {
+ void accept(T t) throws Exception;
+}