Skip to content

Commit

Permalink
- F first attempt at parsing input
Browse files Browse the repository at this point in the history
Co-Authored-By: ollin <oliver.nautsch@gmail.com>
  • Loading branch information
isidore and ollin committed Feb 12, 2024
1 parent ef14036 commit e0ae1d6
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
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> {
}

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));
}
}













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());

}

}

0 comments on commit e0ae1d6

Please sign in to comment.