@Override public String describeMismatch(IMockInvocation invocation) { Condition condition = new Condition(CollectionUtil.<Object>listOf(getPropertyName(invocation), propertyName, false), String.format("propertyName == \"%s\"", propertyName), null, null, null, null); return condition.getRendering(); } }
@Nullable public ExpressionInfo getExpression() { if (expression == null) { createExpression(); } return expression; }
public String getRendering() { if (rendering == null) { createRendering(); } return rendering; }
private void createRendering() { StringBuilder builder = new StringBuilder(); if (getExpression() != null) { ExpressionInfoValueRenderer.render(expression); builder.append(ExpressionInfoRenderer.render(expression)); } else if (text != null) { builder.append(stripAndFlatten(text)); builder.append("\n"); } else { builder.append("(Source code not available)\n"); } if (message != null) { builder.append("\n"); builder.append(message); builder.append("\n"); } rendering = builder.toString(); }
@Override public String getMessage() { return "Condition not satisfied:\n\n" + condition.getRendering(); } }
public static void verifyCondition(@Nullable ErrorCollector errorCollector, @Nullable ValueRecorder recorder, @Nullable String text, int line, int column, @Nullable Object message, @Nullable Object condition) { if (!GroovyRuntimeUtil.isTruthy(condition)) { final ConditionNotSatisfiedError conditionNotSatisfiedError = new ConditionNotSatisfiedError( new Condition(getValues(recorder), text, TextPosition.create(line, column), messageToString(message), null, null)); errorCollector.collectOrThrow(conditionNotSatisfiedError); } }
private boolean isFailedEqualityComparison(Throwable exception) { if (!(exception instanceof ConditionNotSatisfiedError)) return false; ConditionNotSatisfiedError conditionNotSatisfiedError = (ConditionNotSatisfiedError) exception; Condition condition = conditionNotSatisfiedError.getCondition(); ExpressionInfo expr = condition.getExpression(); return expr != null && expr.isEqualityComparison() && // it is equality conditionNotSatisfiedError.getCause() == null; // and it is not failed because of exception }
private void createExpression() { if (text == null || values == null) return; String stripAndFlattenText = stripAndFlatten(text).toString(); expression = new ExpressionInfoBuilder(stripAndFlattenText, TextPosition.create(1, 1), values, notRecordedVarNumberBecauseOfException, exception).build(); }
@Override public String getMessage() { return "Condition not satisfied:\n\n" + condition.getRendering(); }
public static void conditionFailedWithException(@Nullable ErrorCollector errorCollector, @Nullable ValueRecorder recorder, @Nullable String text, int line, int column, @Nullable Object message, Throwable throwable){ if (throwable instanceof SpockAssertionError) { final SpockAssertionError spockAssertionError = (SpockAssertionError) throwable; errorCollector.collectOrThrow(spockAssertionError); // this is our exception - it already has good message return; } if (throwable instanceof SpockException) { final SpockException spockException = (SpockException) throwable; errorCollector.collectOrThrow(spockException); // this is our exception - it already has good message return; } final ConditionFailedWithExceptionError conditionNotSatisfiedError = new ConditionFailedWithExceptionError( new Condition( getValues(recorder), text, TextPosition.create(line, column), messageToString(message), recorder == null ? null : recorder.getCurrentRecordingVarNum(), recorder == null ? null : throwable), throwable); errorCollector.collectOrThrow(conditionNotSatisfiedError); }
private Throwable convertToComparisonFailure(Throwable exception) { assert isFailedEqualityComparison(exception); ConditionNotSatisfiedError conditionNotSatisfiedError = (ConditionNotSatisfiedError) exception; Condition condition = conditionNotSatisfiedError.getCondition(); ExpressionInfo expr = condition.getExpression(); String actual = renderValue(expr.getChildren().get(0).getValue()); String expected = renderValue(expr.getChildren().get(1).getValue()); ComparisonFailure failure = new SpockComparisonFailure(condition, expected, actual); failure.setStackTrace(exception.getStackTrace()); if (conditionNotSatisfiedError.getCause()!=null){ failure.initCause(conditionNotSatisfiedError.getCause()); } return failure; }
@Override public String describeMismatch(Object argument) { if (type.isInstance(argument)) { return constraint.describeMismatch(argument); } Condition condition = new Condition(CollectionUtil.listOf( argument, type, false), String.format("argument instanceof %s", type.getName()), null, null, null, null); return condition.getRendering(); } }
private void writeObject(java.io.ObjectOutputStream out) throws IOException { // create the rendering so that it is available for serialization getRendering(); out.defaultWriteObject(); } }
void verify(@Nullable ErrorCollector errorCollector, @Nullable List<Object> values, @Nullable String text, int line, int column, @Nullable String message) { if (HamcrestFacade.matches(matcher, actual)) return; if (values != null) { CollectionUtil.setLastElement(values, shortSyntax ? actual : false); replaceMatcherValues(values); } String description = HamcrestFacade.getFailureDescription(matcher, actual, message); Condition condition = new Condition(values, text, TextPosition.create(line, column), description, null, null); errorCollector.collectOrThrow(new ConditionNotSatisfiedError(condition)); }
@Override public String describeMismatch(IMockInvocation invocation) { Condition condition = new Condition(CollectionUtil.<Object>listOf(getPropertyName(invocation), pattern.pattern(), false), String.format("propertyName ==~ /%s/", pattern.pattern()), null, null, null, null); return condition.getRendering(); } }
@Override public String getMessage() { return "Condition failed with Exception:\n\n" + getCondition().getRendering(); } }
public static void verifyMethodCondition(@Nullable ErrorCollector errorCollector, @Nullable ValueRecorder recorder, @Nullable String text, int line, int column, @Nullable Object message, Object target, String method, Object[] args, boolean safe, boolean explicit, int lastVariableNum) { MatcherCondition matcherCondition = MatcherCondition.parse(target, method, args, safe); if (matcherCondition != null) { matcherCondition.verify(errorCollector, getValues(recorder), text, line, column, messageToString(message)); return; } if (recorder != null) { recorder.startRecordingValue(lastVariableNum); } Object result = safe ? GroovyRuntimeUtil.invokeMethodNullSafe(target, method, args) : GroovyRuntimeUtil.invokeMethod(target, method, args); if (!explicit && result == null && isVoidMethod(target, method, args)) return; if (!GroovyRuntimeUtil.isTruthy(result)) { List<Object> values = getValues(recorder); if (values != null) CollectionUtil.setLastElement(values, result); final ConditionNotSatisfiedError conditionNotSatisfiedError = new ConditionNotSatisfiedError( new Condition(values, text, TextPosition.create(line, column), messageToString(message), null, null)); errorCollector.collectOrThrow(conditionNotSatisfiedError); } }
@Override public String describeMismatch(IMockInvocation invocation) { Condition condition = new Condition(CollectionUtil.<Object>listOf(invocation.getMethod().getName(), methodName, false), String.format("methodName == \"%s\"", methodName), null, null, null, null); return condition.getRendering(); } }
@Override public String describeMismatch(IMockInvocation invocation) { // need to explicitly call .toString() otherwise the render service will attempt to output the contents of collections and such Condition condition = new Condition(CollectionUtil.<Object>listOf( invocation.getMockObject().getInstance().toString(), target.toString(), false), "instance == target", null, null, null, null); return condition.getRendering(); }
@Override public String describeMismatch(IMockInvocation invocation) { Condition condition = new Condition(CollectionUtil.<Object>listOf(invocation.getMethod().getName(), pattern.pattern(), false), String.format("methodName ==~ /%s/", pattern.pattern()), null, null, null, null); return condition.getRendering(); } }