"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/test/groovy/NamedParameterTest.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.

NamedParameterTest.groovy  (apache-groovy-src-3.0.8):NamedParameterTest.groovy  (apache-groovy-src-3.0.9)
skipping to change at line 28 skipping to change at line 28
*/ */
package groovy package groovy
import groovy.test.GroovyTestCase import groovy.test.GroovyTestCase
import groovy.transform.NamedParam import groovy.transform.NamedParam
import groovy.transform.NamedParams import groovy.transform.NamedParams
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
import static groovy.NamedParameterHelper.myJavaMethod import static groovy.NamedParameterHelper.myJavaMethod
class NamedParameterTest extends GroovyTestCase { final class NamedParameterTest extends GroovyTestCase {
void testPassingNamedParametersToMethod() { void testPassingNamedParametersToMethod() {
someMethod(name:"gromit", eating:"nice cheese", times:2) someMethod(name:"gromit", eating:"nice cheese", times:2)
} }
protected void someMethod(args) { protected void someMethod(args) {
assert args.name == "gromit" assert args.name == "gromit"
assert args.eating == "nice cheese" assert args.eating == "nice cheese"
assert args.times == 2 assert args.times == 2
assert args.size() == 3 assert args.size() == 3
skipping to change at line 73 skipping to change at line 73
assert myJavaMethod(foo: 'FOO', bar: 25, 42) == 'foo = FOO, bar = 25, nu m = 42' assert myJavaMethod(foo: 'FOO', bar: 25, 42) == 'foo = FOO, bar = 25, nu m = 42'
assert myJavaMethod(foo: 'FOO', 142) == 'foo = FOO, bar = null, num = 14 2' assert myJavaMethod(foo: 'FOO', 142) == 'foo = FOO, bar = null, num = 14 2'
assert myMethod(foo: 'FOO', bar: 'BAR') == 'foo = FOO, bar = BAR' assert myMethod(foo: 'FOO', bar: 'BAR') == 'foo = FOO, bar = BAR'
assert myMethod(bar: 'BAR') == 'foo = null, bar = BAR' assert myMethod(bar: 'BAR') == 'foo = null, bar = BAR'
assert myMethod(foo: 'FOO', bar: 35,242) == 'foo = FOO, bar = 35, num = 242' assert myMethod(foo: 'FOO', bar: 35,242) == 'foo = FOO, bar = 35, num = 242'
assert myMethod(foo: 'FOO', 342) == 'foo = FOO, bar = null, num = 342' assert myMethod(foo: 'FOO', 342) == 'foo = FOO, bar = null, num = 342'
assertScript ''' assertScript '''
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod import static groovy.NamedParameterTest.myMethod
int getAnswer() { 42 }
@TypeChecked @TypeChecked
def method() { def method() {
assert myMethod(foo: 'FOO', bar: 'BAR') == 'foo = FOO, bar = BAR assert myMethod(foo: 'FOO', bar: 'BAR') == 'foo = FOO,
' bar = BAR'
assert myMethod(bar: 'BAR') == 'foo = null, bar = BAR' assert myMethod(bar: 'BAR') == 'foo = null,
assert myMethod(foo: 'FOO', bar: 45, 442) == 'foo = FOO, bar = 4 bar = BAR'
5, num = 442' assert myMethod(foo: 'FOO', bar: 45, 442) == 'foo = FOO,
assert myMethod(foo: 'FOO', 542) == 'foo = FOO, bar = null, num bar = 45, num = 442'
= 542' assert myMethod(foo: 'FOO', 542) == 'foo = FOO,
bar = null, num = 542'
assert myMethod(foo: 'string', bar: answer, 666) == 'foo = strin
g, bar = 42, num = 666' // GROOVY-10027
} }
method() method()
''' '''
} }
void testMissingRequiredName() { void testMissingRequiredName() {
def message = shouldFail ''' def message = shouldFail '''
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod import static groovy.NamedParameterTest.myMethod
@TypeChecked @TypeChecked
def method() { def method() {
myMethod(foo: 'FOO') myMethod(foo: 'FOO')
} }
method() method()
''' '''
assert message.contains("required named arg 'bar' not found") assert message.contains("required named param 'bar' not found")
} }
void testUnknownName() { void testUnknownName() {
def message = shouldFail ''' def message = shouldFail '''
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod import static groovy.NamedParameterTest.myMethod
@TypeChecked @TypeChecked
def method() { def method() {
myMethod(bar: 'BAR', baz: 'BAZ') myMethod(bar: 'BAR', baz: 'BAZ')
} }
method() method()
''' '''
assert message.contains("unexpected named arg: baz") assert message.contains("unexpected named arg: baz")
} }
void testInvalidType() { // GROOVY-10027
void testFlowType() {
assertScript '''
import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod
@TypeChecked
def method(arg) {
if (arg instanceof Integer) {
myMethod(foo: 'x', bar: arg, 123)
}
}
assert method(42) == 'foo = x, bar = 42, num = 123'
'''
}
void testInvalidType1() {
def message = shouldFail ''' def message = shouldFail '''
import groovy.transform.TypeChecked import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod import static groovy.NamedParameterTest.myMethod
@TypeChecked @TypeChecked
def method() { def method() {
myMethod(foo: 42, 42) myMethod(foo:42, -1)
} }
method()
''' '''
assert message.contains("parameter for named arg 'foo' has type 'int' bu t expected 'java.lang.String'") assert message.contains("argument for named param 'foo' has type 'int' b ut expected 'java.lang.String'")
} }
void testInvalidType2() {
def message = shouldFail '''
import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod
@TypeChecked
def method() {
int answer = 42
myMethod(foo:answer, -1)
}
'''
assert message.contains("argument for named param 'foo' has type 'int' b
ut expected 'java.lang.String'")
}
// GROOVY-10027
void testInvalidType3() {
def message = shouldFail '''
import groovy.transform.TypeChecked
import static groovy.NamedParameterTest.myMethod
int getAnswer() { 42 }
@TypeChecked
def method() {
myMethod(foo:answer, -1)
}
'''
assert message.contains("argument for named param 'foo' has type 'int' b
ut expected 'java.lang.String'")
}
//--------------------------------------------------------------------------
static String myMethod(@NamedParams([ static String myMethod(@NamedParams([
@NamedParam(value = "foo"), @NamedParam(value = "foo"),
@NamedParam(value = "bar", type = String, required = true) @NamedParam(value = "bar", type = String, required = true)
]) Map params) { ]) Map params) {
"foo = $params.foo, bar = $params.bar" "foo = $params.foo, bar = $params.bar"
} }
static String myMethod(@NamedParams([ static String myMethod(@NamedParams([
@NamedParam(value = "foo", type = String, required = true), @NamedParam(value = "foo", type = String, required = true),
@NamedParam(value = "bar", type = Integer) @NamedParam(value = "bar", type = Integer)
 End of changes. 9 change blocks. 
13 lines changed or deleted 67 lines changed or added

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