diff --git a/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java b/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java index e12169e9..98449b98 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/Parse2InputTest.java @@ -1,7 +1,10 @@ package org.approvaltests; +import org.approvaltests.reporters.AutoApproveReporter; +import org.approvaltests.reporters.UseReporter; import org.approvaltests.utils.parseinput.ParseInput; import org.junit.jupiter.api.Test; +import org.lambda.query.Queryable; public class Parse2InputTest { @@ -35,4 +38,19 @@ void testPersonAge() ParseInput.from(expected).multiline().withTypes(String.class, Integer.class).transformTo(Person::new) .verifyAll(Person::toString); } -} + @Test + @UseReporter(AutoApproveReporter.class) + public void testArrays() + { + var expected = """ + 1, 1 -> 2.0 + 10 ,1, 1 -> 12.0 + 5,5,7,7 -> 24.0 + """; + ParseInput.from(expected).withTypes(Integer[].class).verifyAll(this::sum); + } + private Double sum(Integer[] integers) + { + return Queryable.of(integers).sum(); + } +} \ No newline at end of file diff --git a/approvaltests/src/main/java/org/approvaltests/utils/parseinput/ParseInput.java b/approvaltests/src/main/java/org/approvaltests/utils/parseinput/ParseInput.java index 6d323726..91533947 100644 --- a/approvaltests/src/main/java/org/approvaltests/utils/parseinput/ParseInput.java +++ b/approvaltests/src/main/java/org/approvaltests/utils/parseinput/ParseInput.java @@ -58,10 +58,19 @@ public static Function1 getTransformerForClass(Class tar put(Long.class, s -> Long.parseLong(s)); put(Float.class, s -> Float.parseFloat(s)); put(Short.class, s -> Short.parseShort(s)); + put(Byte.class, s -> Byte.parseByte(s)); } }; - Function1 transformer1 = (Function1) transformers.get(targetType); - return transformer1; + if (targetType.isArray()) + { + Class componentType = targetType.getComponentType(); + Function1 transformer = transformers.get(componentType); + return s -> (OUT) Queryable.as(s.split(",")).select(String::trim).select(transformer).asArray(); + } + else + { + return (Function1) transformers.get(targetType); + } } // ************* 1 parameter public ParseInputWith1Parameters withTypes(Class type1)