Refactor SkylarkNestedSet type checks and tests
Moved some tests, fixed formatting, changed to use assertThat().
--
PiperOrigin-RevId: 144356402
MOS_MIGRATED_REVID=144356402
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
index b2910a2..e0c6c99 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
@@ -15,9 +15,7 @@
package com.google.devtools.build.lib.syntax;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.assertEquals;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
@@ -1410,56 +1408,6 @@
}
@Test
- public void testSetUnionWithList() throws Exception {
- evaluateSet("depset([]).union(['a', 'b', 'c'])", "a", "b", "c");
- evaluateSet("depset(['a']).union(['b', 'c'])", "a", "b", "c");
- evaluateSet("depset(['a', 'b']).union(['c'])", "a", "b", "c");
- evaluateSet("depset(['a', 'b', 'c']).union([])", "a", "b", "c");
- }
-
- @Test
- public void testSetUnionWithSet() throws Exception {
- evaluateSet("depset([]).union(depset(['a', 'b', 'c']))", "a", "b", "c");
- evaluateSet("depset(['a']).union(depset(['b', 'c']))", "a", "b", "c");
- evaluateSet("depset(['a', 'b']).union(depset(['c']))", "a", "b", "c");
- evaluateSet("depset(['a', 'b', 'c']).union(depset([]))", "a", "b", "c");
- }
-
- @Test
- public void testSetUnionDuplicates() throws Exception {
- evaluateSet("depset(['a', 'b', 'c']).union(['a', 'b', 'c'])", "a", "b", "c");
- evaluateSet("depset(['a', 'a', 'a']).union(['a', 'a'])", "a");
-
- evaluateSet("depset(['a', 'b', 'c']).union(depset(['a', 'b', 'c']))", "a", "b", "c");
- evaluateSet("depset(['a', 'a', 'a']).union(depset(['a', 'a']))", "a");
- }
-
- @Test
- public void testSetUnionError() throws Exception {
- new BothModesTest()
- .testIfErrorContains("insufficient arguments received by union", "depset(['a']).union()")
- .testIfErrorContains(
- "method depset.union(new_elements: Iterable) is not applicable for arguments (string): "
- + "'new_elements' is 'string', but should be 'Iterable'",
- "depset(['a']).union('b')");
- }
-
- @Test
- public void testSetUnionSideEffects() throws Exception {
- eval(
- "def func():",
- " n1 = depset(['a'])",
- " n2 = n1.union(['b'])",
- " return n1",
- "n = func()");
- assertEquals(ImmutableList.of("a"), ((SkylarkNestedSet) lookup("n")).toCollection());
- }
-
- private void evaluateSet(String statement, Object... expectedElements) throws Exception {
- new BothModesTest().testCollection(statement, expectedElements);
- }
-
- @Test
public void testListIndexMethod() throws Exception {
new BothModesTest()
.testStatement("['a', 'b', 'c'].index('a')", 0)
@@ -1753,6 +1701,8 @@
.testStatement("type(str)", "function");
}
+ // TODO(bazel-team): Move this into a new BazelLibraryTest.java file, or at least out of
+ // MethodLibraryTest.java.
@Test
public void testSelectFunction() throws Exception {
enableSkylarkMode();
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 b9fd9ac..3f06c54 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
@@ -14,7 +14,6 @@
package com.google.devtools.build.lib.syntax;
import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.assertEquals;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@@ -52,19 +51,87 @@
@Test
public void testNsetOrder() throws Exception {
eval("n = depset(['a', 'b'], order='compile')");
- assertEquals(Order.COMPILE_ORDER, get("n").getSet(String.class).getOrder());
+ assertThat(get("n").getSet(String.class).getOrder()).isEqualTo(Order.COMPILE_ORDER);
}
@Test
public void testEmptyNsetGenericType() throws Exception {
eval("n = depset()");
- assertEquals(SkylarkType.TOP, get("n").getContentType());
+ assertThat(get("n").getContentType()).isEqualTo(SkylarkType.TOP);
+ }
+
+ @Test
+ public void testDepsetBadOrder() throws Exception {
+ new BothModesTest().testIfExactError("Invalid order: bad", "depset(['a'], order='bad')");
+ }
+
+ @Test
+ public void testSetUnionWithList() throws Exception {
+ assertContainsInOrder("depset([]).union(['a', 'b', 'c'])", "a", "b", "c");
+ assertContainsInOrder("depset(['a']).union(['b', 'c'])", "a", "b", "c");
+ assertContainsInOrder("depset(['a', 'b']).union(['c'])", "a", "b", "c");
+ assertContainsInOrder("depset(['a', 'b', 'c']).union([])", "a", "b", "c");
+ }
+
+ @Test
+ public void testSetUnionWithSet() throws Exception {
+ assertContainsInOrder("depset([]).union(depset(['a', 'b', 'c']))", "a", "b", "c");
+ assertContainsInOrder("depset(['a']).union(depset(['b', 'c']))", "a", "b", "c");
+ assertContainsInOrder("depset(['a', 'b']).union(depset(['c']))", "a", "b", "c");
+ assertContainsInOrder("depset(['a', 'b', 'c']).union(depset([]))", "a", "b", "c");
+ }
+
+ @Test
+ public void testDepsetUnionWithBadType() throws Exception {
+ new BothModesTest()
+ .testIfErrorContains("is not applicable for arguments (int)", "depset([]).union(5)");
+ }
+
+ @Test
+ public void testSetUnionDuplicates() throws Exception {
+ assertContainsInOrder("depset(['a', 'b', 'c']).union(['a', 'b', 'c'])", "a", "b", "c");
+ assertContainsInOrder("depset(['a', 'a', 'a']).union(['a', 'a'])", "a");
+
+ assertContainsInOrder("depset(['a', 'b', 'c']).union(depset(['a', 'b', 'c']))", "a", "b", "c");
+ assertContainsInOrder("depset(['a', 'a', 'a']).union(depset(['a', 'a']))", "a");
+ }
+
+ @Test
+ public void testSetUnionError() throws Exception {
+ new BothModesTest()
+ .testIfErrorContains("insufficient arguments received by union", "depset(['a']).union()")
+ .testIfErrorContains(
+ "method depset.union(new_elements: Iterable) is not applicable for arguments (string): "
+ + "'new_elements' is 'string', but should be 'Iterable'",
+ "depset(['a']).union('b')");
+ }
+
+ @Test
+ public void testSetUnionSideEffects() throws Exception {
+ eval(
+ "def func():",
+ " n1 = depset(['a'])",
+ " n2 = n1.union(['b'])",
+ " return n1",
+ "n = func()");
+ assertThat(((SkylarkNestedSet) lookup("n")).toCollection()).isEqualTo(ImmutableList.of("a"));
+ }
+
+ private void assertContainsInOrder(String statement, Object... expectedElements)
+ throws Exception {
+ new BothModesTest().testCollection(statement, expectedElements);
}
@Test
public void testFunctionReturnsNset() throws Exception {
- eval("def func():", " n = depset()", " n += ['a']", " return n", "s = func()");
- assertEquals(ImmutableList.of("a"), get("s").toCollection());
+ eval(
+ "def func():",
+ " n = depset()",
+ " n += ['a']",
+ " return n",
+ "s = func()");
+ assertThat(get("s")).isInstanceOf(SkylarkNestedSet.class);
+ assertThat(get("s").toCollection()).containsExactly("a");
}
@Test
@@ -77,11 +144,11 @@
" n2 += ['b']",
" return n1",
"n = func()");
- assertEquals(ImmutableList.of("a"), get("n").toCollection());
+ assertThat(get("n").toCollection()).containsExactly("a");
}
@Test
- public void testNsetNestedItem() throws Exception {
+ public void testNsetUnionOrder() throws Exception {
eval(
"def func():",
" n1 = depset()",
@@ -91,7 +158,7 @@
" n1 += n2",
" return n1",
"n = func()");
- assertEquals(ImmutableList.of("b", "a"), get("n").toCollection());
+ assertThat(get("n").toCollection()).containsExactly("b", "a").inOrder();
}
@Test
@@ -103,8 +170,13 @@
@Test
public void testNsetItemList() throws Exception {
- eval("def func():", " n = depset()", " n += ['a', 'b']", " return n", "n = func()");
- assertEquals(ImmutableList.of("a", "b"), get("n").toCollection());
+ eval(
+ "def func():",
+ " n = depset()",
+ " n += ['a', 'b']",
+ " return n",
+ "n = func()");
+ assertThat(get("n").toCollection()).containsExactly("a", "b").inOrder();
}
@Test
@@ -118,7 +190,7 @@
" func1(n)",
" return n",
"n = func2()");
- assertEquals(ImmutableList.of("a"), get("n").toCollection());
+ assertThat(get("n").toCollection()).containsExactly("a");
}
@Test
@@ -131,7 +203,7 @@
" return depset() + nb + nc",
"n = func()");
// The iterator lists the Transitive sets first
- assertEquals(ImmutableList.of("b", "a", "c"), get("n").toCollection());
+ assertThat(get("n").toCollection()).containsExactly("b", "a", "c").inOrder();
}
@Test
@@ -145,7 +217,7 @@
" return na",
"n = func()");
// The iterator lists the Transitive sets first
- assertEquals(ImmutableList.of(4, 2, 3, 5), get("n").toCollection());
+ assertThat(get("n").toCollection()).containsExactly(4, 2, 3, 5).inOrder();
}
@Test
@@ -160,14 +232,18 @@
@Test
public void testNsetToString() throws Exception {
- eval("s = depset() + [2, 4, 6] + [3, 4, 5]", "x = str(s)");
- assertEquals("set([2, 4, 6, 3, 5])", lookup("x"));
+ eval(
+ "s = depset() + [2, 4, 6] + [3, 4, 5]",
+ "x = str(s)");
+ assertThat(lookup("x")).isEqualTo("set([2, 4, 6, 3, 5])");
}
@Test
public void testNsetToStringWithOrder() throws Exception {
- eval("s = depset(order = 'link') + [2, 4, 6] + [3, 4, 5]", "x = str(s)");
- assertEquals("set([2, 4, 6, 3, 5], order = \"link\")", lookup("x"));
+ eval(
+ "s = depset(order = 'link') + [2, 4, 6] + [3, 4, 5]",
+ "x = str(s)");
+ assertThat(lookup("x")).isEqualTo("set([2, 4, 6, 3, 5], order = \"link\")");
}
@SuppressWarnings("unchecked")