private String render() { renderText(); sortValues(); renderValues(); return linesToString(); }
/** * Creates a string representation of an assertion and its recorded values. * * @param text the assertion's source text * @param recorder a recorder holding the values recorded during evaluation * of the assertion * @return a string representation of the assertion and its recorded values */ public static String render(String text, ValueRecorder recorder) { return new AssertionRenderer(text, recorder).render(); }
private void renderValues() { List<Value> values = recorder.getValues(); int valuesSize = values.size(); for (int i = 0; i < valuesSize; i++) { final Value value = values.get(i); final int startColumn = value.getColumn(); if (startColumn < 1) continue; // skip values with unknown source position if (next != null && next.getColumn() == startColumn) continue; String str = valueToString(value.getValue()); if (str == null) continue; // null signals the value shouldn't be rendered placeString(lines.get(j), str, startColumn); startColumns.set(j, startColumn); continue nextValue; } else { placeString(lines.get(j), "|", startColumn); if (j > 1) // make sure that no values are ever placed on empty line startColumns.set(j, startColumn + 1); // + 1: no whitespace required between end of value and "|" StringBuilder newLine = new StringBuilder(); lines.add(newLine); placeString(newLine, s, startColumn); startColumns.add(startColumn);
if (!hasPlausibleSourcePosition(stat)) throw new SourceTextNotAvailableException(stat, sourceUnit, "Invalid source position"); String lineText = sourceUnit.getSample(line, 0, janitor); if (lineText == null) throw new SourceTextNotAvailableException(stat, sourceUnit, "SourceUnit.getSample() returned null"); lineOffsets.add(stat.getColumnNumber() - 1); } else lineOffsets.add(countLeadingWhitespace(lineText));
assertionTracker.sourceText = new SourceText(statement, controller.getSourceUnit(), janitor); mv.visitTypeInsn(NEW, "org/codehaus/groovy/runtime/powerassert/ValueRecorder"); mv.visitInsn(DUP); mv.visitLdcInsn(assertionTracker.sourceText.getNormalizedText()); mv.visitVarInsn(ALOAD, assertionTracker.recorderIndex); mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/powerassert/AssertionRenderer", "render", "(Ljava/lang/String;Lorg/codehaus/groovy/runtime/powerassert/ValueRecorder;)Ljava/lang/String;", false);
/** * Returns a string representation of the given value, or <tt>null</tt> if * the value should not be included (because it does not add any valuable * information). * * @param value a value * @return a string representation of the given value */ private static String valueToString(Object value) { String toString; try { toString = InvokerHelper.format(value, true, -1, false); } catch (Exception e) { return String.format("%s (toString() threw %s)", javaLangObjectToString(value), e.getClass().getName()); } if (toString == null) { return String.format("%s (toString() == null)", javaLangObjectToString(value)); } if (toString.equals("")) { if (hasStringLikeType(value)) return "\"\""; return String.format("%s (toString() == \"\")", javaLangObjectToString(value)); } return toString; }
public static void assertFailed(Object expression, Object message) { if (message == null || "".equals(message)) { throw new PowerAssertionError(expression.toString()); } throw new AssertionError(String.valueOf(message) + ". Expression: " + expression); }
public void record(Expression expression) { if (assertionTracker==null) return; record(assertionTracker.sourceText.getNormalizedColumn(expression.getLineNumber(), expression.getColumnNumber())); }
@Override public String toString() { return String.format("Assertion failed: \n\n%s\n", getMessage()); } }
private void sortValues() { // it's important to use a stable sort here, otherwise // renderValues() will skip the wrong values Collections.sort(recorder.getValues(), new Comparator<Value>() { public int compare(Value v1, Value v2) { return v2.getColumn() - v1.getColumn(); } } ); }
public Object record(Object value, int anchor) { values.add(new Value(value, anchor)); return value; }
/** * Creates a string representation of an assertion and its recorded values. * * @param text the assertion's source text * @param recorder a recorder holding the values recorded during evaluation * of the assertion * @return a string representation of the assertion and its recorded values */ public static String render(String text, ValueRecorder recorder) { return new AssertionRenderer(text, recorder).render(); }
private String render() { renderText(); sortValues(); renderValues(); return linesToString(); }
public void record(Token op) { if (assertionTracker==null) return; record(assertionTracker.sourceText.getNormalizedColumn(op.getStartLine(), op.getStartColumn())); }
@Override public String toString() { return String.format("Assertion failed: \n\n%s\n", getMessage()); } }
private void sortValues() { // it's important to use a stable sort here, otherwise // renderValues() will skip the wrong values Collections.sort(recorder.getValues(), new Comparator<Value>() { public int compare(Value v1, Value v2) { return v2.getColumn() - v1.getColumn(); } } ); }
public Object record(Object value, int anchor) { values.add(new Value(value, anchor)); return value; }
public void record(Token op) { if (assertionTracker==null) return; record(assertionTracker.sourceText.getNormalizedColumn(op.getStartLine(), op.getStartColumn())); }