Skip to content

Commit

Permalink
! F ParseInput with 2 parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Feb 19, 2024
1 parent 36b7aa5 commit 9e088cb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.approvaltests;

import org.junit.jupiter.api.Test;

public class Parse2InputTest
{
@Test
void testWithTypesTransformersAndBoth()
{
var expected = """
1,2.2 -> 2.2
1,3.3 -> 3.3
""";
ParseInput.from(expected).withTypes(Integer.class, Double.class).verifyAll(t -> t.getFirst() * t.getSecond());
// TODO: continue here
// ParseInput.from(expected).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString);
// ParseInput.from(expected).withTypes(String.class).transformTo(Integer::parseInt).verifyAll(Integer::toBinaryString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public void verifyAll(Function1<OUT, Object> transform)
Approvals.verifyAll("", parse(), s -> print(s.getFirst(), transform.call(s.getSecond())),
new Options().inline(expected));
}
public <T1, T2> ParseInputWith2Parameters<T1, T2> withTypes(Class<T1> type1, Class<T2> type2)
public <T1, T2> ParseInputWith2Parameters<T1, T2, Tuple<T1, T2>> withTypes(Class<T1> type1, Class<T2> type2)
{
return new ParseInputWith2Parameters<T1, T2>(expected, type1, type2);
return ParseInputWith2Parameters.create(expected, type1, type2);
}
public ParseInput<OUT> multiline()
{
Expand All @@ -107,20 +107,4 @@ public <OUT> ParseInputWith1Parameters<OUT> transformTo(Function1<String, OUT> t
{
return new ParseInputWith1Parameters<>(expected, transformer);
}
public class ParseInputWith2Parameters<T1, T2>
{
private final String expected;
private final Class<T1> type1;
private final Class<T2> type2;
public ParseInputWith2Parameters(String expected, Class<T1> type1, Class<T2> type2)
{
this.expected = expected;
this.type1 = type1;
this.type2 = type2;
}
public <OUT> ParseInput<OUT> transformTo(Function2<T1, T2, OUT> transformer)
{
return ParseInput.create(expected, transformer, type1, type2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.approvaltests;

import com.spun.util.Tuple;
import org.lambda.functions.Function1;
import org.lambda.query.Queryable;

import static org.approvaltests.ParseInput.getTransformerForClass;

public class ParseInputWith2Parameters<IN1, IN2, OUT>
{
private final String expected;
private final Function1<String, OUT> transformer;
public ParseInputWith2Parameters(String expected, Function1<String, OUT> transformer)
{
this.expected = expected;
this.transformer = transformer;
}
public static <IN1, IN2> ParseInputWith2Parameters<IN1, IN2, Tuple<IN1, IN2>> create(String expected,
Class<IN1> type1, Class<IN2> type2)
{
Function1<String, IN1> t1 = getTransformerForClass(type1);
Function1<String, IN2> t2 = getTransformerForClass(type2);
Function1<String, Tuple<IN1, IN2>> f = s -> {
var temp = Queryable.as(s.split(",")).select(String::trim);
IN1 v1 = t1.call(temp.get(0));
IN2 v2 = t2.call(temp.get(1));
return new Tuple<>(v1, v2);
};
return new ParseInputWith2Parameters<>(expected, f);
}
// public <OUT2> ParseInputWith2Parameters<OUT2> transformTo(Function1<IN1, OUT2> transformer1)
// {
// Function1<String, OUT2> transformer2 = (String t) -> transformer1.call(transformer.call(t));
// return new ParseInputWith2Parameters<>(expected, transformer2);
// }
public void verifyAll(Function1<OUT, Object> transform)
{
ParseInput.from(expected, transformer).verifyAll(transform);
}
}

0 comments on commit 9e088cb

Please sign in to comment.