[
https://issues.apache.org/jira/browse/GROOVY-9848?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17734390#comment-17734390
]
ASF GitHub Bot commented on GROOVY-9848:
----------------------------------------
blackdrag commented on code in PR #1904:
URL: https://github.com/apache/groovy/pull/1904#discussion_r1234258964
##########
src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java:
##########
@@ -1213,11 +1206,10 @@ public static boolean isCase(Iterable caseValue, Object
switchValue) {
}
/**
- * 'Case' implementation for maps which tests the groovy truth
- * value obtained using the 'switch' operand as key.
- * For example:
+ * Special 'case' implementation for maps which tests if the 'switch'
operand
+ * exists in the key set. For example:
* <pre class="groovyTestCase">switch( 'foo' ) {
- * case [foo:true, bar:false]:
+ * case [foo:true]:
Review Comment:
To make it really clear I would go one step further and change [foo:true] to
[foo:false]
##########
src/test/groovy/GroovyMethodsTest.groovy:
##########
@@ -548,10 +542,71 @@ class GroovyMethodsTest extends GroovyTestCase {
assert list.size() == 3
}
+ // GROOVY-9848
+ void testInForMaps() {
+ assert 'foo' in [foo:true]
+ assert 'foo' in [foo:null]
+ assert 'bar' !in [foo:true]
+ assert 'bar' !in [:].withDefault{ true }
Review Comment:
Again here I would use false as default to be more clear about the keys
being used
##########
src/test/groovy/GroovyMethodsTest.groovy:
##########
@@ -548,10 +542,71 @@ class GroovyMethodsTest extends GroovyTestCase {
assert list.size() == 3
}
+ // GROOVY-9848
+ void testInForMaps() {
+ assert 'foo' in [foo:true]
+ assert 'foo' in [foo:null]
+ assert 'bar' !in [foo:true]
+ assert 'bar' !in [:].withDefault{ true }
+ }
+
+ void testInForSets() {
+ def set = ['a', 'b', 'c'] as Set
+ assert 'a' in set
+ assert 'b' in set
+ assert 'c' in set
+ assert 'd' !in set
+ assert !('d' in set)
+ assert !(null in set)
+ assert !(true in set)
+ }
+
+ void testInForLists() {
+ def list = ['a', 'b', 'c']
+ assert 'a' in list
+ assert 'b' in list
+ assert 'c' in list
+ assert 'd' !in list
+ assert !('d' in list)
+ assert !(null in list)
+ assert !(true in list)
+ }
+
void testInForArrays() {
- String[] array = ['a', 'b', 'c']
+ def array = new String[]{'a', 'b', 'c'}
+ assert 'a' in array
assert 'b' in array
+ assert 'c' in array
+ assert 'd' !in array
assert !('d' in array)
+ assert !(null in array)
+ assert !(true in array)
+ }
+
+ // GROOVY-2456
+ void testInForStrings() {
+ def string = 'abc'
+ shouldFail { assert 'a' in string }
+ shouldFail { assert 'b' in string }
+ shouldFail { assert 'c' in string }
+ shouldFail { assert 'ab' in string }
+ shouldFail { assert 'bc' in string }
+ assert 'abc' in string
+ assert !('d' in string)
+ assert !(null in string)
+ assert !(true in string)
+ }
+
+ // GROOVY-7919
+ void testInForIterables() {
+ Iterable iter = { -> ['a','b','c'].iterator() }
+ assert 'a' in iter
Review Comment:
actually it would be nice to do assert 'a' in iter twice to show that the
second time it fails
> Allow membership operator to work on maps
> -----------------------------------------
>
> Key: GROOVY-9848
> URL: https://issues.apache.org/jira/browse/GROOVY-9848
> Project: Groovy
> Issue Type: Improvement
> Reporter: Keegan Witt
> Assignee: Eric Milles
> Priority: Major
> Labels: breaking, breaking_change
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)