Skip to content

Commit f27aeab

Browse files
committed
Multiline assert support (fixes #5)
1 parent e84a0b6 commit f27aeab

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Power Assertions for Java
22

33
[![Build Status](https://travis-ci.org/jkschneider/java-power-assert.svg?branch=master)](https://travis-ci.org/jkschneider/java-power-assert)
4-
[![Coverage Status](https://coveralls.io/repos/github/jkschneider/java-power-assert/badge.svg?branch=master)](https://coveralls.io/github/jkschneider/java-power-assert?branch=master)
54
[![Apache 2.0](https://img.shields.io/github/license/jkschneider/java-power-assert.svg)](http://www.apache.org/licenses/LICENSE-2.0)
65

76
Power assertions (a.k.a. diagrammed assertions) augment your assertion failures with information about values produced during the evaluation of a condition, and presents them in an easily digestible form.

src/main/java/org/powerassert/JavacPowerAssertGenerator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ else if(expr instanceof JCTree.JCUnary) {
231231
else if(expr instanceof JCTree.JCMethodInvocation) {
232232
JCTree.JCMethodInvocation method = (JCTree.JCMethodInvocation) expr;
233233
return recordValue(
234-
// oddly, methodSelect is expressed as a JCFieldAccess, and if we recurse through this expression,
235-
// we will attempt to record the method name as a field access, which it is not... so do
236-
// not recurse on method select
237234
treeMaker.Apply(
238235
method.typeargs,
239236
recordAllValues(method.getMethodSelect(), expr),

src/main/java/org/powerassert/synthetic/ExpressionRenderer.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ public String render(RecordedExpression<?> recordedExpr) {
3737
else break;
3838
}
3939

40-
String intro = recordedExpr.getText().trim().replaceAll(";$", ""); // strip trailing semicolons
40+
String intro = recordedExpr.getText().trim()
41+
.replaceAll(";$", ""); // strip trailing semicolons
42+
Map<Integer, Integer> strippedWhitespace = strippedWhitespaceUpToColumn(intro);
43+
intro = intro.replaceAll("\n\\s+", " ");
44+
4145
List<String> lines = new ArrayList<>();
4246

4347
for(RecordedValue recordedValue: filterAndSortByAnchor(recordedExpr.getValues())) {
44-
placeValue(lines, recordedValue.getValue(), recordedValue.getAnchor() - offset);
48+
int offsetAnchor = recordedValue.getAnchor() - offset;
49+
placeValue(lines, recordedValue.getValue(), offsetAnchor - strippedWhitespace.get(offsetAnchor));
4550
}
4651

4752
lines.add(0, intro);
@@ -53,6 +58,33 @@ public String render(RecordedExpression<?> recordedExpr) {
5358
return rendered;
5459
}
5560

61+
/**
62+
* Help count how much newline and margin whitespace we have stripped up to a particular index so we know
63+
* how to offset anchors
64+
*/
65+
Map<Integer, Integer> strippedWhitespaceUpToColumn(String intro) {
66+
Map<Integer, Integer> stripped = new TreeMap<>();
67+
boolean countingWhitespace = false;
68+
int col = 0;
69+
int count = 0;
70+
for (char c : intro.toCharArray()) {
71+
if(c == '\n' && !countingWhitespace) {
72+
// we don't count the first newline, as this will be replaced with a space
73+
// (yielding the same column width in terms of characters)
74+
countingWhitespace = true;
75+
}
76+
else if(countingWhitespace) {
77+
if (Character.isWhitespace(c))
78+
count++;
79+
else
80+
countingWhitespace = false;
81+
}
82+
stripped.put(col, count);
83+
col++;
84+
}
85+
return stripped;
86+
}
87+
5688
private Iterable<RecordedValue> filterAndSortByAnchor(List<RecordedValue> recordedValues) {
5789
Map<Integer, RecordedValue> map = new TreeMap<>(new Comparator<Integer>() {
5890
@Override

src/test/java/org/powerassert/AssertKeywordTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.powerassert;
1818

19-
import org.junit.Before;
2019
import org.junit.Test;
2120

2221
public class AssertKeywordTest extends AbstractAssertTest {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.powerassert;
2+
3+
import org.junit.Test;
4+
5+
public class MultilineAssertTest extends AbstractAssertTest {
6+
7+
@Test
8+
public void chainedMethodCallsOnMultipleLines() {
9+
java.compile(
10+
"public class A {\n" +
11+
" @org.junit.Test public void test() {\n" +
12+
" assert \"abc\"\n" +
13+
" .substring(0)\n" +
14+
" .contains(\"d\");\n" +
15+
" }\n" +
16+
"}\n");
17+
18+
testFailsWithMessage("A", "test",
19+
"\"abc\" .substring(0) .contains(\"d\")",
20+
" | |",
21+
" abc false");
22+
}
23+
}

0 commit comments

Comments
 (0)