@@ -596,6 +596,13 @@ private Statement rewriteCondition(Statement conditionStat, Expression condition
596596 }
597597
598598 private Statement rewriteCondition (Expression expr , Expression message , boolean explicit ) {
599+ boolean optOut = isOptOutExpression (expr );
600+ if (optOut ) {
601+ expr = removeOptOutPrefix (expr );
602+ if (!explicit ) {
603+ return new ExpressionStatement (expr );
604+ }
605+ }
599606 if (isSpecialCollectionCondition (expr )) { // todo check if we can better integrate it
600607 expr = transformSpecialCollectionCondition (expr );
601608 }
@@ -606,21 +613,37 @@ private Statement rewriteCondition(Expression expr, Expression message, boolean
606613 if ((Identifiers .WITH .equals (methodName ) || Identifiers .VERIFY_ALL .equals (methodName ))) {
607614 return surroundSpecialTryCatch (expr );
608615 }
609- return rewriteMethodCondition (methodCallExpression , message , explicit );
616+ return rewriteMethodCondition (methodCallExpression , message , explicit , optOut );
610617 }
611618
612619 if (expr instanceof StaticMethodCallExpression )
613- return rewriteStaticMethodCondition ((StaticMethodCallExpression ) expr , message , explicit );
620+ return rewriteStaticMethodCondition ((StaticMethodCallExpression ) expr , message , explicit , optOut );
621+
622+ return rewriteOtherCondition (expr , message , optOut );
623+ }
624+
625+ /**
626+ * Treat the nonsensical `!!` prefix as opt-out from implicit conditions
627+ */
628+ private boolean isOptOutExpression (Expression expr ) {
629+ return expr instanceof NotExpression && ((NotExpression ) expr ).getExpression () instanceof NotExpression ;
630+ }
614631
615- return rewriteOtherCondition (expr , message );
632+ private Expression removeOptOutPrefix (Expression expr ) {
633+ return ((NotExpression )((NotExpression )expr ).getExpression ()).getExpression ();
616634 }
617635
618- private Statement rewriteMethodCondition (MethodCallExpression condition , Expression message , boolean explicit ) {
636+ private Statement rewriteMethodCondition (MethodCallExpression condition , Expression message , boolean explicit , boolean optOut ) {
619637 MethodCallExpression rewritten ;
620638 int lastVariableNum ;
621- final Expression converted = convert (condition );
622- rewritten = (MethodCallExpression ) unrecord (converted );
623- lastVariableNum = extractVariableNumber (converted );
639+ if (explicit && optOut ) {
640+ rewritten = condition ;
641+ lastVariableNum = -1 ;
642+ } else {
643+ final Expression converted = convert (condition );
644+ rewritten = (MethodCallExpression ) unrecord (converted );
645+ lastVariableNum = extractVariableNumber (converted );
646+ }
624647
625648 List <Expression > args = new ArrayList <>();
626649 args .add (rewritten .getObjectExpression ());
@@ -638,16 +661,21 @@ private Statement rewriteMethodCondition(MethodCallExpression condition, Express
638661 resources .getAstNodeCache ().SpockRuntime_VerifyMethodCondition ,
639662 condition ,
640663 message ,
641- args ) );
664+ args , optOut ), optOut );
642665 }
643666
644667 private Statement rewriteStaticMethodCondition (StaticMethodCallExpression condition , Expression message ,
645- boolean explicit ) {
668+ boolean explicit , boolean optOut ) {
646669 StaticMethodCallExpression rewritten ;
647670 int lastVariableNum ;
648- final Expression converted = convert (condition );
649- rewritten = (StaticMethodCallExpression ) unrecord (converted );
650- lastVariableNum = extractVariableNumber (converted );
671+ if (explicit && optOut ) {
672+ rewritten = condition ;
673+ lastVariableNum = -1 ;
674+ } else {
675+ final Expression converted = convert (condition );
676+ rewritten = (StaticMethodCallExpression ) unrecord (converted );
677+ lastVariableNum = extractVariableNumber (converted );
678+ }
651679
652680 List <Expression > args = new ArrayList <>();
653681 args .add (new ClassExpression (rewritten .getOwnerType ()));
@@ -665,16 +693,18 @@ private Statement rewriteStaticMethodCondition(StaticMethodCallExpression condit
665693 resources .getAstNodeCache ().SpockRuntime_VerifyMethodCondition ,
666694 condition ,
667695 message ,
668- args ));
696+ args ,
697+ optOut ),
698+ optOut );
669699 }
670700
671- private Statement rewriteOtherCondition (Expression condition , Expression message ) {
672- Expression rewritten = convert (condition );
701+ private Statement rewriteOtherCondition (Expression condition , Expression message , boolean optOut ) {
702+ Expression rewritten = optOut ? condition : convert (condition );
673703
674704 final Expression executeAndVerify = rewriteToSpockRuntimeCall (resources .getAstNodeCache ().SpockRuntime_VerifyCondition ,
675- condition , message , singletonList (rewritten ));
705+ condition , message , singletonList (rewritten ), optOut );
676706
677- return surroundWithTryCatch (condition , message , executeAndVerify );
707+ return surroundWithTryCatch (condition , message , executeAndVerify , optOut );
678708 }
679709
680710 private Expression transformSpecialCollectionCondition (Expression condition ) {
@@ -725,7 +755,7 @@ private boolean isStringLikeExpression(Expression expression) {
725755 return false ;
726756 }
727757
728- private TryCatchStatement surroundWithTryCatch (Expression condition , Expression message , Expression executeAndVerify ) {
758+ private TryCatchStatement surroundWithTryCatch (Expression condition , Expression message , Expression executeAndVerify , boolean optOut ) {
729759 final TryCatchStatement tryCatchStatement = new TryCatchStatement (
730760 new ExpressionStatement (executeAndVerify ),
731761 EmptyStatement .INSTANCE
@@ -740,7 +770,7 @@ private TryCatchStatement surroundWithTryCatch(Expression condition, Expression
740770 resources .getAstNodeCache ().SpockRuntime_ConditionFailedWithException ,
741771 new ArgumentListExpression (asList (
742772 new VariableExpression (errorCollectorName ),
743- new VariableExpression ( valueRecorderName ), // recorder
773+ optOut ? ConstantExpression . NULL : new VariableExpression ( valueRecorderName ), // recorder
744774 new ConstantExpression (resources .getSourceText (condition )), // text
745775 new ConstantExpression (condition .getLineNumber ()), // line
746776 new ConstantExpression (condition .getColumnNumber ()), // column
@@ -779,15 +809,15 @@ private TryCatchStatement surroundSpecialTryCatch(Expression executeAndVerify) {
779809 }
780810
781811 private Expression rewriteToSpockRuntimeCall (MethodNode method , Expression condition , Expression message ,
782- List <Expression > additionalArgs ) {
812+ List <Expression > additionalArgs , boolean optOut ) {
783813 List <Expression > args = new ArrayList <>();
784814
785815 MethodCallExpression result = createDirectMethodCall (
786816 new ClassExpression (resources .getAstNodeCache ().SpockRuntime ), method ,
787817 new ArgumentListExpression (args ));
788818
789819 args .add (new VariableExpression (errorCollectorName , resources .getAstNodeCache ().ErrorCollector ));
790- args .add (createDirectMethodCall (
820+ args .add (createDirectMethodCall (optOut ? ConstantExpression . NULL :
791821 new VariableExpression (valueRecorderName ),
792822 resources .getAstNodeCache ().ValueRecorder_Reset ,
793823 ArgumentListExpression .EMPTY_ARGUMENTS ));
0 commit comments