From 233a46e905f9d1fab490fd4ffebf0959d9e94bec Mon Sep 17 00:00:00 2001 From: Florian Weikert Date: Wed, 16 Dec 2015 12:38:38 +0000 Subject: Skylark: implemented all() and any() -- MOS_MIGRATED_REVID=110348607 --- .../devtools/build/lib/syntax/EvalUtilsTest.java | 10 +++ .../build/lib/syntax/MethodLibraryTest.java | 87 +++++++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) (limited to 'src/test/java/com/google/devtools/build/lib/syntax') diff --git a/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java b/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java index 6a6bf70a7b..f5cb497f67 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/EvalUtilsTest.java @@ -48,6 +48,16 @@ public class EvalUtilsTest { return new LinkedHashMap<>(); } + @Test + public void testEmptyStringToIterable() throws Exception { + assertThat(EvalUtils.toIterable("", null)).isEmpty(); + } + + @Test + public void testStringToIterable() throws Exception { + assertThat(EvalUtils.toIterable("abc", null)).hasSize(3); + } + @Test public void testDataTypeNames() throws Exception { assertEquals("string", EvalUtils.getDataTypeName("foo")); 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 de20fc2f7e..f649ce2ab1 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 @@ -265,6 +265,91 @@ public class MethodLibraryTest extends EvaluationTestCase { .testStatement("'\\tA\\n'.istitle()", true); } + @Test + public void testAllWithEmptyValue() throws Exception { + new SkylarkTest() + .testStatement("all('')", true) + .testStatement("all([])", true) + .testIfExactError("type 'NoneType' is not iterable", "any(None)"); + } + + @Test + public void testAllWithPrimitiveType() throws Exception { + new SkylarkTest() + .testStatement("all('test')", true) + .testIfErrorContains("", "all(1)"); + } + + @Test + public void testAllWithList() throws Exception { + new SkylarkTest() + .testStatement("all([False])", false) + .testStatement("all([True, False])", false) + .testStatement("all([False, False])", false) + .testStatement("all([False, True])", false) + .testStatement("all(['', True])", false) + .testStatement("all([0, True])", false) + .testStatement("all([[], True])", false) + .testStatement("all([True, 't', 1])", true); + } + + @Test + public void testAllWithSet() throws Exception { + new SkylarkTest() + .testStatement("all(set([0]))", false) + .testStatement("all(set([1, 0]))", false) + .testStatement("all(set([1]))", true); + } + + @Test + public void testAllWithDict() throws Exception { + new SkylarkTest() + .testStatement("all({1 : None})", true) + .testStatement("all({None : 1})", false); + } + + @Test + public void testAnyWithEmptyValue() throws Exception { + new SkylarkTest() + .testStatement("any('')", false) + .testStatement("any([])", false) + .testIfExactError("type 'NoneType' is not iterable", "any(None)"); + } + + @Test + public void testAnyWithPrimitiveType() throws Exception { + new SkylarkTest() + .testStatement("any('test')", true) + .testIfErrorContains("", "any(1)"); + } + + @Test + public void testAnyWithList() throws Exception { + new SkylarkTest() + .testStatement("any([False])", false) + .testStatement("any([0])", false) + .testStatement("any([''])", false) + .testStatement("any([[]])", false) + .testStatement("any([True, False])", true) + .testStatement("any([False, False])", false) + .testStatement("any([False, '', 0])", false) + .testStatement("any([False, '', 42])", true); + } + + @Test + public void testAnyWithSet() throws Exception { + new SkylarkTest() + .testStatement("any(set([0]))", false) + .testStatement("any(set([1, 0]))", true); + } + + @Test + public void testAnyWithDict() throws Exception { + new SkylarkTest() + .testStatement("any({1 : None, '' : None})", true) + .testStatement("any({None : 1, '' : 2})", false); + } + @Test public void testStackTraceLocation() throws Exception { new SkylarkTest().testIfErrorContains( @@ -867,7 +952,7 @@ public class MethodLibraryTest extends EvaluationTestCase { @Test public void testReversedWithStrings() throws Exception { new BothModesTest() - .testEval("reversed('')", "['']") + .testEval("reversed('')", "[]") .testEval("reversed('a')", "['a']") .testEval("reversed('abc')", "['c', 'b', 'a']") .testEval("reversed('__test ')", "[' ', ' ', 't', 's', 'e', 't', '_', '_']") -- cgit v1.2.3