aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Nyman <jnyman@google.com>2023-10-13 11:03:11 +0000
committerJens Nyman <jnyman@google.com>2023-10-13 11:03:11 +0000
commit782fec0510bf19a3d88ca54f331c37259da19ebc (patch)
treef2ce6d83bacc8e3884bab4a24bb0528dd595af03
parent7bf68d217ce5a14af3b472f3e40373175dc04bc6 (diff)
downloadTestParameterInjector-782fec0510bf19a3d88ca54f331c37259da19ebc.tar.gz
Add support for BigInteger and UnsignedLong
-rw-r--r--junit4/src/main/java/com/google/testing/junit/testparameterinjector/ParameterValueParsing.java22
-rw-r--r--junit4/src/test/java/com/google/testing/junit/testparameterinjector/ParameterValueParsingTest.java26
-rw-r--r--junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/ParameterValueParsing.java22
3 files changed, 70 insertions, 0 deletions
diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/ParameterValueParsing.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/ParameterValueParsing.java
index 09e75af..f18db3d 100644
--- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/ParameterValueParsing.java
+++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/ParameterValueParsing.java
@@ -23,10 +23,12 @@ import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.primitives.Primitives;
+import com.google.common.primitives.UnsignedLong;
import com.google.common.reflect.TypeToken;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
+import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
@@ -63,6 +65,11 @@ final class ParameterValueParsing {
return new Yaml(new SafeConstructor(new LoaderOptions())).load(yamlString);
}
+ private static UnsignedLong parseYamlSignedLongToUnsignedLong(long number) {
+ checkState(number >= 0, "%s should be greater than or equal to zero", number);
+ return UnsignedLong.fromLongBits(number);
+ }
+
@SuppressWarnings({"unchecked"})
static Object parseYamlObjectToJavaType(Object parsedYaml, TypeToken<?> javaType) {
// Pass along null so we don't have to worry about it below
@@ -93,6 +100,21 @@ final class ParameterValueParsing {
.supportParsedType(Integer.class, Integer::longValue);
yamlValueTransformer
+ .ifJavaType(UnsignedLong.class)
+ .supportParsedType(Long.class, self -> parseYamlSignedLongToUnsignedLong(self.longValue()))
+ .supportParsedType(
+ Integer.class, self -> parseYamlSignedLongToUnsignedLong(self.longValue()))
+ // UnsignedLong::valueOf(BigInteger) will validate that BigInteger is in the valid range and
+ // throws otherwise.
+ .supportParsedType(BigInteger.class, UnsignedLong::valueOf);
+
+ yamlValueTransformer
+ .ifJavaType(BigInteger.class)
+ .supportParsedType(Long.class, self -> BigInteger.valueOf(self.longValue()))
+ .supportParsedType(Integer.class, self -> BigInteger.valueOf(self.longValue()))
+ .supportParsedType(BigInteger.class, self -> self);
+
+ yamlValueTransformer
.ifJavaType(Float.class)
.supportParsedType(Float.class, self -> self)
.supportParsedType(Double.class, Double::floatValue)
diff --git a/junit4/src/test/java/com/google/testing/junit/testparameterinjector/ParameterValueParsingTest.java b/junit4/src/test/java/com/google/testing/junit/testparameterinjector/ParameterValueParsingTest.java
index 2a4e1aa..76cac1f 100644
--- a/junit4/src/test/java/com/google/testing/junit/testparameterinjector/ParameterValueParsingTest.java
+++ b/junit4/src/test/java/com/google/testing/junit/testparameterinjector/ParameterValueParsingTest.java
@@ -18,7 +18,9 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.common.base.CharMatcher;
import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedLong;
import com.google.protobuf.ByteString;
+import java.math.BigInteger;
import javax.annotation.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -56,6 +58,30 @@ public class ParameterValueParsingTest {
/* yamlString= */ "442147483648",
/* javaClass= */ String.class,
/* expectedResult= */ "442147483648"),
+ BIG_INTEGER_TO_BIGINTEGER(
+ /* yamlString= */ "1000000000000000000000000000",
+ /* javaClass= */ BigInteger.class,
+ /* expectedResult= */ new BigInteger("1000000000000000000000000000")),
+ BIG_INTEGER_TO_UNSIGNED_LONG(
+ /* yamlString= */ "18446744073709551615", // This is UnsignedLong.MAX_VALUE.
+ /* javaClass= */ UnsignedLong.class,
+ /* expectedResult= */ UnsignedLong.MAX_VALUE),
+ LONG_TO_UNSIGNED_LONG(
+ /* yamlString= */ "10000000000000",
+ /* javaClass= */ UnsignedLong.class,
+ /* expectedResult= */ UnsignedLong.fromLongBits(10000000000000L)),
+ LONG_TO_BIG_INTEGER(
+ /* yamlString= */ "10000000000000",
+ /* javaClass= */ BigInteger.class,
+ /* expectedResult= */ BigInteger.valueOf(10000000000000L)),
+ INTEGER_TO_BIG_INTEGER(
+ /* yamlString= */ "1000000",
+ /* javaClass= */ BigInteger.class,
+ /* expectedResult= */ BigInteger.valueOf(1000000)),
+ INTEGER_TO_UNSIGNED_LONG(
+ /* yamlString= */ "1000000",
+ /* javaClass= */ UnsignedLong.class,
+ /* expectedResult= */ UnsignedLong.fromLongBits(1000000)),
DOUBLE_TO_STRING(
/* yamlString= */ "1.23", /* javaClass= */ String.class, /* expectedResult= */ "1.23"),
diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/ParameterValueParsing.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/ParameterValueParsing.java
index 9080bf5..3183594 100644
--- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/ParameterValueParsing.java
+++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/ParameterValueParsing.java
@@ -23,10 +23,12 @@ import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.common.primitives.Primitives;
+import com.google.common.primitives.UnsignedLong;
import com.google.common.reflect.TypeToken;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
+import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
@@ -63,6 +65,11 @@ final class ParameterValueParsing {
return new Yaml(new SafeConstructor(new LoaderOptions())).load(yamlString);
}
+ private static UnsignedLong parseYamlSignedLongToUnsignedLong(long number) {
+ checkState(number >= 0, "%s should be greater than or equal to zero", number);
+ return UnsignedLong.fromLongBits(number);
+ }
+
@SuppressWarnings({"unchecked"})
static Object parseYamlObjectToJavaType(Object parsedYaml, TypeToken<?> javaType) {
// Pass along null so we don't have to worry about it below
@@ -93,6 +100,21 @@ final class ParameterValueParsing {
.supportParsedType(Integer.class, Integer::longValue);
yamlValueTransformer
+ .ifJavaType(UnsignedLong.class)
+ .supportParsedType(Long.class, self -> parseYamlSignedLongToUnsignedLong(self.longValue()))
+ .supportParsedType(
+ Integer.class, self -> parseYamlSignedLongToUnsignedLong(self.longValue()))
+ // UnsignedLong::valueOf(BigInteger) will validate that BigInteger is in the valid range and
+ // throws otherwise.
+ .supportParsedType(BigInteger.class, UnsignedLong::valueOf);
+
+ yamlValueTransformer
+ .ifJavaType(BigInteger.class)
+ .supportParsedType(Long.class, self -> BigInteger.valueOf(self.longValue()))
+ .supportParsedType(Integer.class, self -> BigInteger.valueOf(self.longValue()))
+ .supportParsedType(BigInteger.class, self -> self);
+
+ yamlValueTransformer
.ifJavaType(Float.class)
.supportParsedType(Float.class, self -> self)
.supportParsedType(Double.class, Double::floatValue)