diff --git a/approvaltests-tests/src/test/java/org/approvaltests/Creator.java b/approvaltests-tests/src/test/java/org/approvaltests/Creator.java new file mode 100644 index 00000000..b6aa0510 --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/Creator.java @@ -0,0 +1,8 @@ +package org.approvaltests; + +import org.lambda.functions.Function1; +import org.lambda.query.Queryable; + +public interface Creator extends Function1, T> { +} + diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java b/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java new file mode 100644 index 00000000..0fadcabc --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java @@ -0,0 +1,64 @@ +package org.approvaltests; + +import com.spun.util.Tuple; +import org.approvaltests.core.Options; +import org.lambda.functions.Function1; +import org.lambda.query.Query; +import org.lambda.query.Queryable; + +public class ParseInput { + + private String expected; + private final Function1> transformer; + + + + public static ParseInput create(String expected) { + return new ParseInput(expected, s -> new Tuple<>(s,s)); + + } + public static ParseInput create(String expected,Function1 transformer) { + return new ParseInput(expected,s -> new Tuple<>(s,transformer.call(s))); + + } + + private ParseInput(String expected, Function1> transformer) { + this.expected = expected; + this.transformer = transformer; + } + + public static ParseInput createFromParts(String expected, Function1, T> transformer) { + return new ParseInput(expected,s -> { + var temp = Queryable.as(s.split(",")).select(String::trim); + return new Tuple<>(s, transformer.call(temp)); + }); + } + + public Queryable> parse(String expected) { + return Queryable.as(expected.lines()) + .select(l -> l.split(" -> ")[0].trim()) + .select(l -> transformer.call(l)); + } + + public String print(String input, Object output) { + return input + " -> " + output; + } + + public void verifyAll(Function1 transform) { + Approvals.verifyAll("", parse(expected), + s -> print(s.getFirst(), transform.call(s.getSecond())), new Options().inline(expected)); + } +} + + + + + + + + + + + + + diff --git a/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java b/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java new file mode 100644 index 00000000..807100b5 --- /dev/null +++ b/approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java @@ -0,0 +1,104 @@ +package org.approvaltests; + +import org.approvaltests.core.Options; +import org.junit.jupiter.api.Test; +import org.lambda.functions.Function1; +import org.lambda.query.Queryable; + +import java.util.Objects; + +public class ParseInputTest { + + @Test + void uppercasse() { + var expected = """ + a -> A + b -> B + hh -> HH + c -> C + d -> D + e -> E + cco -> CCO + f -> F + g -> G + eue -> EUE + aza -> AZA + """; + ParseInput.create(expected).verifyAll(s -> s.toUpperCase()); + + } + + @Test + void toBinary() { + var expected = """ + 1 -> 1 + 2 -> 10 + 4 -> 100 + 8 -> 1000 + 9 -> 1001 + """; + + ParseInput.create(expected, Integer::parseInt) + .verifyAll(s -> Integer.toBinaryString(s)); + + } + + public static final class Person { + private final String name; + private final int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public String getAgeLabel() { + return age > 20 ? "adult" : "teenager"; + } + + public String name() { + return name; + } + + public int age() { + return age; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (Person) obj; + return Objects.equals(this.name, that.name) && + this.age == that.age; + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + + @Override + public String toString() { + return "Person[" + + "name=" + name + ", " + + "age=" + age + ']'; + } + + + } + + @Test + void testPeople() { + var expected = """ + Llewellyn, 25 -> adult + Oliver, 15 -> teenager + """; + + Creator createPerson = (a) -> new Person(a.first(), Integer.parseInt(a.last())); + ParseInput.createFromParts(expected, createPerson) + .verifyAll(s -> s.getAgeLabel()); + + } + + } \ No newline at end of file