From a9560b24d8fabbaf18799960c64e5d3216388be1 Mon Sep 17 00:00:00 2001 From: Llewellyn Falco Date: Mon, 12 Feb 2024 13:57:40 -0700 Subject: [PATCH] - F better object loading --- .../java/org/approvaltests/ParseInput.java | 25 ++++++++++++++++++- .../org/approvaltests/ParseInputTest.java | 10 ++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java b/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java index ec978a9d..69c07a94 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java @@ -3,6 +3,7 @@ import com.spun.util.Tuple; import org.approvaltests.core.Options; import org.lambda.functions.Function1; +import org.lambda.functions.Function2; import org.lambda.query.Query; import org.lambda.query.Queryable; @@ -32,8 +33,28 @@ public static ParseInput createFromParts(String expected, Function1(s, transformer.call(temp)); }); } + + public static ParseInput createFromParts(String expected, Function2 transformer, Class in1, Class in2) + { + Function1 t1 = getTransformerForClass(in1); + Function1 t2 = getTransformerForClass(in2); + + return new ParseInput(expected, s -> { + var temp = Queryable.as(s.split(",")).select(String::trim); + IN1 v1 = t1.call(temp.get(0)); + IN2 v2 = t2.call(temp.get(1)); + T out = transformer.call(v1, v2); + return new Tuple<>(s, out); + }); + } + public static ParseInput create(String expected, Class tranformTo) { + Function1 transformer1 = getTransformerForClass(tranformTo); + return ParseInput.create(expected, transformer1); + } + + private static Function1 getTransformerForClass(Class tranformTo) { var transformers = new HashMap, Function1>() { { @@ -46,8 +67,10 @@ public static ParseInput create(String expected, Class tranformTo) put(Short.class, s -> Short.parseShort(s)); } }; - return ParseInput.create(expected, (Function1) transformers.get(tranformTo)); + Function1 transformer1 = (Function1) transformers.get(tranformTo); + return transformer1; } + public Queryable> parse(String expected) { return Queryable.as(expected.lines()).select(l -> l.split("->")[0].trim()).select(l -> transformer.call(l)); diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java b/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java index ba3ef326..0ab185f4 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java @@ -55,4 +55,14 @@ void testPeople() Creator createPerson = (a) -> new Person(a.get(0), Integer.parseInt(a.get(1))); ParseInput.createFromParts(expected, createPerson).verifyAll(s -> s.getAgeLabel()); } + @Test + void testPeopleEasyLoad() + { + var expected = """ + Llewellyn, 25 -> adult + Oliver, 15 -> teenager + """; + ParseInput.createFromParts(expected, (n,a) -> new Person(n,a), String.class, Integer.class) + .verifyAll(s -> s.getAgeLabel()); + } } \ No newline at end of file