diff options
author | 2016-08-05 08:38:26 +0000 | |
---|---|---|
committer | 2016-08-05 13:32:33 +0000 | |
commit | cdb6ef554397c1d3da7ee9921305f646bec5ae16 (patch) | |
tree | 23cde8c143ecf6496570bf56ace1c6f405acbd35 /src/test/java/com/google/devtools/build/lib/syntax | |
parent | 6df3e71eb05493fa421121fa73e607a6fbf458c3 (diff) |
Make SkylarkClassObject "Bazel-specific".
This in preparation to DeclaredProviders implementation.
--
MOS_MIGRATED_REVID=129420617
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/syntax')
5 files changed, 9 insertions, 205 deletions
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 26763ea796..7ce40aa90c 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 @@ -19,8 +19,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import com.google.common.collect.ImmutableMap; -import com.google.devtools.build.lib.syntax.ClassObject.SkylarkClassObject; import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; @@ -44,21 +42,6 @@ public class EvalUtilsTest extends EvaluationTestCase { return SkylarkDict.<Object, Object>of(env, 1, 1, 2, 2); } - private static SkylarkClassObject makeStruct(String field, Object value) { - return new SkylarkClassObject(ImmutableMap.of(field, value)); - } - - private static SkylarkClassObject makeBigStruct(Environment env) { - // struct(a=[struct(x={1:1}), ()], b=(), c={2:2}) - return new SkylarkClassObject(ImmutableMap.<String, Object>of( - "a", MutableList.<Object>of(env, - new SkylarkClassObject(ImmutableMap.<String, Object>of( - "x", SkylarkDict.<Object, Object>of(env, 1, 1))), - Tuple.of()), - "b", Tuple.of(), - "c", SkylarkDict.<Object, Object>of(env, 2, 2))); - } - @Test public void testEmptyStringToIterable() throws Exception { assertThat(EvalUtils.toIterable("", null)).isEmpty(); @@ -88,7 +71,6 @@ public class EvalUtilsTest extends EvaluationTestCase { @Test public void testDatatypeMutabilityShallow() throws Exception { assertTrue(EvalUtils.isImmutable(Tuple.of(1, 2, 3))); - assertTrue(EvalUtils.isImmutable(makeStruct("a", 1))); // Mutability depends on the environment. assertTrue(EvalUtils.isImmutable(makeList(null))); @@ -100,12 +82,8 @@ public class EvalUtilsTest extends EvaluationTestCase { @Test public void testDatatypeMutabilityDeep() throws Exception { assertTrue(EvalUtils.isImmutable(Tuple.<Object>of(makeList(null)))); - assertTrue(EvalUtils.isImmutable(makeStruct("a", makeList(null)))); - assertTrue(EvalUtils.isImmutable(makeBigStruct(null))); assertFalse(EvalUtils.isImmutable(Tuple.<Object>of(makeList(env)))); - assertFalse(EvalUtils.isImmutable(makeStruct("a", makeList(env)))); - assertFalse(EvalUtils.isImmutable(makeBigStruct(env))); } @Test 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 1eaecb7e46..5f994dbd1c 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 @@ -22,7 +22,6 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -1661,9 +1660,6 @@ public class MethodLibraryTest extends EvaluationTestCase { .testStatement("str(False)", "False") .testStatement("str(None)", "None") .testStatement("str(str)", "<function str>"); - - new SkylarkTest() - .testStatement("str(struct(x = 2, y = 3, z = 4))", "struct(x = 2, y = 3, z = 4)"); } @Test diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java index 2b6ebfc884..235968f7f2 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java @@ -20,7 +20,6 @@ import static org.junit.Assert.assertTrue; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; import com.google.devtools.build.lib.analysis.FileConfiguredTarget; @@ -30,7 +29,6 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.skylarkinterface.Param; import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; -import com.google.devtools.build.lib.syntax.ClassObject.SkylarkClassObject; import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.testutil.TestMode; import org.junit.Before; @@ -712,22 +710,6 @@ public class SkylarkEvaluationTest extends EvaluationTest { } @Test - public void testConditionalStructConcatenation() throws Exception { - // TODO(fwe): cannot be handled by current testing suite - eval("def func():", - " x = struct(a = 1, b = 2)", - " if True:", - " x += struct(c = 1, d = 2)", - " return x", - "x = func()"); - SkylarkClassObject x = (SkylarkClassObject) lookup("x"); - assertEquals(1, x.getValue("a")); - assertEquals(2, x.getValue("b")); - assertEquals(1, x.getValue("c")); - assertEquals(2, x.getValue("d")); - } - - @Test public void testJavaFunctionReturnsMutableObject() throws Exception { new SkylarkTest() .update("mock", new Mock()) @@ -825,93 +807,6 @@ public class SkylarkEvaluationTest extends EvaluationTest { } @Test - public void testStructCreation() throws Exception { - // TODO(fwe): cannot be handled by current testing suite - eval("x = struct(a = 1, b = 2)"); - assertThat(lookup("x")).isInstanceOf(ClassObject.class); - } - - @Test - public void testStructFields() throws Exception { - // TODO(fwe): cannot be handled by current testing suite - eval("x = struct(a = 1, b = 2)"); - ClassObject x = (ClassObject) lookup("x"); - assertEquals(1, x.getValue("a")); - assertEquals(2, x.getValue("b")); - } - - @Test - public void testStructAccessingFieldsFromSkylark() throws Exception { - new SkylarkTest() - .setUp("x = struct(a = 1, b = 2)", "x1 = x.a", "x2 = x.b") - .testLookup("x1", 1) - .testLookup("x2", 2); - } - - @Test - public void testStructAccessingUnknownField() throws Exception { - new SkylarkTest() - .testIfErrorContains( - "'struct' object has no attribute 'c'\n" + "Available attributes: a, b", - "x = struct(a = 1, b = 2)", - "y = x.c"); - } - - @Test - public void testStructAccessingUnknownFieldWithArgs() throws Exception { - new SkylarkTest().testIfExactError( - "struct has no method 'c'", "x = struct(a = 1, b = 2)", "y = x.c()"); - } - - @Test - public void testStructAccessingNonFunctionFieldWithArgs() throws Exception { - new SkylarkTest().testIfExactError( - "struct field 'a' is not a function", "x = struct(a = 1, b = 2)", "x1 = x.a(1)"); - } - - @Test - public void testStructAccessingFunctionFieldWithArgs() throws Exception { - new SkylarkTest() - .setUp("def f(x): return x+5", "x = struct(a = f, b = 2)", "x1 = x.a(1)") - .testLookup("x1", 6); - } - - @Test - public void testStructPosArgs() throws Exception { - new SkylarkTest().testIfExactError( - "struct(**kwargs) does not accept positional arguments, but got 1", "x = struct(1, b = 2)"); - } - - @Test - public void testStructConcatenationFieldNames() throws Exception { - // TODO(fwe): cannot be handled by current testing suite - eval("x = struct(a = 1, b = 2)", - "y = struct(c = 1, d = 2)", - "z = x + y\n"); - SkylarkClassObject z = (SkylarkClassObject) lookup("z"); - assertEquals(ImmutableSet.of("a", "b", "c", "d"), z.getKeys()); - } - - @Test - public void testStructConcatenationFieldValues() throws Exception { - // TODO(fwe): cannot be handled by current testing suite - eval("x = struct(a = 1, b = 2)", - "y = struct(c = 1, d = 2)", - "z = x + y\n"); - SkylarkClassObject z = (SkylarkClassObject) lookup("z"); - assertEquals(1, z.getValue("a")); - assertEquals(2, z.getValue("b")); - assertEquals(1, z.getValue("c")); - assertEquals(2, z.getValue("d")); - } - - @Test - public void testStructConcatenationCommonFields() throws Exception { - new SkylarkTest().testIfExactError("Cannot concat structs with common field(s): a", - "x = struct(a = 1, b = 2)", "y = struct(c = 1, a = 2)", "z = x + y\n"); - } - - @Test public void testDotExpressionOnNonStructObject() throws Exception { new SkylarkTest().testIfExactError("Object of type 'string' has no field \"field\"", "x = 'a'.field"); @@ -1026,15 +921,6 @@ public class SkylarkEvaluationTest extends EvaluationTest { } @Test - public void testHasattr() throws Exception { - new SkylarkTest().setUp("s = struct(a=1)", - "x = hasattr(s, 'a')", - "y = hasattr(s, 'b')\n") - .testLookup("x", Boolean.TRUE) - .testLookup("y", Boolean.FALSE); - } - - @Test public void testHasattrMethods() throws Exception { new SkylarkTest() .update("mock", new Mock()) @@ -1049,23 +935,6 @@ public class SkylarkEvaluationTest extends EvaluationTest { } @Test - public void testGetattr() throws Exception { - new SkylarkTest() - .setUp("s = struct(a='val')", "x = getattr(s, 'a')", "y = getattr(s, 'b', 'def')", - "z = getattr(s, 'b', default = 'def')", "w = getattr(s, 'a', default='ignored')") - .testLookup("x", "val") - .testLookup("y", "def") - .testLookup("z", "def") - .testLookup("w", "val"); - } - - @Test - public void testGetattrNoAttr() throws Exception { - new SkylarkTest().testIfExactError("Object of type 'struct' has no attribute \"b\"", - "s = struct(a='val')", "getattr(s, 'b')"); - } - - @Test public void testListAnTupleConcatenationDoesNotWorkInSkylark() throws Exception { new SkylarkTest().testIfExactError("unsupported operand type(s) for +: 'list' and 'tuple'", "[1, 2] + (3, 4)"); 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 5c6535e8c9..a0d84206e8 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 @@ -18,19 +18,15 @@ import static org.junit.Assert.assertEquals; import com.google.common.collect.ImmutableList; import com.google.devtools.build.lib.collect.nestedset.Order; -import com.google.devtools.build.lib.syntax.ClassObject.SkylarkClassObject; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; /** * Tests for SkylarkNestedSet. @@ -156,22 +152,6 @@ public class SkylarkNestedSetTest extends EvaluationTestCase { } @Test - public void testNsetGoodCompositeItem() throws Exception { - eval("def func():", - " return set([struct(a='a')])", - "s = func()"); - Collection<Object> result = get("s").toCollection(); - assertThat(result).hasSize(1); - assertThat(result.iterator().next()).isInstanceOf(SkylarkClassObject.class); - } - - @Test - public void testNsetBadMutableItem() throws Exception { - checkEvalError("sets cannot contain mutable items", "set([([],)])"); - checkEvalError("sets cannot contain mutable items", "set([struct(a=[])])"); - } - - @Test public void testNsetToString() throws Exception { eval("s = set() + [2, 4, 6] + [3, 4, 5]", "x = str(s)"); diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java index c44e5c918b..b7bd6f6306 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java @@ -18,11 +18,11 @@ import static com.google.common.truth.Truth.assertThat; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.RuleConfiguredTarget; import com.google.devtools.build.lib.events.Event; +import com.google.devtools.build.lib.packages.SkylarkClassObject; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -112,22 +112,6 @@ public class ValidationTest extends EvaluationTestCase { } @Test - public void testStructMembersAreImmutable() { - checkError( - "can only assign to variables and tuples, not to 's.x'", - "s = struct(x = 'a')", - "s.x = 'b'\n"); - } - - @Test - public void testStructDictMembersAreImmutable() { - checkError( - "can only assign to variables and tuples, not to 's.x['b']'", - "s = struct(x = {'a' : 1})", - "s.x['b'] = 2\n"); - } - - @Test public void testTupleLiteralWorksForDifferentTypes() throws Exception { parse("('a', 1)"); } @@ -299,11 +283,11 @@ public class ValidationTest extends EvaluationTestCase { // TODO(bazel-team): fix that? assertThat(ClassObject.class.isAnnotationPresent(SkylarkModule.class)).isFalse(); - assertThat(ClassObject.SkylarkClassObject.class.isAnnotationPresent(SkylarkModule.class)) + assertThat(SkylarkClassObject.class.isAnnotationPresent(SkylarkModule.class)) .isTrue(); assertThat( - EvalUtils.getParentWithSkylarkModule(ClassObject.SkylarkClassObject.class) - == ClassObject.SkylarkClassObject.class) + EvalUtils.getParentWithSkylarkModule(SkylarkClassObject.class) + == SkylarkClassObject.class) .isTrue(); assertThat(EvalUtils.getParentWithSkylarkModule(ClassObject.class)).isNull(); } @@ -320,8 +304,6 @@ public class ValidationTest extends EvaluationTestCase { assertThat(SkylarkType.of(tupleClass)).isEqualTo(SkylarkType.TUPLE); assertThat(SkylarkType.TUPLE).isNotEqualTo(SkylarkType.LIST); - // Also for ClassObject - assertThat(SkylarkType.of(ClassObject.SkylarkClassObject.class)).isEqualTo(SkylarkType.STRUCT); try { SkylarkType.of(ClassObject.class); throw new Exception("foo"); @@ -335,7 +317,7 @@ public class ValidationTest extends EvaluationTestCase { // TODO(bazel-team): move to some other place to remove dependency of syntax tests on Artifact? assertThat(SkylarkType.of(Artifact.SpecialArtifact.class)) .isEqualTo(SkylarkType.of(Artifact.class)); - assertThat(SkylarkType.of(RuleConfiguredTarget.class)).isNotEqualTo(SkylarkType.STRUCT); + assertThat(SkylarkType.of(RuleConfiguredTarget.class)).isNotEqualTo(SkylarkType.of(SkylarkClassObject.class)); } @Test @@ -348,9 +330,8 @@ public class ValidationTest extends EvaluationTestCase { assertThat(SkylarkType.LIST.includes(combo1)).isTrue(); SkylarkType union1 = - SkylarkType.Union.of(SkylarkType.DICT, SkylarkType.LIST, SkylarkType.STRUCT); + SkylarkType.Union.of(SkylarkType.DICT, SkylarkType.LIST); assertThat(union1.includes(SkylarkType.DICT)).isTrue(); - assertThat(union1.includes(SkylarkType.STRUCT)).isTrue(); assertThat(union1.includes(combo1)).isTrue(); assertThat(union1.includes(SkylarkType.STRING)).isFalse(); |