Skip to content

Commit

Permalink
Extend tests of range()'s list-like behavior
Browse files Browse the repository at this point in the history
This expands on 33f08e7 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
  • Loading branch information
brandjon authored and Copybara-Service committed May 25, 2018
1 parent fd260c5 commit 585418d
Showing 1 changed file with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,65 @@ public void testRange() throws Exception {

@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
Expand Down

0 comments on commit 585418d

Please sign in to comment.