Skip to content

Commit

Permalink
Added MonthDay tests to everything test. Also, now outputting number …
Browse files Browse the repository at this point in the history
…passed (when there is an error) and minimum number of conversions still needed to be tested.
  • Loading branch information
jdereg committed Feb 4, 2024
1 parent 833649a commit 938afee
Showing 1 changed file with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.MonthDay;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -177,33 +178,21 @@ class ConverterEverythingTest

{ mapOf("_v", "0"), (byte) 0 },
{ mapOf("_v", 0), (byte) 0 },
{ mapOf("value", "0"), (byte) 0 },
{ mapOf("value", 0L), (byte) 0 },

{ mapOf("_v", "1"), (byte) 1 },
{ mapOf("_v", 1), (byte) 1 },
{ mapOf("value", "1"), (byte) 1 },
{ mapOf("value", 1d), (byte) 1 },

{ mapOf("_v","-128"), Byte.MIN_VALUE },
{ mapOf("_v",-128), Byte.MIN_VALUE },
{ mapOf("value","-128"), Byte.MIN_VALUE },
{ mapOf("value",-128L), Byte.MIN_VALUE },

{ mapOf("_v", "127"), Byte.MAX_VALUE },
{ mapOf("_v", 127), Byte.MAX_VALUE },
{ mapOf("value", "127"), Byte.MAX_VALUE },
{ mapOf("value", 127L), Byte.MAX_VALUE },

{ mapOf("_v", "-129"), new IllegalArgumentException("'-129' not parseable as a byte value or outside -128 to 127") },
{ mapOf("_v", -129), (byte)127 },
{ mapOf("value", "-129"), new IllegalArgumentException("'-129' not parseable as a byte value or outside -128 to 127") },
{ mapOf("value", -129L), (byte) 127 },

{ mapOf("_v", "128"), new IllegalArgumentException("'128' not parseable as a byte value or outside -128 to 127") },
{ mapOf("_v", 128), (byte) -128 },
{ mapOf("value", "128"), new IllegalArgumentException("'128' not parseable as a byte value or outside -128 to 127") },
{ mapOf("value", 128L), (byte) -128 },
});
TEST_FACTORY.put(pair(String.class, Byte.class), new Object[][] {
{ "-1", (byte) -1 },
Expand All @@ -224,7 +213,44 @@ class ConverterEverythingTest
{ "-129", new IllegalArgumentException("'-129' not parseable as a byte value or outside -128 to 127") },
{ "128", new IllegalArgumentException("'128' not parseable as a byte value or outside -128 to 127") },
});

TEST_FACTORY.put(pair(Void.class, MonthDay.class), new Object[][] {
{ null, null },
});
TEST_FACTORY.put(pair(MonthDay.class, MonthDay.class), new Object[][] {
{ MonthDay.of(1, 1), MonthDay.of(1, 1) },
{ MonthDay.of(12, 31), MonthDay.of(12, 31) },
{ MonthDay.of(6, 30), MonthDay.of(6, 30) },
});
TEST_FACTORY.put(pair(String.class, MonthDay.class), new Object[][] {
{ "1-1", MonthDay.of(1, 1) },
{ "01-01", MonthDay.of(1, 1) },
{ "--01-01", MonthDay.of(1, 1) },
{ "--1-1", new IllegalArgumentException("Unable to extract Month-Day from string: --1-1") },
{ "12-31", MonthDay.of(12, 31) },
{ "--12-31", MonthDay.of(12, 31) },
{ "-12-31", new IllegalArgumentException("Unable to extract Month-Day from string: -12-31") },
{ "6-30", MonthDay.of(6, 30) },
{ "06-30", MonthDay.of(6, 30) },
{ "--06-30", MonthDay.of(6, 30) },
{ "--6-30", new IllegalArgumentException("Unable to extract Month-Day from string: --6-30") },
});
TEST_FACTORY.put(pair(Map.class, MonthDay.class), new Object[][] {
{ mapOf("_v", "1-1"), MonthDay.of(1, 1) },
{ mapOf("value", "1-1"), MonthDay.of(1, 1) },
{ mapOf("_v", "01-01"), MonthDay.of(1, 1) },
{ mapOf("_v","--01-01"), MonthDay.of(1, 1) },
{ mapOf("_v","--1-1"), new IllegalArgumentException("Unable to extract Month-Day from string: --1-1") },
{ mapOf("_v","12-31"), MonthDay.of(12, 31) },
{ mapOf("_v","--12-31"), MonthDay.of(12, 31) },
{ mapOf("_v","-12-31"), new IllegalArgumentException("Unable to extract Month-Day from string: -12-31") },
{ mapOf("_v","6-30"), MonthDay.of(6, 30) },
{ mapOf("_v","06-30"), MonthDay.of(6, 30) },
{ mapOf("_v","--06-30"), MonthDay.of(6, 30) },
{ mapOf("_v","--6-30"), new IllegalArgumentException("Unable to extract Month-Day from string: --6-30") },
});
}

@BeforeEach
public void before() {
// create converter with default options
Expand All @@ -235,6 +261,8 @@ public void before() {
void testEverything() {
boolean failed = false;
Map<Class<?>, Set<Class<?>>> map = converter.allSupportedConversions();
int neededTests = 0;
int count = 0;

for (Map.Entry<Class<?>, Set<Class<?>>> entry : map.entrySet()) {
Class<?> sourceClass = entry.getKey();
Expand All @@ -247,13 +275,15 @@ void testEverything() {
// Change to throw exception, so that when new conversions are added, the tests will fail until
// an "everything" test entry is added.
System.err.println("No test data for: " + getShortName(sourceClass) + " ==> " + getShortName(targetClass));
neededTests++;
continue;
}

for (int i=0; i < testData.length; i++) {
Object[] testPair = testData[i];
try {
verifyTestPair(sourceClass, targetClass, testPair);
count++;
} catch (Throwable e) {
System.err.println();
System.err.println("{ " + getShortName(sourceClass) + ".class ==> " + getShortName(targetClass) + ".class }");
Expand All @@ -270,24 +300,32 @@ void testEverything() {
}
}

if (neededTests > 0) {
System.err.println("Conversions needing tests: " + neededTests);
}
if (failed) {
throw new RuntimeException("One or more tests failed.");
}
if (neededTests > 0 || failed) {
System.out.println("Tests passed: " + count);
}
}

private void verifyTestPair(Class<?> sourceClass, Class<?> targetClass, Object[] testPair) {
if (testPair.length != 2) {
throw new IllegalArgumentException("Test cases must have two values : { source instance, target instance }");
}

if (testPair[0] != null) {
assertThat(testPair[0]).isInstanceOf(sourceClass);
}

if (testPair[1] instanceof Throwable) {
Throwable t = (Throwable) testPair[1];
assertThatExceptionOfType(t.getClass())
.isThrownBy(() -> converter.convert(testPair[0], targetClass))
.withMessageContaining(((Throwable) testPair[1]).getMessage());
} else {
if (testPair[0] != null) {
assertThat(testPair[0]).isInstanceOf(sourceClass);
}
assertEquals(testPair[1], converter.convert(testPair[0], targetClass));
}
}
Expand Down

0 comments on commit 938afee

Please sign in to comment.