Commit cc596c09 authored by Martti Käärik's avatar Martti Käärik
Browse files

Add details about Assertion failure to exception message (if predefined function call) #135

parent 0a86e08d
Loading
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -1127,7 +1127,7 @@ public class JUnitTestGenerator extends Renderer {
					append("VerdictImpl.fail");
				line(");");

				line("throw new " + ASSERTION_EXCEPTION + "(\"" + getMessage(b) + "\");");
				writeAssertionFailure(b, ((Assertion) b).getCondition(), dataUseVariables);

				blockClose();

@@ -2611,6 +2611,36 @@ public class JUnitTestGenerator extends Renderer {
		return b.eClass().getName() + ": " + b.getName();
	}


	private void writeAssertionFailure(Behaviour b, DataUse condition, Map<DataUse, String> dataUseVariables) {
		DataUse top = condition;
		if (top instanceof CastDataUse)
			top = ((CastDataUse) top).getDataUse();

		List<DataUse> operands = (top instanceof PredefinedFunctionCall) ? getDataUseArgumentValues(top)
				: Collections.emptyList();

		if (operands.size() > 1) {
			// Echo the operator name and each argument value on its own line.
			String fn = ((PredefinedFunctionCall) top).getFunction().getName();
			append("throw new " + ASSERTION_EXCEPTION + "(\"" + getMessage(b) + " evaluated to false:"
					+ "\\n\\toperator: " + fn + "\"");
			for (int i = 0; i < operands.size(); i++) {
				append(" + \"\\n\\targ[" + i + "] = \" + String.valueOf(");
				write(operands.get(i), dataUseVariables);
				append(")");
			}
			line(");");
			return;
		}

		// No multi-argument operator to split on: echo the condition's own value.
		append("throw new " + ASSERTION_EXCEPTION + "(\"" + getMessage(b) + " evaluated to false:"
				+ "\\n\\tvalue = \" + String.valueOf(");
		write(condition, dataUseVariables);
		line(");");
	}

	private boolean isParticipating(Behaviour b) {
		return b.getParticipatingComponents().stream().anyMatch(c -> isCurrentComponentInstance(c));
	}