Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Benjamin Peterson | bf1db78 | 2018-08-08 10:03:22 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 18 | import static java.util.stream.Collectors.joining; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 19 | |
| 20 | import com.google.common.collect.ImmutableCollection; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
cparsons | 645a35e | 2018-10-01 13:03:23 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.test.AnalysisFailure; |
| 25 | import com.google.devtools.build.lib.analysis.test.AnalysisFailureInfo; |
| 26 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
dslomov | 423dccc | 2017-04-11 12:18:09 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.events.Location; |
dslomov | f129657 | 2017-08-22 16:29:06 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.NativeInfo; |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.NativeProvider; |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.StructProvider; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.skylarkinterface.Param; |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 34 | import com.google.devtools.build.lib.skylarkinterface.ParamType; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.skylarkinterface.SkylarkGlobalLibrary; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.syntax.StarlarkSemantics.FlagIdentifier; |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.testutil.TestMode; |
cparsons | aaab11f | 2018-01-31 11:00:11 -0800 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Map; |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 42 | import org.junit.Before; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.junit.runners.JUnit4; |
| 46 | |
Googler | bb0d36a | 2019-09-26 13:19:28 -0700 | [diff] [blame] | 47 | /** Tests of Starlark evaluation. */ |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 48 | // This test uses 'extends' to make a copy of EvaluationTest whose |
| 49 | // mode is overridden to SKYLARK, changing various environmental parameters. |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 50 | @SkylarkGlobalLibrary // required for @SkylarkCallable-annotated methods |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 51 | @RunWith(JUnit4.class) |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 52 | public final class SkylarkEvaluationTest extends EvaluationTest { |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 53 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 54 | @Before |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 55 | public final void setup() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 56 | setMode(TestMode.SKYLARK); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Creates an instance of {@code SkylarkTest} in order to run the tests from the base class in a |
| 61 | * Skylark context |
| 62 | */ |
| 63 | @Override |
vladmos | 6ff634d | 2017-07-05 10:25:01 -0400 | [diff] [blame] | 64 | protected ModalTestCase newTest(String... skylarkOptions) { |
| 65 | return new SkylarkTest(skylarkOptions); |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 66 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 67 | |
dslomov | 423dccc | 2017-04-11 12:18:09 +0000 | [diff] [blame] | 68 | @Immutable |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 69 | static class Bad { |
| 70 | Bad () { |
| 71 | } |
| 72 | } |
| 73 | |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 74 | @SkylarkCallable(name = "foobar", documented = false) |
| 75 | public String foobar() { |
| 76 | return "foobar"; |
| 77 | } |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 78 | |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 79 | @SkylarkCallable(name = "interrupted_function", documented = false) |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 80 | public NoneType interruptedFunction() throws InterruptedException { |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 81 | throw new InterruptedException(); |
| 82 | } |
| 83 | |
| 84 | private static final NativeProvider<NativeInfoMock> CONSTRUCTOR = |
| 85 | new NativeProvider<NativeInfoMock>(NativeInfoMock.class, "native_info_mock") {}; |
Benjamin Peterson | bf1db78 | 2018-08-08 10:03:22 -0700 | [diff] [blame] | 86 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 87 | @SkylarkModule(name = "Mock", doc = "") |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 88 | class NativeInfoMock extends NativeInfo { |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 89 | |
| 90 | public NativeInfoMock() { |
| 91 | super(CONSTRUCTOR); |
| 92 | } |
| 93 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 94 | @SkylarkCallable(name = "callable_string", documented = false, structField = false) |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 95 | public String callableString() { |
| 96 | return "a"; |
| 97 | } |
| 98 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 99 | @SkylarkCallable(name = "struct_field_string", documented = false, structField = true) |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 100 | public String structFieldString() { |
| 101 | return "a"; |
| 102 | } |
| 103 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 104 | @SkylarkCallable(name = "struct_field_callable", documented = false, structField = true) |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 105 | public BuiltinCallable structFieldCallable() { |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 106 | return new BuiltinCallable(SkylarkEvaluationTest.this, "foobar"); |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @SkylarkCallable( |
| 110 | name = "struct_field_none", |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 111 | documented = false, |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 112 | structField = true, |
| 113 | allowReturnNones = true |
| 114 | ) |
| 115 | public String structFieldNone() { |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @SkylarkModule(name = "Mock", doc = "") |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 121 | class Mock implements StarlarkValue { |
cparsons | 6c19720 | 2018-10-31 15:49:23 -0700 | [diff] [blame] | 122 | @SkylarkCallable( |
| 123 | name = "MockFn", |
| 124 | selfCall = true, |
| 125 | documented = false, |
cparsons | 0e86686 | 2018-04-24 08:07:02 -0700 | [diff] [blame] | 126 | parameters = { |
cparsons | 6c19720 | 2018-10-31 15:49:23 -0700 | [diff] [blame] | 127 | @Param(name = "pos", positional = true, type = String.class), |
| 128 | }) |
| 129 | public String selfCall(String myName) { |
cparsons | 0e86686 | 2018-04-24 08:07:02 -0700 | [diff] [blame] | 130 | return "I'm a mock named " + myName; |
| 131 | } |
| 132 | |
cparsons | 6c19720 | 2018-10-31 15:49:23 -0700 | [diff] [blame] | 133 | @SkylarkCallable( |
| 134 | name = "value_of", |
| 135 | parameters = {@Param(name = "str", type = String.class)}, |
cparsons | e661f88 | 2018-06-28 10:12:05 -0700 | [diff] [blame] | 136 | documented = false) |
cparsons | 6c19720 | 2018-10-31 15:49:23 -0700 | [diff] [blame] | 137 | public Integer valueOf(String str) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 138 | return Integer.valueOf(str); |
| 139 | } |
cparsons | e661f88 | 2018-06-28 10:12:05 -0700 | [diff] [blame] | 140 | @SkylarkCallable(name = "is_empty", |
| 141 | parameters = { @Param(name = "str", type = String.class) }, |
| 142 | documented = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 143 | public Boolean isEmpty(String str) { |
| 144 | return str.isEmpty(); |
| 145 | } |
| 146 | public void value() {} |
cparsons | 88d1cae | 2018-06-22 15:12:00 -0700 | [diff] [blame] | 147 | @SkylarkCallable(name = "return_bad", documented = false) |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 148 | public Bad returnBad() { |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 149 | return new Bad(); // not a legal Starlark value |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 150 | } |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 151 | @SkylarkCallable(name = "struct_field", documented = false, structField = true) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 152 | public String structField() { |
| 153 | return "a"; |
| 154 | } |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 155 | |
| 156 | @SkylarkCallable( |
| 157 | name = "struct_field_with_extra", |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 158 | documented = false, |
| 159 | structField = true, |
laurentlb | 28c30a7 | 2019-06-13 07:52:42 -0700 | [diff] [blame] | 160 | useStarlarkSemantics = true) |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 161 | public String structFieldWithExtra(StarlarkSemantics sem) { |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 162 | return "struct_field_with_extra(" |
| 163 | + (sem != null) |
| 164 | + ")"; |
| 165 | } |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 166 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 167 | @SkylarkCallable(name = "struct_field_callable", documented = false, structField = true) |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 168 | public Object structFieldCallable() { |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 169 | return new BuiltinCallable(SkylarkEvaluationTest.this, "foobar"); |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 170 | } |
Benjamin Peterson | bf1db78 | 2018-08-08 10:03:22 -0700 | [diff] [blame] | 171 | |
| 172 | @SkylarkCallable(name = "interrupted_struct_field", documented = false, structField = true) |
| 173 | public BuiltinFunction structFieldInterruptedCallable() throws InterruptedException { |
| 174 | throw new InterruptedException(); |
| 175 | } |
| 176 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 177 | @SkylarkCallable(name = "function", documented = false, structField = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 178 | public String function() { |
| 179 | return "a"; |
| 180 | } |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 181 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 182 | @SuppressWarnings("unused") |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 183 | @SkylarkCallable( |
| 184 | name = "nullfunc_failing", |
cparsons | e661f88 | 2018-06-28 10:12:05 -0700 | [diff] [blame] | 185 | parameters = { |
| 186 | @Param(name = "p1", type = String.class), |
| 187 | @Param(name = "p2", type = Integer.class), |
| 188 | }, |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 189 | documented = false, |
| 190 | allowReturnNones = false) |
| 191 | public StarlarkValue nullfuncFailing(String p1, Integer p2) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 192 | return null; |
| 193 | } |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 194 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 195 | @SkylarkCallable(name = "nullfunc_working", documented = false, allowReturnNones = true) |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 196 | public StarlarkValue nullfuncWorking() { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 197 | return null; |
| 198 | } |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 199 | @SkylarkCallable(name = "voidfunc", documented = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 200 | public void voidfunc() {} |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 201 | @SkylarkCallable(name = "string_list", documented = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 202 | public ImmutableList<String> stringList() { |
| 203 | return ImmutableList.<String>of("a", "b"); |
| 204 | } |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 205 | @SkylarkCallable(name = "string", documented = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 206 | public String string() { |
| 207 | return "a"; |
| 208 | } |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 209 | @SkylarkCallable(name = "string_list_dict", documented = false) |
cparsons | aaab11f | 2018-01-31 11:00:11 -0800 | [diff] [blame] | 210 | public Map<String, List<String>> stringListDict() { |
| 211 | return ImmutableMap.of("a", ImmutableList.of("b", "c")); |
| 212 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 213 | |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 214 | @SkylarkCallable( |
| 215 | name = "legacy_method", |
| 216 | documented = false, |
| 217 | parameters = { |
| 218 | @Param(name = "pos", positional = true, type = Boolean.class), |
| 219 | @Param(name = "legacyNamed", type = Boolean.class, positional = true, named = false, |
| 220 | legacyNamed = true), |
| 221 | @Param(name = "named", type = Boolean.class, positional = false, named = true), |
| 222 | }) |
| 223 | public String legacyMethod(Boolean pos, Boolean legacyNamed, Boolean named) { |
| 224 | return "legacy_method(" |
| 225 | + pos |
| 226 | + ", " |
| 227 | + legacyNamed |
| 228 | + ", " |
| 229 | + named |
| 230 | + ")"; |
| 231 | } |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 232 | |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 233 | @SkylarkCallable( |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 234 | name = "with_params", |
| 235 | documented = false, |
| 236 | parameters = { |
| 237 | @Param(name = "pos1"), |
| 238 | @Param(name = "pos2", defaultValue = "False", type = Boolean.class), |
| 239 | @Param( |
| 240 | name = "posOrNamed", |
| 241 | defaultValue = "False", |
| 242 | type = Boolean.class, |
| 243 | positional = true, |
| 244 | named = true), |
| 245 | @Param(name = "named", type = Boolean.class, positional = false, named = true), |
| 246 | @Param( |
| 247 | name = "optionalNamed", |
| 248 | type = Boolean.class, |
| 249 | defaultValue = "False", |
| 250 | positional = false, |
| 251 | named = true), |
| 252 | @Param( |
| 253 | name = "nonNoneable", |
| 254 | type = Object.class, |
| 255 | defaultValue = "\"a\"", |
| 256 | positional = false, |
| 257 | named = true), |
| 258 | @Param( |
| 259 | name = "noneable", |
| 260 | type = Integer.class, |
| 261 | defaultValue = "None", |
| 262 | noneable = true, |
| 263 | positional = false, |
| 264 | named = true), |
| 265 | @Param( |
| 266 | name = "multi", |
| 267 | allowedTypes = { |
| 268 | @ParamType(type = String.class), |
| 269 | @ParamType(type = Integer.class), |
| 270 | @ParamType(type = Sequence.class, generic1 = Integer.class), |
| 271 | }, |
| 272 | defaultValue = "None", |
| 273 | noneable = true, |
| 274 | positional = false, |
| 275 | named = true) |
| 276 | }) |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 277 | public String withParams( |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 278 | Integer pos1, |
| 279 | boolean pos2, |
| 280 | boolean posOrNamed, |
| 281 | boolean named, |
| 282 | boolean optionalNamed, |
| 283 | Object nonNoneable, |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 284 | Object noneable, |
| 285 | Object multi) { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 286 | return "with_params(" |
| 287 | + pos1 |
| 288 | + ", " |
| 289 | + pos2 |
| 290 | + ", " |
| 291 | + posOrNamed |
| 292 | + ", " |
| 293 | + named |
| 294 | + ", " |
| 295 | + optionalNamed |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 296 | + ", " |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 297 | + nonNoneable |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 298 | + (noneable != Starlark.NONE ? ", " + noneable : "") |
| 299 | + (multi != Starlark.NONE ? ", " + multi : "") |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 300 | + ")"; |
| 301 | } |
| 302 | |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 303 | @SkylarkCallable( |
cparsons | b380dc9 | 2018-12-05 13:57:39 -0800 | [diff] [blame] | 304 | name = "with_extra", |
| 305 | documented = false, |
| 306 | useLocation = true, |
| 307 | useAst = true, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 308 | useStarlarkThread = true, |
Googler | 7fa0dd2 | 2019-09-24 20:07:53 -0700 | [diff] [blame] | 309 | useStarlarkSemantics = true) |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 310 | public String withExtraInterpreterParams( |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 311 | Location location, FuncallExpression func, StarlarkThread thread, StarlarkSemantics sem) { |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 312 | return "with_extra(" |
| 313 | + location.getStartLine() |
| 314 | + ", " |
| 315 | + func.getArguments().size() |
| 316 | + ", " |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 317 | + thread.isGlobal() |
cparsons | 73e1016 | 2018-03-22 10:02:02 -0700 | [diff] [blame] | 318 | + ", " |
| 319 | + (sem != null) |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 320 | + ")"; |
| 321 | } |
| 322 | |
| 323 | @SkylarkCallable( |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 324 | name = "with_params_and_extra", |
| 325 | documented = false, |
| 326 | parameters = { |
| 327 | @Param(name = "pos1"), |
| 328 | @Param(name = "pos2", defaultValue = "False", type = Boolean.class), |
| 329 | @Param( |
| 330 | name = "posOrNamed", |
| 331 | defaultValue = "False", |
| 332 | type = Boolean.class, |
| 333 | positional = true, |
| 334 | named = true), |
| 335 | @Param(name = "named", type = Boolean.class, positional = false, named = true), |
| 336 | @Param( |
| 337 | name = "optionalNamed", |
| 338 | type = Boolean.class, |
| 339 | defaultValue = "False", |
| 340 | positional = false, |
| 341 | named = true), |
| 342 | @Param( |
| 343 | name = "nonNoneable", |
| 344 | type = Object.class, |
| 345 | defaultValue = "\"a\"", |
| 346 | positional = false, |
| 347 | named = true), |
| 348 | @Param( |
| 349 | name = "noneable", |
| 350 | type = Integer.class, |
| 351 | defaultValue = "None", |
| 352 | noneable = true, |
| 353 | positional = false, |
| 354 | named = true), |
| 355 | @Param( |
| 356 | name = "multi", |
| 357 | allowedTypes = { |
| 358 | @ParamType(type = String.class), |
| 359 | @ParamType(type = Integer.class), |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 360 | @ParamType(type = Sequence.class, generic1 = Integer.class), |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 361 | }, |
| 362 | defaultValue = "None", |
| 363 | noneable = true, |
| 364 | positional = false, |
| 365 | named = true) |
| 366 | }, |
| 367 | useAst = true, |
| 368 | useLocation = true, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 369 | useStarlarkThread = true, |
laurentlb | 28c30a7 | 2019-06-13 07:52:42 -0700 | [diff] [blame] | 370 | useStarlarkSemantics = true) |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 371 | public String withParamsAndExtraInterpreterParams( |
| 372 | Integer pos1, |
| 373 | boolean pos2, |
| 374 | boolean posOrNamed, |
| 375 | boolean named, |
| 376 | boolean optionalNamed, |
| 377 | Object nonNoneable, |
| 378 | Object noneable, |
| 379 | Object multi, |
| 380 | Location location, |
| 381 | FuncallExpression func, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 382 | StarlarkThread thread, |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 383 | StarlarkSemantics sem) { |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 384 | return "with_params_and_extra(" |
| 385 | + pos1 |
| 386 | + ", " |
| 387 | + pos2 |
| 388 | + ", " |
| 389 | + posOrNamed |
| 390 | + ", " |
| 391 | + named |
| 392 | + ", " |
| 393 | + optionalNamed |
| 394 | + ", " |
| 395 | + nonNoneable |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 396 | + (noneable != Starlark.NONE ? ", " + noneable : "") |
| 397 | + (multi != Starlark.NONE ? ", " + multi : "") |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 398 | + ", " |
| 399 | + location.getStartLine() |
| 400 | + ", " |
| 401 | + func.getArguments().size() |
| 402 | + ", " |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 403 | + thread.isGlobal() |
cparsons | 73e1016 | 2018-03-22 10:02:02 -0700 | [diff] [blame] | 404 | + ", " |
| 405 | + (sem != null) |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 406 | + ")"; |
| 407 | } |
| 408 | |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 409 | @SkylarkCallable(name = "proxy_methods_object", |
| 410 | doc = "Returns a struct containing all callable method objects of this mock", |
| 411 | allowReturnNones = true) |
| 412 | public ClassObject proxyMethodsObject() { |
| 413 | ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); |
Googler | 2e63b2a | 2019-11-11 08:59:12 -0800 | [diff] [blame] | 414 | Starlark.addMethods(builder, this); |
cparsons | 0c5c1c6 | 2018-05-24 10:37:03 -0700 | [diff] [blame] | 415 | return StructProvider.STRUCT.create(builder.build(), "no native callable '%s'"); |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 416 | } |
| 417 | |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 418 | @SkylarkCallable( |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 419 | name = "with_args_and_thread", |
| 420 | documented = false, |
| 421 | parameters = { |
| 422 | @Param(name = "pos1", type = Integer.class), |
| 423 | @Param(name = "pos2", defaultValue = "False", type = Boolean.class), |
| 424 | @Param(name = "named", type = Boolean.class, positional = false, named = true), |
| 425 | }, |
| 426 | extraPositionals = @Param(name = "args"), |
| 427 | useStarlarkThread = true) |
| 428 | public String withArgsAndThread( |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 429 | Integer pos1, boolean pos2, boolean named, Sequence<?> args, StarlarkThread thread) { |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 430 | String argsString = debugPrintArgs(args); |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 431 | return "with_args_and_thread(" |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 432 | + pos1 |
| 433 | + ", " |
| 434 | + pos2 |
| 435 | + ", " |
| 436 | + named |
| 437 | + ", " |
| 438 | + argsString |
| 439 | + ", " |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 440 | + thread.isGlobal() |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 441 | + ")"; |
| 442 | } |
| 443 | |
| 444 | @SkylarkCallable( |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 445 | name = "with_kwargs", |
| 446 | documented = false, |
| 447 | parameters = { |
| 448 | @Param(name = "pos", defaultValue = "False", type = Boolean.class), |
| 449 | @Param(name = "named", type = Boolean.class, positional = false, named = true), |
| 450 | }, |
| 451 | extraKeywords = @Param(name = "kwargs")) |
| 452 | public String withKwargs(boolean pos, boolean named, Dict<?, ?> kwargs) throws EvalException { |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 453 | String kwargsString = |
| 454 | "kwargs(" |
| 455 | + kwargs |
| 456 | .getContents(String.class, Object.class, "kwargs") |
| 457 | .entrySet() |
| 458 | .stream() |
| 459 | .map(entry -> entry.getKey() + "=" + entry.getValue()) |
| 460 | .collect(joining(", ")) |
| 461 | + ")"; |
| 462 | return "with_kwargs(" + pos + ", " + named + ", " + kwargsString + ")"; |
| 463 | } |
| 464 | |
| 465 | @SkylarkCallable( |
Googler | 4871cb0 | 2019-11-12 17:16:42 -0800 | [diff] [blame] | 466 | name = "with_args_and_kwargs", |
| 467 | documented = false, |
| 468 | parameters = { |
| 469 | @Param(name = "foo", named = true, positional = true, type = String.class), |
| 470 | }, |
| 471 | extraPositionals = @Param(name = "args"), |
| 472 | extraKeywords = @Param(name = "kwargs")) |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 473 | public String withArgsAndKwargs(String foo, Sequence<?> args, Dict<?, ?> kwargs) |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 474 | throws EvalException { |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 475 | String argsString = debugPrintArgs(args); |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 476 | String kwargsString = |
| 477 | "kwargs(" |
| 478 | + kwargs |
| 479 | .getContents(String.class, Object.class, "kwargs") |
| 480 | .entrySet() |
| 481 | .stream() |
| 482 | .map(entry -> entry.getKey() + "=" + entry.getValue()) |
| 483 | .collect(joining(", ")) |
| 484 | + ")"; |
| 485 | return "with_args_and_kwargs(" + foo + ", " + argsString + ", " + kwargsString + ")"; |
| 486 | } |
| 487 | |
Benjamin Peterson | af53a11 | 2019-06-05 10:47:11 -0700 | [diff] [blame] | 488 | @SkylarkCallable(name = "raise_unchecked_exception", documented = false) |
| 489 | public void raiseUncheckedException() { |
| 490 | throw new InternalError("buggy code"); |
| 491 | } |
| 492 | |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 493 | @Override |
| 494 | public String toString() { |
| 495 | return "<mock>"; |
| 496 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 499 | private static String debugPrintArgs(Iterable<?> args) { |
| 500 | Printer p = Printer.getPrinter(); |
| 501 | p.append("args("); |
| 502 | String sep = ""; |
| 503 | for (Object arg : args) { |
| 504 | p.append(sep).debugPrint(arg); |
| 505 | sep = ", "; |
| 506 | } |
| 507 | return p.append(")").toString(); |
| 508 | } |
| 509 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 510 | @SkylarkModule(name = "MockInterface", doc = "") |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 511 | static interface MockInterface extends StarlarkValue { |
cparsons | e661f88 | 2018-06-28 10:12:05 -0700 | [diff] [blame] | 512 | @SkylarkCallable(name = "is_empty_interface", |
| 513 | parameters = { @Param(name = "str", type = String.class) }, |
| 514 | documented = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 515 | public Boolean isEmptyInterface(String str); |
| 516 | } |
| 517 | |
cparsons | 4dc97ff | 2018-05-24 13:48:22 -0700 | [diff] [blame] | 518 | @SkylarkModule(name = "MockSubClass", doc = "") |
Googler | ab3cadc | 2019-11-08 15:17:21 -0800 | [diff] [blame] | 519 | final class MockSubClass extends Mock implements MockInterface { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 520 | @Override |
| 521 | public Boolean isEmpty(String str) { |
| 522 | return str.isEmpty(); |
| 523 | } |
| 524 | @Override |
| 525 | public Boolean isEmptyInterface(String str) { |
| 526 | return str.isEmpty(); |
| 527 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 528 | } |
| 529 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 530 | @SkylarkModule(name = "MockClassObject", documented = false, doc = "") |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 531 | static final class MockClassObject implements ClassObject, StarlarkValue { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 532 | @Override |
| 533 | public Object getValue(String name) { |
| 534 | switch (name) { |
| 535 | case "field": return "a"; |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 536 | case "nset": |
| 537 | return NestedSetBuilder.stableOrder().build(); // not a legal Starlark value |
vladmos | 3ad44e3 | 2017-05-04 18:30:51 +0200 | [diff] [blame] | 538 | default: return null; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 539 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | @Override |
brandjon | d331fa7 | 2017-12-28 07:38:31 -0800 | [diff] [blame] | 543 | public ImmutableCollection<String> getFieldNames() { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 544 | return ImmutableList.of("field", "nset"); |
| 545 | } |
| 546 | |
| 547 | @Override |
brandjon | d331fa7 | 2017-12-28 07:38:31 -0800 | [diff] [blame] | 548 | public String getErrorMessageForUnknownField(String name) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 549 | return null; |
| 550 | } |
| 551 | } |
| 552 | |
cparsons | d3d05b2 | 2018-05-24 12:12:07 -0700 | [diff] [blame] | 553 | @SkylarkModule(name = "ParamterizedMock", doc = "") |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 554 | static interface ParameterizedApi<ObjectT> extends StarlarkValue { |
cparsons | d3d05b2 | 2018-05-24 12:12:07 -0700 | [diff] [blame] | 555 | @SkylarkCallable( |
| 556 | name = "method", |
| 557 | documented = false, |
| 558 | parameters = { |
| 559 | @Param(name = "foo", named = true, positional = true, type = Object.class), |
| 560 | } |
| 561 | ) |
| 562 | public ObjectT method(ObjectT o); |
| 563 | } |
| 564 | |
| 565 | static final class ParameterizedMock implements ParameterizedApi<String> { |
| 566 | @Override |
| 567 | public String method(String o) { |
| 568 | return o; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // Verifies that a method implementation overriding a parameterized annotated interface method |
| 573 | // is still treated as skylark-callable. Concretely, method() below should be treated as |
| 574 | // callable even though its method signature isn't an *exact* match of the annotated method |
| 575 | // declaration, due to the interface's method declaration being generic. |
| 576 | @Test |
| 577 | public void testParameterizedMock() throws Exception { |
| 578 | new SkylarkTest() |
| 579 | .update("mock", new ParameterizedMock()) |
| 580 | .setUp("result = mock.method('bar')") |
| 581 | .testLookup("result", "bar"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 584 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 585 | public void testSimpleIf() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 586 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 587 | " a = 0", |
| 588 | " x = 0", |
| 589 | " if x: a = 5", |
| 590 | " return a", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 591 | "a = foo()").testLookup("a", 0); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 594 | @Test |
Laurent Le Brun | 0942ee9 | 2015-03-17 20:22:16 +0000 | [diff] [blame] | 595 | public void testIfPass() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 596 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 597 | " a = 1", |
| 598 | " x = True", |
| 599 | " if x: pass", |
| 600 | " return a", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 601 | "a = foo()").testLookup("a", 1); |
Laurent Le Brun | 0942ee9 | 2015-03-17 20:22:16 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 605 | public void testNestedIf() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 606 | executeNestedIf(0, 0, 0); |
| 607 | executeNestedIf(1, 0, 3); |
| 608 | executeNestedIf(1, 1, 5); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 611 | private void executeNestedIf(int x, int y, int expected) throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 612 | String fun = String.format("foo%s%s", x, y); |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 613 | new SkylarkTest().setUp("def " + fun + "():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 614 | " x = " + x, |
| 615 | " y = " + y, |
| 616 | " a = 0", |
| 617 | " b = 0", |
| 618 | " if x:", |
| 619 | " if y:", |
| 620 | " a = 2", |
| 621 | " b = 3", |
| 622 | " return a + b", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 623 | "x = " + fun + "()").testLookup("x", expected); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 626 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 627 | public void testIfElse() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 628 | executeIfElse("foo", "something", 2); |
| 629 | executeIfElse("bar", "", 3); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 632 | private void executeIfElse(String fun, String y, int expected) throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 633 | new SkylarkTest().setUp("def " + fun + "():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 634 | " y = '" + y + "'", |
| 635 | " x = 5", |
| 636 | " if x:", |
| 637 | " if y: a = 2", |
| 638 | " else: a = 3", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 639 | " return a", |
| 640 | "z = " + fun + "()").testLookup("z", expected); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 643 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 644 | public void testIfElifElse_IfExecutes() throws Exception { |
| 645 | execIfElifElse(1, 0, 1); |
| 646 | } |
| 647 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 648 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 649 | public void testIfElifElse_ElifExecutes() throws Exception { |
| 650 | execIfElifElse(0, 1, 2); |
| 651 | } |
| 652 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 653 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 654 | public void testIfElifElse_ElseExecutes() throws Exception { |
| 655 | execIfElifElse(0, 0, 3); |
| 656 | } |
| 657 | |
| 658 | private void execIfElifElse(int x, int y, int v) throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 659 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 660 | " x = " + x + "", |
| 661 | " y = " + y + "", |
| 662 | " if x:", |
| 663 | " return 1", |
| 664 | " elif y:", |
| 665 | " return 2", |
| 666 | " else:", |
| 667 | " return 3", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 668 | "v = foo()").testLookup("v", v); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 671 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 672 | public void testForOnList() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 673 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 674 | " s = ''", |
| 675 | " for i in ['hello', ' ', 'world']:", |
| 676 | " s = s + i", |
| 677 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 678 | "s = foo()").testLookup("s", "hello world"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 681 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 682 | public void testForAssignmentList() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 683 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 684 | " d = ['a', 'b', 'c']", |
| 685 | " s = ''", |
| 686 | " for i in d:", |
| 687 | " s = s + i", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 688 | " d = ['d', 'e', 'f']", // check that we use the old list |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 689 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 690 | "s = foo()").testLookup("s", "abc"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 693 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 694 | public void testForAssignmentDict() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 695 | new SkylarkTest().setUp("def func():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 696 | " d = {'a' : 1, 'b' : 2, 'c' : 3}", |
| 697 | " s = ''", |
| 698 | " for i in d:", |
| 699 | " s = s + i", |
| 700 | " d = {'d' : 1, 'e' : 2, 'f' : 3}", |
| 701 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 702 | "s = func()").testLookup("s", "abc"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 705 | @Test |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 706 | public void testForUpdateList() throws Exception { |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 707 | new SkylarkTest().setUp("def foo():", |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 708 | " xs = [1, 2, 3]", |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 709 | " for x in xs:", |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 710 | " if x == 1:", |
| 711 | " xs.append(10)" |
| 712 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Test |
| 716 | public void testForUpdateDict() throws Exception { |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 717 | new SkylarkTest().setUp("def foo():", |
| 718 | " d = {'a': 1, 'b': 2, 'c': 3}", |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 719 | " for k in d:", |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 720 | " d[k] *= 2" |
| 721 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
| 722 | } |
| 723 | |
| 724 | @Test |
| 725 | public void testForUnlockedAfterBreak() throws Exception { |
| 726 | new SkylarkTest().setUp("def foo():", |
| 727 | " xs = [1, 2]", |
| 728 | " for x in xs:", |
| 729 | " break", |
| 730 | " xs.append(3)", |
| 731 | " return xs" |
| 732 | ).testEval("foo()", "[1, 2, 3]"); |
| 733 | } |
| 734 | |
| 735 | @Test |
| 736 | public void testForNestedOnSameListStillLocked() throws Exception { |
| 737 | new SkylarkTest().setUp("def foo():", |
| 738 | " xs = [1, 2]", |
| 739 | " ys = []", |
| 740 | " for x1 in xs:", |
| 741 | " for x2 in xs:", |
| 742 | " ys.append(x1 * x2)", |
| 743 | " xs.append(4)", |
| 744 | " return ys" |
| 745 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
| 746 | } |
| 747 | |
| 748 | @Test |
| 749 | public void testForNestedOnSameListErrorMessage() throws Exception { |
| 750 | new SkylarkTest().setUp("def foo():", |
| 751 | " xs = [1, 2]", |
| 752 | " ys = []", |
| 753 | " for x1 in xs:", |
| 754 | " for x2 in xs:", |
| 755 | " ys.append(x1 * x2)", |
| 756 | " xs.append(4)", |
| 757 | " return ys" |
| 758 | // No file name in message, due to how test is set up. |
Carmi Grushko | 46bf88c | 2017-02-20 22:37:15 +0000 | [diff] [blame] | 759 | ).testIfErrorContains("Object locked at the following location(s): :4:3, :5:5", "foo()"); |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | @Test |
| 763 | public void testForNestedOnSameListUnlockedAtEnd() throws Exception { |
| 764 | new SkylarkTest().setUp("def foo():", |
| 765 | " xs = [1, 2]", |
| 766 | " ys = []", |
| 767 | " for x1 in xs:", |
| 768 | " for x2 in xs:", |
| 769 | " ys.append(x1 * x2)", |
| 770 | " xs.append(4)", |
| 771 | " return ys" |
| 772 | ).testEval("foo()", "[1, 2, 2, 4]"); |
| 773 | } |
| 774 | |
| 775 | @Test |
| 776 | public void testForNestedWithListCompGood() throws Exception { |
| 777 | new SkylarkTest().setUp("def foo():", |
| 778 | " xs = [1, 2]", |
| 779 | " ys = []", |
| 780 | " for x in xs:", |
| 781 | " zs = [None for x in xs for y in (ys.append(x) or ys)]", |
| 782 | " return ys" |
| 783 | ).testEval("foo()", "[1, 2, 1, 2]"); |
| 784 | } |
| 785 | @Test |
| 786 | public void testForNestedWithListCompBad() throws Exception { |
| 787 | new SkylarkTest().setUp("def foo():", |
| 788 | " xs = [1, 2, 3]", |
| 789 | " ys = []", |
| 790 | " for x in xs:", |
| 791 | " zs = [None for x in xs for y in (xs.append(x) or ys)]", |
| 792 | " return ys" |
| 793 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | @Test |
| 797 | public void testForDeepUpdate() throws Exception { |
| 798 | // Check that indirectly reachable values can still be manipulated as normal. |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 799 | new SkylarkTest() |
| 800 | .setUp( |
| 801 | "def foo():", |
| 802 | " xs = [['a'], ['b'], ['c']]", |
| 803 | " ys = []", |
| 804 | " for x in xs:", |
| 805 | " for y in x:", |
| 806 | " ys.append(y)", |
| 807 | " xs[2].append(x[0])", |
| 808 | " return ys", |
| 809 | "ys = foo()") |
| 810 | .testLookup("ys", StarlarkList.of(null, "a", "b", "c", "a", "b")); |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 814 | public void testForNotIterable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 815 | new SkylarkTest() |
| 816 | .update("mock", new Mock()) |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 817 | .testIfErrorContains( |
| 818 | "type 'int' is not iterable", |
| 819 | "def func():", |
| 820 | " for i in mock.value_of('1'): a = i", |
| 821 | "func()\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 824 | @Test |
laurentlb | 26f843d | 2019-02-20 06:44:05 -0800 | [diff] [blame] | 825 | public void testForStringNotIterable() throws Exception { |
| 826 | new SkylarkTest() |
| 827 | .update("mock", new Mock()) |
| 828 | .testIfErrorContains( |
| 829 | "type 'string' is not iterable", "def func():", " for i in 'abc': a = i", "func()\n"); |
| 830 | } |
| 831 | |
| 832 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 833 | public void testForOnDictionary() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 834 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 835 | " d = {1: 'a', 2: 'b', 3: 'c'}", |
| 836 | " s = ''", |
| 837 | " for i in d: s = s + d[i]", |
| 838 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 839 | "s = foo()").testLookup("s", "abc"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 842 | @Test |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 843 | public void testBadDictKey() throws Exception { |
| 844 | new SkylarkTest().testIfErrorContains( |
| 845 | "unhashable type: 'list'", |
| 846 | "{ [1, 2]: [3, 4] }"); |
| 847 | } |
| 848 | |
| 849 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 850 | public void testForLoopReuseVariable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 851 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 852 | " s = ''", |
| 853 | " for i in ['a', 'b']:", |
| 854 | " for i in ['c', 'd']: s = s + i", |
| 855 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 856 | "s = foo()").testLookup("s", "cdcd"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 859 | @Test |
Laurent Le Brun | 741824b | 2015-03-20 15:10:19 +0000 | [diff] [blame] | 860 | public void testForLoopMultipleVariables() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 861 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 862 | " s = ''", |
| 863 | " for [i, j] in [[1, 2], [3, 4]]:", |
| 864 | " s = s + str(i) + str(j) + '.'", |
| 865 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 866 | "s = foo()").testLookup("s", "12.34."); |
Laurent Le Brun | 741824b | 2015-03-20 15:10:19 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | @Test |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 870 | public void testForLoopBreak() throws Exception { |
| 871 | simpleFlowTest("break", 1); |
| 872 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 873 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 874 | @Test |
| 875 | public void testForLoopContinue() throws Exception { |
| 876 | simpleFlowTest("continue", 10); |
| 877 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 878 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 879 | @SuppressWarnings("unchecked") |
| 880 | private void simpleFlowTest(String statement, int expected) throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 881 | exec( |
| 882 | "def foo():", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 883 | " s = 0", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 884 | " hit = 0", |
| 885 | " for i in range(0, 10):", |
| 886 | " s = s + 1", |
| 887 | " " + statement + "", |
| 888 | " hit = 1", |
| 889 | " return [s, hit]", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 890 | "x = foo()"); |
| 891 | assertThat((Iterable<Object>) lookup("x")).containsExactly(expected, 0).inOrder(); |
| 892 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 893 | |
| 894 | @Test |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 895 | public void testForLoopBreakFromDeeperBlock() throws Exception { |
| 896 | flowFromDeeperBlock("break", 1); |
| 897 | flowFromNestedBlocks("break", 29); |
| 898 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 899 | |
| 900 | @Test |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 901 | public void testForLoopContinueFromDeeperBlock() throws Exception { |
| 902 | flowFromDeeperBlock("continue", 5); |
| 903 | flowFromNestedBlocks("continue", 39); |
| 904 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 905 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 906 | private void flowFromDeeperBlock(String statement, int expected) throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 907 | exec( |
| 908 | "def foo():", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 909 | " s = 0", |
| 910 | " for i in range(0, 10):", |
| 911 | " if i % 2 != 0:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 912 | " " + statement + "", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 913 | " s = s + 1", |
| 914 | " return s", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 915 | "x = foo()"); |
| 916 | assertThat(lookup("x")).isEqualTo(expected); |
| 917 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 918 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 919 | private void flowFromNestedBlocks(String statement, int expected) throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 920 | exec( |
| 921 | "def foo2():", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 922 | " s = 0", |
| 923 | " for i in range(1, 41):", |
| 924 | " if i % 2 == 0:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 925 | " if i % 3 == 0:", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 926 | " if i % 5 == 0:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 927 | " " + statement + "", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 928 | " s = s + 1", |
| 929 | " return s", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 930 | "y = foo2()"); |
| 931 | assertThat(lookup("y")).isEqualTo(expected); |
| 932 | } |
| 933 | |
| 934 | @Test |
| 935 | public void testNestedForLoopsMultipleBreaks() throws Exception { |
| 936 | nestedLoopsTest("break", 2, 6, 6); |
| 937 | } |
| 938 | |
| 939 | @Test |
| 940 | public void testNestedForLoopsMultipleContinues() throws Exception { |
| 941 | nestedLoopsTest("continue", 4, 20, 20); |
| 942 | } |
| 943 | |
| 944 | @SuppressWarnings("unchecked") |
| 945 | private void nestedLoopsTest(String statement, Integer outerExpected, int firstExpected, |
| 946 | int secondExpected) throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 947 | exec( |
| 948 | "def foo():", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 949 | " outer = 0", |
| 950 | " first = 0", |
| 951 | " second = 0", |
| 952 | " for i in range(0, 5):", |
| 953 | " for j in range(0, 5):", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 954 | " if j == 2:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 955 | " " + statement + "", |
| 956 | " first = first + 1", |
| 957 | " for k in range(0, 5):", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 958 | " if k == 2:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 959 | " " + statement + "", |
| 960 | " second = second + 1", |
| 961 | " if i == 2:", |
| 962 | " " + statement + "", |
| 963 | " outer = outer + 1", |
| 964 | " return [outer, first, second]", |
| 965 | "x = foo()"); |
| 966 | assertThat((Iterable<Object>) lookup("x")) |
| 967 | .containsExactly(outerExpected, firstExpected, secondExpected).inOrder(); |
| 968 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 969 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 970 | @Test |
| 971 | public void testForLoopBreakError() throws Exception { |
| 972 | flowStatementInsideFunction("break"); |
| 973 | flowStatementAfterLoop("break"); |
| 974 | } |
| 975 | |
| 976 | @Test |
| 977 | public void testForLoopContinueError() throws Exception { |
| 978 | flowStatementInsideFunction("continue"); |
| 979 | flowStatementAfterLoop("continue"); |
| 980 | } |
| 981 | |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 982 | // TODO(adonovan): move this and all tests that use it to Validation tests. |
| 983 | private void assertValidationError(String expectedError, final String... lines) throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 984 | SyntaxError error = assertThrows(SyntaxError.class, () -> exec(lines)); |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 985 | assertThat(error).hasMessageThat().contains(expectedError); |
| 986 | } |
| 987 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 988 | private void flowStatementInsideFunction(String statement) throws Exception { |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 989 | assertValidationError( |
| 990 | statement + " statement must be inside a for loop", |
| 991 | // |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 992 | "def foo():", |
brandjon | e5d95fb | 2017-07-13 17:23:09 +0200 | [diff] [blame] | 993 | " " + statement, |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 994 | "x = foo()"); |
| 995 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 996 | |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 997 | private void flowStatementAfterLoop(String statement) throws Exception { |
| 998 | assertValidationError( |
| 999 | statement + " statement must be inside a for loop", |
| 1000 | // |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 1001 | "def foo2():", |
| 1002 | " for i in range(0, 3):", |
| 1003 | " pass", |
brandjon | e5d95fb | 2017-07-13 17:23:09 +0200 | [diff] [blame] | 1004 | " " + statement, |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 1005 | "y = foo2()"); |
| 1006 | } |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1007 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 1008 | @Test |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1009 | public void testStructMembersAreImmutable() throws Exception { |
| 1010 | assertValidationError("cannot assign to 's.x'", "s = struct(x = 'a')", "s.x = 'b'\n"); |
| 1011 | } |
| 1012 | |
| 1013 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1014 | public void testNoneAssignment() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1015 | new SkylarkTest() |
| 1016 | .setUp("def foo(x=None):", " x = 1", " x = None", " return 2", "s = foo()") |
| 1017 | .testLookup("s", 2); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1020 | @Test |
Laurent Le Brun | 88014fe | 2015-06-17 16:02:16 +0000 | [diff] [blame] | 1021 | public void testReassignment() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1022 | exec( |
| 1023 | "def foo(x=None):", // |
Laurent Le Brun | 88014fe | 2015-06-17 16:02:16 +0000 | [diff] [blame] | 1024 | " x = 1", |
| 1025 | " x = [1, 2]", |
| 1026 | " x = 'str'", |
| 1027 | " return x", |
| 1028 | "s = foo()"); |
| 1029 | assertThat(lookup("s")).isEqualTo("str"); |
| 1030 | } |
| 1031 | |
| 1032 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1033 | public void testJavaCalls() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1034 | new SkylarkTest() |
| 1035 | .update("mock", new Mock()) |
| 1036 | .setUp("b = mock.is_empty('a')") |
| 1037 | .testLookup("b", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1040 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1041 | public void testJavaCallsOnSubClass() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1042 | new SkylarkTest() |
| 1043 | .update("mock", new MockSubClass()) |
| 1044 | .setUp("b = mock.is_empty('a')") |
| 1045 | .testLookup("b", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1048 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1049 | public void testJavaCallsOnInterface() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1050 | new SkylarkTest() |
| 1051 | .update("mock", new MockSubClass()) |
| 1052 | .setUp("b = mock.is_empty_interface('a')") |
| 1053 | .testLookup("b", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1056 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1057 | public void testJavaCallsNotSkylarkCallable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1058 | new SkylarkTest() |
| 1059 | .update("mock", new Mock()) |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1060 | .testIfExactError("'Mock' value has no field or method 'value'", "mock.value()"); |
Laurent Le Brun | 648f8f3 | 2015-09-09 19:46:29 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | @Test |
| 1064 | public void testNoOperatorIndex() throws Exception { |
| 1065 | new SkylarkTest() |
| 1066 | .update("mock", new Mock()) |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 1067 | .testIfExactError("type 'Mock' has no operator [](int)", "mock[2]"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1070 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1071 | public void testJavaCallsNoMethod() throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 1072 | new SkylarkTest() |
| 1073 | .update("mock", new Mock()) |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1074 | .testIfExactError("'Mock' value has no field or method 'bad'", "mock.bad()"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1077 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1078 | public void testJavaCallsNoMethodErrorMsg() throws Exception { |
Laurent Le Brun | 648f8f3 | 2015-09-09 19:46:29 +0000 | [diff] [blame] | 1079 | new SkylarkTest() |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1080 | .testIfExactError("'int' value has no field or method 'bad'", "s = 3.bad('a', 'b', 'c')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1083 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1084 | public void testJavaCallWithKwargs() throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 1085 | new SkylarkTest() |
| 1086 | .update("mock", new Mock()) |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1087 | .testIfExactError( |
| 1088 | "'Mock' value has no field or method 'isEmpty'", "mock.isEmpty(str='abc')"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
cparsons | aaab11f | 2018-01-31 11:00:11 -0800 | [diff] [blame] | 1091 | @Test |
| 1092 | public void testStringListDictValues() throws Exception { |
| 1093 | new SkylarkTest() |
| 1094 | .update("mock", new Mock()) |
| 1095 | .setUp( |
| 1096 | "def func(mock):", |
| 1097 | " for i, v in mock.string_list_dict().items():", |
| 1098 | " modified_list = v + ['extra_string']", |
| 1099 | " return modified_list", |
| 1100 | "m = func(mock)") |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1101 | .testLookup("m", StarlarkList.of(thread.mutability(), "b", "c", "extra_string")); |
cparsons | aaab11f | 2018-01-31 11:00:11 -0800 | [diff] [blame] | 1102 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1103 | |
| 1104 | @Test |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 1105 | public void testProxyMethodsObject() throws Exception { |
| 1106 | new SkylarkTest() |
| 1107 | .update("mock", new Mock()) |
| 1108 | .setUp( |
| 1109 | "m = mock.proxy_methods_object()", |
| 1110 | "b = m.with_params(1, True, named=True)") |
| 1111 | .testLookup("b", "with_params(1, true, false, true, false, a)"); |
| 1112 | } |
| 1113 | |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1114 | @Test |
| 1115 | public void testLegacyNamed() throws Exception { |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1116 | new SkylarkTest("--incompatible_restrict_named_params=false") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1117 | .update("mock", new Mock()) |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1118 | .setUp("b = mock.legacy_method(True, legacyNamed=True, named=True)") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1119 | .testLookup("b", "legacy_method(true, true, true)"); |
| 1120 | |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1121 | new SkylarkTest("--incompatible_restrict_named_params=false") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1122 | .update("mock", new Mock()) |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1123 | .setUp("b = mock.legacy_method(True, True, named=True)") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1124 | .testLookup("b", "legacy_method(true, true, true)"); |
| 1125 | |
| 1126 | // Verify legacyNamed also works with proxy method objects. |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1127 | new SkylarkTest("--incompatible_restrict_named_params=false") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1128 | .update("mock", new Mock()) |
| 1129 | .setUp( |
| 1130 | "m = mock.proxy_methods_object()", |
| 1131 | "b = m.legacy_method(True, legacyNamed=True, named=True)") |
| 1132 | .testLookup("b", "legacy_method(true, true, true)"); |
| 1133 | |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1134 | new SkylarkTest("--incompatible_restrict_named_params=false") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1135 | .update("mock", new Mock()) |
cparsons | 65ecfea | 2019-08-28 14:37:23 -0700 | [diff] [blame] | 1136 | .setUp("m = mock.proxy_methods_object()", "b = m.legacy_method(True, True, named=True)") |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1137 | .testLookup("b", "legacy_method(true, true, true)"); |
| 1138 | } |
| 1139 | |
cparsons | 74f7897 | 2018-04-06 12:55:47 -0700 | [diff] [blame] | 1140 | /** |
| 1141 | * This test verifies an error is raised when a method parameter is set both positionally and |
| 1142 | * by name. |
| 1143 | */ |
| 1144 | @Test |
| 1145 | public void testArgSpecifiedBothByNameAndPosition() throws Exception { |
| 1146 | // in with_params, 'posOrNamed' is positional parameter index 2. So by specifying both |
| 1147 | // posOrNamed by name and three positional parameters, there is a conflict. |
| 1148 | new SkylarkTest() |
| 1149 | .update("mock", new Mock()) |
| 1150 | .testIfErrorContains("got multiple values for keyword argument 'posOrNamed'", |
| 1151 | "mock.with_params(1, True, True, posOrNamed=True, named=True)"); |
| 1152 | } |
| 1153 | |
| 1154 | @Test |
| 1155 | public void testTooManyPositionalArgs() throws Exception { |
| 1156 | new SkylarkTest() |
| 1157 | .update("mock", new Mock()) |
| 1158 | .testIfErrorContains("expected no more than 3 positional arguments, but got 4", |
| 1159 | "mock.with_params(1, True, True, 'toomany', named=True)"); |
| 1160 | |
| 1161 | new SkylarkTest() |
| 1162 | .update("mock", new Mock()) |
| 1163 | .testIfErrorContains("expected no more than 3 positional arguments, but got 5", |
| 1164 | "mock.with_params(1, True, True, 'toomany', 'alsotoomany', named=True)"); |
| 1165 | |
| 1166 | new SkylarkTest() |
| 1167 | .update("mock", new Mock()) |
| 1168 | .testIfErrorContains("expected no more than 1 positional arguments, but got 2", |
| 1169 | "mock.is_empty('a', 'b')"); |
| 1170 | } |
| 1171 | |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 1172 | @Test |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1173 | public void testJavaCallWithPositionalAndKwargs() throws Exception { |
| 1174 | new SkylarkTest() |
| 1175 | .update("mock", new Mock()) |
| 1176 | .setUp("b = mock.with_params(1, True, named=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 1177 | .testLookup("b", "with_params(1, true, false, true, false, a)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1178 | new SkylarkTest() |
| 1179 | .update("mock", new Mock()) |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 1180 | .setUp("b = mock.with_params(1, True, named=True, multi=1)") |
| 1181 | .testLookup("b", "with_params(1, true, false, true, false, a, 1)"); |
| 1182 | new SkylarkTest() |
| 1183 | .update("mock", new Mock()) |
| 1184 | .setUp("b = mock.with_params(1, True, named=True, multi='abc')") |
| 1185 | .testLookup("b", "with_params(1, true, false, true, false, a, abc)"); |
| 1186 | |
| 1187 | new SkylarkTest() |
| 1188 | .update("mock", new Mock()) |
| 1189 | .setUp("b = mock.with_params(1, True, named=True, multi=[1,2,3])") |
| 1190 | .testLookup("b", "with_params(1, true, false, true, false, a, [1, 2, 3])"); |
| 1191 | |
| 1192 | new SkylarkTest() |
| 1193 | .update("mock", new Mock()) |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1194 | .setUp("") |
| 1195 | .testIfExactError( |
cparsons | 0303514 | 2019-01-15 13:35:22 -0800 | [diff] [blame] | 1196 | "parameter 'named' has no default value, for call to " |
| 1197 | + "method with_params(pos1, pos2 = False, posOrNamed = False, named, " |
| 1198 | + "optionalNamed = False, nonNoneable = \"a\", noneable = None, multi = None) " |
| 1199 | + "of 'Mock'", |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 1200 | "mock.with_params(1, True)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1201 | new SkylarkTest() |
| 1202 | .update("mock", new Mock()) |
| 1203 | .setUp("") |
| 1204 | .testIfExactError( |
cparsons | 0303514 | 2019-01-15 13:35:22 -0800 | [diff] [blame] | 1205 | "parameter 'named' has no default value, for call to " |
| 1206 | + "method with_params(pos1, pos2 = False, posOrNamed = False, named, " |
| 1207 | + "optionalNamed = False, nonNoneable = \"a\", noneable = None, multi = None) " |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 1208 | + "of 'Mock'", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1209 | "mock.with_params(1, True, True)"); |
| 1210 | new SkylarkTest() |
| 1211 | .update("mock", new Mock()) |
| 1212 | .setUp("b = mock.with_params(1, True, True, named=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 1213 | .testLookup("b", "with_params(1, true, true, true, false, a)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1214 | new SkylarkTest() |
| 1215 | .update("mock", new Mock()) |
| 1216 | .setUp("b = mock.with_params(1, True, named=True, posOrNamed=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 1217 | .testLookup("b", "with_params(1, true, true, true, false, a)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1218 | new SkylarkTest() |
| 1219 | .update("mock", new Mock()) |
| 1220 | .setUp("b = mock.with_params(1, True, named=True, posOrNamed=True, optionalNamed=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 1221 | .testLookup("b", "with_params(1, true, true, true, true, a)"); |
Pedro Liberal Fernandez | 837dbc1 | 2016-08-18 14:13:01 +0000 | [diff] [blame] | 1222 | new SkylarkTest() |
| 1223 | .update("mock", new Mock()) |
| 1224 | .setUp("") |
| 1225 | .testIfExactError( |
cparsons | 0303514 | 2019-01-15 13:35:22 -0800 | [diff] [blame] | 1226 | "unexpected keyword 'n', for call to " |
| 1227 | + "method with_params(pos1, pos2 = False, posOrNamed = False, named, " |
| 1228 | + "optionalNamed = False, nonNoneable = \"a\", noneable = None, multi = None) " |
| 1229 | + "of 'Mock'", |
Pedro Liberal Fernandez | 837dbc1 | 2016-08-18 14:13:01 +0000 | [diff] [blame] | 1230 | "mock.with_params(1, True, named=True, posOrNamed=True, n=2)"); |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 1231 | new SkylarkTest() |
| 1232 | .update("mock", new Mock()) |
| 1233 | .setUp("") |
| 1234 | .testIfExactError( |
cparsons | 0303514 | 2019-01-15 13:35:22 -0800 | [diff] [blame] | 1235 | "parameter 'nonNoneable' cannot be None, for call to method " |
| 1236 | + "with_params(pos1, pos2 = False, posOrNamed = False, named, " |
| 1237 | + "optionalNamed = False, nonNoneable = \"a\", noneable = None, multi = None) " |
| 1238 | + "of 'Mock'", |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 1239 | "mock.with_params(1, True, True, named=True, optionalNamed=False, nonNoneable=None)"); |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 1240 | |
| 1241 | new SkylarkTest() |
| 1242 | .update("mock", new Mock()) |
| 1243 | .setUp("") |
| 1244 | .testIfExactError( |
cparsons | a95884e | 2018-03-21 11:06:13 -0700 | [diff] [blame] | 1245 | "expected value of type 'string or int or sequence of ints or NoneType' for parameter" |
cparsons | 0303514 | 2019-01-15 13:35:22 -0800 | [diff] [blame] | 1246 | + " 'multi', for call to method " |
| 1247 | + "with_params(pos1, pos2 = False, posOrNamed = False, named, " |
| 1248 | + "optionalNamed = False, nonNoneable = \"a\", noneable = None, multi = None) " |
| 1249 | + "of 'Mock'", |
Dmitry Lomov | d22e1de | 2017-09-25 08:53:50 -0400 | [diff] [blame] | 1250 | "mock.with_params(1, True, named=True, multi=False)"); |
| 1251 | |
| 1252 | // We do not enforce list item parameter type constraints. |
| 1253 | // Test for this behavior. |
| 1254 | new SkylarkTest() |
| 1255 | .update("mock", new Mock()) |
| 1256 | .setUp("b = mock.with_params(1, True, named=True, multi=['a', 'b'])") |
| 1257 | .testLookup("b", "with_params(1, true, false, true, false, a, [\"a\", \"b\"])"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1260 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1261 | public void testNoJavaCallsWithoutSkylark() throws Exception { |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1262 | new SkylarkTest() |
| 1263 | .testIfExactError("'int' value has no field or method 'to_string'", "s = 3.to_string()"); |
Laurent Le Brun | 648f8f3 | 2015-09-09 19:46:29 +0000 | [diff] [blame] | 1264 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1265 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1266 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1267 | public void testStructAccess() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1268 | new SkylarkTest() |
| 1269 | .update("mock", new Mock()) |
| 1270 | .setUp("v = mock.struct_field") |
| 1271 | .testLookup("v", "a"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1274 | @Test |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 1275 | public void testStructAccessAsFuncallNonCallable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1276 | new SkylarkTest() |
| 1277 | .update("mock", new Mock()) |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 1278 | .testIfExactError("'string' object is not callable", "v = mock.struct_field()"); |
| 1279 | } |
| 1280 | |
| 1281 | @Test |
cparsons | 0e86686 | 2018-04-24 08:07:02 -0700 | [diff] [blame] | 1282 | public void testSelfCall() throws Exception { |
| 1283 | new SkylarkTest() |
| 1284 | .update("mock", new Mock()) |
| 1285 | .setUp("v = mock('bestmock')") |
| 1286 | .testLookup("v", "I'm a mock named bestmock"); |
| 1287 | |
| 1288 | new SkylarkTest() |
| 1289 | .update("mock", new Mock()) |
| 1290 | .setUp("mockfunction = mock", "v = mockfunction('bestmock')") |
| 1291 | .testLookup("v", "I'm a mock named bestmock"); |
| 1292 | |
| 1293 | new SkylarkTest() |
| 1294 | .update("mock", new Mock()) |
| 1295 | .testIfErrorContains( |
cparsons | 0303514 | 2019-01-15 13:35:22 -0800 | [diff] [blame] | 1296 | "expected value of type 'string' for parameter 'pos', for call to function MockFn(pos)", |
cparsons | 0e86686 | 2018-04-24 08:07:02 -0700 | [diff] [blame] | 1297 | "v = mock(1)"); |
| 1298 | } |
| 1299 | |
| 1300 | @Test |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 1301 | public void testStructAccessAsFuncall() throws Exception { |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 1302 | new SkylarkTest() |
| 1303 | .update("mock", new Mock()) |
| 1304 | .setUp("v = mock.struct_field_callable()") |
| 1305 | .testLookup("v", "foobar"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1308 | @Test |
Benjamin Peterson | bf1db78 | 2018-08-08 10:03:22 -0700 | [diff] [blame] | 1309 | public void testCallingInterruptedStructField() throws Exception { |
| 1310 | update("mock", new Mock()); |
| 1311 | assertThrows(InterruptedException.class, () -> eval("mock.interrupted_struct_field()")); |
| 1312 | } |
| 1313 | |
| 1314 | @Test |
| 1315 | public void testCallingInterruptedFunction() throws Exception { |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1316 | update("interrupted_function", new BuiltinCallable(this, "interrupted_function")); |
Benjamin Peterson | bf1db78 | 2018-08-08 10:03:22 -0700 | [diff] [blame] | 1317 | assertThrows(InterruptedException.class, () -> eval("interrupted_function()")); |
| 1318 | } |
| 1319 | |
| 1320 | @Test |
Benjamin Peterson | af53a11 | 2019-06-05 10:47:11 -0700 | [diff] [blame] | 1321 | public void testCallingMethodThatRaisesUncheckedException() throws Exception { |
| 1322 | update("mock", new Mock()); |
| 1323 | assertThrows(InternalError.class, () -> eval("mock.raise_unchecked_exception()")); |
| 1324 | } |
| 1325 | |
| 1326 | @Test |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 1327 | public void testJavaFunctionWithExtraInterpreterParams() throws Exception { |
| 1328 | new SkylarkTest() |
| 1329 | .update("mock", new Mock()) |
| 1330 | .setUp("v = mock.with_extra()") |
Googler | 7fa0dd2 | 2019-09-24 20:07:53 -0700 | [diff] [blame] | 1331 | .testLookup("v", "with_extra(1, 0, true, true)"); |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | @Test |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 1335 | public void testStructFieldWithExtraInterpreterParams() throws Exception { |
| 1336 | new SkylarkTest() |
| 1337 | .update("mock", new Mock()) |
| 1338 | .setUp("v = mock.struct_field_with_extra") |
| 1339 | .testLookup("v", "struct_field_with_extra(true)"); |
| 1340 | } |
| 1341 | |
| 1342 | @Test |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 1343 | public void testJavaFunctionWithParamsAndExtraInterpreterParams() throws Exception { |
| 1344 | new SkylarkTest() |
| 1345 | .update("mock", new Mock()) |
| 1346 | .setUp("b = mock.with_params_and_extra(1, True, named=True)") |
cparsons | 73e1016 | 2018-03-22 10:02:02 -0700 | [diff] [blame] | 1347 | .testLookup("b", "with_params_and_extra(1, true, false, true, false, a, 1, 3, true, true)"); |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | @Test |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1351 | public void testJavaFunctionWithExtraArgsAndThread() throws Exception { |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 1352 | new SkylarkTest() |
| 1353 | .update("mock", new Mock()) |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1354 | .setUp("b = mock.with_args_and_thread(1, True, 'extraArg1', 'extraArg2', named=True)") |
| 1355 | .testLookup("b", "with_args_and_thread(1, true, true, args(extraArg1, extraArg2), true)"); |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 1356 | |
| 1357 | // Use an args list. |
| 1358 | new SkylarkTest() |
| 1359 | .update("mock", new Mock()) |
| 1360 | .setUp( |
| 1361 | "myargs = ['extraArg2']", |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1362 | "b = mock.with_args_and_thread(1, True, 'extraArg1', named=True, *myargs)") |
| 1363 | .testLookup("b", "with_args_and_thread(1, true, true, args(extraArg1, extraArg2), true)"); |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | @Test |
| 1367 | public void testJavaFunctionWithExtraKwargs() throws Exception { |
| 1368 | new SkylarkTest() |
| 1369 | .update("mock", new Mock()) |
| 1370 | .setUp("b = mock.with_kwargs(True, extraKey1=True, named=True, extraKey2='x')") |
| 1371 | .testLookup("b", "with_kwargs(true, true, kwargs(extraKey1=true, extraKey2=x))"); |
| 1372 | |
| 1373 | // Use a kwargs dict. |
| 1374 | new SkylarkTest() |
| 1375 | .update("mock", new Mock()) |
| 1376 | .setUp( |
| 1377 | "mykwargs = {'extraKey2':'x', 'named':True}", |
| 1378 | "b = mock.with_kwargs(True, extraKey1=True, **mykwargs)") |
| 1379 | .testLookup("b", "with_kwargs(true, true, kwargs(extraKey1=true, extraKey2=x))"); |
| 1380 | } |
| 1381 | |
| 1382 | @Test |
| 1383 | public void testJavaFunctionWithArgsAndKwargs() throws Exception { |
| 1384 | // Foo is used positionally |
| 1385 | new SkylarkTest() |
| 1386 | .update("mock", new Mock()) |
| 1387 | .setUp("b = mock.with_args_and_kwargs('foo', 'bar', 'baz', extraKey1=True, extraKey2='x')") |
| 1388 | .testLookup( |
| 1389 | "b", "with_args_and_kwargs(foo, args(bar, baz), kwargs(extraKey1=true, extraKey2=x))"); |
| 1390 | |
| 1391 | // Use an args list and a kwargs dict |
| 1392 | new SkylarkTest() |
| 1393 | .update("mock", new Mock()) |
| 1394 | .setUp( |
| 1395 | "mykwargs = {'extraKey1':True}", |
| 1396 | "myargs = ['baz']", |
| 1397 | "b = mock.with_args_and_kwargs('foo', 'bar', extraKey2='x', *myargs, **mykwargs)") |
| 1398 | .testLookup( |
| 1399 | "b", "with_args_and_kwargs(foo, args(bar, baz), kwargs(extraKey2=x, extraKey1=true))"); |
| 1400 | |
| 1401 | // Foo is used by name |
| 1402 | new SkylarkTest() |
| 1403 | .update("mock", new Mock()) |
| 1404 | .setUp("b = mock.with_args_and_kwargs(foo='foo', extraKey1=True)") |
| 1405 | .testLookup("b", "with_args_and_kwargs(foo, args(), kwargs(extraKey1=true))"); |
| 1406 | |
| 1407 | // Empty args and kwargs. |
| 1408 | new SkylarkTest() |
| 1409 | .update("mock", new Mock()) |
| 1410 | .setUp("b = mock.with_args_and_kwargs('foo')") |
| 1411 | .testLookup("b", "with_args_and_kwargs(foo, args(), kwargs())"); |
| 1412 | } |
| 1413 | |
| 1414 | @Test |
| 1415 | public void testProxyMethodsObjectWithArgsAndKwargs() throws Exception { |
| 1416 | // Foo is used positionally |
| 1417 | new SkylarkTest() |
| 1418 | .update("mock", new Mock()) |
| 1419 | .setUp( |
| 1420 | "m = mock.proxy_methods_object()", |
| 1421 | "b = m.with_args_and_kwargs('foo', 'bar', 'baz', extraKey1=True, extraKey2='x')") |
| 1422 | .testLookup( |
| 1423 | "b", "with_args_and_kwargs(foo, args(bar, baz), kwargs(extraKey1=true, extraKey2=x))"); |
| 1424 | |
| 1425 | // Use an args list and a kwargs dict |
| 1426 | new SkylarkTest() |
| 1427 | .update("mock", new Mock()) |
| 1428 | .setUp( |
| 1429 | "mykwargs = {'extraKey1':True}", |
| 1430 | "myargs = ['baz']", |
| 1431 | "m = mock.proxy_methods_object()", |
| 1432 | "b = m.with_args_and_kwargs('foo', 'bar', extraKey2='x', *myargs, **mykwargs)") |
| 1433 | .testLookup( |
| 1434 | "b", "with_args_and_kwargs(foo, args(bar, baz), kwargs(extraKey2=x, extraKey1=true))"); |
| 1435 | |
| 1436 | // Foo is used by name |
| 1437 | new SkylarkTest() |
| 1438 | .update("mock", new Mock()) |
| 1439 | .setUp( |
| 1440 | "m = mock.proxy_methods_object()", |
| 1441 | "b = m.with_args_and_kwargs(foo='foo', extraKey1=True)") |
| 1442 | .testLookup("b", "with_args_and_kwargs(foo, args(), kwargs(extraKey1=true))"); |
| 1443 | |
| 1444 | // Empty args and kwargs. |
| 1445 | new SkylarkTest() |
| 1446 | .update("mock", new Mock()) |
| 1447 | .setUp("m = mock.proxy_methods_object()", "b = m.with_args_and_kwargs('foo')") |
| 1448 | .testLookup("b", "with_args_and_kwargs(foo, args(), kwargs())"); |
| 1449 | } |
| 1450 | |
| 1451 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1452 | public void testStructAccessOfMethod() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1453 | new SkylarkTest().update("mock", new Mock()).testExpression("type(mock.function)", "function"); |
| 1454 | new SkylarkTest().update("mock", new Mock()).testExpression("mock.function()", "a"); |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | @Test |
| 1458 | public void testStructAccessTypo() throws Exception { |
| 1459 | new SkylarkTest() |
| 1460 | .update("mock", new MockClassObject()) |
| 1461 | .testIfExactError( |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1462 | "'MockClassObject' value has no field or method 'fild' (did you mean 'field'?)", |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 1463 | "mock.fild"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1466 | @Test |
Googler | 055d6c6 | 2018-05-22 13:21:04 -0700 | [diff] [blame] | 1467 | public void testStructAccessType_nonClassObject() throws Exception { |
| 1468 | new SkylarkTest() |
| 1469 | .update("mock", new Mock()) |
| 1470 | .testIfExactError( |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1471 | "'Mock' value has no field or method 'sturct_field' (did you mean 'struct_field'?)", |
Googler | 055d6c6 | 2018-05-22 13:21:04 -0700 | [diff] [blame] | 1472 | "v = mock.sturct_field"); |
| 1473 | } |
| 1474 | |
| 1475 | @Test |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 1476 | public void testJavaFunctionReturnsIllegalValue() throws Exception { |
| 1477 | update("mock", new Mock()); |
| 1478 | IllegalArgumentException e = |
| 1479 | assertThrows(IllegalArgumentException.class, () -> eval("mock.return_bad()")); |
| 1480 | assertThat(e) |
| 1481 | .hasMessageThat() |
| 1482 | .contains( |
| 1483 | "cannot expose internal type to Starlark: class" |
| 1484 | + " com.google.devtools.build.lib.syntax.SkylarkEvaluationTest$Bad"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1487 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1488 | public void testJavaFunctionReturnsNullFails() throws Exception { |
Benjamin Peterson | af53a11 | 2019-06-05 10:47:11 -0700 | [diff] [blame] | 1489 | update("mock", new Mock()); |
| 1490 | IllegalStateException e = |
| 1491 | assertThrows(IllegalStateException.class, () -> eval("mock.nullfunc_failing('abc', 1)")); |
| 1492 | assertThat(e).hasMessageThat().contains("method invocation returned None"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1495 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1496 | public void testClassObjectAccess() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1497 | new SkylarkTest() |
| 1498 | .update("mock", new MockClassObject()) |
| 1499 | .setUp("v = mock.field") |
| 1500 | .testLookup("v", "a"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1503 | @Test |
Laurent Le Brun | 092f13b | 2015-08-24 14:50:00 +0000 | [diff] [blame] | 1504 | public void testUnionSet() throws Exception { |
laurentlb | 63c7ea6 | 2019-04-25 08:45:21 -0700 | [diff] [blame] | 1505 | new SkylarkTest("--incompatible_depset_union=false") |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1506 | .testExpression("str(depset([1, 3]) | depset([1, 2]))", "depset([1, 2, 3])") |
| 1507 | .testExpression("str(depset([1, 2]) | [1, 3])", "depset([1, 2, 3])") |
Marwan Tammam | 66aa424 | 2019-07-24 06:56:08 -0700 | [diff] [blame] | 1508 | .testIfExactError("unsupported operand type(s) for |: 'int' and 'bool'", "2 | False"); |
Laurent Le Brun | 092f13b | 2015-08-24 14:50:00 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | @Test |
laurentlb | c9b6f4a | 2017-06-21 11:58:50 +0200 | [diff] [blame] | 1512 | public void testSetIsNotIterable() throws Exception { |
laurentlb | 9f9f5ec | 2019-11-15 10:37:49 -0800 | [diff] [blame] | 1513 | new SkylarkTest() |
Googler | 8ac2210 | 2019-11-26 19:07:38 -0800 | [diff] [blame] | 1514 | .testIfErrorContains("not iterable", "list(depset(['a', 'b']))") |
laurentlb | c9b6f4a | 2017-06-21 11:58:50 +0200 | [diff] [blame] | 1515 | .testIfErrorContains("not iterable", "max(depset([1, 2, 3]))") |
laurentlb | 0f5b720 | 2017-06-23 16:55:01 +0200 | [diff] [blame] | 1516 | .testIfErrorContains("not iterable", "1 in depset([1, 2, 3])") |
Googler | 8ac2210 | 2019-11-26 19:07:38 -0800 | [diff] [blame] | 1517 | .testIfErrorContains("not iterable", "sorted(depset(['a', 'b']))") |
| 1518 | .testIfErrorContains("not iterable", "tuple(depset(['a', 'b']))") |
laurentlb | c32efc8 | 2017-06-23 16:03:00 +0200 | [diff] [blame] | 1519 | .testIfErrorContains("not iterable", "[x for x in depset()]") |
| 1520 | .testIfErrorContains("not iterable", "len(depset(['a']))"); |
laurentlb | c9b6f4a | 2017-06-21 11:58:50 +0200 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | @Test |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 1524 | public void testFieldReturnsNestedSet() throws Exception { |
| 1525 | update("mock", new MockClassObject()); |
| 1526 | IllegalArgumentException e = |
| 1527 | assertThrows(IllegalArgumentException.class, () -> eval("mock.nset")); |
| 1528 | assertThat(e) |
| 1529 | .hasMessageThat() |
| 1530 | .contains( |
adonovan | b8eae2d | 2019-12-09 15:11:06 -0800 | [diff] [blame] | 1531 | "invalid Starlark value: class" |
Googler | 084b64b | 2019-11-19 14:41:30 -0800 | [diff] [blame] | 1532 | + " com.google.devtools.build.lib.collect.nestedset.NestedSet"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1535 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1536 | public void testJavaFunctionReturnsNone() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1537 | new SkylarkTest() |
| 1538 | .update("mock", new Mock()) |
| 1539 | .setUp("v = mock.nullfunc_working()") |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 1540 | .testLookup("v", Starlark.NONE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1543 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1544 | public void testVoidJavaFunctionReturnsNone() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1545 | new SkylarkTest() |
| 1546 | .update("mock", new Mock()) |
| 1547 | .setUp("v = mock.voidfunc()") |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 1548 | .testLookup("v", Starlark.NONE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1551 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1552 | public void testAugmentedAssignment() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1553 | new SkylarkTest().setUp("def f1(x):", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1554 | " x += 1", |
| 1555 | " return x", |
| 1556 | "", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1557 | "foo = f1(41)").testLookup("foo", 42); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1560 | @Test |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 1561 | public void testAugmentedAssignmentHasNoSideEffects() throws Exception { |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 1562 | // Check object position. |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1563 | new SkylarkTest() |
| 1564 | .setUp( |
| 1565 | "counter = [0]", |
| 1566 | "value = [1, 2]", |
| 1567 | "", |
| 1568 | "def f():", |
| 1569 | " counter[0] = counter[0] + 1", |
| 1570 | " return value", |
| 1571 | "", |
| 1572 | "f()[1] += 1") // `f()` should be called only once here |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1573 | .testLookup("counter", StarlarkList.of(thread.mutability(), 1)); |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 1574 | |
| 1575 | // Check key position. |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1576 | new SkylarkTest() |
| 1577 | .setUp( |
| 1578 | "counter = [0]", |
| 1579 | "value = [1, 2]", |
| 1580 | "", |
| 1581 | "def f():", |
| 1582 | " counter[0] = counter[0] + 1", |
| 1583 | " return 1", |
| 1584 | "", |
| 1585 | "value[f()] += 1") // `f()` should be called only once here |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1586 | .testLookup("counter", StarlarkList.of(thread.mutability(), 1)); |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | @Test |
Googler | b6f33cd | 2019-09-16 10:32:03 -0700 | [diff] [blame] | 1590 | public void testInvalidAugmentedAssignment_ListExpression() throws Exception { |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1591 | assertValidationError( |
laurentlb | e368449 | 2017-08-21 12:02:46 +0200 | [diff] [blame] | 1592 | "cannot perform augmented assignment on a list or tuple expression", |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1593 | // |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 1594 | "def f(a, b):", |
| 1595 | " [a, b] += []", |
| 1596 | "f(1, 2)"); |
| 1597 | } |
| 1598 | |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1599 | |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 1600 | @Test |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 1601 | public void testInvalidAugmentedAssignment_NotAnLValue() throws Exception { |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1602 | assertValidationError( |
| 1603 | "cannot assign to 'x + 1'", |
| 1604 | // |
| 1605 | "x + 1 += 2"); |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | @Test |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 1609 | public void testAssignmentEvaluationOrder() throws Exception { |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1610 | new SkylarkTest() |
| 1611 | .setUp( |
| 1612 | "ordinary = []", |
| 1613 | "augmented = []", |
| 1614 | "value = [1, 2]", |
| 1615 | "", |
| 1616 | "def f(record):", |
| 1617 | " record.append('f')", |
| 1618 | " return value", |
| 1619 | "", |
| 1620 | "def g(record):", |
| 1621 | " record.append('g')", |
| 1622 | " return value", |
| 1623 | "", |
| 1624 | "f(ordinary)[0] = g(ordinary)[1]", |
| 1625 | "f(augmented)[0] += g(augmented)[1]") |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1626 | .testLookup( |
| 1627 | "ordinary", StarlarkList.of(thread.mutability(), "g", "f")) // This order is consistent |
| 1628 | .testLookup("augmented", StarlarkList.of(thread.mutability(), "f", "g")); // with Python |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1632 | public void testDictComprehensions_IterationOrder() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1633 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1634 | " d = {x : x for x in ['c', 'a', 'b']}", |
| 1635 | " s = ''", |
| 1636 | " for a in d:", |
| 1637 | " s += a", |
| 1638 | " return s", |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 1639 | "s = foo()").testLookup("s", "cab"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1642 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1643 | public void testDotExpressionOnNonStructObject() throws Exception { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 1644 | new SkylarkTest() |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 1645 | .testIfExactError("'string' value has no field or method 'field'", "x = 'a'.field"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1648 | @Test |
vladmos | 25da19d | 2017-05-04 18:00:59 +0200 | [diff] [blame] | 1649 | public void testPlusEqualsOnListMutating() throws Exception { |
vladmos | c5301e9 | 2017-12-08 03:07:47 -0800 | [diff] [blame] | 1650 | new SkylarkTest() |
vladmos | 25da19d | 2017-05-04 18:00:59 +0200 | [diff] [blame] | 1651 | .setUp( |
| 1652 | "def func():", |
| 1653 | " l1 = [1, 2]", |
| 1654 | " l2 = l1", |
| 1655 | " l2 += [3, 4]", |
| 1656 | " return l1, l2", |
| 1657 | "lists = str(func())") |
| 1658 | .testLookup("lists", "([1, 2, 3, 4], [1, 2, 3, 4])"); |
| 1659 | |
| 1660 | // The same but with += after an IndexExpression |
vladmos | c5301e9 | 2017-12-08 03:07:47 -0800 | [diff] [blame] | 1661 | new SkylarkTest() |
vladmos | 25da19d | 2017-05-04 18:00:59 +0200 | [diff] [blame] | 1662 | .setUp( |
| 1663 | "def func():", |
| 1664 | " l = [1, 2]", |
| 1665 | " d = {0: l}", |
| 1666 | " d[0] += [3, 4]", |
| 1667 | " return l, d[0]", |
| 1668 | "lists = str(func())") |
| 1669 | .testLookup("lists", "([1, 2, 3, 4], [1, 2, 3, 4])"); |
| 1670 | } |
| 1671 | |
| 1672 | @Test |
| 1673 | public void testPlusEqualsOnTuple() throws Exception { |
vladmos | c5301e9 | 2017-12-08 03:07:47 -0800 | [diff] [blame] | 1674 | new SkylarkTest() |
vladmos | 25da19d | 2017-05-04 18:00:59 +0200 | [diff] [blame] | 1675 | .setUp( |
| 1676 | "def func():", |
| 1677 | " t1 = (1, 2)", |
| 1678 | " t2 = t1", |
| 1679 | " t2 += (3, 4)", |
| 1680 | " return t1, t2", |
| 1681 | "tuples = func()") |
Googler | cfd681f | 2019-11-11 07:24:02 -0800 | [diff] [blame] | 1682 | .testLookup("tuples", Tuple.of(Tuple.of(1, 2), Tuple.of(1, 2, 3, 4))); |
vladmos | 25da19d | 2017-05-04 18:00:59 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | @Test |
vladmos | ee0a8bb | 2017-05-04 19:26:48 +0200 | [diff] [blame] | 1686 | public void testPlusOnDictDeprecated() throws Exception { |
laurentlb | 3418cd5 | 2019-11-08 05:06:48 -0800 | [diff] [blame] | 1687 | new SkylarkTest() |
vladmos | ee0a8bb | 2017-05-04 19:26:48 +0200 | [diff] [blame] | 1688 | .testIfErrorContains( |
laurentlb | 3418cd5 | 2019-11-08 05:06:48 -0800 | [diff] [blame] | 1689 | "unsupported operand type(s) for +: 'dict' and 'dict'", "{1: 2} + {3: 4}"); |
| 1690 | new SkylarkTest() |
vladmos | ee0a8bb | 2017-05-04 19:26:48 +0200 | [diff] [blame] | 1691 | .testIfErrorContains( |
laurentlb | 3418cd5 | 2019-11-08 05:06:48 -0800 | [diff] [blame] | 1692 | "unsupported operand type(s) for +: 'dict' and 'dict'", |
vladmos | ee0a8bb | 2017-05-04 19:26:48 +0200 | [diff] [blame] | 1693 | "def func():", |
| 1694 | " d = {1: 2}", |
| 1695 | " d += {3: 4}", |
| 1696 | "func()"); |
| 1697 | } |
| 1698 | |
| 1699 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1700 | public void testDictAssignmentAsLValue() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1701 | new SkylarkTest().setUp("def func():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1702 | " d = {'a' : 1}", |
| 1703 | " d['b'] = 2", |
| 1704 | " return d", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1705 | "d = func()").testLookup("d", ImmutableMap.of("a", 1, "b", 2)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1708 | @Test |
Vladimir Moskva | 1077038 | 2016-08-23 15:04:54 +0000 | [diff] [blame] | 1709 | public void testNestedDictAssignmentAsLValue() throws Exception { |
| 1710 | new SkylarkTest().setUp("def func():", |
| 1711 | " d = {'a' : 1}", |
| 1712 | " e = {'d': d}", |
| 1713 | " e['d']['b'] = 2", |
| 1714 | " return e", |
| 1715 | "e = func()").testLookup("e", ImmutableMap.of("d", ImmutableMap.of("a", 1, "b", 2))); |
| 1716 | } |
| 1717 | |
| 1718 | @Test |
| 1719 | public void testListAssignmentAsLValue() throws Exception { |
| 1720 | new SkylarkTest().setUp("def func():", |
| 1721 | " a = [1, 2]", |
| 1722 | " a[1] = 3", |
| 1723 | " a[-2] = 4", |
| 1724 | " return a", |
| 1725 | "a = str(func())").testLookup("a", "[4, 3]"); |
| 1726 | } |
| 1727 | |
| 1728 | @Test |
| 1729 | public void testNestedListAssignmentAsLValue() throws Exception { |
| 1730 | new SkylarkTest().setUp("def func():", |
| 1731 | " d = [1, 2]", |
| 1732 | " e = [3, d]", |
| 1733 | " e[1][1] = 4", |
| 1734 | " return e", |
| 1735 | "e = str(func())").testLookup("e", "[3, [1, 4]]"); |
| 1736 | } |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 1737 | |
Vladimir Moskva | 1077038 | 2016-08-23 15:04:54 +0000 | [diff] [blame] | 1738 | @Test |
Laurent Le Brun | d640bd3 | 2016-01-07 13:58:43 +0000 | [diff] [blame] | 1739 | public void testDictTupleAssignmentAsLValue() throws Exception { |
| 1740 | new SkylarkTest().setUp("def func():", |
| 1741 | " d = {'a' : 1}", |
| 1742 | " d['b'], d['c'] = 2, 3", |
| 1743 | " return d", |
| 1744 | "d = func()").testLookup("d", ImmutableMap.of("a", 1, "b", 2, "c", 3)); |
| 1745 | } |
| 1746 | |
| 1747 | @Test |
| 1748 | public void testDictItemPlusEqual() throws Exception { |
| 1749 | new SkylarkTest().setUp("def func():", |
| 1750 | " d = {'a' : 2}", |
| 1751 | " d['a'] += 3", |
| 1752 | " return d", |
| 1753 | "d = func()").testLookup("d", ImmutableMap.of("a", 5)); |
| 1754 | } |
| 1755 | |
| 1756 | @Test |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 1757 | public void testDictAssignmentAsLValueSideEffects() throws Exception { |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 1758 | new SkylarkTest() |
| 1759 | .setUp("def func(d):", " d['b'] = 2", "d = {'a' : 1}", "func(d)") |
Googler | 9257870 | 2019-11-21 12:19:31 -0800 | [diff] [blame] | 1760 | .testLookup("d", Dict.of((Mutability) null, "a", 1, "b", 2)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1763 | @Test |
Francois-Rene Rideau | ef7a8a5 | 2016-01-29 17:37:48 +0000 | [diff] [blame] | 1764 | public void testAssignmentToListInDictSideEffect() throws Exception { |
Googler | 942e1c4 | 2019-11-12 13:11:44 -0800 | [diff] [blame] | 1765 | new SkylarkTest() |
| 1766 | .setUp("l = [1, 2]", "d = {0: l}", "d[0].append(3)") |
| 1767 | .testLookup("l", StarlarkList.of(null, 1, 2, 3)); |
Francois-Rene Rideau | ef7a8a5 | 2016-01-29 17:37:48 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1771 | public void testUserFunctionKeywordArgs() throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 1772 | new SkylarkTest().setUp("def foo(a, b, c):", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1773 | " return a + b + c", "s = foo(1, c=2, b=3)") |
| 1774 | .testLookup("s", 6); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1777 | @Test |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1778 | public void testFunctionCallOrdering() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1779 | new SkylarkTest().setUp("def func(): return foo() * 2", |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1780 | "def foo(): return 2", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1781 | "x = func()") |
| 1782 | .testLookup("x", 4); |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | @Test |
| 1786 | public void testFunctionCallBadOrdering() throws Exception { |
laurentlb | 3f4ee54 | 2018-09-11 05:41:26 -0700 | [diff] [blame] | 1787 | new SkylarkTest() |
| 1788 | .testIfErrorContains( |
laurentlb | 3a979f7 | 2018-11-20 07:25:11 -0800 | [diff] [blame] | 1789 | "global variable 'foo' is referenced before assignment.", |
laurentlb | 3f4ee54 | 2018-09-11 05:41:26 -0700 | [diff] [blame] | 1790 | "def func(): return foo() * 2", |
| 1791 | "x = func()", |
| 1792 | "def foo(): return 2"); |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | @Test |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1796 | public void testLocalVariableDefinedBelow() throws Exception { |
laurentlb | 3f4ee54 | 2018-09-11 05:41:26 -0700 | [diff] [blame] | 1797 | new SkylarkTest() |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1798 | .setUp( |
| 1799 | "def beforeEven(li):", // returns the value before the first even number |
| 1800 | " for i in li:", |
| 1801 | " if i % 2 == 0:", |
| 1802 | " return a", |
| 1803 | " else:", |
| 1804 | " a = i", |
| 1805 | "res = beforeEven([1, 3, 4, 5])") |
| 1806 | .testLookup("res", 3); |
| 1807 | } |
| 1808 | |
| 1809 | @Test |
| 1810 | public void testShadowisNotInitialized() throws Exception { |
laurentlb | 2b3ad18 | 2018-12-05 05:49:45 -0800 | [diff] [blame] | 1811 | new SkylarkTest() |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1812 | .testIfErrorContains( |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 1813 | /* error message */ "local variable 'gl' is referenced before assignment", |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1814 | "gl = 5", |
laurentlb | 3f4ee54 | 2018-09-11 05:41:26 -0700 | [diff] [blame] | 1815 | "def foo():", |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 1816 | " if False: gl = 2", |
| 1817 | " return gl", |
| 1818 | "res = foo()"); |
| 1819 | } |
| 1820 | |
| 1821 | @Test |
laurentlb | 3f4ee54 | 2018-09-11 05:41:26 -0700 | [diff] [blame] | 1822 | public void testShadowBuiltin() throws Exception { |
laurentlb | 2b3ad18 | 2018-12-05 05:49:45 -0800 | [diff] [blame] | 1823 | new SkylarkTest() |
laurentlb | f635062 | 2018-09-19 10:15:34 -0700 | [diff] [blame] | 1824 | .testIfErrorContains( |
| 1825 | "global variable 'len' is referenced before assignment", |
| 1826 | "x = len('abc')", |
| 1827 | "len = 2", |
| 1828 | "y = x + len"); |
| 1829 | } |
| 1830 | |
| 1831 | @Test |
brandjon | 65fd136 | 2017-10-22 19:49:35 +0200 | [diff] [blame] | 1832 | public void testFunctionCallRecursion() throws Exception { |
| 1833 | new SkylarkTest().testIfErrorContains("Recursion was detected when calling 'f' from 'g'", |
| 1834 | "def main():", |
| 1835 | " f(5)", |
| 1836 | "def f(n):", |
| 1837 | " if n > 0: g(n - 1)", |
| 1838 | "def g(n):", |
| 1839 | " if n > 0: f(n - 1)", |
| 1840 | "main()"); |
| 1841 | } |
| 1842 | |
| 1843 | @Test |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1844 | // TODO(adonovan): move to Validation tests. |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 1845 | public void testTypo() throws Exception { |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 1846 | assertValidationError( |
| 1847 | "name 'my_variable' is not defined (did you mean 'myVariable'?)", |
| 1848 | // |
| 1849 | "myVariable = 2", |
| 1850 | "x = my_variable + 1"); |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1854 | public void testNoneTrueFalseInSkylark() throws Exception { |
Googler | 641bdf7 | 2019-11-12 10:32:26 -0800 | [diff] [blame] | 1855 | new SkylarkTest() |
| 1856 | .setUp("a = None", "b = True", "c = False") |
| 1857 | .testLookup("a", Starlark.NONE) |
| 1858 | .testLookup("b", Boolean.TRUE) |
| 1859 | .testLookup("c", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1862 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1863 | public void testHasattrMethods() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1864 | new SkylarkTest() |
| 1865 | .update("mock", new Mock()) |
| 1866 | .setUp("a = hasattr(mock, 'struct_field')", "b = hasattr(mock, 'function')", |
| 1867 | "c = hasattr(mock, 'is_empty')", "d = hasattr('str', 'replace')", |
| 1868 | "e = hasattr(mock, 'other')\n") |
| 1869 | .testLookup("a", Boolean.TRUE) |
| 1870 | .testLookup("b", Boolean.TRUE) |
| 1871 | .testLookup("c", Boolean.TRUE) |
| 1872 | .testLookup("d", Boolean.TRUE) |
| 1873 | .testLookup("e", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1876 | @Test |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 1877 | public void testGetattrMethods() throws Exception { |
| 1878 | new SkylarkTest() |
| 1879 | .update("mock", new Mock()) |
Marwan Tammam | a7b2b11 | 2019-07-24 03:45:55 -0700 | [diff] [blame] | 1880 | .setUp( |
| 1881 | "a = str(getattr(mock, 'struct_field', 'no'))", |
| 1882 | "b = str(getattr(mock, 'function', 'no'))", |
| 1883 | "c = str(getattr(mock, 'is_empty', 'no'))", |
| 1884 | "d = str(getattr('str', 'replace', 'no'))", |
| 1885 | "e = str(getattr(mock, 'other', 'no'))\n") |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 1886 | .testLookup("a", "a") |
Marwan Tammam | a7b2b11 | 2019-07-24 03:45:55 -0700 | [diff] [blame] | 1887 | .testLookup("b", "<built-in function function>") |
| 1888 | .testLookup("c", "<built-in function is_empty>") |
| 1889 | .testLookup("d", "<built-in function replace>") |
cparsons | e70aafe | 2018-02-28 12:16:38 -0800 | [diff] [blame] | 1890 | .testLookup("e", "no"); |
| 1891 | } |
| 1892 | |
| 1893 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1894 | public void testListAnTupleConcatenationDoesNotWorkInSkylark() throws Exception { |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 1895 | new SkylarkTest().testIfExactError("unsupported operand type(s) for +: 'list' and 'tuple'", |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 1896 | "[1, 2] + (3, 4)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1899 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1900 | public void testCannotCreateMixedListInSkylark() throws Exception { |
Laurent Le Brun | e083a91 | 2015-08-10 15:13:34 +0000 | [diff] [blame] | 1901 | new SkylarkTest().testExactOrder("['a', 'b', 1, 2]", "a", "b", 1, 2); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1904 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1905 | public void testCannotConcatListInSkylarkWithDifferentGenericTypes() throws Exception { |
Laurent Le Brun | e083a91 | 2015-08-10 15:13:34 +0000 | [diff] [blame] | 1906 | new SkylarkTest().testExactOrder("[1, 2] + ['a', 'b']", 1, 2, "a", "b"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1909 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1910 | public void testConcatEmptyListWithNonEmptyWorks() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1911 | new SkylarkTest().testExactOrder("[] + ['a', 'b']", "a", "b"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1914 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1915 | public void testFormatStringWithTuple() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1916 | new SkylarkTest().setUp("v = '%s%s' % ('a', 1)").testLookup("v", "a1"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Francois-Rene Rideau | e8cfead | 2015-03-17 16:01:47 +0000 | [diff] [blame] | 1919 | @Test |
| 1920 | public void testSingletonTuple() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1921 | new SkylarkTest().testExactOrder("(1,)", 1); |
Francois-Rene Rideau | e8cfead | 2015-03-17 16:01:47 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1924 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1925 | public void testDirFindsClassObjectFields() throws Exception { |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 1926 | new SkylarkTest().update("mock", new MockClassObject()) |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1927 | .testExactOrder("dir(mock)", "field", "nset"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1930 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1931 | public void testDirFindsJavaObjectStructFieldsAndMethods() throws Exception { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1932 | new SkylarkTest() |
| 1933 | .update("mock", new Mock()) |
| 1934 | .testExactOrder( |
| 1935 | "dir(mock)", |
| 1936 | "function", |
Benjamin Peterson | bf1db78 | 2018-08-08 10:03:22 -0700 | [diff] [blame] | 1937 | "interrupted_struct_field", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1938 | "is_empty", |
cparsons | 4baafac | 2018-04-11 11:09:17 -0700 | [diff] [blame] | 1939 | "legacy_method", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1940 | "nullfunc_failing", |
| 1941 | "nullfunc_working", |
cparsons | 7520dcc | 2018-04-04 13:59:27 -0700 | [diff] [blame] | 1942 | "proxy_methods_object", |
Benjamin Peterson | af53a11 | 2019-06-05 10:47:11 -0700 | [diff] [blame] | 1943 | "raise_unchecked_exception", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1944 | "return_bad", |
| 1945 | "string", |
| 1946 | "string_list", |
cparsons | aaab11f | 2018-01-31 11:00:11 -0800 | [diff] [blame] | 1947 | "string_list_dict", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1948 | "struct_field", |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 1949 | "struct_field_callable", |
cparsons | 07460fc | 2018-06-20 10:41:48 -0700 | [diff] [blame] | 1950 | "struct_field_with_extra", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1951 | "value_of", |
| 1952 | "voidfunc", |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 1953 | "with_args_and_kwargs", |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1954 | "with_args_and_thread", |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 1955 | "with_extra", |
cparsons | 979195e | 2018-04-09 15:43:22 -0700 | [diff] [blame] | 1956 | "with_kwargs", |
cparsons | 5d446a7 | 2018-03-07 11:01:17 -0800 | [diff] [blame] | 1957 | "with_params", |
| 1958 | "with_params_and_extra"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1961 | @Test |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 1962 | public void testStrNativeInfo() throws Exception { |
| 1963 | new SkylarkTest() |
| 1964 | .update("mock", new NativeInfoMock()) |
| 1965 | .testEval( |
| 1966 | "str(mock)", |
| 1967 | "'struct(struct_field_callable = <built-in function foobar>, struct_field_none = None, " |
| 1968 | + "struct_field_string = \"a\")'"); |
| 1969 | } |
| 1970 | |
| 1971 | @Test |
| 1972 | public void testDirNativeInfo() throws Exception { |
| 1973 | new SkylarkTest() |
| 1974 | .update("mock", new NativeInfoMock()) |
| 1975 | .testEval( |
| 1976 | "dir(mock)", |
cparsons | 63c2555 | 2018-04-10 13:36:29 -0700 | [diff] [blame] | 1977 | "['callable_string', 'struct_field_callable', 'struct_field_none', " |
Googler | 59245ca | 2019-12-02 15:44:53 -0800 | [diff] [blame] | 1978 | + "'struct_field_string', 'to_json', 'to_proto']") |
| 1979 | .testExpression("str(mock.to_json)", "<built-in function to_json>") |
| 1980 | .testExpression("str(getattr(mock, 'to_json'))", "<built-in function to_json>"); |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1984 | public void testPrint() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1985 | // TODO(fwe): cannot be handled by current testing suite |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1986 | setFailFast(false); |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1987 | exec("print('hello')"); |
vladmos | a664a51 | 2017-08-10 20:30:17 +0200 | [diff] [blame] | 1988 | assertContainsDebug("hello"); |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1989 | exec("print('a', 'b')"); |
vladmos | a664a51 | 2017-08-10 20:30:17 +0200 | [diff] [blame] | 1990 | assertContainsDebug("a b"); |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 1991 | exec("print('a', 'b', sep='x')"); |
vladmos | a664a51 | 2017-08-10 20:30:17 +0200 | [diff] [blame] | 1992 | assertContainsDebug("axb"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1995 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1996 | public void testPrintBadKwargs() throws Exception { |
cparsons | 7ac265d | 2019-04-16 15:31:17 -0700 | [diff] [blame] | 1997 | new SkylarkTest() |
| 1998 | .testIfErrorContains( |
| 1999 | "unexpected keywords 'end', 'other', for call to function print(sep = \" \", *args)", |
| 2000 | "print(end='x', other='y')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2003 | // Override tests in EvaluationTest incompatible with Skylark |
| 2004 | |
| 2005 | @SuppressWarnings("unchecked") |
| 2006 | @Override |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 2007 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2008 | public void testConcatLists() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 2009 | new SkylarkTest().testExactOrder("[1,2] + [3,4]", 1, 2, 3, 4).testExactOrder("(1,2)", 1, 2) |
| 2010 | .testExactOrder("(1,2) + (3,4)", 1, 2, 3, 4); |
| 2011 | |
| 2012 | // TODO(fwe): cannot be handled by current testing suite |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2013 | // list |
| 2014 | Object x = eval("[1,2] + [3,4]"); |
| 2015 | assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2016 | |
| 2017 | // tuple |
| 2018 | x = eval("(1,2)"); |
| 2019 | assertThat((Iterable<Object>) x).containsExactly(1, 2).inOrder(); |
Googler | cfd681f | 2019-11-11 07:24:02 -0800 | [diff] [blame] | 2020 | assertThat(x).isInstanceOf(Tuple.class); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2021 | |
| 2022 | x = eval("(1,2) + (3,4)"); |
| 2023 | assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder(); |
Googler | cfd681f | 2019-11-11 07:24:02 -0800 | [diff] [blame] | 2024 | assertThat(x).isInstanceOf(Tuple.class); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2027 | @Override |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 2028 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2029 | public void testListConcatenation() throws Exception {} |
| 2030 | |
Florian Weikert | f07e544 | 2015-07-01 13:08:43 +0000 | [diff] [blame] | 2031 | @Override |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2032 | @Test |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2033 | public void testListComprehensionsMultipleVariablesFail() throws Exception { |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 2034 | new SkylarkTest() |
| 2035 | .testIfErrorContains( |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 2036 | "assignment length mismatch: left-hand side has length 3, but right-hand side " |
| 2037 | + "evaluates to value of length 2", |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 2038 | "def foo (): return [x + y for x, y, z in [(1, 2), (3, 4)]]", |
| 2039 | "foo()"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2040 | |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 2041 | new SkylarkTest() |
| 2042 | .testIfErrorContains( |
Googler | 8ac2210 | 2019-11-26 19:07:38 -0800 | [diff] [blame] | 2043 | "type 'int' is not iterable", "def bar (): return [x + y for x, y in (1, 2)]", "bar()"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2044 | |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 2045 | new SkylarkTest() |
| 2046 | .testIfErrorContains( |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 2047 | "assignment length mismatch: left-hand side has length 3, but right-hand side " |
| 2048 | + "evaluates to value of length 2", |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 2049 | "[x + y for x, y, z in [(1, 2), (3, 4)]]"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2050 | |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 2051 | new SkylarkTest() |
Googler | 8ac2210 | 2019-11-26 19:07:38 -0800 | [diff] [blame] | 2052 | .testIfErrorContains("type 'int' is not iterable", "[x2 + y2 for x2, y2 in (1, 2)]"); |
laurentlb | 2db5270 | 2017-07-05 14:19:22 -0400 | [diff] [blame] | 2053 | |
| 2054 | new SkylarkTest() |
| 2055 | // returns [2] in Python, it's an error in Skylark |
brandjon | 2b51f78 | 2017-07-25 21:05:04 +0200 | [diff] [blame] | 2056 | .testIfErrorContains("must have at least one item", "[2 for [] in [()]]"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | @Override |
| 2060 | @Test |
| 2061 | public void testNotCallInt() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 2062 | new SkylarkTest() |
| 2063 | .setUp("sum = 123456") |
| 2064 | .testLookup("sum", 123456) |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 2065 | .testIfExactError("'int' object is not callable", "sum(1, 2, 3, 4, 5, 6)") |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 2066 | .testExpression("sum", 123456); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 2067 | } |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 2068 | |
| 2069 | @Test |
| 2070 | public void testConditionalExpressionAtToplevel() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 2071 | new SkylarkTest().setUp("x = 1 if 2 else 3").testLookup("x", 1); |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | @Test |
| 2075 | public void testConditionalExpressionInFunction() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 2076 | new SkylarkTest() |
| 2077 | .setUp("def foo(a, b, c): return a+b if c else a-b\n") |
| 2078 | .testExpression("foo(23, 5, 0)", 18); |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 2079 | } |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2080 | |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2081 | // This class extends NativeInfo (which provides @SkylarkCallable-annotated fields) |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 2082 | // with additional fields from a map. The only production code that currently |
| 2083 | // does that is ToolchainInfo and its subclasses. |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2084 | @SkylarkModule(name = "SkylarkClassObjectWithSkylarkCallables", doc = "") |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2085 | private static final class SkylarkClassObjectWithSkylarkCallables extends NativeInfo { |
| 2086 | |
| 2087 | static final NativeProvider<SkylarkClassObjectWithSkylarkCallables> CONSTRUCTOR = |
dslomov | de965ac | 2017-07-31 21:07:51 +0200 | [diff] [blame] | 2088 | new NativeProvider<SkylarkClassObjectWithSkylarkCallables>( |
| 2089 | SkylarkClassObjectWithSkylarkCallables.class, "struct_with_skylark_callables") {}; |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2090 | |
adonovan | f3a344e8 | 2019-12-11 11:10:11 -0800 | [diff] [blame] | 2091 | // A function that returns "fromValues". |
| 2092 | Object returnFromValues = |
| 2093 | new StarlarkCallable() { |
| 2094 | @Override |
| 2095 | public String getName() { |
| 2096 | return "returnFromValues"; |
| 2097 | } |
| 2098 | |
| 2099 | @Override |
adonovan | 770606a | 2019-12-11 11:52:31 -0800 | [diff] [blame^] | 2100 | public Object callImpl( |
| 2101 | StarlarkThread thread, |
adonovan | f3a344e8 | 2019-12-11 11:10:11 -0800 | [diff] [blame] | 2102 | FuncallExpression call, |
adonovan | 770606a | 2019-12-11 11:52:31 -0800 | [diff] [blame^] | 2103 | List<Object> args, |
| 2104 | Map<String, Object> kwargs) { |
adonovan | f3a344e8 | 2019-12-11 11:10:11 -0800 | [diff] [blame] | 2105 | return "fromValues"; |
| 2106 | } |
| 2107 | }; |
| 2108 | |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2109 | final Map<String, Object> fields = |
| 2110 | ImmutableMap.of( |
| 2111 | "values_only_field", |
| 2112 | "fromValues", |
| 2113 | "values_only_method", |
adonovan | f3a344e8 | 2019-12-11 11:10:11 -0800 | [diff] [blame] | 2114 | returnFromValues, |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2115 | "collision_field", |
| 2116 | "fromValues", |
| 2117 | "collision_method", |
adonovan | f3a344e8 | 2019-12-11 11:10:11 -0800 | [diff] [blame] | 2118 | returnFromValues); |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2119 | |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2120 | SkylarkClassObjectWithSkylarkCallables() { |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2121 | super(CONSTRUCTOR, Location.BUILTIN); |
| 2122 | } |
Googler | 9eca6d5 | 2019-11-13 06:26:47 -0800 | [diff] [blame] | 2123 | |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2124 | @Override |
| 2125 | public Object getValue(String name) throws EvalException { |
| 2126 | Object x = fields.get(name); |
| 2127 | return x != null ? x : super.getValue(name); |
| 2128 | } |
Googler | 9eca6d5 | 2019-11-13 06:26:47 -0800 | [diff] [blame] | 2129 | |
adonovan | 7202d10 | 2019-12-09 13:58:57 -0800 | [diff] [blame] | 2130 | @Override |
| 2131 | public ImmutableCollection<String> getFieldNames() { |
| 2132 | return ImmutableSet.<String>builder() |
| 2133 | .addAll(fields.keySet()) |
| 2134 | .addAll(super.getFieldNames()) |
| 2135 | .build(); |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 2138 | @SkylarkCallable(name = "callable_only_field", documented = false, structField = true) |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2139 | public String getCallableOnlyField() { |
| 2140 | return "fromSkylarkCallable"; |
| 2141 | } |
| 2142 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 2143 | @SkylarkCallable(name = "callable_only_method", documented = false, structField = false) |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2144 | public String getCallableOnlyMethod() { |
| 2145 | return "fromSkylarkCallable"; |
| 2146 | } |
| 2147 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 2148 | @SkylarkCallable(name = "collision_field", documented = false, structField = true) |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2149 | public String getCollisionField() { |
| 2150 | return "fromSkylarkCallable"; |
| 2151 | } |
| 2152 | |
cparsons | 7f475d7 | 2018-03-30 13:54:46 -0700 | [diff] [blame] | 2153 | @SkylarkCallable(name = "collision_method", documented = false, structField = false) |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2154 | public String getCollisionMethod() { |
| 2155 | return "fromSkylarkCallable"; |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | @Test |
| 2160 | public void testStructFieldDefinedOnlyInValues() throws Exception { |
| 2161 | new SkylarkTest() |
| 2162 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2163 | .setUp("v = val.values_only_field") |
| 2164 | .testLookup("v", "fromValues"); |
| 2165 | } |
| 2166 | |
| 2167 | @Test |
| 2168 | public void testStructMethodDefinedOnlyInValues() throws Exception { |
| 2169 | new SkylarkTest() |
| 2170 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2171 | .setUp("v = val.values_only_method()") |
| 2172 | .testLookup("v", "fromValues"); |
| 2173 | } |
| 2174 | |
| 2175 | @Test |
| 2176 | public void testStructFieldDefinedOnlyInSkylarkCallable() throws Exception { |
| 2177 | new SkylarkTest() |
| 2178 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2179 | .setUp("v = val.callable_only_field") |
| 2180 | .testLookup("v", "fromSkylarkCallable"); |
| 2181 | } |
| 2182 | |
| 2183 | @Test |
| 2184 | public void testStructMethodDefinedOnlyInSkylarkCallable() throws Exception { |
| 2185 | new SkylarkTest() |
| 2186 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2187 | .setUp("v = val.callable_only_method()") |
| 2188 | .testLookup("v", "fromSkylarkCallable"); |
| 2189 | } |
| 2190 | |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 2191 | |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2192 | @Test |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2193 | public void testStructMethodDefinedInValuesAndSkylarkCallable() throws Exception { |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 2194 | // This test exercises the resolution of ambiguity between @SkylarkCallable-annotated |
| 2195 | // fields and those reported by ClassObject.getValue. |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2196 | new SkylarkTest() |
| 2197 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2198 | .setUp("v = val.collision_method()") |
cparsons | da39293 | 2018-11-02 16:04:13 -0700 | [diff] [blame] | 2199 | .testLookup("v", "fromSkylarkCallable"); |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | @Test |
| 2203 | public void testStructFieldNotDefined() throws Exception { |
| 2204 | new SkylarkTest() |
| 2205 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2206 | .testIfExactError( |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 2207 | // TODO(bazel-team): This should probably list callable_only_method as well. |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 2208 | "'struct_with_skylark_callables' value has no field or method 'nonexistent_field'\n" |
cparsons | 6c1c066 | 2018-02-05 02:01:28 -0800 | [diff] [blame] | 2209 | + "Available attributes: callable_only_field, collision_field, collision_method, " |
| 2210 | + "values_only_field, values_only_method", |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2211 | "v = val.nonexistent_field"); |
| 2212 | } |
| 2213 | |
| 2214 | @Test |
| 2215 | public void testStructMethodNotDefined() throws Exception { |
| 2216 | new SkylarkTest() |
| 2217 | .update("val", new SkylarkClassObjectWithSkylarkCallables()) |
| 2218 | .testIfExactError( |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 2219 | "'struct_with_skylark_callables' value has no field or method 'nonexistent_method'\n" |
| 2220 | + "Available attributes: callable_only_field, collision_field, collision_method, " |
| 2221 | + "values_only_field, values_only_method", |
cparsons | 71b4dcc | 2018-10-24 13:46:37 -0700 | [diff] [blame] | 2222 | "v = val.nonexistent_method()"); |
Michael Staib | 79bd2c2 | 2017-03-22 14:37:03 +0000 | [diff] [blame] | 2223 | } |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 2224 | |
| 2225 | @Test |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 2226 | public void testListComprehensionsShadowGlobalVariable() throws Exception { |
Googler | 1a1fca2 | 2019-10-14 09:31:22 -0700 | [diff] [blame] | 2227 | exec( |
| 2228 | "a = 18", // |
| 2229 | "def foo():", |
| 2230 | " b = [a for a in range(3)]", |
| 2231 | " return a", |
| 2232 | "x = foo()"); |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 2233 | assertThat(lookup("x")).isEqualTo(18); |
| 2234 | } |
| 2235 | |
| 2236 | @Test |
cparsons | 645a35e | 2018-10-01 13:03:23 -0700 | [diff] [blame] | 2237 | public void testAnalysisFailureInfo() throws Exception { |
| 2238 | AnalysisFailure cause = new AnalysisFailure(Label.create("test", "test"), "ErrorMessage"); |
| 2239 | |
cparsons | e8d450c | 2018-10-04 16:01:53 -0700 | [diff] [blame] | 2240 | AnalysisFailureInfo info = AnalysisFailureInfo.forAnalysisFailures(ImmutableList.of(cause)); |
cparsons | 645a35e | 2018-10-01 13:03:23 -0700 | [diff] [blame] | 2241 | |
cparsons | 8ec3639 | 2018-11-19 11:16:26 -0800 | [diff] [blame] | 2242 | new SkylarkTest() |
cparsons | 645a35e | 2018-10-01 13:03:23 -0700 | [diff] [blame] | 2243 | .update("val", info) |
| 2244 | .setUp( |
| 2245 | "causes = val.causes", |
| 2246 | "label = causes.to_list()[0].label", |
| 2247 | "message = causes.to_list()[0].message") |
| 2248 | .testLookup("label", Label.create("test", "test")) |
| 2249 | .testLookup("message", "ErrorMessage"); |
cparsons | 645a35e | 2018-10-01 13:03:23 -0700 | [diff] [blame] | 2250 | } |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2251 | |
| 2252 | @Test |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 2253 | // TODO(adonovan): move to Validation tests. |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2254 | public void testExperimentalFlagGuardedValue() throws Exception { |
| 2255 | // This test uses an arbitrary experimental flag to verify this functionality. If this |
| 2256 | // experimental flag were to go away, this test may be updated to use any experimental flag. |
| 2257 | // The flag itself is unimportant to the test. |
cparsons | 8ec3639 | 2018-11-19 11:16:26 -0800 | [diff] [blame] | 2258 | FlagGuardedValue val = |
| 2259 | FlagGuardedValue.onlyWhenExperimentalFlagIsTrue( |
| 2260 | FlagIdentifier.EXPERIMENTAL_BUILD_SETTING_API, "foo"); |
| 2261 | String errorMessage = |
| 2262 | "GlobalSymbol is experimental and thus unavailable with the current " |
| 2263 | + "flags. It may be enabled by setting --experimental_build_setting_api"; |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2264 | |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 2265 | |
cparsons | 8ec3639 | 2018-11-19 11:16:26 -0800 | [diff] [blame] | 2266 | new SkylarkTest(ImmutableMap.of("GlobalSymbol", val), "--experimental_build_setting_api=true") |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2267 | .setUp("var = GlobalSymbol") |
| 2268 | .testLookup("var", "foo"); |
| 2269 | |
cparsons | 8ec3639 | 2018-11-19 11:16:26 -0800 | [diff] [blame] | 2270 | new SkylarkTest(ImmutableMap.of("GlobalSymbol", val), "--experimental_build_setting_api=false") |
| 2271 | .testIfErrorContains(errorMessage, "var = GlobalSymbol"); |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2272 | |
cparsons | 8ec3639 | 2018-11-19 11:16:26 -0800 | [diff] [blame] | 2273 | new SkylarkTest(ImmutableMap.of("GlobalSymbol", val), "--experimental_build_setting_api=false") |
| 2274 | .testIfErrorContains(errorMessage, "def my_function():", " var = GlobalSymbol"); |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2275 | |
cparsons | 8ec3639 | 2018-11-19 11:16:26 -0800 | [diff] [blame] | 2276 | new SkylarkTest(ImmutableMap.of("GlobalSymbol", val), "--experimental_build_setting_api=false") |
| 2277 | .setUp("GlobalSymbol = 'other'", "var = GlobalSymbol") |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 2278 | .testLookup("var", "other"); |
| 2279 | } |
| 2280 | |
| 2281 | @Test |
| 2282 | public void testIncompatibleFlagGuardedValue() throws Exception { |
| 2283 | // This test uses an arbitrary incompatible flag to verify this functionality. If this |
| 2284 | // incompatible flag were to go away, this test may be updated to use any incompatible flag. |
| 2285 | // The flag itself is unimportant to the test. |
| 2286 | FlagGuardedValue val = FlagGuardedValue.onlyWhenIncompatibleFlagIsFalse( |
| 2287 | FlagIdentifier.INCOMPATIBLE_NO_TARGET_OUTPUT_GROUP, |
| 2288 | "foo"); |
| 2289 | String errorMessage = "GlobalSymbol is deprecated and will be removed soon. It may be " |
| 2290 | + "temporarily re-enabled by setting --incompatible_no_target_output_group=false"; |
| 2291 | |
| 2292 | new SkylarkTest( |
| 2293 | ImmutableMap.of("GlobalSymbol", val), |
| 2294 | "--incompatible_no_target_output_group=false") |
| 2295 | .setUp("var = GlobalSymbol") |
| 2296 | .testLookup("var", "foo"); |
| 2297 | |
| 2298 | new SkylarkTest( |
| 2299 | ImmutableMap.of("GlobalSymbol", val), |
| 2300 | "--incompatible_no_target_output_group=true") |
| 2301 | .testIfErrorContains(errorMessage, |
| 2302 | "var = GlobalSymbol"); |
| 2303 | |
| 2304 | new SkylarkTest( |
| 2305 | ImmutableMap.of("GlobalSymbol", val), |
| 2306 | "--incompatible_no_target_output_group=true") |
| 2307 | .testIfErrorContains(errorMessage, |
| 2308 | "def my_function():", |
| 2309 | " var = GlobalSymbol"); |
| 2310 | |
| 2311 | new SkylarkTest( |
| 2312 | ImmutableMap.of("GlobalSymbol", val), |
| 2313 | "--incompatible_no_target_output_group=true") |
| 2314 | .setUp("GlobalSymbol = 'other'", |
| 2315 | "var = GlobalSymbol") |
| 2316 | .testLookup("var", "other"); |
| 2317 | } |
adonovan | 4e2b495 | 2019-12-10 12:19:20 -0800 | [diff] [blame] | 2318 | |
| 2319 | @Test |
| 2320 | public void testFunctionEvaluatedBeforeArguments() throws Exception { |
| 2321 | // ''.nonesuch must be evaluated (and fail) before f(). |
| 2322 | new SkylarkTest() |
| 2323 | .testIfErrorContains( |
| 2324 | "'string' value has no field or method 'nonesuch'", |
| 2325 | "def f(): x = 1//0", |
| 2326 | "''.nonesuch(f())"); |
| 2327 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2328 | } |