diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java b/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java index adaf336eed8..7aa816ae26a 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java @@ -322,7 +322,7 @@ public class StarlarkSemanticsOptions extends OptionsBase implements Serializabl @Option( name = "incompatible_disallow_legacy_java_provider", - defaultValue = "true", + defaultValue = "false", documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS, effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS}, metadataTags = { diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java b/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java index 96b54a4fef0..87c65056f93 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java @@ -260,7 +260,7 @@ public static Builder builderWithDefaults() { .incompatibleDisableDepsetItems(false) .incompatibleDisallowDictPlus(true) .incompatibleDisallowEmptyGlob(false) - .incompatibleDisallowLegacyJavaProvider(true) + .incompatibleDisallowLegacyJavaProvider(false) .incompatibleDisallowOldStyleArgsAdd(true) .incompatibleDisallowRuleExecutionPlatformConstraintsAllowed(true) .incompatibleDisallowStructProviderSyntax(false) diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java index 002297c032c..6743eb96ef2 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java @@ -45,21 +45,19 @@ import com.google.devtools.build.lib.packages.SkylarkProviderIdentifier; import com.google.devtools.build.lib.packages.StructImpl; import com.google.devtools.build.lib.packages.StructProvider; -import com.google.devtools.build.lib.packages.Type; import com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction; import com.google.devtools.build.lib.skylark.util.SkylarkTestCase; +import com.google.devtools.build.lib.syntax.BuildFileAST; import com.google.devtools.build.lib.syntax.ClassObject; +import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; -import com.google.devtools.build.lib.syntax.ParserInput; import com.google.devtools.build.lib.syntax.SkylarkDict; 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.SkylarkNestedSet; -import com.google.devtools.build.lib.syntax.StarlarkFile; import com.google.devtools.build.lib.syntax.StarlarkSemantics; -import com.google.devtools.build.lib.syntax.StarlarkThread; -import com.google.devtools.build.lib.syntax.SyntaxError; +import com.google.devtools.build.lib.syntax.Type; import com.google.devtools.build.lib.testutil.MoreAsserts; import com.google.devtools.build.lib.util.FileTypeSet; import java.util.Collection; @@ -704,13 +702,10 @@ public void testRuleAddAttribute() throws Exception { } protected void evalAndExport(String... lines) throws Exception { - ParserInput input = ParserInput.fromLines(lines); - StarlarkFile file = StarlarkFile.parseAndValidateSkylark(input, ev.getStarlarkThread()); - if (!file.errors().isEmpty()) { - throw new SyntaxError(file.errors()); - } + BuildFileAST buildFileAST = BuildFileAST.parseAndValidateSkylarkString( + ev.getEnvironment(), lines); SkylarkImportLookupFunction.execAndExport( - file, FAKE_LABEL, ev.getEventHandler(), ev.getStarlarkThread()); + buildFileAST, FAKE_LABEL, ev.getEventHandler(), ev.getEnvironment()); } @Test @@ -1258,6 +1253,14 @@ public void testStructsInDicts() throws Exception { "{struct(a = []): 'foo'}"); } + @Test + public void testStructMembersAreImmutable() throws Exception { + checkErrorContains( + "cannot assign to 's.x'", + "s = struct(x = 'a')", + "s.x = 'b'\n"); + } + @Test public void testStructDictMembersAreMutable() throws Exception { eval( @@ -1284,20 +1287,20 @@ private static StructImpl makeStruct(String field, Object value) { return StructProvider.STRUCT.create(ImmutableMap.of(field, value), "no field '%'"); } - private static StructImpl makeBigStruct(StarlarkThread thread) { + private static StructImpl makeBigStruct(Environment env) { // struct(a=[struct(x={1:1}), ()], b=(), c={2:2}) return StructProvider.STRUCT.create( ImmutableMap.of( "a", MutableList.of( - thread, + env, StructProvider.STRUCT.create( ImmutableMap.of( - "x", SkylarkDict.of(thread, 1, 1)), + "x", SkylarkDict.of(env, 1, 1)), "no field '%s'"), Tuple.of()), "b", Tuple.of(), - "c", SkylarkDict.of(thread, 2, 2)), + "c", SkylarkDict.of(env, 2, 2)), "no field '%s'"); } @@ -1306,8 +1309,8 @@ public void testStructMutabilityShallow() throws Exception { assertThat(EvalUtils.isImmutable(makeStruct("a", 1))).isTrue(); } - private static MutableList makeList(StarlarkThread thread) { - return MutableList.of(thread, 1, 2, 3); + private static MutableList makeList(Environment env) { + return MutableList.of(env, 1, 2, 3); } @Test @@ -1316,9 +1319,9 @@ public void testStructMutabilityDeep() throws Exception { assertThat(EvalUtils.isImmutable(makeStruct("a", makeList(null)))).isTrue(); assertThat(EvalUtils.isImmutable(makeBigStruct(null))).isTrue(); - assertThat(EvalUtils.isImmutable(Tuple.of(makeList(ev.getStarlarkThread())))).isFalse(); - assertThat(EvalUtils.isImmutable(makeStruct("a", makeList(ev.getStarlarkThread())))).isFalse(); - assertThat(EvalUtils.isImmutable(makeBigStruct(ev.getStarlarkThread()))).isFalse(); + assertThat(EvalUtils.isImmutable(Tuple.of(makeList(ev.getEnvironment())))).isFalse(); + assertThat(EvalUtils.isImmutable(makeStruct("a", makeList(ev.getEnvironment())))).isFalse(); + assertThat(EvalUtils.isImmutable(makeBigStruct(ev.getEnvironment()))).isFalse(); } @Test diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java index 41b0ff0f5f2..06c39051992 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java @@ -16,8 +16,6 @@ import static com.google.common.truth.Truth.assertThat; import com.google.common.collect.ImmutableMap; -import com.google.devtools.build.lib.events.EventCollector; -import com.google.devtools.build.lib.packages.BazelLibrary; import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter; import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; @@ -53,30 +51,6 @@ protected ModalTestCase newTest(String... skylarkOptions) { return new BuildTest(skylarkOptions); } - @Test - public void testExecutionStopsAtFirstError() throws Exception { - EventCollector printEvents = new EventCollector(); // for print events - StarlarkThread thread = - StarlarkThread.builder(mutability) - .useDefaultSemantics() - .setGlobals(BazelLibrary.GLOBALS) // for print... this should not be necessary - .setEventHandler(printEvents) - .build(); - ParserInput input = ParserInput.fromLines("print('hello'); x = 1//0; print('goodbye')"); - try { - StarlarkFile.eval(input, thread); - throw new AssertionError("execution succeeded unexpectedly"); - } catch (EvalException ex) { - // ok, division by zero - } - if (!printEvents.toString().contains("hello")) { - throw new AssertionError("first print statement not executed: " + printEvents); - } - if (printEvents.toString().contains("goodbye")) { - throw new AssertionError("first print statement unexpected executed: " + printEvents); - } - } - @Test public void testExprs() throws Exception { newTest() @@ -144,21 +118,17 @@ public void testSetComparison() throws Exception { @Test public void testSumFunction() throws Exception { - BaseFunction sum = - new BaseFunction("sum") { - @Override - public Object call( - List args, - Map kwargs, - FuncallExpression ast, - StarlarkThread thread) { - int sum = 0; - for (Object arg : args) { - sum += (Integer) arg; - } - return sum; - } - }; + BaseFunction sum = new BaseFunction("sum") { + @Override + public Object call(List args, Map kwargs, + FuncallExpression ast, Environment env) { + int sum = 0; + for (Object arg : args) { + sum += (Integer) arg; + } + return sum; + } + }; newTest().update(sum.getName(), sum).testStatement("sum(1, 2, 3, 4, 5, 6)", 21) .testStatement("sum", sum).testStatement("sum(a=1, b=2)", 0); @@ -181,17 +151,15 @@ public void testComplexFunctionCall() throws Exception { public void testKeywordArgs() throws Exception { // This function returns the map of keyword arguments passed to it. - BaseFunction kwargs = - new BaseFunction("kwargs") { - @Override - public Object call( - List args, - final Map kwargs, - FuncallExpression ast, - StarlarkThread thread) { - return SkylarkDict.copyOf(thread, kwargs); - } - }; + BaseFunction kwargs = new BaseFunction("kwargs") { + @Override + public Object call(List args, + final Map kwargs, + FuncallExpression ast, + Environment env) { + return SkylarkDict.copyOf(env, kwargs); + } + }; newTest() .update(kwargs.getName(), kwargs) @@ -277,7 +245,7 @@ public void testConcatLists() throws Exception { // list Object x = eval("[1,2] + [3,4]"); assertThat((Iterable) x).containsExactly(1, 2, 3, 4).inOrder(); - assertThat(x).isEqualTo(MutableList.of(thread, 1, 2, 3, 4)); + assertThat(x).isEqualTo(MutableList.of(env, 1, 2, 3, 4)); assertThat(EvalUtils.isImmutable(x)).isFalse(); // tuple @@ -464,26 +432,26 @@ public void testDictComprehensions_MultipleKey() throws Exception { @Test public void testListConcatenation() throws Exception { newTest() - .testStatement("[1, 2] + [3, 4]", MutableList.of(thread, 1, 2, 3, 4)) + .testStatement("[1, 2] + [3, 4]", MutableList.of(env, 1, 2, 3, 4)) .testStatement("(1, 2) + (3, 4)", Tuple.of(1, 2, 3, 4)) - .testIfExactError( - "unsupported operand type(s) for +: 'list' and 'tuple'", "[1, 2] + (3, 4)") - .testIfExactError( - "unsupported operand type(s) for +: 'tuple' and 'list'", "(1, 2) + [3, 4]"); + .testIfExactError("unsupported operand type(s) for +: 'list' and 'tuple'", + "[1, 2] + (3, 4)") + .testIfExactError("unsupported operand type(s) for +: 'tuple' and 'list'", + "(1, 2) + [3, 4]"); } @Test public void testListMultiply() throws Exception { newTest() - .testStatement("[1, 2, 3] * 1", MutableList.of(thread, 1, 2, 3)) - .testStatement("[1, 2] * 2", MutableList.of(thread, 1, 2, 1, 2)) - .testStatement("[1, 2] * 3", MutableList.of(thread, 1, 2, 1, 2, 1, 2)) - .testStatement("[1, 2] * 4", MutableList.of(thread, 1, 2, 1, 2, 1, 2, 1, 2)) - .testStatement("[8] * 5", MutableList.of(thread, 8, 8, 8, 8, 8)) + .testStatement("[1, 2, 3] * 1", MutableList.of(env, 1, 2, 3)) + .testStatement("[1, 2] * 2", MutableList.of(env, 1, 2, 1, 2)) + .testStatement("[1, 2] * 3", MutableList.of(env, 1, 2, 1, 2, 1, 2)) + .testStatement("[1, 2] * 4", MutableList.of(env, 1, 2, 1, 2, 1, 2, 1, 2)) + .testStatement("[8] * 5", MutableList.of(env, 8, 8, 8, 8, 8)) .testStatement("[ ] * 10", MutableList.empty()) .testStatement("[1, 2] * 0", MutableList.empty()) .testStatement("[1, 2] * -4", MutableList.empty()) - .testStatement("2 * [1, 2]", MutableList.of(thread, 1, 2, 1, 2)) + .testStatement("2 * [1, 2]", MutableList.of(env, 1, 2, 1, 2)) .testStatement("10 * []", MutableList.empty()) .testStatement("0 * [1, 2]", MutableList.empty()) .testStatement("-4 * [1, 2]", MutableList.empty()); @@ -558,7 +526,7 @@ public void testListComprehensionOnDictionary() throws Exception { public void testListComprehensionOnDictionaryCompositeExpression() throws Exception { new BuildTest() .setUp("d = {1:'a',2:'b'}", "l = [d[x] for x in d]") - .testLookup("l", MutableList.of(thread, "a", "b")); + .testLookup("l", MutableList.of(env, "a", "b")); } @Test diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java index 5ca992a80ea..d5d075c3d4d 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java @@ -19,7 +19,6 @@ import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; import org.junit.Before; @@ -153,16 +152,19 @@ public void testBuiltinFunctionErrorMessage() throws Exception { "expected value of type 'string or tuple of strings' for parameter 'sub', " + "for call to method startswith(sub, start = 0, end = None) of 'string'", "'test'.startswith(1)") - .testIfErrorContains("in dict, got string, want iterable", "dict('a')"); + .testIfErrorContains( + "expected value of type 'list(object)' for parameter args in dict(), " + + "but got \"a\" (string)", + "dict('a')"); } @Test public void testHasAttr() throws Exception { new SkylarkTest() - .testExpression("hasattr(depset(), 'union')", Boolean.TRUE) - .testExpression("hasattr('test', 'count')", Boolean.TRUE) - .testExpression("hasattr(dict(a = 1, b = 2), 'items')", Boolean.TRUE) - .testExpression("hasattr({}, 'items')", Boolean.TRUE); + .testStatement("hasattr(depset(), 'union')", Boolean.TRUE) + .testStatement("hasattr('test', 'count')", Boolean.TRUE) + .testStatement("hasattr(dict(a = 1, b = 2), 'items')", Boolean.TRUE) + .testStatement("hasattr({}, 'items')", Boolean.TRUE); } @Test @@ -171,8 +173,8 @@ public void testGetAttrMissingField() throws Exception { .testIfExactError( "object of type 'string' has no attribute 'not_there'", "getattr('a string', 'not_there')") - .testExpression("getattr('a string', 'not_there', 'use this')", "use this") - .testExpression("getattr('a string', 'not there', None)", Runtime.NONE); + .testStatement("getattr('a string', 'not_there', 'use this')", "use this") + .testStatement("getattr('a string', 'not there', None)", Runtime.NONE); } @SkylarkModule(name = "AStruct", documented = false, doc = "") @@ -212,13 +214,13 @@ public void testGetAttrWithMethods() throws Exception { String msg = "object of type 'string' has no attribute 'cnt'"; new SkylarkTest() .testIfExactError(msg, "getattr('a string', 'cnt')") - .testExpression("getattr('a string', 'cnt', 'default')", "default"); + .testStatement("getattr('a string', 'cnt', 'default')", "default"); } @Test public void testDir() throws Exception { new SkylarkTest() - .testExpression( + .testStatement( "str(dir({}))", "[\"clear\", \"get\", \"items\", \"keys\"," + " \"pop\", \"popitem\", \"setdefault\", \"update\", \"values\"]"); @@ -226,7 +228,7 @@ public void testDir() throws Exception { @Test public void testBoolean() throws Exception { - new BothModesTest().testExpression("False", Boolean.FALSE).testExpression("True", Boolean.TRUE); + new BothModesTest().testStatement("False", Boolean.FALSE).testStatement("True", Boolean.TRUE); } @Test @@ -276,13 +278,13 @@ public void testDictionaryKeyNotFound() throws Exception { public void testDictionaryAccess() throws Exception { new BothModesTest() .testEval("{1: ['foo']}[1]", "['foo']") - .testExpression("{'4': 8}['4']", 8) - .testExpression("{'a': 'aa', 'b': 'bb', 'c': 'cc'}['b']", "bb"); + .testStatement("{'4': 8}['4']", 8) + .testStatement("{'a': 'aa', 'b': 'bb', 'c': 'cc'}['b']", "bb"); } @Test public void testDictionaryVariableAccess() throws Exception { - new BothModesTest().setUp("d = {'a' : 1}", "a = d['a']").testLookup("a", 1); + new BothModesTest().setUp("d = {'a' : 1}", "a = d['a']\n").testLookup("a", 1); } @Test @@ -327,13 +329,12 @@ public void testDictionaryCreationKeyCollision() throws Exception { @Test public void testDictionaryCreationInvalidPositional() throws Exception { new BothModesTest() - .testIfErrorContains("in dict, got string, want iterable", "dict('a')") - .testIfErrorContains( - "in dict, dictionary update sequence element #0 is not iterable (string)", - "dict([('a')])") .testIfErrorContains( - "in dict, dictionary update sequence element #0 is not iterable (string)", - "dict([('a')])") + "expected value of type 'list(object)' for parameter args in dict(), " + + "but got \"a\" (string)", + "dict('a')") + .testIfErrorContains("cannot convert item #0 to a sequence", "dict(['a'])") + .testIfErrorContains("cannot convert item #0 to a sequence", "dict([('a')])") .testIfErrorContains( "expected no more than 1 positional arguments, but got 3", "dict((3,4), (3,2), (1,2))") .testIfErrorContains( @@ -364,11 +365,11 @@ public void testDictionaryKeys() throws Exception { @Test public void testDictionaryGet() throws Exception { new BuildTest() - .testExpression("{1: 'foo'}.get(1)", "foo") - .testExpression("{1: 'foo'}.get(2)", Runtime.NONE) - .testExpression("{1: 'foo'}.get(2, 'a')", "a") - .testExpression("{1: 'foo'}.get(2, default='a')", "a") - .testExpression("{1: 'foo'}.get(2, default=None)", Runtime.NONE); + .testStatement("{1: 'foo'}.get(1)", "foo") + .testStatement("{1: 'foo'}.get(2)", Runtime.NONE) + .testStatement("{1: 'foo'}.get(2, 'a')", "a") + .testStatement("{1: 'foo'}.get(2, default='a')", "a") + .testStatement("{1: 'foo'}.get(2, default=None)", Runtime.NONE); } @Test @@ -383,11 +384,12 @@ public void testDictionaryItems() throws Exception { @Test public void testDictionaryClear() throws Exception { new SkylarkTest() - .setUp( - "d = {1: 'foo', 2: 'bar', 3: 'baz'}", - "len(d) == 3 or fail('clear 1')", - "d.clear() == None or fail('clear 2')") - .testEval("d", "{}"); + .testEval( + "d = {1: 'foo', 2: 'bar', 3: 'baz'}\n" + + "len(d) == 3 or fail('clear 1')\n" + + "d.clear() == None or fail('clear 2')\n" + + "d", + "{}"); } @Test @@ -422,35 +424,36 @@ public void testDictionaryPopItem() throws Exception { @Test public void testDictionaryUpdate() throws Exception { new BothModesTest() - .setUp("foo = {'a': 2}", "foo.update({'b': 4})") - .testEval("foo", "{'a': 2, 'b': 4}"); + .setUp("foo = {'a': 2}") + .testEval("foo.update({'b': 4}); foo", "{'a': 2, 'b': 4}"); new BothModesTest() - .setUp("foo = {'a': 2}", "foo.update({'a': 3, 'b': 4})") - .testEval("foo", "{'a': 3, 'b': 4}"); + .setUp("foo = {'a': 2}") + .testEval("foo.update({'a': 3, 'b': 4}); foo", "{'a': 3, 'b': 4}"); } @Test public void testDictionarySetDefault() throws Exception { new SkylarkTest() - .setUp( - "d = {2: 'bar', 1: 'foo'}", - "len(d) == 2 or fail('setdefault 0')", - "d.setdefault(1, 'a') == 'foo' or fail('setdefault 1')", - "d.setdefault(2) == 'bar' or fail('setdefault 2')", - "d.setdefault(3) == None or fail('setdefault 3')", - "d.setdefault(4, 'b') == 'b' or fail('setdefault 4')") - .testEval("d", "{1: 'foo', 2: 'bar', 3: None, 4: 'b'}"); + .testEval( + "d = {2: 'bar', 1: 'foo'}\n" + + "len(d) == 2 or fail('setdefault 0')\n" + + "d.setdefault(1, 'a') == 'foo' or fail('setdefault 1')\n" + + "d.setdefault(2) == 'bar' or fail('setdefault 2')\n" + + "d.setdefault(3) == None or fail('setdefault 3')\n" + + "d.setdefault(4, 'b') == 'b' or fail('setdefault 4')\n" + + "d", + "{1: 'foo', 2: 'bar', 3: None, 4: 'b'}"); } @Test public void testListIndexMethod() throws Exception { new BothModesTest() - .testExpression("['a', 'b', 'c'].index('a')", 0) - .testExpression("['a', 'b', 'c'].index('b')", 1) - .testExpression("['a', 'b', 'c'].index('c')", 2) - .testExpression("[2, 4, 6].index(4)", 1) - .testExpression("[2, 4, 6].index(4)", 1) - .testExpression("[0, 1, [1]].index([1])", 2) + .testStatement("['a', 'b', 'c'].index('a')", 0) + .testStatement("['a', 'b', 'c'].index('b')", 1) + .testStatement("['a', 'b', 'c'].index('c')", 2) + .testStatement("[2, 4, 6].index(4)", 1) + .testStatement("[2, 4, 6].index(4)", 1) + .testStatement("[0, 1, [1]].index([1])", 2) .testIfErrorContains("item \"a\" not found in list", "[1, 2].index('a')") .testIfErrorContains("item 0 not found in list", "[].index(0)"); } @@ -459,8 +462,8 @@ public void testListIndexMethod() throws Exception { public void testHash() throws Exception { // We specify the same string hashing algorithm as String.hashCode(). new SkylarkTest() - .testExpression("hash('skylark')", "skylark".hashCode()) - .testExpression("hash('google')", "google".hashCode()) + .testStatement("hash('skylark')", "skylark".hashCode()) + .testStatement("hash('google')", "google".hashCode()) .testIfErrorContains( "expected value of type 'string' for parameter 'value', " + "for call to function hash(value)", @@ -471,61 +474,61 @@ public void testHash() throws Exception { public void testRangeType() throws Exception { new BothModesTest() .setUp("a = range(3)") - .testExpression("len(a)", 3) - .testExpression("str(a)", "range(0, 3)") - .testExpression("str(range(1,2,3))", "range(1, 2, 3)") - .testExpression("repr(a)", "range(0, 3)") - .testExpression("repr(range(1,2,3))", "range(1, 2, 3)") - .testExpression("type(a)", "range") + .testStatement("len(a)", 3) + .testStatement("str(a)", "range(0, 3)") + .testStatement("str(range(1,2,3))", "range(1, 2, 3)") + .testStatement("repr(a)", "range(0, 3)") + .testStatement("repr(range(1,2,3))", "range(1, 2, 3)") + .testStatement("type(a)", "range") .testIfErrorContains("unsupported operand type(s) for +: 'range' and 'range'", "a + a") .testIfErrorContains("type 'range' has no method append()", "a.append(3)") - .testExpression("str(list(range(5)))", "[0, 1, 2, 3, 4]") - .testExpression("str(list(range(0)))", "[]") - .testExpression("str(list(range(1)))", "[0]") - .testExpression("str(list(range(-2)))", "[]") - .testExpression("str(list(range(-3, 2)))", "[-3, -2, -1, 0, 1]") - .testExpression("str(list(range(3, 2)))", "[]") - .testExpression("str(list(range(3, 3)))", "[]") - .testExpression("str(list(range(3, 4)))", "[3]") - .testExpression("str(list(range(3, 5)))", "[3, 4]") - .testExpression("str(list(range(-3, 5, 2)))", "[-3, -1, 1, 3]") - .testExpression("str(list(range(-3, 6, 2)))", "[-3, -1, 1, 3, 5]") - .testExpression("str(list(range(5, 0, -1)))", "[5, 4, 3, 2, 1]") - .testExpression("str(list(range(5, 0, -10)))", "[5]") - .testExpression("str(list(range(0, -3, -2)))", "[0, -2]") - .testExpression("range(3)[-1]", 2) + .testStatement("str(list(range(5)))", "[0, 1, 2, 3, 4]") + .testStatement("str(list(range(0)))", "[]") + .testStatement("str(list(range(1)))", "[0]") + .testStatement("str(list(range(-2)))", "[]") + .testStatement("str(list(range(-3, 2)))", "[-3, -2, -1, 0, 1]") + .testStatement("str(list(range(3, 2)))", "[]") + .testStatement("str(list(range(3, 3)))", "[]") + .testStatement("str(list(range(3, 4)))", "[3]") + .testStatement("str(list(range(3, 5)))", "[3, 4]") + .testStatement("str(list(range(-3, 5, 2)))", "[-3, -1, 1, 3]") + .testStatement("str(list(range(-3, 6, 2)))", "[-3, -1, 1, 3, 5]") + .testStatement("str(list(range(5, 0, -1)))", "[5, 4, 3, 2, 1]") + .testStatement("str(list(range(5, 0, -10)))", "[5]") + .testStatement("str(list(range(0, -3, -2)))", "[0, -2]") + .testStatement("range(3)[-1]", 2) .testIfErrorContains( "index out of range (index is 3, but sequence has 3 elements)", "range(3)[3]") - .testExpression("str(range(5)[1:])", "range(1, 5)") - .testExpression("len(range(5)[1:])", 4) - .testExpression("str(range(5)[:2])", "range(0, 2)") - .testExpression("str(range(10)[1:9:2])", "range(1, 9, 2)") - .testExpression("str(list(range(10)[1:9:2]))", "[1, 3, 5, 7]") - .testExpression("str(range(10)[1:10:2])", "range(1, 10, 2)") - .testExpression("str(range(10)[1:11:2])", "range(1, 10, 2)") - .testExpression("str(range(0, 10, 2)[::2])", "range(0, 10, 4)") - .testExpression("str(range(0, 10, 2)[::-2])", "range(8, -2, -4)") - .testExpression("str(range(5)[1::-1])", "range(1, -1, -1)") + .testStatement("str(range(5)[1:])", "range(1, 5)") + .testStatement("len(range(5)[1:])", 4) + .testStatement("str(range(5)[:2])", "range(0, 2)") + .testStatement("str(range(10)[1:9:2])", "range(1, 9, 2)") + .testStatement("str(list(range(10)[1:9:2]))", "[1, 3, 5, 7]") + .testStatement("str(range(10)[1:10:2])", "range(1, 10, 2)") + .testStatement("str(range(10)[1:11:2])", "range(1, 10, 2)") + .testStatement("str(range(0, 10, 2)[::2])", "range(0, 10, 4)") + .testStatement("str(range(0, 10, 2)[::-2])", "range(8, -2, -4)") + .testStatement("str(range(5)[1::-1])", "range(1, -1, -1)") .testIfErrorContains("step cannot be 0", "range(2, 3, 0)") .testIfErrorContains("unsupported operand type(s) for *: 'range' and 'int'", "range(3) * 3") .testIfErrorContains("Cannot compare range objects", "range(3) < range(5)") .testIfErrorContains("Cannot compare range objects", "range(4) > [1]") - .testExpression("4 in range(1, 10)", true) - .testExpression("4 in range(1, 3)", false) - .testExpression("4 in range(0, 8, 2)", true) - .testExpression("4 in range(1, 8, 2)", false) - .testExpression("range(0, 5, 10) == range(0, 5, 11)", true) - .testExpression("range(0, 5, 2) == [0, 2, 4]", false); + .testStatement("4 in range(1, 10)", true) + .testStatement("4 in range(1, 3)", false) + .testStatement("4 in range(0, 8, 2)", true) + .testStatement("4 in range(1, 8, 2)", false) + .testStatement("range(0, 5, 10) == range(0, 5, 11)", true) + .testStatement("range(0, 5, 2) == [0, 2, 4]", false); } @Test public void testEnumerate() throws Exception { new BothModesTest() - .testExpression("str(enumerate([]))", "[]") - .testExpression("str(enumerate([5]))", "[(0, 5)]") - .testExpression("str(enumerate([5, 3]))", "[(0, 5), (1, 3)]") - .testExpression("str(enumerate(['a', 'b', 'c']))", "[(0, \"a\"), (1, \"b\"), (2, \"c\")]") - .testExpression("str(enumerate(['a']) + [(1, 'b')])", "[(0, \"a\"), (1, \"b\")]"); + .testStatement("str(enumerate([]))", "[]") + .testStatement("str(enumerate([5]))", "[(0, 5)]") + .testStatement("str(enumerate([5, 3]))", "[(0, 5), (1, 3)]") + .testStatement("str(enumerate(['a', 'b', 'c']))", "[(0, \"a\"), (1, \"b\"), (2, \"c\")]") + .testStatement("str(enumerate(['a']) + [(1, 'b')])", "[(0, \"a\"), (1, \"b\")]"); } @Test @@ -544,17 +547,17 @@ public void testReassignmentOfPrimitivesNotForbiddenByCoreLanguage() throws Exce @Test public void testLenOnString() throws Exception { - new BothModesTest().testExpression("len('abc')", 3); + new BothModesTest().testStatement("len('abc')", 3); } @Test public void testLenOnList() throws Exception { - new BothModesTest().testExpression("len([1,2,3])", 3); + new BothModesTest().testStatement("len([1,2,3])", 3); } @Test public void testLenOnDict() throws Exception { - new BothModesTest().testExpression("len({'a' : 1, 'b' : 2})", 2); + new BothModesTest().testStatement("len({'a' : 1, 'b' : 2})", 2); } @Test @@ -572,40 +575,40 @@ public void testIndexOnFunction() throws Exception { @Test public void testBool() throws Exception { new BothModesTest() - .testExpression("bool(1)", Boolean.TRUE) - .testExpression("bool(0)", Boolean.FALSE) - .testExpression("bool([1, 2])", Boolean.TRUE) - .testExpression("bool([])", Boolean.FALSE) - .testExpression("bool(None)", Boolean.FALSE); + .testStatement("bool(1)", Boolean.TRUE) + .testStatement("bool(0)", Boolean.FALSE) + .testStatement("bool([1, 2])", Boolean.TRUE) + .testStatement("bool([])", Boolean.FALSE) + .testStatement("bool(None)", Boolean.FALSE); } @Test public void testStr() throws Exception { new BothModesTest() - .testExpression("str(1)", "1") - .testExpression("str(-2)", "-2") - .testExpression("str([1, 2])", "[1, 2]") - .testExpression("str(True)", "True") - .testExpression("str(False)", "False") - .testExpression("str(None)", "None") - .testExpression("str(str)", ""); + .testStatement("str(1)", "1") + .testStatement("str(-2)", "-2") + .testStatement("str([1, 2])", "[1, 2]") + .testStatement("str(True)", "True") + .testStatement("str(False)", "False") + .testStatement("str(None)", "None") + .testStatement("str(str)", ""); } @Test public void testStrFunction() throws Exception { - new SkylarkTest().setUp("def foo(x): pass").testExpression("str(foo)", ""); + new SkylarkTest().testStatement("def foo(x): return x\nstr(foo)", ""); } @Test public void testType() throws Exception { new SkylarkTest() - .testExpression("type(1)", "int") - .testExpression("type('a')", "string") - .testExpression("type([1, 2])", "list") - .testExpression("type((1, 2))", "tuple") - .testExpression("type(True)", "bool") - .testExpression("type(None)", "NoneType") - .testExpression("type(str)", "function"); + .testStatement("type(1)", "int") + .testStatement("type('a')", "string") + .testStatement("type([1, 2])", "list") + .testStatement("type((1, 2))", "tuple") + .testStatement("type(True)", "bool") + .testStatement("type(None)", "NoneType") + .testStatement("type(str)", "function"); } // TODO(bazel-team): Move this into a new BazelLibraryTest.java file, or at least out of @@ -613,7 +616,7 @@ public void testType() throws Exception { @Test public void testSelectFunction() throws Exception { enableSkylarkMode(); - exec("a = select({'a': 1})"); + eval("a = select({'a': 1})"); SelectorList result = (SelectorList) lookup("a"); assertThat(((SelectorValue) Iterables.getOnlyElement(result.getElements())).getDictionary()) .containsExactly("a", 1); @@ -622,13 +625,13 @@ public void testSelectFunction() throws Exception { @Test public void testZipFunction() throws Exception { new BothModesTest() - .testExpression("str(zip())", "[]") - .testExpression("str(zip([1, 2]))", "[(1,), (2,)]") - .testExpression("str(zip([1, 2], ['a', 'b']))", "[(1, \"a\"), (2, \"b\")]") - .testExpression("str(zip([1, 2, 3], ['a', 'b']))", "[(1, \"a\"), (2, \"b\")]") - .testExpression("str(zip([1], [2], [3]))", "[(1, 2, 3)]") - .testExpression("str(zip([1], {2: 'a'}))", "[(1, 2)]") - .testExpression("str(zip([1], []))", "[]") + .testStatement("str(zip())", "[]") + .testStatement("str(zip([1, 2]))", "[(1,), (2,)]") + .testStatement("str(zip([1, 2], ['a', 'b']))", "[(1, \"a\"), (2, \"b\")]") + .testStatement("str(zip([1, 2, 3], ['a', 'b']))", "[(1, \"a\"), (2, \"b\")]") + .testStatement("str(zip([1], [2], [3]))", "[(1, 2, 3)]") + .testStatement("str(zip([1], {2: 'a'}))", "[(1, 2)]") + .testStatement("str(zip([1], []))", "[]") .testIfErrorContains("type 'int' is not iterable", "zip(123)") .testIfErrorContains("type 'int' is not iterable", "zip([1], 1)"); } @@ -643,16 +646,16 @@ private void checkStrip( if (chars == null) { new BothModesTest() .update("s", input) - .testExpression("s.lstrip()", expLeft) - .testExpression("s.rstrip()", expRight) - .testExpression("s.strip()", expBoth); + .testStatement("s.lstrip()", expLeft) + .testStatement("s.rstrip()", expRight) + .testStatement("s.strip()", expBoth); } else { new BothModesTest() .update("s", input) .update("chars", chars) - .testExpression("s.lstrip(chars)", expLeft) - .testExpression("s.rstrip(chars)", expRight) - .testExpression("s.strip(chars)", expBoth); + .testStatement("s.lstrip(chars)", expLeft) + .testStatement("s.rstrip(chars)", expRight) + .testStatement("s.strip(chars)", expBoth); } } @@ -692,9 +695,9 @@ public void testFail() throws Exception { @Test public void testTupleCoercion() throws Exception { new BothModesTest() - .testExpression("tuple([1, 2]) == (1, 2)", true) + .testStatement("tuple([1, 2]) == (1, 2)", true) // Depends on current implementation of dict - .testExpression("tuple({1: 'foo', 2: 'bar'}) == (1, 2)", true); + .testStatement("tuple({1: 'foo', 2: 'bar'}) == (1, 2)", true); } // Verifies some legacy functionality that should be deprecated and removed via @@ -704,8 +707,8 @@ public void testTupleCoercion() throws Exception { public void testLegacyNamed() throws Exception { new SkylarkTest("--incompatible_restrict_named_params=false") // Parameters which may be specified by keyword but are not explicitly 'named'. - .testExpression("all(elements=[True, True])", Boolean.TRUE) - .testExpression("any(elements=[True, False])", Boolean.TRUE) + .testStatement("all(elements=[True, True])", Boolean.TRUE) + .testStatement("any(elements=[True, False])", Boolean.TRUE) .testEval("sorted(self=[3, 0, 2], key=None, reverse=False)", "[0, 2, 3]") .testEval("reversed(sequence=[3, 2, 0])", "[0, 2, 3]") .testEval("tuple(x=[1, 2])", "(1, 2)") @@ -713,16 +716,16 @@ public void testLegacyNamed() throws Exception { .testEval("len(x=(1, 2))", "2") .testEval("str(x=(1, 2))", "'(1, 2)'") .testEval("repr(x=(1, 2))", "'(1, 2)'") - .testExpression("bool(x=3)", Boolean.TRUE) + .testStatement("bool(x=3)", Boolean.TRUE) .testEval("int(x=3)", "3") .testEval("dict(args=[(1, 2)])", "{1 : 2}") - .testExpression("bool(x=3)", Boolean.TRUE) + .testStatement("bool(x=3)", Boolean.TRUE) .testEval("enumerate(list=[40, 41])", "[(0, 40), (1, 41)]") - .testExpression("hash(value='hello')", "hello".hashCode()) + .testStatement("hash(value='hello')", "hello".hashCode()) .testEval("range(start_or_stop=3, stop_or_none=9, step=2)", "range(3, 9, 2)") - .testExpression("hasattr(x=depset(), name='union')", Boolean.TRUE) - .testExpression("bool(x=3)", Boolean.TRUE) - .testExpression("getattr(x='hello', name='cnt', default='default')", "default") + .testStatement("hasattr(x=depset(), name='union')", Boolean.TRUE) + .testStatement("bool(x=3)", Boolean.TRUE) + .testStatement("getattr(x='hello', name='cnt', default='default')", "default") .testEval( "dir(x={})", "[\"clear\", \"get\", \"items\", \"keys\"," @@ -731,7 +734,7 @@ public void testLegacyNamed() throws Exception { .testEval("str(depset(items=[0,1]))", "'depset([0, 1])'") .testIfErrorContains("hello", "fail(msg='hello', attr='someattr')") // Parameters which may be None but are not explicitly 'noneable' - .testExpression("hasattr(x=None, name='union')", Boolean.FALSE) + .testStatement("hasattr(x=None, name='union')", Boolean.FALSE) .testEval("getattr(x=None, name='count', default=None)", "None") .testEval("dir(None)", "[]") .testIfErrorContains("None", "fail(msg=None)") @@ -768,14 +771,6 @@ public void testDepsetItemsKeywordAndPositional() throws Exception { "depset([0, 1], 'default', items=[0,1])"); } - @Test - public void testDepsetDirectInvalidType() throws Exception { - new SkylarkTest() - .testIfErrorContains( - "expected type 'sequence' for direct but got type 'string' instead", - "depset(direct='hello')"); - } - @Test public void testDisableDepsetItems() throws Exception { new SkylarkTest("--incompatible_disable_depset_items") @@ -790,23 +785,4 @@ public void testDisableDepsetItems() throws Exception { + "--incompatible_disable_depset_inputs=false", "depset(items=[0,1])"); } - - @Test - public void testDepsetDepthLimit() throws Exception { - NestedSet.setApplicationDepthLimit(2000); - new SkylarkTest() - .setUp( - "def create_depset(depth):", - " x = depset([0])", - " for i in range(1, depth):", - " x = depset([i], transitive = [x])", - " return x", - "too_deep_depset = create_depset(3000)", - "fine_depset = create_depset(900)") - .testEval("fine_depset.to_list()[0]", "0") - .testEval("str(fine_depset)[0:6]", "'depset'") - .testIfErrorContains("depset exceeded maximum depth 2000", "print(too_deep_depset)") - .testIfErrorContains("depset exceeded maximum depth 2000", "str(too_deep_depset)") - .testIfErrorContains("depset exceeded maximum depth 2000", "too_deep_depset.to_list()"); - } } diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java index e577506ada5..69de4048527 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java @@ -428,8 +428,9 @@ public String withParamsAndExtraInterpreterParams( allowReturnNones = true) public ClassObject proxyMethodsObject() { ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); - for (String nativeFunction : CallUtils.getMethodNames(Mock.class)) { - builder.put(nativeFunction, CallUtils.getBuiltinCallable(this, nativeFunction)); + for (String nativeFunction : FuncallExpression.getMethodNames(Mock.class)) { + builder.put(nativeFunction, + FuncallExpression.getBuiltinCallable(this, nativeFunction)); } return StructProvider.STRUCT.create(builder.build(), "no native callable '%s'"); } @@ -1586,7 +1587,7 @@ public void testAugmentedAssignmentHasNoSideEffects() throws Exception { } @Test - public void testInvalidAugmentedAssignment_ListExpression() throws Exception { + public void testInvalidAugmentedAssignment_ListLiteral() throws Exception { new SkylarkTest().testIfErrorContains( "cannot perform augmented assignment on a list or tuple expression", "def f(a, b):",