Skip to content

Commit

Permalink
- r clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Feb 26, 2024
1 parent 7717338 commit f7bf60e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ void testWithTypesTransformersAndBoth()
1,3.3 -> 3.3
""";
ParseInput.from(expected).withTypes(Integer.class, Double.class).verifyAll(t -> t.getFirst() * t.getSecond());
ParseInput.from(expected).withTypes(Integer.class, Double.class).verifyAll((i,d) -> i * d);
ParseInput.from(expected).transformTo(Integer::parseInt, Double::parseDouble).verifyAll(t -> t.getFirst() * t.getSecond());












// ParseInput.from(expected).withTypes(String.class).transformTo(Integer::parseInt)
// .verifyAll(Integer::toBinaryString);
ParseInput.from(expected).withTypes(Integer.class, Double.class).verifyAll((i, d) -> i * d);
ParseInput.from(expected).transformTo(Integer::parseInt, Double::parseDouble)
.verifyAll(t -> t.getFirst() * t.getSecond());
ParseInput.from(expected).withTypes(Integer.class, Double.class).transformTo((i, d) -> i * d)
.verifyAll(t -> t);
}
@Test
void testPersonAge()
{
var expected = """
Llewellyn, 25 -> Person[
name=Llewellyn
label=adult
]
Oliver, 15 -> Person[
name=Oliver
label=teenager
]
""";
ParseInput.from(expected).multiline().withTypes(String.class, Integer.class).transformTo(Person::new)
.verifyAll(Person::toString);
}
}
93 changes: 28 additions & 65 deletions approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,48 @@
import com.spun.util.Tuple;
import org.approvaltests.core.Options;
import org.lambda.functions.Function1;
import org.lambda.functions.Function2;
import org.lambda.query.Queryable;

import java.util.HashMap;

public class ParseInput<OUT>
{
private final String expected;
private final boolean multiline;
private final Function1<String, Tuple<String, OUT>> transformer;
private boolean multiline;
public ParseInput(String expected, Function1<String, Tuple<String, OUT>> transformer, boolean multiline)
ParseInput(String expected, Function1<String, Tuple<String, OUT>> transformer, boolean multiline)
{
this.expected = expected;
this.transformer = transformer;
this.multiline = multiline;
}
ParseInput(String expected, Function1<String, Tuple<String, OUT>> transformer)
{
this(expected, transformer, false);
}
public static ParseInput<String> from(String expected)
{
return new ParseInput<String>(expected, s -> new Tuple<>(s, s));
return new ParseInput<String>(expected, s -> new Tuple<>(s, s), false);
}

public static <OUT> ParseInput<OUT> create(String expected, Function1<Queryable<String>, OUT> transformer)
public ParseInput<OUT> multiline()
{
return new ParseInput<OUT>(expected, s -> {
var temp = Queryable.as(s.split(",")).select(String::trim);
return new Tuple<>(s, transformer.call(temp));
});
return new ParseInput<>(expected, transformer, true);
}
public static <IN1, IN2, OUT> ParseInput<OUT> create(String expected, Function2<IN1, IN2, OUT> transformer,
Class<IN1> in1, Class<IN2> in2)
public Queryable<Tuple<String, OUT>> parse()
{
Function1<String, IN1> t1 = getTransformerForClass(in1);
Function1<String, IN2> t2 = getTransformerForClass(in2);
return new ParseInput<OUT>(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));
OUT out = transformer.call(v1, v2);
return new Tuple<>(temp.join(", "), out);
});
Function1<String, Boolean> f = multiline ? s -> s.contains("->") : s -> true;
return Queryable.as(expected.lines()) //
.where(f) //
.select(l -> l.split("->")[0].trim()) //
.where(l -> !l.isEmpty()) //
.select(l -> transformer.call(l));
}
public static <OUT> ParseInput<OUT> from(String expected, Class<OUT> tranformTo)
public Queryable<OUT> getInputs()
{
Function1<String, OUT> transformer1 = getTransformerForClass(tranformTo);
return new ParseInput<OUT>(expected, s -> new Tuple<>(s, transformer1.call(s)));
return parse().select(Tuple::getSecond);
}
public <OUT> ParseInputWith1Parameters<OUT> withTypes(Class<OUT> type1)
public void verifyAll(Function1<OUT, Object> transform)
{
return ParseInputWith1Parameters.create(expected, type1, multiline);
Approvals.verifyAll("", parse(), s -> String.format("%s -> %s", s.getFirst(), transform.call(s.getSecond())),
new Options().inline(expected));
}
// public <OUT> ParseInput<OUT> transformTo(Function1<String, OUT> transformer)
// {
// return new ParseInput<OUT>(expected, s -> new Tuple<>(s, transformer.call(s)));
// }
public static <OUT> Function1<String, OUT> getTransformerForClass(Class<OUT> tranformTo)
public static <OUT> Function1<String, OUT> getTransformerForClass(Class<OUT> targetType)
{
var transformers = new HashMap<Class<?>, Function1<String, Object>>()
{
Expand All @@ -75,47 +58,27 @@ public static <OUT> Function1<String, OUT> getTransformerForClass(Class<OUT> tra
put(Short.class, s -> Short.parseShort(s));
}
};
Function1<String, OUT> transformer1 = (Function1<String, OUT>) transformers.get(tranformTo);
Function1<String, OUT> transformer1 = (Function1<String, OUT>) transformers.get(targetType);
return transformer1;
}
public Queryable<Tuple<String, OUT>> parse()
// ************* 1 parameter
public <T1> ParseInputWith1Parameters<T1> withTypes(Class<T1> type1)
{
Function1<String, Boolean> f = multiline ? s -> s.contains("->") : s -> true;
return Queryable.as(expected.lines()) //
.where(f) //
.select(l -> l.split("->")[0].trim()) //
.where(l -> !l.isEmpty()) //
.select(l -> transformer.call(l));
}
public String print(String input, Object output)
{
return input + " -> " + output;
return ParseInputWith1Parameters.create(expected, type1, multiline);
}
public void verifyAll(Function1<OUT, Object> transform)
public <T1> ParseInputWith1Parameters<T1> transformTo(Function1<String, T1> transformer)
{
Approvals.verifyAll("", parse(), s -> print(s.getFirst(), transform.call(s.getSecond())),
new Options().inline(expected));
return new ParseInputWith1Parameters<>(expected, transformer, multiline);
}
// ************* 2 parameters
public <T1, T2> ParseInputWith2Parameters<T1, T2> withTypes(Class<T1> type1, Class<T2> type2)
{
return ParseInputWith2Parameters.create(expected, getTransformerForClass(type1),
getTransformerForClass(type2));
return ParseInputWith2Parameters.create(expected, getTransformerForClass(type1), getTransformerForClass(type2),
multiline);
}
public <T1, T2> ParseInputWith2Parameters<T1, T2> transformTo(Function1<String, T1> transformer1,
Function1<String, T2> transformer2)
{
return ParseInputWith2Parameters.create(expected, transformer1, transformer2);
}
public ParseInput<OUT> multiline()
{
return new ParseInput<>(expected, transformer, true);
}
public <OUT> ParseInputWith1Parameters<OUT> transformTo(Function1<String, OUT> transformer)
{
return new ParseInputWith1Parameters<>(expected, transformer, multiline);
}
public Queryable<OUT> getInputs()
{
return parse().select(Tuple::getSecond);
return ParseInputWith2Parameters.create(expected, transformer1, transformer2, multiline);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@

public class ParseInputWith2Parameters<IN1, IN2>
{
private final String expected;
private final String expected;
private final Function1<String, Tuple<IN1, IN2>> transformer;
public ParseInputWith2Parameters(String expected, Function1<String, Tuple<IN1, IN2>> transformer)
private final boolean multiline;

public ParseInputWith2Parameters(String expected, Function1<String, Tuple<IN1, IN2>> transformer, boolean multiline)
{
this.expected = expected;
this.transformer = transformer;
this.multiline = multiline;
}
public static <IN1, IN2> ParseInputWith2Parameters<IN1, IN2> create(String expected,
Function1<String, IN1> t1, Function1<String, IN2> t2)
public static <IN1, IN2> ParseInputWith2Parameters<IN1, IN2> create(String expected, Function1<String, IN1> t1,
Function1<String, IN2> t2, boolean multiline)
{
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);
return new ParseInputWith2Parameters<>(expected, f, multiline);
}
public void verifyAll(Function1<Tuple<IN1, IN2>, Object> transform)
{
Expand All @@ -35,6 +38,14 @@ public void verifyAll(Function2<IN1, IN2, Object> transform)
}
private ParseInput<Tuple<IN1, IN2>> getParseInput()
{
return new ParseInput<>(expected, s -> new Tuple<>(s, transformer.call(s)));
return new ParseInput<>(expected, s -> new Tuple<>(s, transformer.call(s)), multiline);
}
public <OUT> ParseInputWith1Parameters<OUT> transformTo(Function2<IN1, IN2, OUT> transform)
{
Function1<String, OUT> f1 = (t) -> {
Tuple<IN1, IN2> transformed = transformer.call(t);
return transform.call(transformed.getFirst(), transformed.getSecond());
};
return new ParseInputWith1Parameters<>(expected, f1, multiline);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public int hashCode()
@Override
public String toString()
{
return "Person[" + "name=" + name + ", " + "age=" + age + ']';
return "Person[\n " + "name=" + name + "\n " + "label=" + getAgeLabel() + "\n]";
}
}

0 comments on commit f7bf60e

Please sign in to comment.