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