private static List<Object> getValues(ValueRecorder recorder) { return recorder == null ? null : recorder.getValues(); }
/** * Records and returns the specified value. Hence an expression can be replaced * with record(expression) without impacting evaluation of the expression. */ public Object record(int index, Object value) { realizeNas(index + 1, null); values.set(index, value); boolean foundThisCallOnStack = false; while (!startedRecordings.isEmpty()){ final Integer indexFromStack = startedRecordings.pop(); if (indexFromStack==index){ foundThisCallOnStack = true; break; } } if (!foundThisCallOnStack){ throw new IllegalStateException("Did not find call #"+index+" on stack. Invalid call of record method?"); } return value; }
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); } }
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); }