diff --git a/src/main/java/org/codehaus/groovy/ast/expr/BitwiseNegationExpression.java b/src/main/java/org/codehaus/groovy/ast/expr/BitwiseNegationExpression.java index 58872812787..5841c9dcad3 100644 --- a/src/main/java/org/codehaus/groovy/ast/expr/BitwiseNegationExpression.java +++ b/src/main/java/org/codehaus/groovy/ast/expr/BitwiseNegationExpression.java @@ -18,6 +18,7 @@ */ package org.codehaus.groovy.ast.expr; +import org.codehaus.groovy.ast.ClassHelper; import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.GroovyCodeVisitor; @@ -33,10 +34,12 @@ public Expression getExpression() { return expression; } + @Override public void visit(GroovyCodeVisitor visitor) { visitor.visitBitwiseNegationExpression(this); } + @Override public Expression transformExpression(ExpressionTransformer transformer) { Expression ret = new BitwiseNegationExpression(transformer.transform(expression)); ret.setSourcePosition(this); @@ -44,12 +47,20 @@ public Expression transformExpression(ExpressionTransformer transformer) { return ret; } + @Override public String getText() { - return expression.getText(); + return "~(" + expression.getText() + ")"; } + /** + * @see org.codehaus.groovy.runtime.InvokerHelper#bitwiseNegate(Object) + */ + @Override public ClassNode getType() { - return expression.getType(); + ClassNode type = expression.getType(); + if (ClassHelper.STRING_TYPE.equals(type) || ClassHelper.GSTRING_TYPE.equals(type)) { + type = ClassHelper.PATTERN_TYPE; // GROOVY-10936 + } + return type; } - } diff --git a/src/test/groovy/bugs/TernaryOperatorTest.groovy b/src/test/groovy/bugs/TernaryOperatorTest.groovy deleted file mode 100644 index fd740d6f7ea..00000000000 --- a/src/test/groovy/bugs/TernaryOperatorTest.groovy +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package groovy.bugs - -import groovy.test.GroovyTestCase - -class TernaryOperatorBugTest extends GroovyTestCase { - void testTernaryOperator() { - assertScript ''' - Class dsClass = true ? LinkedHashSet : HashSet - ''' - } -} diff --git a/src/test/groovy/operator/TernaryOperatorsTest.groovy b/src/test/groovy/operator/TernaryOperatorsTest.groovy index e96f8ac0998..edb90b580bc 100644 --- a/src/test/groovy/operator/TernaryOperatorsTest.groovy +++ b/src/test/groovy/operator/TernaryOperatorsTest.groovy @@ -18,33 +18,37 @@ */ package groovy.operator -import groovy.test.GroovyTestCase +import org.junit.Test -class TernaryOperatorsTest extends GroovyTestCase { +import java.util.regex.Pattern +final class TernaryOperatorsTest { + + @Test void testSimpleUse() { def y = 5 def x = (y > 1) ? "worked" : "failed" assert x == "worked" - - x = (y < 4) ? "failed" : "worked" + x = (y < 4) ? "failed" : "worked" assert x == "worked" } + @Test void testUseInParameterCalling() { def z = 123 assertCalledWithFoo(z > 100 ? "foo" : "bar") assertCalledWithFoo(z < 100 ? "bar" : "foo") - } + } - def assertCalledWithFoo(param) { + void assertCalledWithFoo(param) { println "called with param ${param}" assert param == "foo" } - - void testWithBoolean(){ + + @Test + void testWithBoolean() { def a = 1 def x = a!=null ? a!=2 : a!=1 assert x == true @@ -52,6 +56,7 @@ class TernaryOperatorsTest extends GroovyTestCase { assert y == false } + @Test void testElvisOperator() { def a = 1 def x = a?:2 @@ -63,7 +68,7 @@ class TernaryOperatorsTest extends GroovyTestCase { a = null x = a?:2 assert x==2 - + def list = ['a','b','c'] def index = 0 def ret = list[index++]?:"something else" @@ -73,13 +78,15 @@ class TernaryOperatorsTest extends GroovyTestCase { ?: "something else entirely" assert ret2 == 'b' } - + + @Test void testForType() { boolean b = false int anInt = b ? 100 : 100 / 3 assert anInt.class == Integer } - + + @Test void testBytecodeRegisters() { // this code will blow up if the true and false parts // are not handled correctly in regards to the registers. @@ -88,6 +95,7 @@ class TernaryOperatorsTest extends GroovyTestCase { assert true } + @Test void testLineBreaks() { def bar = 0 ? "moo" : "cow" assert bar == 'cow' @@ -119,4 +127,25 @@ class TernaryOperatorsTest extends GroovyTestCase { : "cow" assert bar == 'cow' } -} + + // GROOVY-10936 + @Test + void testCommonType() { + def random = new Random() + + def staticPatternSlashy = ~/some static pattern \w+/ + def staticPatternString = ~"some static pattern \\w+" + def dynamicPatternSlashy = (random.nextInt() % 2 == 0) ? ~/pattern one \w+/ : ~/pattern two \w+/ + def dynamicPatternString = (random.nextInt() % 2 == 0) ? ~"pattern one \\w+" : ~"pattern two \\w+" + + assert staticPatternSlashy instanceof Pattern + assert staticPatternString instanceof Pattern + assert dynamicPatternSlashy instanceof Pattern + assert dynamicPatternString instanceof Pattern + } + + @Test // see StatementMetaTypeChooser#resolveType + void testClassExpressionIsJavaLangClassNotLiteralType() { + Class dsClass = true ? LinkedHashSet : HashSet + } +}