diff options
author | Jon Brandvein <brandjon@google.com> | 2017-01-13 17:46:29 +0000 |
---|---|---|
committer | Vladimir Moskva <vladmos@google.com> | 2017-01-16 13:43:23 +0000 |
commit | b3d0bdd0e0c2f0d766a7fd8e8428ab5b5a43ad48 (patch) | |
tree | 1b7d7f64ddb6ad54cce0553303da71c7699e3c79 /src/test/java/com/google/devtools/build/lib/syntax | |
parent | ae0ad666322855eb3641dc107964fc90ec80f042 (diff) |
Add a to_list() method to depsets
This is the preferred way to test for membership in, or iterate over, depsets (aka nested sets, aka plain old set()). The old way of doing membership tests or iterations over the raw depset itself is deprecated and may be removed in the future.
Note that membership testing in a depset was always an O(n) operation, perhaps contrary to the user's expectation, so using to_list() does not make things asymptotically worse. It just makes things more explicit.
RELNOTES: To iterate over or test for membership in a set, prefer using the new to_list() method. E.g., "for x in myset.to_list():", or "print(x in myset.to_list())". Iteration/membership-test on the raw set itself is deprecated.
--
PiperOrigin-RevId: 144452510
MOS_MIGRATED_REVID=144452510
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java index 3f06c5495a..fc5b685cb2 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java @@ -16,14 +16,15 @@ package com.google.devtools.build.lib.syntax; import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.collect.nestedset.Order; +import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -39,7 +40,7 @@ public class SkylarkNestedSetTest extends EvaluationTestCase { eval("ds = set([1, 2, 3], order='compile')"); SkylarkNestedSet ds = get("ds"); assertThat(ds.getOrder().getName()).isEqualTo("compile"); - assertThat(ds.expandedSet()).isEqualTo(ImmutableSet.of(1, 2, 3)); + assertThat(ds.getSet(Object.class)).containsExactly(1, 2, 3); } @Test @@ -49,6 +50,18 @@ public class SkylarkNestedSetTest extends EvaluationTestCase { } @Test + public void testGetSet() throws Exception { + eval("s = depset(['a', 'b'])"); + assertThat(get("s").getSet(String.class)).containsExactly("a", "b").inOrder(); + assertThat(get("s").getSet(Object.class)).containsExactly("a", "b").inOrder(); + try { + get("s").getSet(Integer.class); + Assert.fail("getSet() with wrong type should have raised IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + } + + @Test public void testNsetOrder() throws Exception { eval("n = depset(['a', 'b'], order='compile')"); assertThat(get("n").getSet(String.class).getOrder()).isEqualTo(Order.COMPILE_ORDER); @@ -61,6 +74,12 @@ public class SkylarkNestedSetTest extends EvaluationTestCase { } @Test + public void testNsetHomogeneousGenericType() throws Exception { + eval("n = depset(['a', 'b', 'c'])"); + assertThat(get("n").getContentType()).isEqualTo(SkylarkType.of(String.class)); + } + + @Test public void testDepsetBadOrder() throws Exception { new BothModesTest().testIfExactError("Invalid order: bad", "depset(['a'], order='bad')"); } @@ -252,6 +271,16 @@ public class SkylarkNestedSetTest extends EvaluationTestCase { } @Test + public void testToList() throws Exception { + eval( + "s = depset() + [2, 4, 6] + [3, 4, 5]", + "x = s.to_list()"); + Object value = lookup("x"); + assertThat(value).isInstanceOf(MutableList.class); + assertThat((Iterable<?>) value).containsExactly(2, 4, 6, 3, 5).inOrder(); + } + + @Test public void testSetOrderCompatibility() throws Exception { // Two sets are compatible if // (a) both have the same order or |