Skip to content

Commit

Permalink
- F ParseInput with array inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Mar 4, 2024
1 parent ac99032 commit 0e7435a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,19 @@ public static <OUT> Function1<String, OUT> getTransformerForClass(Class<OUT> 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<String, OUT> transformer1 = (Function1<String, OUT>) transformers.get(targetType);
return transformer1;
if (targetType.isArray())
{
Class<?> componentType = targetType.getComponentType();
Function1<String, Object> transformer = transformers.get(componentType);
return s -> (OUT) Queryable.as(s.split(",")).select(String::trim).select(transformer).asArray();
}
else
{
return (Function1<String, OUT>) transformers.get(targetType);
}
}
// ************* 1 parameter
public <T1> ParseInputWith1Parameters<T1> withTypes(Class<T1> type1)
Expand Down

0 comments on commit 0e7435a

Please sign in to comment.