"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/test/groovy/transform/stc/ClosuresSTCTest.groovy" between
apache-groovy-src-3.0.8.zip and apache-groovy-src-3.0.9.zip

About: Groovy is an agile dynamic (scripting) language for the Java Platform. It has features similar to those of Python, Ruby, Perl, and Smalltalk. Source release.

ClosuresSTCTest.groovy  (apache-groovy-src-3.0.8):ClosuresSTCTest.groovy  (apache-groovy-src-3.0.9)
skipping to change at line 21 skipping to change at line 21
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package groovy.transform.stc package groovy.transform.stc
import groovy.test.NotYetImplemented
/** /**
* Unit tests for static type checking : closures. * Unit tests for static type checking : closures.
*/ */
class ClosuresSTCTest extends StaticTypeCheckingTestCase { class ClosuresSTCTest extends StaticTypeCheckingTestCase {
void testClosureWithoutArguments() { void testClosureWithoutArguments() {
assertScript ''' assertScript '''
def clos = { println "hello!" } def clos = { println "hello!" }
println "Executing the Closure:" println "Executing the Closure:"
clos() //prints "hello!" clos() //prints "hello!"
''' '''
} }
// GROOVY-9079: no params to statically type check but shouldn't get NPE
void testClosureWithoutArgumentsExplicit() { void testClosureWithoutArgumentsExplicit() {
// GROOVY-9079: no params to statically type check but shouldn't get NPE
assertScript ''' assertScript '''
import java.util.concurrent.Callable import java.util.concurrent.Callable
String makeFoo() { String makeFoo() {
Callable<String> call = { -> 'foo' } Callable<String> call = { -> 'foo' }
call() call()
} }
assert makeFoo() == 'foo' assert makeFoo() == 'foo'
''' '''
skipping to change at line 120 skipping to change at line 122
if (x instanceof Integer) { if (x instanceof Integer) {
def bar = { -> return x } def bar = { -> return x }
return bar.call() return bar.call()
} }
return 0 return 0
} }
assert foo(1) == 1 assert foo(1) == 1
''' '''
} }
// GROOVY-8427
void testClosureReturnTypeInference6() {
assertScript '''
import java.util.function.Consumer
class C {
static <T> void m(T a, Consumer<T> c) {
c.accept(a)
}
static void main(args) {
def c = { ->
int x = 0
m('') {
print 'void return'
}
}
c.call()
}
}
'''
}
// GROOVY-8202
void testClosureReturnTypeInference7() {
assertScript '''
void proc() {
}
String test0(flag) {
if (flag) {
'foo'
} else {
proc()
}
}
String test1(flag) {
Closure<String> c = { ->
if (flag) {
'bar'
} else {
proc()
null
}
}
c.call()
}
String test2(flag) {
Closure<String> c = { -> // Cannot assign Closure<Object> to Closu
re<String>
if (flag) {
'baz'
} else {
proc()
}
}
c.call()
}
assert test0(true) == 'foo'
assert test1(true) == 'bar'
assert test2(true) == 'baz'
assert test0(false) == null
assert test1(false) == null
assert test2(false) == null
'''
assertScript '''
Closure<Void> c = { flag ->
if (flag) {
print 'true'
} else {
print 'false'
}
}
'''
}
// GROOVY-9971
void testClosureReturnTypeInference8() {
assertScript '''
def m(Closure<String> c) {
c.call()
}
final x = 123
Closure<String> c = { -> "x=$x" }
String type = c.call().class.name
assert type == 'java.lang.String'
type = (m { -> "x=$x" }).class.name
assert type == 'java.lang.String' // not GStringImpl
'''
}
// GROOVY-5145 // GROOVY-5145
void testCollect() { void testCollect() {
assertScript ''' assertScript '''
List<String> strings = [1,2,3].collect { it.toString() } List<String> strings = [1,2,3].collect { it.toString() }
''' '''
} }
// GROOVY-5145 // GROOVY-5145
void testCollectWithSubclass() { void testCollectWithSubclass() {
assertScript ''' assertScript '''
skipping to change at line 200 skipping to change at line 292
void test() { void test() {
def x = new B(); def x = new B();
{ -> x = new C() }(); { -> x = new C() }();
def c = x def c = x
c.m() c.m()
} }
''', 'Cannot find matching method A#m()' ''', 'Cannot find matching method A#m()'
} }
// GROOVY-10052
void testClosureSharedVariable4() {
assertScript '''
String x
def f = { ->
x = Optional.of('x').orElseThrow{ new Exception() }
}
assert f() == 'x'
assert x == 'x'
'''
}
@NotYetImplemented // GROOVY-10052
void testClosureSharedVariable5() {
assertScript '''
def x
def f = { ->
x = Optional.of('x').orElseThrow{ new Exception() }
}
assert f() == 'x'
assert x == 'x'
'''
}
// GROOVY-10052
void testNotClosureSharedVariable() {
assertScript '''
String x = Optional.of('x').orElseThrow{ new Exception() }
def f = { ->
String y = Optional.of('y').orElseThrow{ new Exception() }
}
assert x == 'x'
assert f() == 'y'
'''
}
void testClosureCallAsAMethod() { void testClosureCallAsAMethod() {
assertScript ''' assertScript '''
Closure cl = { 'foo' } Closure cl = { 'foo' }
assert cl() == 'foo' assert cl() == 'foo'
''' '''
} }
void testClosureCallWithOneArgAsAMethod() { void testClosureCallWithOneArgAsAMethod() {
assertScript ''' assertScript '''
Closure cl = { int x -> "foo$x" } Closure cl = { int x -> "foo$x" }
 End of changes. 5 change blocks. 
1 lines changed or deleted 131 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)