aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar laurentlb <laurentlb@google.com>2017-10-16 17:02:42 +0200
committerGravatar Jakob Buchgraber <buchgr@google.com>2017-10-16 17:49:12 +0200
commit573a47ad80eb3553566087180d3f02149a2dc9f4 (patch)
tree5dd505f3548fbcf98caed1cf467a379ae2d815d8 /src
parent3d16add7bf4e5176f1d469c0c6addc5f1b8827ef (diff)
Move min/max tests to new test suite
RELNOTES: None. PiperOrigin-RevId: 172325367
Diffstat (limited to 'src')
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java108
-rw-r--r--src/test/skylark/skylark_test.py24
-rw-r--r--src/test/skylark/testdata/and_or_not.sky5
-rw-r--r--src/test/skylark/testdata/equality.sky5
-rw-r--r--src/test/skylark/testdata/int.sky8
-rw-r--r--src/test/skylark/testdata/min_max.sky47
6 files changed, 67 insertions, 130 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 0bc19519c6..e57d8c8f79 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
@@ -38,114 +38,6 @@ public class MethodLibraryTest extends EvaluationTestCase {
}
@Test
- public void testMinWithInvalidArgs() throws Exception {
- new SkylarkTest()
- .testIfExactError("type 'int' is not iterable", "min(1)")
- .testIfExactError("expected at least one item", "min([])");
- }
-
- @Test
- public void testMinWithString() throws Exception {
- new SkylarkTest()
- .testStatement("min('abcdefxyz')", "a")
- .testStatement("min('test', 'xyz')", "test");
- }
-
- @Test
- public void testMinWithList() throws Exception {
- new BothModesTest()
- .testEval("min([4, 5], [1])", "[1]")
- .testEval("min([1, 2], [3])", "[1, 2]")
- .testEval("min([1, 5], [1, 6], [2, 4], [0, 6])", "[0, 6]")
- .testStatement("min([-1])", -1)
- .testStatement("min([5, 2, 3])", 2);
- }
-
- @Test
- public void testMinWithDict() throws Exception {
- new BothModesTest().testStatement("min({1: 2, -1 : 3})", -1).testStatement("min({2: None})", 2);
- }
-
- @Test
- public void testMinWithSet() throws Exception {
- new BothModesTest()
- .testStatement("min(depset([-1]))", -1)
- .testStatement("min(depset([5, 2, 3]))", 2);
- }
-
- @Test
- public void testMinWithPositionalArguments() throws Exception {
- new BothModesTest().testStatement("min(-1, 2)", -1).testStatement("min(5, 2, 3)", 2);
- }
-
- @Test
- public void testMinWithSameValues() throws Exception {
- new BothModesTest()
- .testStatement("min(1, 1, 1, 1, 1, 1)", 1)
- .testStatement("min([1, 1, 1, 1, 1, 1])", 1);
- }
-
- @Test
- public void testMinWithDifferentTypes() throws Exception {
- new BothModesTest()
- .testIfExactError("Cannot compare int with string", "min(1, '2', True)")
- .testIfExactError("Cannot compare int with string", "min([1, '2', True])");
- }
-
- @Test
- public void testMaxWithInvalidArgs() throws Exception {
- new BothModesTest()
- .testIfExactError("type 'int' is not iterable", "max(1)")
- .testIfExactError("expected at least one item", "max([])");
- }
-
- @Test
- public void testMaxWithString() throws Exception {
- new BothModesTest()
- .testStatement("max('abcdefxyz')", "z")
- .testStatement("max('test', 'xyz')", "xyz");
- }
-
- @Test
- public void testMaxWithList() throws Exception {
- new BothModesTest()
- .testEval("max([1, 2], [5])", "[5]")
- .testStatement("max([-1])", -1)
- .testStatement("max([5, 2, 3])", 5);
- }
-
- @Test
- public void testMaxWithDict() throws Exception {
- new BothModesTest().testStatement("max({1: 2, -1 : 3})", 1).testStatement("max({2: None})", 2);
- }
-
- @Test
- public void testMaxWithSet() throws Exception {
- new BothModesTest()
- .testStatement("max(depset([-1]))", -1)
- .testStatement("max(depset([5, 2, 3]))", 5);
- }
-
- @Test
- public void testMaxWithPositionalArguments() throws Exception {
- new BothModesTest().testStatement("max(-1, 2)", 2).testStatement("max(5, 2, 3)", 5);
- }
-
- @Test
- public void testMaxWithSameValues() throws Exception {
- new BothModesTest()
- .testStatement("max(1, 1, 1, 1, 1, 1)", 1)
- .testStatement("max([1, 1, 1, 1, 1, 1])", 1);
- }
-
- @Test
- public void testMaxWithDifferentTypes() throws Exception {
- new BothModesTest()
- .testIfExactError("Cannot compare int with string", "max(1, '2', True)")
- .testIfExactError("Cannot compare int with string", "max([1, '2', True])");
- }
-
- @Test
public void testSplitLines_EmptyLine() throws Exception {
new BothModesTest().testEval("''.splitlines()", "[]").testEval("'\\n'.splitlines()", "['']");
}
diff --git a/src/test/skylark/skylark_test.py b/src/test/skylark/skylark_test.py
index 91a95e4fcc..b7bc2371cf 100644
--- a/src/test/skylark/skylark_test.py
+++ b/src/test/skylark/skylark_test.py
@@ -68,14 +68,30 @@ class SkylarkTest(unittest.TestCase):
if not re.search(exp, output):
raise Exception("Error `{}` not found, got: {}".format(exp, output))
- def testSuccess(self):
- tests = ["int.sky", "equality.sky", "and_or_not.sky"]
- for t in tests:
+ TESTS = [
+ "int.sky",
+ "equality.sky",
+ "and_or_not.sky",
+ "min_max.sky",
+ ]
+
+ PRELUDE = """
+def assert_eq(x, y):
+ if x != y:
+ fail("%r != %r" % (x, y))
+
+def assert_(cond, msg="assertion failed"):
+ if not cond:
+ fail(msg)
+"""
+
+ def testAll(self):
+ for t in self.TESTS:
print("===", t, "===")
f = os.path.join(testenv.SKYLARK_TESTDATA_PATH, t)
for chunk, expected in self.chunks(f):
with tempfile.NamedTemporaryFile(suffix=".sky", delete=False) as tmp:
- tmp.writelines(chunk)
+ tmp.writelines([self.PRELUDE] + chunk)
output = self.evaluate(tmp.name)
os.unlink(tmp.name)
self.check_output(output, expected)
diff --git a/src/test/skylark/testdata/and_or_not.sky b/src/test/skylark/testdata/and_or_not.sky
index 53cfb98f68..aa4ab78436 100644
--- a/src/test/skylark/testdata/and_or_not.sky
+++ b/src/test/skylark/testdata/and_or_not.sky
@@ -1,8 +1,3 @@
-def assert_eq(x, y):
- if x != y:
- fail("%r != %r" % (x, y))
-
-
assert_eq(8 or 9, 8)
assert_eq(0 or 9, 9)
assert_eq(8 and 9, 9)
diff --git a/src/test/skylark/testdata/equality.sky b/src/test/skylark/testdata/equality.sky
index 0f72f61a29..89a6009c83 100644
--- a/src/test/skylark/testdata/equality.sky
+++ b/src/test/skylark/testdata/equality.sky
@@ -1,8 +1,3 @@
-def assert_eq(x, y):
- if x != y:
- fail("%r != %r" % (x, y))
-
-
# == operator
assert_eq(1 == 1, True)
assert_eq(1 == 2, False)
diff --git a/src/test/skylark/testdata/int.sky b/src/test/skylark/testdata/int.sky
index 095e9810d8..49bf69e469 100644
--- a/src/test/skylark/testdata/int.sky
+++ b/src/test/skylark/testdata/int.sky
@@ -1,13 +1,5 @@
# Tests of Skylark 'int'
-def assert_eq(x, y):
- if x != y:
- fail("%r != %r" % (x, y))
-
-def assert_(cond, msg="assertion failed"):
- if not cond:
- fail(msg)
-
# basic arithmetic
assert_eq(0 - 1, -1)
assert_eq(1 + 1, 2)
diff --git a/src/test/skylark/testdata/min_max.sky b/src/test/skylark/testdata/min_max.sky
new file mode 100644
index 0000000000..0349d916d4
--- /dev/null
+++ b/src/test/skylark/testdata/min_max.sky
@@ -0,0 +1,47 @@
+# min / max
+
+assert_eq(min("abcdefxyz"), "a")
+assert_eq(min("test", "xyz"), "test")
+
+assert_eq(min([4, 5], [1]), [1])
+assert_eq(min([1, 2], [3]), [1, 2])
+assert_eq(min([1, 5], [1, 6], [2, 4], [0, 6]), [0, 6])
+assert_eq(min([-1]), -1)
+assert_eq(min([5, 2, 3]), 2)
+assert_eq(min({1: 2, -1 : 3}), -1)
+assert_eq(min({2: None}), 2)
+assert_eq(min(-1, 2), -1)
+assert_eq(min(5, 2, 3), 2)
+assert_eq(min(1, 1, 1, 1, 1, 1), 1)
+assert_eq(min([1, 1, 1, 1, 1, 1]), 1)
+
+assert_eq(max("abcdefxyz"), "z")
+assert_eq(max("test", "xyz"), "xyz")
+assert_eq(max("test", "xyz"), "xyz")
+assert_eq(max([1, 2], [5]), [5])
+assert_eq(max([-1]), -1)
+assert_eq(max([5, 2, 3]), 5)
+assert_eq(max({1: 2, -1 : 3}), 1)
+assert_eq(max({2: None}), 2)
+assert_eq(max(-1, 2), 2)
+assert_eq(max(5, 2, 3), 5)
+assert_eq(max(1, 1, 1, 1, 1, 1), 1)
+assert_eq(max([1, 1, 1, 1, 1, 1]), 1)
+
+---
+min(1) ### type 'int' is not iterable
+---
+min([]) ### expected at least one item
+---
+min(1, "2", True) ### Cannot compare int with string
+---
+min([1, "2", True]) ### Cannot compare int with string
+---
+max(1) ### type 'int' is not iterable
+---
+max([]) ### expected at least one item
+---
+max(1, '2', True) ### Cannot compare int with string
+---
+max([1, '2', True]) ### Cannot compare int with string
+---