Skip to content

Commit f1ae222

Browse files
committed
*** added CodeParts
1 parent 53cc2e9 commit f1ae222

File tree

6 files changed

+169
-7
lines changed

6 files changed

+169
-7
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public void testyMctest(int foo) {
7474
""\";
7575
Approvals.verify("", Options.inline(expected));
7676
}
77+
""", """
78+
@Test
79+
void testyMctest() {
80+
Customer customer = Customer.create()
81+
.existingCustomer()
82+
.speakingEnglish()
83+
.build();
84+
stateService.save(TestUtils.userId().build(), customer);
85+
var expected = ""\"
86+
[Customer]: account lookup
87+
[ Bot]: Hi there!
88+
[ Bot]: Let me try to help.
89+
[ Bot]: routes to '12345'
90+
""\";
91+
verifyConversation(expected, "account lookup");
92+
}
7793
""");
7894
Approvals.verifyAll("Substitution", inputs, i -> "******\n" + i + "\nBecomes:\n"
7995
+ InlineJavaReporter.createNewReceivedFileText(i, "1\n2", "testyMctest"));

approvaltests-tests/src/test/java/org/approvaltests/InlineApprovalsTest.testCreateReceivedFileText.approved.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,35 @@ Becomes:
7878
Approvals.verify("", Options.inline(expected));
7979
}
8080

81+
******
82+
@Test
83+
void testyMctest() {
84+
Customer customer = Customer.create()
85+
.existingCustomer()
86+
.speakingEnglish()
87+
.build();
88+
stateService.save(TestUtils.userId().build(), customer);
89+
var expected = """
90+
[Customer]: account lookup
91+
[ Bot]: Hi there!
92+
[ Bot]: Let me try to help.
93+
[ Bot]: routes to '12345'
94+
""";
95+
verifyConversation(expected, "account lookup");
96+
}
97+
98+
Becomes:
99+
@Test
100+
void testyMctest() {
101+
Customer customer = Customer.create()
102+
.existingCustomer()
103+
.speakingEnglish()
104+
.build();
105+
stateService.save(TestUtils.userId().build(), customer);
106+
var expected = """
107+
1
108+
2
109+
""";
110+
verifyConversation(expected, "account lookup");
111+
}
112+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.approvaltests.inline;
2+
3+
import org.approvaltests.Approvals;
4+
import org.junit.jupiter.api.Test;
5+
6+
class CodePartsTest {
7+
@Test
8+
public void testSplit() {
9+
var code = """
10+
public class Test {
11+
@Test
12+
void testyMctest() {
13+
Customer customer = Customer.create()
14+
.existingCustomer()
15+
.speakingEnglish()
16+
.build();
17+
stateService.save(TestUtils.userId().build(), customer);
18+
var expected = ""\"
19+
[Customer]: account lookup
20+
[ Bot]: Hi there!
21+
[ Bot]: Let me try to help.
22+
[ Bot]: routes to '12345'
23+
""\";
24+
verifyConversation(expected, "account lookup");
25+
}
26+
}
27+
""";
28+
Approvals.verify(CodeParts.splitCode(code, "testyMctest"));
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CodeParts{
2+
before:
3+
public class Test {
4+
@Test
5+
method:
6+
void testyMctest() {
7+
Customer customer = Customer.create()
8+
.existingCustomer()
9+
.speakingEnglish()
10+
.build();
11+
stateService.save(TestUtils.userId().build(), customer);
12+
var expected = """
13+
[Customer]: account lookup
14+
[ Bot]: Hi there!
15+
[ Bot]: Let me try to help.
16+
[ Bot]: routes to '12345'
17+
""";
18+
verifyConversation(expected, "account lookup");
19+
}
20+
after:
21+
}
22+
tab:
23+
24+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.approvaltests.inline;
2+
3+
import com.spun.util.ArrayUtils;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public class CodeParts {
9+
public String before;
10+
public String method;
11+
public String after;
12+
public String tab;
13+
14+
public static CodeParts splitCode(String text, String methodName) {
15+
CodeParts codeParts = new CodeParts();
16+
String[] lines = text.split("\n");
17+
int start = 0;
18+
int end = 0;
19+
for (int i = 0; i < lines.length; i++) {
20+
String line = lines[i];
21+
if (start == 0) {
22+
if (line.contains("void " + methodName + "(")) {
23+
start = i;
24+
codeParts.tab = extractLeadingWhitespace(line);
25+
26+
}
27+
} else if (end == 0) {
28+
if (line.startsWith(codeParts.tab + "}")) {
29+
end = i + 1;
30+
break;
31+
}
32+
}
33+
}
34+
codeParts.before = String.join("\n", ArrayUtils.getSubsection(lines, 0, start));
35+
codeParts.method = String.join("\n", ArrayUtils.getSubsection(lines, start, end));
36+
codeParts.after = String.join("\n", ArrayUtils.getSubsection(lines, end, lines.length));
37+
return codeParts;
38+
}
39+
40+
private static String extractLeadingWhitespace(String text)
41+
{
42+
Pattern pattern = Pattern.compile("^\\s+");
43+
Matcher matcher = pattern.matcher(text);
44+
if (matcher.find())
45+
{ return matcher.group(); }
46+
return "\t";
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "CodeParts{\n" +
52+
"before:\n" + before + "\n" +
53+
"method:\n" + method + "\n" +
54+
"after:\n" + after + "\n" +
55+
"tab:\n" + tab + "\n" +
56+
'}';
57+
}
58+
}

approvaltests/src/main/java/org/approvaltests/inline/InlineJavaReporter.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ public String createReceived(String actual)
4242
public static String createNewReceivedFileText(String text, String actual, String methodName)
4343
{
4444
text = text.replaceAll("\r\n", "\n");
45+
CodeParts codeParts = CodeParts.splitCode(text, methodName);
4546
int start = text.indexOf("void " + methodName + "(");
4647
int startOfLine = text.substring(0, start).lastIndexOf("\n") + 1;
4748
String line = text.substring(startOfLine, start);
4849
String tab = extractLeadingWhitespace(line);
4950
start = text.indexOf("{", start);
50-
int next = text.indexOf("\n", start);
51-
int end = text.indexOf("}", next);
52-
int endString = text.indexOf("\"\"\";", next);
53-
String part1 = text.substring(0, next);
51+
int codeBeforeExpected = text.indexOf("\n", start);
52+
int end = text.indexOf("}", codeBeforeExpected);
53+
// if there is an expected = then set the code before to the beginning of this line
54+
int endString = text.indexOf("\"\"\";", codeBeforeExpected);
55+
String part1 = text.substring(0, codeBeforeExpected);
5456
String part2 = null;
5557
if (0 < endString && endString < end)
5658
{
@@ -60,12 +62,12 @@ public static String createNewReceivedFileText(String text, String actual, Strin
6062
}
6163
else
6264
{
63-
part2 = text.substring(next + 1);
65+
part2 = text.substring(codeBeforeExpected + 1);
6466
}
65-
String fullText = String.format("%s\n%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n%s", part1, tab, tab,
67+
return String.format("%s\n%s%svar expected = \"\"\"\n%s%s%s%s\"\"\";\n%s", part1, tab, tab,
6668
indent(actual, tab), tab, tab, tab, part2);
67-
return fullText;
6869
}
70+
6971
public static String indent(String actual, String tab)
7072
{
7173
String[] split = actual.split("\n");

0 commit comments

Comments
 (0)