-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathInlineApprovalsTest.java
More file actions
246 lines (229 loc) · 6.89 KB
/
InlineApprovalsTest.java
File metadata and controls
246 lines (229 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package org.approvaltests.inline;
import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
import org.approvaltests.reporters.AutoApproveReporter;
import org.approvaltests.reporters.ReportWithDiffMerge;
import org.approvaltests.reporters.FirstWorkingReporter;
import org.approvaltests.reporters.ReportNothing;
import org.approvaltests.reporters.UseReporter;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.lambda.actions.Action1;
import org.lambda.utils.Mutable;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class InlineApprovalsTest
{
@Test
@UseReporter(ReportWithDiffMerge.class)
public void testWithBuiltinReporter()
{
var expected = """
Hello There***
""";
Options inline =
// begin-snippet: inline_approvals
new Options().inline(expected);
// end-snippet
Approvals.verify("Hello There***", inline);
}
@UseReporter(ReportWithDiffMerge.class)
@Test
public void testWithSpecificReporter()
{
var expected = """
Hello Lada***
""";
Options inline = new Options().inline(expected).withReporter(ReportWithDiffMerge.INSTANCE);
Approvals.verify("Hello Lada***", inline);
}
@Test
public void testCreateReceivedFileText()
{
var inputs = List.of("""
@Test
public void testyMctest() {
var expected = ""\"
Hello World***
""\";
Approvals.verify("", Options.inline(expected));
}
""", """
@Test
public void testyMctest () {
var expected = ""\"
Hello World***
""\";
Approvals.verify("", Options.inline(expected));
}
""", """
\t@Test
\tpublic void testyMctest()
\t{
\t\tApprovals.verify("", Options.inline(expected));
\t}
""", """
@Test
public void testyMctest()
{
var expected = ""\"
Hello World***
""\";
Approvals.verify("", Options.inline(expected));
}
""", """
@Test
public void testyMctest(int foo) {
var expected = ""\"
Hello World***
""\";
Approvals.verify("", Options.inline(expected));
}
""", """
@Test
void testyMctest() {
Customer customer = Customer.create()
.existingCustomer()
.speakingEnglish()
.build();
stateService.save(TestUtils.userId().build(), customer);
var expected = ""\"
[Customer]: account lookup
[ Bot]: Hi there!
[ Bot]: Let me try to help.
[ Bot]: routes to '12345'
""\";
verifyConversation(expected, "account lookup");
}
""");
Approvals.verifyAll("Substitution", inputs, i -> "******\n" + i + "\nBecomes:\n"
+ InlineJavaReporter.createNewReceivedFileText(i, "1\n2", "testyMctest"));
}
@Test
@UseReporter(ReportNothing.class)
public void testReportingCode()
{
Options inlineWithCode = new Options().inline("", InlineOptions.showCode(true));
Options inlineNoCode = new Options().inline("", InlineOptions.showCode(false));
var resultWithCode = inlineWithCode.getReporter();
assertEquals(InlineJavaReporter.class, resultWithCode.getClass());
assertEquals(ReportNothing.class,
((FirstWorkingReporter) inlineNoCode.getReporter()).getReporters()[1].getClass());
}
@Test
void testEmptyLineAtTheEnd()
{
var expected = """
Jeff Jeffty Jeff
born on Jeffteen of Jeff, Nineteen-eighty-Jeff
""";
Approvals.verify(greet("Jeff"), new Options().inline(expected, InlineOptions.automatic()));
}
private String greet(String name)
{
return """
%s %sty %s
born on %steen of %s, Nineteen-eighty-%s
""".formatted(name, name, name, name, name, name);
}
@Test
void testSemiAutomatic()
{
var expected = """
hello Lars
***** DELETE ME TO APPROVE *****
""";
Options options = new Options().inline("", InlineOptions.semiAutomatic());
Mutable<String> result = hijackInlineReporter(options);
Action1<Throwable> assertion = e -> assertEquals(expected, result.get());
assertApprovalFailure("hello Lars", options, assertion);
}
@Test
void testAutomatic()
{
var expected = """
hello Oskar
""";
Options options = new Options().inline("", InlineOptions.automatic());
Mutable<String> result = hijackInlineReporter(options);
assertApprovalFailure("hello Oskar", options, e -> assertEquals(expected, result.get()));
}
private static void assertApprovalFailure(String actual, Options options, Action1<Throwable> azzert)
{
boolean failed = true;
try
{
Approvals.verify(actual, options);
failed = false;
}
catch (Throwable t)
{
azzert.call(t);
}
assertTrue(failed, "Approval should have failed");
}
private static Mutable<String> hijackInlineReporter(Options options)
{
InlineJavaReporter reporter = (InlineJavaReporter) options.getReporter();
assertEquals(reporter.reporter.getClass(), AutoApproveReporter.class);
reporter.reporter = new ReportNothing();
Mutable<String> result = new Mutable<>("");
reporter.createNewReceivedFileText = (s, a, m) -> result.set(a);
return result;
}
@Test
void testSemiAutomaticMessage()
{
var expected = """
41
***** DELETE ME TO APPROVE *****
""";
try
{
var options = new Options().inline(expected, InlineOptions.semiAutomatic());
InlineJavaReporter reporter = (InlineJavaReporter) options.getReporter();
reporter.reporter = new ReportNothing();
Approvals.verify("41", options);
}
catch (Throwable e)
{
}
Approvals.verify(expected);
}
// @formatter:off
@Test
void testSemiAutomaticWithPreviousApproved()
{
var expected = """
42
***** DELETE ME TO APPROVE *****
vvvvv PREVIOUS RESULT vvvvv
41
""";
var options = new Options().inline(expected, InlineOptions.semiAutomaticWithPreviousApproved());
try
{
Approvals.verify("42", options);
}
catch (Throwable e)
{
}
Approvals.verify(expected);
}
// @formatter:on
@Nested
class NestedTest
{
@Test
void testInlineFromInnerClass()
{
var expected = """
hello Oskar
""";
Options options = new Options().inline("", InlineOptions.automatic());
Mutable<String> result = hijackInlineReporter(options);
assertApprovalFailure("hello Oskar", options, e -> assertEquals(expected, result.get()));
}
}
}