-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: ollin <[email protected]>
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
approvaltests-tests/src/test/java/org/approvaltests/Creator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.approvaltests; | ||
|
||
import org.lambda.functions.Function1; | ||
import org.lambda.query.Queryable; | ||
|
||
public interface Creator<T> extends Function1<Queryable<String>, T> { | ||
} | ||
|
64 changes: 64 additions & 0 deletions
64
approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T>{ | ||
|
||
private String expected; | ||
private final Function1<String, Tuple<String, T>> transformer; | ||
|
||
|
||
|
||
public static ParseInput<String> create(String expected) { | ||
return new ParseInput<String>(expected, s -> new Tuple<>(s,s)); | ||
|
||
} | ||
public static<T> ParseInput<T> create(String expected,Function1<String, T> transformer) { | ||
return new ParseInput<T>(expected,s -> new Tuple<>(s,transformer.call(s))); | ||
|
||
} | ||
|
||
private ParseInput(String expected, Function1<String, Tuple<String, T>> transformer) { | ||
this.expected = expected; | ||
this.transformer = transformer; | ||
} | ||
|
||
public static <T> ParseInput<T> createFromParts(String expected, Function1<Queryable<String>, T> transformer) { | ||
return new ParseInput<T>(expected,s -> { | ||
var temp = Queryable.as(s.split(",")).select(String::trim); | ||
return new Tuple<>(s, transformer.call(temp)); | ||
}); | ||
} | ||
|
||
public Queryable<Tuple<String, T>> 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<T, Object> transform) { | ||
Approvals.verifyAll("", parse(expected), | ||
s -> print(s.getFirst(), transform.call(s.getSecond())), new Options().inline(expected)); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
104 changes: 104 additions & 0 deletions
104
approvaltests-tests/src/test/java/org/approvaltests/ParseInputTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Person> createPerson = (a) -> new Person(a.first(), Integer.parseInt(a.last())); | ||
ParseInput.createFromParts(expected, createPerson) | ||
.verifyAll(s -> s.getAgeLabel()); | ||
|
||
} | ||
|
||
} |