Skip to content

Commit e9e354e

Browse files
committed
F!! MarkdownTable supports multiple column headers
1 parent f71a5c6 commit e9e354e

File tree

4 files changed

+65
-17
lines changed

4 files changed

+65
-17
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| input | Camel Case | Snake Case | kebab case |
2+
| --- | --- | --- | --- |
3+
| verify json | verifyJson | verify_json | verify-json |
4+
| verify all | verifyAll | verify_all | verify-all |
5+
| verify parameters | verifyParameters | verify_parameters | verify-parameters |
6+
| verify as json | verifyAsJson | verify_as_json | verify-as-json |

approvaltests-tests/src/test/java/org/approvaltests/TableTest.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,44 @@
44
import org.approvaltests.utils.MarkdownTable;
55
import org.junit.jupiter.api.Test;
66

7-
public class TableTest
8-
{
9-
@Test
10-
void abilityModifier()
11-
{
12-
// begin-snippet: markdown_table_example
13-
Integer[] inputs = Range.get(1, 20);
14-
MarkdownTable table = MarkdownTable.create(inputs, a -> getModifier(a), "Score", "Modifier");
15-
Approvals.verify(table);
16-
// end-snippet
17-
}
18-
private Integer getModifier(Integer ability)
19-
{
20-
return ability / 2 - 5;
21-
}
7+
public class TableTest {
8+
@Test
9+
void abilityModifier() {
10+
Integer[] inputs = Range.get(1, 20);
11+
MarkdownTable table = MarkdownTable.create(inputs, a -> getModifier(a), "Score", "Modifier");
12+
Approvals.verify(table);
13+
}
14+
15+
@Test
16+
void differentCases() {
17+
// begin-snippet: markdown_table_example
18+
String[] inputs = {"verify json", "verify all", "verify parameters", "verify as json"};
19+
MarkdownTable table = MarkdownTable.withHeaders("Input", "Camel Case", "Snake Case", "Kebab Case");
20+
table.addRowsForInputs(inputs, this::toCamelCase, this::toSnakeCase, this::toKebabCase);
21+
Approvals.verify(table);
22+
// end-snippet
23+
}
24+
25+
private String toKebabCase(String input) {
26+
String[] parts = input.split(" ");
27+
return String.join("-", parts);
28+
}
29+
30+
private String toSnakeCase(String input) {
31+
String[] parts = input.split(" ");
32+
return String.join("_", parts);
33+
}
34+
35+
private String toCamelCase(String input) {
36+
String[] parts = input.split(" ");
37+
String out = parts[0];
38+
for (int i = 1; i < parts.length; i++) {
39+
out += Character.toUpperCase(parts[i].charAt(0)) + parts[i].substring(1);
40+
}
41+
return out;
42+
}
43+
44+
private Integer getModifier(Integer ability) {
45+
return ability / 2 - 5;
46+
}
2247
}

approvaltests/docs/how_to/TestAVarietyOfValues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Approvals.verify(table);
6767

6868
which will produce:
6969

70-
<!-- include: TableTest.abilityModifier.approved.md -->
70+
<!-- include: TableTest.differentCases.approved.md -->
7171
| Score | Modifier |
7272
| --- | --- |
7373
| 1 | -5 |

approvaltests/src/main/java/org/approvaltests/utils/MarkdownTable.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.approvaltests.core.VerifyParameters;
77
import org.approvaltests.strings.MarkdownCompatible;
88
import org.lambda.functions.Function1;
9+
import org.lambda.query.Queryable;
910

1011
public class MarkdownTable implements Verifiable, MarkdownCompatible
1112
{
@@ -19,7 +20,23 @@ public static <I, O> MarkdownTable create(I[] inputs, Function1<I, O> o, String
1920
}
2021
return table;
2122
}
22-
private MarkdownTable addRow(Object... columns)
23+
24+
public static MarkdownTable withHeaders(String... columnNames) {
25+
MarkdownTable table = new MarkdownTable();
26+
return table.withColumnHeaders(columnNames);
27+
}
28+
29+
public <I> MarkdownTable addRowsForInputs(I[] inputs, Function1<I, Object>... transfers)
30+
{
31+
for (I input : inputs)
32+
{
33+
Queryable<Object> row = Queryable.as(transfers).select(f -> f.call(input));
34+
row.add(0, input);
35+
addRow(row.toArray());
36+
}
37+
return this;
38+
}
39+
public MarkdownTable addRow(Object... columns)
2340
{
2441
markdown += printRow(columns);
2542
return this;

0 commit comments

Comments
 (0)