Skip to content

Commit

Permalink
a reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 12, 2024
1 parent 3f5d11e commit 8c65e74
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 176 deletions.
123 changes: 53 additions & 70 deletions approvaltests-tests/src/test/java/org/approvaltests/ParseInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,57 @@

import java.util.HashMap;

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 static <T> ParseInput<T> create(String expected, Class<T> tranformTo) {
var transformers = new HashMap<Class<?>, Function1<String,Object>>()
{{
put(Integer.class, s -> Integer.parseInt(s));
put(String.class, s -> s);
put(Double.class, s -> Double.parseDouble(s));
put(Boolean.class, s -> Boolean.parseBoolean(s));
put(Long.class, s -> Long.parseLong(s));
put(Float.class, s -> Float.parseFloat(s));
put(Short.class, s -> Short.parseShort(s));
}}
;
return ParseInput.create(expected, (Function1<String, T>) transformers.get(tranformTo));
}

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));
}
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 static <T> ParseInput<T> create(String expected, Class<T> tranformTo)
{
var transformers = new HashMap<Class<?>, Function1<String, Object>>()
{
{
put(Integer.class, s -> Integer.parseInt(s));
put(String.class, s -> s);
put(Double.class, s -> Double.parseDouble(s));
put(Boolean.class, s -> Boolean.parseBoolean(s));
put(Long.class, s -> Long.parseLong(s));
put(Float.class, s -> Float.parseFloat(s));
put(Short.class, s -> Short.parseShort(s));
}
};
return ParseInput.create(expected, (Function1<String, T>) transformers.get(tranformTo));
}
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
Expand Up @@ -2,67 +2,57 @@

import org.junit.jupiter.api.Test;

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

}

@Test
void toHex() {
var expected = """
1 -> 1
10 -> a
16 -> 10
22 -> 16
""";

ParseInput.create(expected, Integer.class)
.verifyAll(s -> Integer.toHexString(s));

}

@Test
void testPeople() {
var expected = """
Llewellyn, 25 -> adult
Oliver, 15 -> teenager
""";

Creator<Person> createPerson = (a) -> new Person(a.get(0), Integer.parseInt(a.get(1)));
ParseInput.createFromParts(expected, createPerson)
.verifyAll(s -> s.getAgeLabel());

}

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));
}
@Test
void toHex()
{
var expected = """
1 -> 1
10 -> a
16 -> 10
22 -> 16
""";
ParseInput.create(expected, Integer.class).verifyAll(s -> Integer.toHexString(s));
}
@Test
void testPeople()
{
var expected = """
Llewellyn, 25 -> adult
Oliver, 15 -> teenager
""";
Creator<Person> createPerson = (a) -> new Person(a.get(0), Integer.parseInt(a.get(1)));
ParseInput.createFromParts(expected, createPerson).verifyAll(s -> s.getAgeLabel());
}
}
84 changes: 41 additions & 43 deletions approvaltests-tests/src/test/java/org/approvaltests/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,45 @@

import java.util.Objects;

public 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 + ']';
}


public 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 + ']';
}
}

0 comments on commit 8c65e74

Please sign in to comment.