Skip to content

Commit

Permalink
- F promote to production
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Feb 26, 2024
1 parent f7bf60e commit e3c1c64
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.spun.util.JsonUtils;
import org.approvaltests.core.Options;
import org.approvaltests.utils.parseinput.ParseInput;
import org.junit.jupiter.api.Test;
import org.lambda.query.Queryable;

Expand Down Expand Up @@ -39,16 +40,18 @@ void testWithTypesTransformersAndBoth()
void testMultiLineSupport()
{
var expected = """
1 -> {
"name": "name",
"age": 1
}
9 -> {
"name": "name",
"age": 9
}
""";
ParseInput.from(expected).multiline().verifyAll(s -> JsonUtils.asJson(new Person("name", Integer.parseInt(s))));
ParseInput.from(expected).multiline().withTypes(Integer.class).transformTo(i -> new Person("name", i)).verifyAll(s -> JsonUtils.asJson(s));
1 -> {
"name": "name",
"age": 1
}
9 -> {
"name": "name",
"age": 9
}
""";
ParseInput.from(expected).multiline()
.verifyAll(s -> JsonUtils.asJson(new Person("name", Integer.parseInt(s))));
ParseInput.from(expected).multiline().withTypes(Integer.class).transformTo(i -> new Person("name", i))
.verifyAll(s -> JsonUtils.asJson(s));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.approvaltests;

import org.approvaltests.utils.parseinput.ParseInput;
import org.junit.jupiter.api.Test;

public class Parse2InputTest
Expand All @@ -22,15 +23,15 @@ void testWithTypesTransformersAndBoth()
void testPersonAge()
{
var expected = """
Llewellyn, 25 -> Person[
name=Llewellyn
label=adult
]
Oliver, 15 -> Person[
name=Oliver
label=teenager
]
""";
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.approvaltests;
package org.approvaltests.utils.parseinput;

import com.spun.util.Tuple;
import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
import org.lambda.functions.Function1;
import org.lambda.query.Queryable;

import java.util.HashMap;
import java.util.Map;

public class ParseInput<OUT>
{
Expand All @@ -29,11 +31,11 @@ public ParseInput<OUT> multiline()
public Queryable<Tuple<String, OUT>> parse()
{
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));
return Queryable.as(expected.split("\n")) //
.where(f) //
.select(l -> l.split("->")[0].trim()) //
.where(l -> !l.isEmpty()) //
.select(l -> transformer.call(l));
}
public Queryable<OUT> getInputs()
{
Expand All @@ -46,7 +48,7 @@ public void verifyAll(Function1<OUT, Object> transform)
}
public static <OUT> Function1<String, OUT> getTransformerForClass(Class<OUT> targetType)
{
var transformers = new HashMap<Class<?>, Function1<String, Object>>()
Map<Class<?>, Function1<String, Object>> transformers = new HashMap<Class<?>, Function1<String, Object>>()
{
{
put(Integer.class, s -> Integer.parseInt(s));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.approvaltests;
package org.approvaltests.utils.parseinput;

import com.spun.util.Tuple;
import org.lambda.functions.Function1;
Expand All @@ -7,8 +7,8 @@
public class ParseInputWith1Parameters<OUT>
{
private final String expected;
private final Function1<String, OUT> transformer;
private final boolean multiline;
private final Function1<String, OUT> transformer;
public ParseInputWith1Parameters(String expected, Function1<String, OUT> transformer, boolean multiline)
{
this.expected = expected;
Expand All @@ -24,17 +24,17 @@ public <OUT2> ParseInputWith1Parameters<OUT2> transformTo(Function1<OUT, OUT2> t
Function1<String, OUT2> transformer2 = (String t) -> transformer1.call(transformer.call(t));
return new ParseInputWith1Parameters<>(expected, transformer2, multiline);
}
public void verifyAll(Function1<OUT, Object> transform)
{
getParseInput().verifyAll(transform);
}
public Queryable<OUT> getInputs()
{
return getParseInput().parse().select(Tuple::getSecond);
return getParseInput().parse().select(Tuple::getSecond);
}
private ParseInput<OUT> getParseInput()
{
Function1<String, Tuple<String, OUT>> transformer = s -> new Tuple<>(s, this.transformer.call(s));
return new ParseInput<OUT>(expected, transformer, multiline);
}
public void verifyAll(Function1<OUT, Object> transform)
{
getParseInput().verifyAll(transform);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.approvaltests;
package org.approvaltests.utils.parseinput;

import com.spun.util.Tuple;
import org.lambda.functions.Function1;
Expand All @@ -9,33 +9,25 @@ public class ParseInputWith2Parameters<IN1, IN2>
{
private final String expected;
private final Function1<String, Tuple<IN1, IN2>> transformer;
private final boolean multiline;

public ParseInputWith2Parameters(String expected, Function1<String, Tuple<IN1, IN2>> transformer, boolean multiline)
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, boolean multiline)
Function1<String, IN2> t2, boolean multiline)
{
Function1<String, Tuple<IN1, IN2>> f = s -> {
var temp = Queryable.as(s.split(",")).select(String::trim);
Queryable<String> 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, multiline);
}
public void verifyAll(Function1<Tuple<IN1, IN2>, Object> transform)
{
getParseInput().verifyAll(transform);
}
public void verifyAll(Function2<IN1, IN2, Object> transform)
{
getParseInput().verifyAll((t) -> transform.call(t.getFirst(), t.getSecond()));
}
private ParseInput<Tuple<IN1, IN2>> getParseInput()
{
return new ParseInput<>(expected, s -> new Tuple<>(s, transformer.call(s)), multiline);
Expand All @@ -48,4 +40,12 @@ public <OUT> ParseInputWith1Parameters<OUT> transformTo(Function2<IN1, IN2, OUT>
};
return new ParseInputWith1Parameters<>(expected, f1, multiline);
}
public void verifyAll(Function2<IN1, IN2, Object> transform)
{
getParseInput().verifyAll((t) -> transform.call(t.getFirst(), t.getSecond()));
}
public void verifyAll(Function1<Tuple<IN1, IN2>, Object> transform)
{
getParseInput().verifyAll(transform);
}
}

0 comments on commit e3c1c64

Please sign in to comment.