Skip to content

Commit

Permalink
Make Correspondence tests work with older versions of Android.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cpovirk authored and ronshapiro committed Dec 4, 2017
1 parent 8c5dba4 commit 4066d17
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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, Integer> STRING_PARSES_TO_INTEGER_CORRESPONDENCE =
new Correspondence<String, Integer>() {

@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<String> actual = ImmutableList.of("not a number", "+123", "+456", "+789");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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, Integer> STRING_PARSES_TO_INTEGER_CORRESPONDENCE =
new Correspondence<String, Integer>() {

@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() {}
}

0 comments on commit 4066d17

Please sign in to comment.