-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
8c5dba4
commit 4066d17
Showing
4 changed files
with
58 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
core/src/test/java/com/google/common/truth/TestCorrespondences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |