Skip to content

Commit

Permalink
GROOVY-10936: ~string is a java.util.regex.Pattern
Browse files Browse the repository at this point in the history
3_0_X backport
  • Loading branch information
eric-milles committed Dec 15, 2023
1 parent c9f87f7 commit 47100b0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,23 +34,33 @@ 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);
ret.copyNodeMetaData(this);
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;
}

}
29 changes: 0 additions & 29 deletions src/test/groovy/bugs/TernaryOperatorTest.groovy

This file was deleted.

53 changes: 41 additions & 12 deletions src/test/groovy/operator/TernaryOperatorsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,45 @@
*/
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
def y = a!=1 ? a!=2 : a!=1
assert y == false
}

@Test
void testElvisOperator() {
def a = 1
def x = a?:2
Expand All @@ -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"
Expand All @@ -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.
Expand All @@ -88,6 +95,7 @@ class TernaryOperatorsTest extends GroovyTestCase {
assert true
}

@Test
void testLineBreaks() {
def bar = 0 ? "moo" : "cow"
assert bar == 'cow'
Expand Down Expand Up @@ -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
}
}

0 comments on commit 47100b0

Please sign in to comment.