From 4066d175eddbde9f6ab093996e5eb1b5998fc55d Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 1 Dec 2017 06:32:59 -0800 Subject: [PATCH] Make Correspondence tests work with older versions of Android. They, like older versions of Java, refuse to parse numbers with leading plus signs. With that, the tests run on Android, at least after I also: - increase the timeout, since IterableSubjectTest is slow -- not from any particular test, just from the accumulation of tests. I suppose we could split it, presumably along Correspondence/non-Correspondence lines. What do you think? - pull the Correspondence implementation into a separate class. If we kept it in IterableSubjectTest, then MapSubjectTest and MultimapSubjectTest would also try to run IterableSubjectTest. Or we could avoid that by putting IterableSubjectTest into a separate target, but then we'd have 2 copies of the class when running IterableSubjectTest itself, unless we inserted a special case for *that*... RELNOTES=n/a ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=177585076 --- .../common/truth/IterableSubjectTest.java | 28 +--------- .../google/common/truth/MapSubjectTest.java | 2 +- .../common/truth/MultimapSubjectTest.java | 2 +- .../common/truth/TestCorrespondences.java | 55 +++++++++++++++++++ 4 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 core/src/test/java/com/google/common/truth/TestCorrespondences.java diff --git a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java index cb1601288..4ff642d8d 100644 --- a/core/src/test/java/com/google/common/truth/IterableSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/IterableSubjectTest.java @@ -17,6 +17,7 @@ import static com.google.common.collect.Collections2.permutations; import static com.google.common.truth.Correspondence.tolerance; +import static com.google.common.truth.TestCorrespondences.STRING_PARSES_TO_INTEGER_CORRESPONDENCE; import static com.google.common.truth.Truth.assertThat; import static java.util.Arrays.asList; import static org.junit.Assert.fail; @@ -28,7 +29,6 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; -import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -1093,32 +1093,6 @@ public void isNoneOf() { assertThat(actual).isNoneOf(expectedB, expectedC); } - /** - * A correspondence between strings and integers which tests whether the string parses as the - * integer. Parsing is as specified by {@link Integer#decode(String)}. It considers null to - * correspond to null only. - */ - static final Correspondence STRING_PARSES_TO_INTEGER_CORRESPONDENCE = - new Correspondence() { - - @Override - public boolean compare(@Nullable String actual, @Nullable Integer expected) { - if (actual == null) { - return expected == null; - } - try { - return Integer.decode(actual).equals(expected); - } catch (NumberFormatException e) { - return false; - } - } - - @Override - public String toString() { - return "parses to"; - } - }; - @Test public void comparingElementsUsing_contains_success() { ImmutableList actual = ImmutableList.of("not a number", "+123", "+456", "+789"); diff --git a/core/src/test/java/com/google/common/truth/MapSubjectTest.java b/core/src/test/java/com/google/common/truth/MapSubjectTest.java index fda33e434..1cd27a3cd 100644 --- a/core/src/test/java/com/google/common/truth/MapSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/MapSubjectTest.java @@ -15,7 +15,7 @@ */ package com.google.common.truth; -import static com.google.common.truth.IterableSubjectTest.STRING_PARSES_TO_INTEGER_CORRESPONDENCE; +import static com.google.common.truth.TestCorrespondences.STRING_PARSES_TO_INTEGER_CORRESPONDENCE; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; diff --git a/core/src/test/java/com/google/common/truth/MultimapSubjectTest.java b/core/src/test/java/com/google/common/truth/MultimapSubjectTest.java index a8c177d29..8fd761841 100644 --- a/core/src/test/java/com/google/common/truth/MultimapSubjectTest.java +++ b/core/src/test/java/com/google/common/truth/MultimapSubjectTest.java @@ -15,7 +15,7 @@ */ package com.google.common.truth; -import static com.google.common.truth.IterableSubjectTest.STRING_PARSES_TO_INTEGER_CORRESPONDENCE; +import static com.google.common.truth.TestCorrespondences.STRING_PARSES_TO_INTEGER_CORRESPONDENCE; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; diff --git a/core/src/test/java/com/google/common/truth/TestCorrespondences.java b/core/src/test/java/com/google/common/truth/TestCorrespondences.java new file mode 100644 index 000000000..2d019690a --- /dev/null +++ b/core/src/test/java/com/google/common/truth/TestCorrespondences.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011 Google, Inc. + * + * 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.google.common.truth; + +import javax.annotation.Nullable; + +/** {@link Correspondence} implementations for testing purposes. */ +final class TestCorrespondences { + /** + * A correspondence between strings and integers which tests whether the string parses as the + * integer. Parsing is as specified by {@link Integer#decode(String)}. It considers null to + * correspond to null only. + */ + static final Correspondence STRING_PARSES_TO_INTEGER_CORRESPONDENCE = + new Correspondence() { + + @Override + public boolean compare(@Nullable String actual, @Nullable Integer expected) { + if (actual == null) { + return expected == null; + } + try { + // Older versions of Android reject leading plus signs, per the pre-Java-7 contract: + // https://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#decode(java.lang.String) + // https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#decode(java.lang.String) + if (actual.startsWith("+")) { + actual = actual.substring(1); + } + return Integer.decode(actual).equals(expected); + } catch (NumberFormatException e) { + return false; + } + } + + @Override + public String toString() { + return "parses to"; + } + }; + + private TestCorrespondences() {} +}