Skip to content

Commit

Permalink
GROOVY-11195: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 16, 2023
1 parent 0db1953 commit 898c2c4
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
object = string
}
}
;
def obj = new Baz()
obj.setString('xx')
assert obj.object == 'xx'
Expand Down Expand Up @@ -561,7 +561,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
class Main {
def bar(Date date1, Date date2) {
}
def bar(String o, Date date2) {
def bar(String str, Date date) {
}
def foo() {
bar(null, new Date())
Expand All @@ -574,10 +574,10 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
// GROOVY-5175
void testDisambiguateCallMethodWithNullAndAnotherParameter() {
assertClass '''
class Test {
class Main {
def bar(Date date1, Date date2) {
}
def bar(String o, Date date2) {
def bar(String str, Date date) {
}
def foo() {
bar((Date)null, new Date())
Expand Down Expand Up @@ -811,11 +811,8 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
// GROOVY-7061
void testSAMWithExplicitParameter3() {
assertScript '''
void test() {
List<Integer> nums = [1, 2, 3, -2, -5, 6]
Collections.sort(nums, { a, b -> a.abs() <=> b.abs() })
}
test()
List<Integer> nums = [1, 2, 3, -2, -5, 6]
Collections.sort(nums, { a, b -> a.abs() <=> b.abs() })
'''
}

Expand Down Expand Up @@ -1091,15 +1088,14 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {

// GROOVY-5540
void testChoosePublicMethodInHierarchy() {
assertScript '''
import groovy.transform.stc.MethodCallsSTCTest.Child2 as C2
assertScript '''import groovy.transform.stc.MethodCallsSTCTest.Child2
class A {
int delegate() {
@ASTTest(phase=INSTRUCTION_SELECTION, value={
def md = node.rightExpression.getNodeMetaData(DIRECT_METHOD_CALL_TARGET)
assert md.declaringClass.nameWithoutPackage == 'MethodCallsSTCTest$ChildWithPublic'
})
int res = new C2().m()
int res = new Child2().m()
res
}
}
Expand Down Expand Up @@ -1198,7 +1194,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
interface Lower extends Upper {}
class Foo implements Lower { String getName() { 'bar' } }
String foo(Foo impl) {
impl.getName()
impl.getName()
}
assert foo(new Foo()) == 'bar'
'''
Expand All @@ -1211,7 +1207,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
class Foo implements Lower { String getName() { 'bar' } }
class Bar extends Foo {}
String foo(Bar impl) {
impl.getName()
impl.getName()
}
assert foo(new Bar()) == 'bar'
'''
Expand Down Expand Up @@ -1560,7 +1556,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
void testClosureAsParameter() {
assertScript '''
Integer a( String s, Closure<Integer> b ) {
b( s )
b( s )
}
assert a( 'tim' ) { 0 } == 0
Expand All @@ -1571,7 +1567,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
void testClosureAsParameterWithDefaultValue() {
assertScript '''
Integer a( String s, Closure<Integer> b = {String it -> it.length()}) {
b( s )
b( s )
}
assert a( 'tim' ) == 3
Expand All @@ -1580,8 +1576,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {

// GROOVY-5712
void testClassForNameVsCharsetForName() {
assertScript '''
import java.nio.charset.Charset
assertScript '''import java.nio.charset.Charset
Charset charset = Charset.forName('UTF-8')
assert charset instanceof Charset
'''
Expand Down Expand Up @@ -1624,7 +1619,6 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
public static foo() {
super.foo()
}
}
Bottom.foo()
assert Top.called
Expand All @@ -1633,25 +1627,35 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {

void testShouldFindSetProperty() {
assertScript '''
class A {
int x
void foo() {
this.setProperty('x', 1)
class C {
int p
void m() {
this.setProperty('p', 1)
}
}
def a = new A()
a.foo()
assert a.x == 1
def c = new C()
c.m()
assert c.p == 1
'''
}

// GROOVY-5888
void testStaticContextScoping() {
void testStaticContext1() {
assertScript '''
class A {
static List foo = 'a,b,c'.split(/,/)*.trim()
class C {
static List p = 'a,b,c'.split(/,/)*.trim()
}
assert A.foo == ['a','b','c']
assert C.p == ['a','b','c']
'''
}

// GROOVY-11195
void testStaticContext2() {
assertScript '''
class C {
static String p = this.getName() // instance method of Class
}
assert C.p == 'C'
'''
}

Expand Down Expand Up @@ -1789,8 +1793,8 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
// GROOVY-8445
void testClosureToFunctionalInterface() {
assertScript '''
public class Main {
public static void main(String[] args) {
class Main {
static main(args) {
assert 13 == [1, 2, 3].stream().reduce(7, {Integer r, Integer e -> r + e})
}
}
Expand Down

0 comments on commit 898c2c4

Please sign in to comment.