Skip to content

Commit

Permalink
GROOVY-9848: in operator: key set membership for isCase(Map,Object)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 19, 2023
1 parent a475136 commit 9c75727
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
27 changes: 12 additions & 15 deletions src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ public static boolean isCase(Object caseValue, Object switchValue) {
}

/**
* Special 'Case' implementation for Class, which allows testing
* Special 'case' implementation for Class, which allows testing
* whether some switch value is assignable from the given case class.
*
* If the switch value is an object, {@code isCase} will return true if the
Expand Down Expand Up @@ -1163,9 +1163,8 @@ public static boolean isCase(Class caseValue, Object switchValue) {
}

/**
* 'Case' implementation for collections which tests if the 'switch'
* operand is contained in any of the 'case' values.
* For example:
* Special 'case' implementation for collections which tests if the 'switch'
* operand is contained in any of the 'case' values. For example:
* <pre class="groovyTestCase">switch( 3 ) {
* case [1,3,5]:
* assert true
Expand All @@ -1185,9 +1184,8 @@ public static boolean isCase(Collection caseValue, Object switchValue) {
}

/**
* 'Case' implementation for iterable types which tests if the 'switch'
* operand is contained in any of the 'case' values.
* For example:
* Special 'case' implementation for iterables which tests if the 'switch'
* operand is contained in any of the 'case' values. For example:
* <pre class="groovyTestCase">Iterable it = {[1,3,5].iterator()}
* switch( 3 ) {
* case it:
Expand All @@ -1197,7 +1195,7 @@ public static boolean isCase(Collection caseValue, Object switchValue) {
* assert false
* }
*
* //GROOVY-7919
* // GROOVY-7919
* assert 1 in it
* assert 2 !in it
* </pre>
Expand All @@ -1213,11 +1211,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]:
* assert true
* break
* default:
Expand All @@ -1226,11 +1223,11 @@ public static boolean isCase(Iterable caseValue, Object switchValue) {
*
* @param caseValue the case value
* @param switchValue the switch value
* @return the groovy truth value from caseValue corresponding to the switchValue key
* @return true if the key set of caseValue contains the switchValue
* @since 1.7.6
*/
public static boolean isCase(Map caseValue, Object switchValue) {
return DefaultTypeTransformation.castToBoolean(caseValue.get(switchValue));
public static boolean isCase(Map<?,?> caseValue, Object switchValue) {
return caseValue.containsKey(switchValue);
}

/**
Expand Down
46 changes: 30 additions & 16 deletions src/test/groovy/MapTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -329,31 +329,45 @@ final class MapTest extends GroovyTestCase {
assert result2 == 'c2 b3 a1 '
}

void testMapWithDefault() {
def m = [:].withDefault {k -> k * 2}
m[1] = 3
assert m[1] == 3
assert m[2] == 4
assert [1: 3, 2: 4] == m
assert m == [1: 3, 2: 4]
// GROOVY-9848
void testMapIsCaseWithIn() {
assert 'foo' in [foo:true]
assert 'foo' in [foo:null]
assert 'bar' !in [foo:true]
assert 'bar' !in [:].withDefault{ true }
}

void testMapIsCaseWithGrep() {
def predicate = [apple:true, banana:true, lemon:false, orange:false, pear:true]
def fruitList = ['apple', 'apple', 'pear', 'orange', 'pear', 'lemon', 'banana']
def expected = ['apple', 'apple', 'pear', 'pear', 'banana']
assert fruitList.grep(predicate) == expected
def predicate = [apple:true, banana:true, lemon:false, orange:false, pear:true]
predicate.retainAll{ e -> e.value } // GROOVY-9848: retain truthy entrys

def nonCitrus = fruitList.grep(predicate)
assert nonCitrus == ['apple', 'apple', 'pear', 'pear', 'banana']
}

void testMapIsCaseWithSwitch() {
switch ('foo') {
case [foo: true, bar: false]: assert true; break
default: assert false
assert switch ('foo') {
case [foo: true, bar: false] -> true
default -> false
}
switch ('bar') {
case [foo: true, bar: false]: assert false; break
default: assert true
assert switch ('foo') {
case [foo: false, bar: true] -> true
default -> false
}
assert switch ('bar') {
case [foo: true] -> false
default -> true
}
}

void testMapWithDefault() {
def m = [:].withDefault { k -> k * 2 }
m[1] = 3
assert m[1] == 3
assert m[2] == 4
assert [1: 3, 2: 4] == m
assert m == [1: 3, 2: 4]
}

void testMapWithDefaultCanBeConfiguredToNotStoreDefaultValue() {
Expand Down

0 comments on commit 9c75727

Please sign in to comment.