aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com
diff options
context:
space:
mode:
authorGravatar brandjon <brandjon@google.com>2018-05-25 07:29:36 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-25 07:30:46 -0700
commit585418dca70e29dbe478a6095be2c1c97e24d596 (patch)
tree2a04fd3b76960b434429f729d408a80442121211 /src/test/java/com
parentfd260c56b46fbf68a07b29b0fc1b8cdecc4fe854 (diff)
Extend tests of range()'s list-like behavior
This expands on https://github.com/bazelbuild/bazel/commit/33f08e75fa4e7480f8d46b8305ce03a171adefa0 to ensure list-like behavior of range for the following operations - str/repr/type - equality and ordering - append / augmented assignment - concatenation - all of the above with slices This is a prerequisite to both optimizing range() while preserving its current API, as well as changing its API via an incompatible change flag. RELNOTES: None PiperOrigin-RevId: 198046941
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java59
1 files changed, 52 insertions, 7 deletions
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 20db34609c..7420cde493 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
@@ -491,20 +491,65 @@ public class MethodLibraryTest extends EvaluationTestCase {
@Test
public void testRangeIsList() throws Exception {
- // range() may change in the future to a read-only view, but for now it's a list and can be
- // updated. This test ensures we won't break backward compatibility until we intend to.
+ // range(), and slices of ranges, may change in the future to return read-only views. But for
+ // now it's just a list and can therefore be ordered, mutated, and concatenated. This test
+ // ensures we don't break backward compatibility until we intend to, even if range() is
+ // optimized to return lazy list-like values.
+ runRangeIsListAssertions("range(3)");
+ runRangeIsListAssertions("range(4)[:3]");
+ }
+
+ /**
+ * Helper function for testRangeIsList that expects a range or range slice expression producing
+ * the range value containing [0, 1, 2].
+ */
+ private void runRangeIsListAssertions(String range3expr) throws Exception {
+ // Check stringifications.
new BothModesTest()
- .testStatement("a = range(2); a.append(2); str(a)", "[0, 1, 2]")
- .testStatement("a = range(2); type(a)", "list");
+ .setUp("a = " + range3expr)
+ .testStatement("str(a)", "[0, 1, 2]")
+ .testStatement("repr(a)", "[0, 1, 2]")
+ .testStatement("type(a)", "list");
+
+ // Check comparisons.
+ new BothModesTest()
+ .setUp("a = " + range3expr)
+ .setUp("b = range(0, 3, 1)")
+ .setUp("c = range(1, 4)")
+ .setUp("L = [0, 1, 2]")
+ .setUp("T = (0, 1, 2)")
+ .testStatement("a == b", true)
+ .testStatement("a == c", false)
+ .testStatement("a < b", false)
+ .testStatement("a < c", true)
+ .testStatement("a == L", true)
+ .testStatement("a == T", false)
+ .testStatement("a < L", false)
+ .testIfErrorContains("Cannot compare list with tuple", "a < T");
+
+ // Check mutations.
+ new BothModesTest()
+ .setUp("a = " + range3expr)
+ .testStatement("a.append(3); str(a)", "[0, 1, 2, 3]");
new SkylarkTest()
.testStatement(
"def f():\n"
- + " a = range(2)\n"
+ + " a = " + range3expr + "\n"
+ " b = a\n"
- + " a += [2]\n"
+ + " a += [3]\n"
+ " return str(b)\n"
+ "f()\n",
- "[0, 1, 2]");
+ "[0, 1, 2, 3]");
+
+ // Check concatenations.
+ new BothModesTest()
+ .setUp("a = " + range3expr)
+ .setUp("b = range(3, 4)")
+ .setUp("L = [3]")
+ .setUp("T = (3,)")
+ .testStatement("str(a + b)", "[0, 1, 2, 3]")
+ .testStatement("str(a + L)", "[0, 1, 2, 3]")
+ .testIfErrorContains("unsupported operand type(s) for +: 'list' and 'tuple", "str(a + T)");
}
@Test