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; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 17 | import static org.junit.Assert.assertEquals; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.assertTrue; |
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; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.Artifact; |
| 24 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
| 25 | import com.google.devtools.build.lib.analysis.FileConfiguredTarget; |
| 26 | import com.google.devtools.build.lib.analysis.RuleConfiguredTarget; |
| 27 | import com.google.devtools.build.lib.analysis.TransitiveInfoCollection; |
| 28 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skylarkinterface.Param; |
John Field | 585d1a0 | 2015-12-16 16:03:52 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable; |
| 31 | import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; |
Francois-Rene Rideau | ef7a8a5 | 2016-01-29 17:37:48 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.testutil.TestMode; |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 35 | import org.junit.Before; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 36 | import org.junit.Test; |
| 37 | import org.junit.runner.RunWith; |
| 38 | import org.junit.runners.JUnit4; |
| 39 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Evaluation tests with Skylark Environment. |
| 42 | */ |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 43 | @RunWith(JUnit4.class) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 44 | public class SkylarkEvaluationTest extends EvaluationTest { |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 45 | |
Han-Wen Nienhuys | 33ce211 | 2015-09-25 14:25:38 +0000 | [diff] [blame] | 46 | @Before |
Florian Weikert | b4c5904 | 2015-12-01 10:47:18 +0000 | [diff] [blame] | 47 | public final void setup() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 48 | setMode(TestMode.SKYLARK); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Creates an instance of {@code SkylarkTest} in order to run the tests from the base class in a |
| 53 | * Skylark context |
| 54 | */ |
| 55 | @Override |
| 56 | protected ModalTestCase newTest() { |
| 57 | return new SkylarkTest(); |
| 58 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 59 | |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 60 | static class Bad { |
| 61 | Bad () { |
| 62 | } |
| 63 | } |
| 64 | |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 65 | @SkylarkSignature(name = "foobar", returnType = String.class, documented = false) |
| 66 | static BuiltinFunction foobar = new BuiltinFunction("foobar") { |
| 67 | public String invoke() throws EvalException { |
| 68 | return "foobar"; |
| 69 | } |
| 70 | }; |
| 71 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 72 | @SkylarkModule(name = "Mock", doc = "") |
| 73 | static class Mock { |
| 74 | @SkylarkCallable(doc = "") |
| 75 | public static Integer valueOf(String str) { |
| 76 | return Integer.valueOf(str); |
| 77 | } |
| 78 | @SkylarkCallable(doc = "") |
| 79 | public Boolean isEmpty(String str) { |
| 80 | return str.isEmpty(); |
| 81 | } |
| 82 | public void value() {} |
| 83 | @SkylarkCallable(doc = "") |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 84 | public Bad returnBad() { |
| 85 | return new Bad(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 86 | } |
| 87 | @SkylarkCallable(name = "struct_field", doc = "", structField = true) |
| 88 | public String structField() { |
| 89 | return "a"; |
| 90 | } |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 91 | @SkylarkCallable(name = "struct_field_callable", doc = "", structField = true) |
| 92 | public BuiltinFunction structFieldCallable() { |
| 93 | return foobar; |
| 94 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 95 | @SkylarkCallable(name = "function", doc = "", structField = false) |
| 96 | public String function() { |
| 97 | return "a"; |
| 98 | } |
| 99 | @SuppressWarnings("unused") |
| 100 | @SkylarkCallable(name = "nullfunc_failing", doc = "", allowReturnNones = false) |
| 101 | public Object nullfuncFailing(String p1, Integer p2) { |
| 102 | return null; |
| 103 | } |
| 104 | @SkylarkCallable(name = "nullfunc_working", doc = "", allowReturnNones = true) |
| 105 | public Object nullfuncWorking() { |
| 106 | return null; |
| 107 | } |
| 108 | @SkylarkCallable(name = "voidfunc", doc = "") |
| 109 | public void voidfunc() {} |
| 110 | @SkylarkCallable(name = "string_list", doc = "") |
| 111 | public ImmutableList<String> stringList() { |
| 112 | return ImmutableList.<String>of("a", "b"); |
| 113 | } |
| 114 | @SkylarkCallable(name = "string", doc = "") |
| 115 | public String string() { |
| 116 | return "a"; |
| 117 | } |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 118 | |
| 119 | @SkylarkCallable( |
| 120 | name = "with_params", |
| 121 | doc = "", |
| 122 | mandatoryPositionals = 1, |
| 123 | parameters = { |
| 124 | @Param(name = "pos2", defaultValue = "False", type = Boolean.class), |
| 125 | @Param( |
| 126 | name = "posOrNamed", |
| 127 | defaultValue = "False", |
| 128 | type = Boolean.class, |
| 129 | positional = true, |
| 130 | named = true |
| 131 | ), |
| 132 | @Param(name = "named", type = Boolean.class, positional = false, named = true), |
| 133 | @Param( |
| 134 | name = "optionalNamed", |
| 135 | type = Boolean.class, |
| 136 | defaultValue = "False", |
| 137 | positional = false, |
| 138 | named = true |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 139 | ), |
| 140 | @Param( |
| 141 | name = "nonNoneable", |
| 142 | type = Object.class, |
| 143 | defaultValue = "\"a\"", |
| 144 | positional = false, |
| 145 | named = true |
| 146 | ), |
| 147 | @Param( |
| 148 | name = "noneable", |
Googler | e5da53c | 2016-09-21 12:34:44 +0000 | [diff] [blame] | 149 | type = Integer.class, |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 150 | defaultValue = "None", |
| 151 | noneable = true, |
| 152 | positional = false, |
| 153 | named = true |
| 154 | ), |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 155 | } |
| 156 | ) |
| 157 | public String withParams( |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 158 | Integer pos1, |
| 159 | boolean pos2, |
| 160 | boolean posOrNamed, |
| 161 | boolean named, |
| 162 | boolean optionalNamed, |
| 163 | Object nonNoneable, |
| 164 | Object noneable) { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 165 | return "with_params(" |
| 166 | + pos1 |
| 167 | + ", " |
| 168 | + pos2 |
| 169 | + ", " |
| 170 | + posOrNamed |
| 171 | + ", " |
| 172 | + named |
| 173 | + ", " |
| 174 | + optionalNamed |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 175 | + ", " |
| 176 | + nonNoneable.toString() |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 177 | + ")"; |
| 178 | } |
| 179 | |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 180 | @Override |
| 181 | public String toString() { |
| 182 | return "<mock>"; |
| 183 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @SkylarkModule(name = "MockInterface", doc = "") |
| 187 | static interface MockInterface { |
| 188 | @SkylarkCallable(doc = "") |
| 189 | public Boolean isEmptyInterface(String str); |
| 190 | } |
| 191 | |
| 192 | static final class MockSubClass extends Mock implements MockInterface { |
| 193 | @Override |
| 194 | public Boolean isEmpty(String str) { |
| 195 | return str.isEmpty(); |
| 196 | } |
| 197 | @Override |
| 198 | public Boolean isEmptyInterface(String str) { |
| 199 | return str.isEmpty(); |
| 200 | } |
| 201 | @SkylarkCallable(doc = "") |
| 202 | public Boolean isEmptyClassNotAnnotated(String str) { |
| 203 | return str.isEmpty(); |
| 204 | } |
| 205 | } |
| 206 | |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 207 | @SkylarkModule(name = "MockClassObject", doc = "", documented = false) |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 208 | static final class MockClassObject implements ClassObject { |
| 209 | @Override |
| 210 | public Object getValue(String name) { |
| 211 | switch (name) { |
| 212 | case "field": return "a"; |
| 213 | case "nset": return NestedSetBuilder.stableOrder().build(); |
| 214 | } |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 215 | return null; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public ImmutableCollection<String> getKeys() { |
| 220 | return ImmutableList.of("field", "nset"); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public String errorMessage(String name) { |
| 225 | return null; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @SkylarkModule(name = "MockMultipleMethodClass", doc = "") |
| 230 | static final class MockMultipleMethodClass { |
| 231 | @SuppressWarnings("unused") |
| 232 | @SkylarkCallable(doc = "") |
| 233 | public void method(Object o) {} |
| 234 | @SuppressWarnings("unused") |
| 235 | @SkylarkCallable(doc = "") |
| 236 | public void method(String i) {} |
| 237 | } |
| 238 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 239 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 240 | public void testSimpleIf() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 241 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 242 | " a = 0", |
| 243 | " x = 0", |
| 244 | " if x: a = 5", |
| 245 | " return a", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 246 | "a = foo()").testLookup("a", 0); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 249 | @Test |
Laurent Le Brun | 0942ee9 | 2015-03-17 20:22:16 +0000 | [diff] [blame] | 250 | public void testIfPass() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 251 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 252 | " a = 1", |
| 253 | " x = True", |
| 254 | " if x: pass", |
| 255 | " return a", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 256 | "a = foo()").testLookup("a", 1); |
Laurent Le Brun | 0942ee9 | 2015-03-17 20:22:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 260 | public void testNestedIf() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 261 | executeNestedIf(0, 0, 0); |
| 262 | executeNestedIf(1, 0, 3); |
| 263 | executeNestedIf(1, 1, 5); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 266 | private void executeNestedIf(int x, int y, int expected) throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 267 | String fun = String.format("foo%s%s", x, y); |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 268 | new SkylarkTest().setUp("def " + fun + "():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 269 | " x = " + x, |
| 270 | " y = " + y, |
| 271 | " a = 0", |
| 272 | " b = 0", |
| 273 | " if x:", |
| 274 | " if y:", |
| 275 | " a = 2", |
| 276 | " b = 3", |
| 277 | " return a + b", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 278 | "x = " + fun + "()").testLookup("x", expected); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 281 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 282 | public void testIfElse() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 283 | executeIfElse("foo", "something", 2); |
| 284 | executeIfElse("bar", "", 3); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 287 | private void executeIfElse(String fun, String y, int expected) throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 288 | new SkylarkTest().setUp("def " + fun + "():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 289 | " y = '" + y + "'", |
| 290 | " x = 5", |
| 291 | " if x:", |
| 292 | " if y: a = 2", |
| 293 | " else: a = 3", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 294 | " return a", |
| 295 | "z = " + fun + "()").testLookup("z", expected); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 298 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 299 | public void testIfElifElse_IfExecutes() throws Exception { |
| 300 | execIfElifElse(1, 0, 1); |
| 301 | } |
| 302 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 303 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 304 | public void testIfElifElse_ElifExecutes() throws Exception { |
| 305 | execIfElifElse(0, 1, 2); |
| 306 | } |
| 307 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 308 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 309 | public void testIfElifElse_ElseExecutes() throws Exception { |
| 310 | execIfElifElse(0, 0, 3); |
| 311 | } |
| 312 | |
| 313 | private void execIfElifElse(int x, int y, int v) throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 314 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 315 | " x = " + x + "", |
| 316 | " y = " + y + "", |
| 317 | " if x:", |
| 318 | " return 1", |
| 319 | " elif y:", |
| 320 | " return 2", |
| 321 | " else:", |
| 322 | " return 3", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 323 | "v = foo()").testLookup("v", v); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 326 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 327 | public void testForOnList() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 328 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 329 | " s = ''", |
| 330 | " for i in ['hello', ' ', 'world']:", |
| 331 | " s = s + i", |
| 332 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 333 | "s = foo()").testLookup("s", "hello world"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 336 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 337 | public void testForOnString() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 338 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 339 | " s = []", |
| 340 | " for i in 'abc':", |
| 341 | " s = s + [i]", |
| 342 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 343 | "s = foo()").testExactOrder("s", "a", "b", "c"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 346 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 347 | public void testForAssignmentList() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 348 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 349 | " d = ['a', 'b', 'c']", |
| 350 | " s = ''", |
| 351 | " for i in d:", |
| 352 | " s = s + i", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 353 | " d = ['d', 'e', 'f']", // check that we use the old list |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 354 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 355 | "s = foo()").testLookup("s", "abc"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 358 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 359 | public void testForAssignmentDict() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 360 | new SkylarkTest().setUp("def func():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 361 | " d = {'a' : 1, 'b' : 2, 'c' : 3}", |
| 362 | " s = ''", |
| 363 | " for i in d:", |
| 364 | " s = s + i", |
| 365 | " d = {'d' : 1, 'e' : 2, 'f' : 3}", |
| 366 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 367 | "s = func()").testLookup("s", "abc"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 370 | @Test |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 371 | public void testForUpdateList() throws Exception { |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 372 | new SkylarkTest().setUp("def foo():", |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 373 | " xs = [1, 2, 3]", |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 374 | " for x in xs:", |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 375 | " if x == 1:", |
| 376 | " xs.append(10)" |
| 377 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Test |
| 381 | public void testForUpdateDict() throws Exception { |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 382 | new SkylarkTest().setUp("def foo():", |
| 383 | " d = {'a': 1, 'b': 2, 'c': 3}", |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 384 | " for k in d:", |
Jon Brandvein | 65e3ae6 | 2016-09-27 19:57:40 +0000 | [diff] [blame] | 385 | " d[k] *= 2" |
| 386 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
| 387 | } |
| 388 | |
| 389 | @Test |
| 390 | public void testForUnlockedAfterBreak() throws Exception { |
| 391 | new SkylarkTest().setUp("def foo():", |
| 392 | " xs = [1, 2]", |
| 393 | " for x in xs:", |
| 394 | " break", |
| 395 | " xs.append(3)", |
| 396 | " return xs" |
| 397 | ).testEval("foo()", "[1, 2, 3]"); |
| 398 | } |
| 399 | |
| 400 | @Test |
| 401 | public void testForNestedOnSameListStillLocked() throws Exception { |
| 402 | new SkylarkTest().setUp("def foo():", |
| 403 | " xs = [1, 2]", |
| 404 | " ys = []", |
| 405 | " for x1 in xs:", |
| 406 | " for x2 in xs:", |
| 407 | " ys.append(x1 * x2)", |
| 408 | " xs.append(4)", |
| 409 | " return ys" |
| 410 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
| 411 | } |
| 412 | |
| 413 | @Test |
| 414 | public void testForNestedOnSameListErrorMessage() throws Exception { |
| 415 | new SkylarkTest().setUp("def foo():", |
| 416 | " xs = [1, 2]", |
| 417 | " ys = []", |
| 418 | " for x1 in xs:", |
| 419 | " for x2 in xs:", |
| 420 | " ys.append(x1 * x2)", |
| 421 | " xs.append(4)", |
| 422 | " return ys" |
| 423 | // No file name in message, due to how test is set up. |
Carmi Grushko | 46bf88c | 2017-02-20 22:37:15 +0000 | [diff] [blame] | 424 | ).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] | 425 | } |
| 426 | |
| 427 | @Test |
| 428 | public void testForNestedOnSameListUnlockedAtEnd() throws Exception { |
| 429 | new SkylarkTest().setUp("def foo():", |
| 430 | " xs = [1, 2]", |
| 431 | " ys = []", |
| 432 | " for x1 in xs:", |
| 433 | " for x2 in xs:", |
| 434 | " ys.append(x1 * x2)", |
| 435 | " xs.append(4)", |
| 436 | " return ys" |
| 437 | ).testEval("foo()", "[1, 2, 2, 4]"); |
| 438 | } |
| 439 | |
| 440 | @Test |
| 441 | public void testForNestedWithListCompGood() throws Exception { |
| 442 | new SkylarkTest().setUp("def foo():", |
| 443 | " xs = [1, 2]", |
| 444 | " ys = []", |
| 445 | " for x in xs:", |
| 446 | " zs = [None for x in xs for y in (ys.append(x) or ys)]", |
| 447 | " return ys" |
| 448 | ).testEval("foo()", "[1, 2, 1, 2]"); |
| 449 | } |
| 450 | @Test |
| 451 | public void testForNestedWithListCompBad() throws Exception { |
| 452 | new SkylarkTest().setUp("def foo():", |
| 453 | " xs = [1, 2, 3]", |
| 454 | " ys = []", |
| 455 | " for x in xs:", |
| 456 | " zs = [None for x in xs for y in (xs.append(x) or ys)]", |
| 457 | " return ys" |
| 458 | ).testIfErrorContains("trying to mutate a locked object", "foo()"); |
Jon Brandvein | 15775b2 | 2016-07-25 15:13:44 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @Test |
| 462 | public void testForDeepUpdate() throws Exception { |
| 463 | // Check that indirectly reachable values can still be manipulated as normal. |
| 464 | new SkylarkTest().setUp("def foo():", |
| 465 | " xs = [['a'], ['b'], ['c']]", |
| 466 | " ys = []", |
| 467 | " for x in xs:", |
| 468 | " for y in x:", |
| 469 | " ys.append(y)", |
| 470 | " xs[2].append(x[0])", |
| 471 | " return ys", |
| 472 | "ys = foo()").testLookup("ys", MutableList.of(null, "a", "b", "c", "a", "b")); |
| 473 | } |
| 474 | |
| 475 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 476 | public void testForNotIterable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 477 | new SkylarkTest() |
| 478 | .update("mock", new Mock()) |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 479 | .testIfErrorContains( |
| 480 | "type 'int' is not iterable", |
| 481 | "def func():", |
| 482 | " for i in mock.value_of('1'): a = i", |
| 483 | "func()\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 486 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 487 | public void testForOnDictionary() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 488 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 489 | " d = {1: 'a', 2: 'b', 3: 'c'}", |
| 490 | " s = ''", |
| 491 | " for i in d: s = s + d[i]", |
| 492 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 493 | "s = foo()").testLookup("s", "abc"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 496 | @Test |
Francois-Rene Rideau | 6c10eac | 2015-09-17 19:17:20 +0000 | [diff] [blame] | 497 | public void testBadDictKey() throws Exception { |
| 498 | new SkylarkTest().testIfErrorContains( |
| 499 | "unhashable type: 'list'", |
| 500 | "{ [1, 2]: [3, 4] }"); |
| 501 | } |
| 502 | |
| 503 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 504 | public void testForLoopReuseVariable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 505 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 506 | " s = ''", |
| 507 | " for i in ['a', 'b']:", |
| 508 | " for i in ['c', 'd']: s = s + i", |
| 509 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 510 | "s = foo()").testLookup("s", "cdcd"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 513 | @Test |
Laurent Le Brun | 741824b | 2015-03-20 15:10:19 +0000 | [diff] [blame] | 514 | public void testForLoopMultipleVariables() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 515 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 516 | " s = ''", |
| 517 | " for [i, j] in [[1, 2], [3, 4]]:", |
| 518 | " s = s + str(i) + str(j) + '.'", |
| 519 | " return s", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 520 | "s = foo()").testLookup("s", "12.34."); |
Laurent Le Brun | 741824b | 2015-03-20 15:10:19 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | @Test |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 524 | public void testForLoopBreak() throws Exception { |
| 525 | simpleFlowTest("break", 1); |
| 526 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 527 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 528 | @Test |
| 529 | public void testForLoopContinue() throws Exception { |
| 530 | simpleFlowTest("continue", 10); |
| 531 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 532 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 533 | @SuppressWarnings("unchecked") |
| 534 | private void simpleFlowTest(String statement, int expected) throws Exception { |
| 535 | eval("def foo():", |
| 536 | " s = 0", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 537 | " hit = 0", |
| 538 | " for i in range(0, 10):", |
| 539 | " s = s + 1", |
| 540 | " " + statement + "", |
| 541 | " hit = 1", |
| 542 | " return [s, hit]", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 543 | "x = foo()"); |
| 544 | assertThat((Iterable<Object>) lookup("x")).containsExactly(expected, 0).inOrder(); |
| 545 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 546 | |
| 547 | @Test |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 548 | public void testForLoopBreakFromDeeperBlock() throws Exception { |
| 549 | flowFromDeeperBlock("break", 1); |
| 550 | flowFromNestedBlocks("break", 29); |
| 551 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 552 | |
| 553 | @Test |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 554 | public void testForLoopContinueFromDeeperBlock() throws Exception { |
| 555 | flowFromDeeperBlock("continue", 5); |
| 556 | flowFromNestedBlocks("continue", 39); |
| 557 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 558 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 559 | private void flowFromDeeperBlock(String statement, int expected) throws Exception { |
| 560 | eval("def foo():", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 561 | " s = 0", |
| 562 | " for i in range(0, 10):", |
| 563 | " if i % 2 != 0:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 564 | " " + statement + "", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 565 | " s = s + 1", |
| 566 | " return s", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 567 | "x = foo()"); |
| 568 | assertThat(lookup("x")).isEqualTo(expected); |
| 569 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 570 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 571 | private void flowFromNestedBlocks(String statement, int expected) throws Exception { |
| 572 | eval("def foo2():", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 573 | " s = 0", |
| 574 | " for i in range(1, 41):", |
| 575 | " if i % 2 == 0:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 576 | " if i % 3 == 0:", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 577 | " if i % 5 == 0:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 578 | " " + statement + "", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 579 | " s = s + 1", |
| 580 | " return s", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 581 | "y = foo2()"); |
| 582 | assertThat(lookup("y")).isEqualTo(expected); |
| 583 | } |
| 584 | |
| 585 | @Test |
| 586 | public void testNestedForLoopsMultipleBreaks() throws Exception { |
| 587 | nestedLoopsTest("break", 2, 6, 6); |
| 588 | } |
| 589 | |
| 590 | @Test |
| 591 | public void testNestedForLoopsMultipleContinues() throws Exception { |
| 592 | nestedLoopsTest("continue", 4, 20, 20); |
| 593 | } |
| 594 | |
| 595 | @SuppressWarnings("unchecked") |
| 596 | private void nestedLoopsTest(String statement, Integer outerExpected, int firstExpected, |
| 597 | int secondExpected) throws Exception { |
| 598 | eval("def foo():", |
| 599 | " outer = 0", |
| 600 | " first = 0", |
| 601 | " second = 0", |
| 602 | " for i in range(0, 5):", |
| 603 | " for j in range(0, 5):", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 604 | " if j == 2:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 605 | " " + statement + "", |
| 606 | " first = first + 1", |
| 607 | " for k in range(0, 5):", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 608 | " if k == 2:", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 609 | " " + statement + "", |
| 610 | " second = second + 1", |
| 611 | " if i == 2:", |
| 612 | " " + statement + "", |
| 613 | " outer = outer + 1", |
| 614 | " return [outer, first, second]", |
| 615 | "x = foo()"); |
| 616 | assertThat((Iterable<Object>) lookup("x")) |
| 617 | .containsExactly(outerExpected, firstExpected, secondExpected).inOrder(); |
| 618 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 619 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 620 | @Test |
| 621 | public void testForLoopBreakError() throws Exception { |
| 622 | flowStatementInsideFunction("break"); |
| 623 | flowStatementAfterLoop("break"); |
| 624 | } |
| 625 | |
| 626 | @Test |
| 627 | public void testForLoopContinueError() throws Exception { |
| 628 | flowStatementInsideFunction("continue"); |
| 629 | flowStatementAfterLoop("continue"); |
| 630 | } |
| 631 | |
| 632 | private void flowStatementInsideFunction(String statement) throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 633 | checkEvalErrorContains(statement + " statement must be inside a for loop", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 634 | "def foo():", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 635 | " " + statement + "", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 636 | "x = foo()"); |
| 637 | } |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 638 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 639 | private void flowStatementAfterLoop(String statement) throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 640 | checkEvalErrorContains(statement + " statement must be inside a for loop", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 641 | "def foo2():", |
| 642 | " for i in range(0, 3):", |
| 643 | " pass", |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 644 | " " + statement + "", |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 645 | "y = foo2()"); |
| 646 | } |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 647 | |
Florian Weikert | 917ceaa | 2015-06-10 13:54:26 +0000 | [diff] [blame] | 648 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 649 | public void testNoneAssignment() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 650 | new SkylarkTest() |
| 651 | .setUp("def foo(x=None):", " x = 1", " x = None", " return 2", "s = foo()") |
| 652 | .testLookup("s", 2); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 655 | @Test |
Laurent Le Brun | 88014fe | 2015-06-17 16:02:16 +0000 | [diff] [blame] | 656 | public void testReassignment() throws Exception { |
| 657 | eval("def foo(x=None):", |
| 658 | " x = 1", |
| 659 | " x = [1, 2]", |
| 660 | " x = 'str'", |
| 661 | " return x", |
| 662 | "s = foo()"); |
| 663 | assertThat(lookup("s")).isEqualTo("str"); |
| 664 | } |
| 665 | |
| 666 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 667 | public void testJavaCalls() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 668 | new SkylarkTest() |
| 669 | .update("mock", new Mock()) |
| 670 | .setUp("b = mock.is_empty('a')") |
| 671 | .testLookup("b", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 674 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 675 | public void testJavaCallsOnSubClass() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 676 | new SkylarkTest() |
| 677 | .update("mock", new MockSubClass()) |
| 678 | .setUp("b = mock.is_empty('a')") |
| 679 | .testLookup("b", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 682 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 683 | public void testJavaCallsOnInterface() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 684 | new SkylarkTest() |
| 685 | .update("mock", new MockSubClass()) |
| 686 | .setUp("b = mock.is_empty_interface('a')") |
| 687 | .testLookup("b", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 690 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 691 | public void testJavaCallsNotSkylarkCallable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 692 | new SkylarkTest() |
| 693 | .update("mock", new Mock()) |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 694 | .testIfExactError("type 'Mock' has no method value()", "mock.value()"); |
Laurent Le Brun | 648f8f3 | 2015-09-09 19:46:29 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | @Test |
| 698 | public void testNoOperatorIndex() throws Exception { |
| 699 | new SkylarkTest() |
| 700 | .update("mock", new Mock()) |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 701 | .testIfExactError("type 'Mock' has no operator [](int)", "mock[2]"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 704 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 705 | public void testJavaCallsNoMethod() throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 706 | new SkylarkTest() |
| 707 | .update("mock", new Mock()) |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 708 | .testIfExactError("type 'Mock' has no method bad()", "mock.bad()"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 711 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 712 | public void testJavaCallsNoMethodErrorMsg() throws Exception { |
Laurent Le Brun | 648f8f3 | 2015-09-09 19:46:29 +0000 | [diff] [blame] | 713 | new SkylarkTest() |
| 714 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 715 | "type 'int' has no method bad(string, string, string)", "s = 3.bad('a', 'b', 'c')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 718 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 719 | public void testJavaCallsMultipleMethod() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 720 | new SkylarkTest() |
| 721 | .update("mock", new MockMultipleMethodClass()) |
| 722 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 723 | "type 'MockMultipleMethodClass' has multiple matches for function method(string)", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 724 | "s = mock.method('string')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 727 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 728 | public void testJavaCallWithKwargs() throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 729 | new SkylarkTest() |
| 730 | .update("mock", new Mock()) |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 731 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 732 | "type 'Mock' has no method isEmpty(string str)", "mock.isEmpty(str='abc')"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | |
| 736 | @Test |
| 737 | public void testJavaCallWithPositionalAndKwargs() throws Exception { |
| 738 | new SkylarkTest() |
| 739 | .update("mock", new Mock()) |
| 740 | .setUp("b = mock.with_params(1, True, named=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 741 | .testLookup("b", "with_params(1, true, false, true, false, a)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 742 | new SkylarkTest() |
| 743 | .update("mock", new Mock()) |
| 744 | .setUp("") |
| 745 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 746 | "parameter 'named' has no default value, in method with_params(int, bool) of 'Mock'", |
Pedro Liberal Fernandez | 92f06a5 | 2016-09-28 07:33:42 +0000 | [diff] [blame] | 747 | "mock.with_params(1, True)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 748 | new SkylarkTest() |
| 749 | .update("mock", new Mock()) |
| 750 | .setUp("") |
| 751 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 752 | "parameter 'named' has no default value, in method with_params(int, bool, bool) " |
| 753 | + "of 'Mock'", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 754 | "mock.with_params(1, True, True)"); |
| 755 | new SkylarkTest() |
| 756 | .update("mock", new Mock()) |
| 757 | .setUp("b = mock.with_params(1, True, True, named=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 758 | .testLookup("b", "with_params(1, true, true, true, false, a)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 759 | new SkylarkTest() |
| 760 | .update("mock", new Mock()) |
| 761 | .setUp("b = mock.with_params(1, True, named=True, posOrNamed=True)") |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 762 | .testLookup("b", "with_params(1, true, true, true, false, a)"); |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 763 | new SkylarkTest() |
| 764 | .update("mock", new Mock()) |
| 765 | .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] | 766 | .testLookup("b", "with_params(1, true, true, true, true, a)"); |
Pedro Liberal Fernandez | 837dbc1 | 2016-08-18 14:13:01 +0000 | [diff] [blame] | 767 | new SkylarkTest() |
| 768 | .update("mock", new Mock()) |
| 769 | .setUp("") |
| 770 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 771 | "too many arguments, in method with_params(int, bool, bool named, " |
| 772 | + "bool posOrNamed, int n) of 'Mock'", |
Pedro Liberal Fernandez | 837dbc1 | 2016-08-18 14:13:01 +0000 | [diff] [blame] | 773 | "mock.with_params(1, True, named=True, posOrNamed=True, n=2)"); |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 774 | new SkylarkTest() |
| 775 | .update("mock", new Mock()) |
| 776 | .setUp("") |
| 777 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 778 | "parameter 'nonNoneable' cannot be None, in method with_params(int, bool, bool, " |
| 779 | + "bool named, bool optionalNamed, NoneType nonNoneable) of 'Mock'", |
Pedro Liberal Fernandez | d0c5ff2 | 2016-08-18 18:53:46 +0000 | [diff] [blame] | 780 | "mock.with_params(1, True, True, named=True, optionalNamed=False, nonNoneable=None)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 783 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 784 | public void testNoJavaCallsWithoutSkylark() throws Exception { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 785 | 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] | 786 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 787 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 788 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 789 | public void testNoJavaCallsIfClassNotAnnotated() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 790 | new SkylarkTest() |
| 791 | .update("mock", new MockSubClass()) |
| 792 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 793 | "type 'Mock' has no method is_empty_class_not_annotated(string)", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 794 | "b = mock.is_empty_class_not_annotated('a')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 797 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 798 | public void testStructAccess() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 799 | new SkylarkTest() |
| 800 | .update("mock", new Mock()) |
| 801 | .setUp("v = mock.struct_field") |
| 802 | .testLookup("v", "a"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 805 | @Test |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 806 | public void testStructAccessAsFuncallNonCallable() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 807 | new SkylarkTest() |
| 808 | .update("mock", new Mock()) |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 809 | .testIfExactError("'string' object is not callable", "v = mock.struct_field()"); |
| 810 | } |
| 811 | |
| 812 | @Test |
| 813 | public void testStructAccessAsFuncall() throws Exception { |
| 814 | foobar.configure(getClass().getDeclaredField("foobar").getAnnotation(SkylarkSignature.class)); |
| 815 | new SkylarkTest() |
| 816 | .update("mock", new Mock()) |
| 817 | .setUp("v = mock.struct_field_callable()") |
| 818 | .testLookup("v", "foobar"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 821 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 822 | public void testStructAccessOfMethod() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 823 | new SkylarkTest() |
| 824 | .update("mock", new Mock()) |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 825 | .testIfExactError("object of type 'Mock' has no field 'function'", "v = mock.function"); |
| 826 | } |
| 827 | |
| 828 | @Test |
| 829 | public void testStructAccessTypo() throws Exception { |
| 830 | new SkylarkTest() |
| 831 | .update("mock", new MockClassObject()) |
| 832 | .testIfExactError( |
| 833 | "object of type 'MockClassObject' has no field 'fild' (did you mean 'field'?)", |
| 834 | "mock.fild"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 837 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 838 | public void testJavaFunctionReturnsMutableObject() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 839 | new SkylarkTest() |
| 840 | .update("mock", new Mock()) |
| 841 | .testIfExactError( |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 842 | "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] | 843 | } |
| 844 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 845 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 846 | public void testJavaFunctionReturnsNullFails() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 847 | new SkylarkTest() |
| 848 | .update("mock", new Mock()) |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 849 | .testIfErrorContains( |
| 850 | "method invocation returned None," |
| 851 | + " please file a bug report: nullfunc_failing(\"abc\", 1)", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 852 | "mock.nullfunc_failing('abc', 1)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 855 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 856 | public void testClassObjectAccess() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 857 | new SkylarkTest() |
| 858 | .update("mock", new MockClassObject()) |
| 859 | .setUp("v = mock.field") |
| 860 | .testLookup("v", "a"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 863 | @Test |
Laurent Le Brun | ab0ca1a | 2015-03-31 17:13:25 +0000 | [diff] [blame] | 864 | public void testInSet() throws Exception { |
Vladimir Moskva | d200daf | 2016-12-23 16:35:37 +0000 | [diff] [blame] | 865 | new SkylarkTest() |
| 866 | .testStatement("'b' in depset(['a', 'b'])", Boolean.TRUE) |
| 867 | .testStatement("'c' in depset(['a', 'b'])", Boolean.FALSE) |
| 868 | .testStatement("1 in depset(['a', 'b'])", Boolean.FALSE); |
Laurent Le Brun | ab0ca1a | 2015-03-31 17:13:25 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | @Test |
Laurent Le Brun | 092f13b | 2015-08-24 14:50:00 +0000 | [diff] [blame] | 872 | public void testUnionSet() throws Exception { |
| 873 | new SkylarkTest() |
Vladimir Moskva | ba4f0bb | 2017-01-30 15:45:49 +0000 | [diff] [blame] | 874 | .testStatement("str(depset([1, 3]) | depset([1, 2]))", "depset([1, 2, 3])") |
| 875 | .testStatement("str(depset([1, 2]) | [1, 3])", "depset([1, 2, 3])") |
Laurent Le Brun | 092f13b | 2015-08-24 14:50:00 +0000 | [diff] [blame] | 876 | .testIfExactError("unsupported operand type(s) for |: 'int' and 'int'", "2 | 4"); |
| 877 | } |
| 878 | |
| 879 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 880 | public void testClassObjectCannotAccessNestedSet() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 881 | new SkylarkTest() |
| 882 | .update("mock", new MockClassObject()) |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 883 | .testIfErrorContains("internal error: type 'NestedSet' is not allowed", "v = mock.nset"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 886 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 887 | public void testJavaFunctionReturnsNone() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 888 | new SkylarkTest() |
| 889 | .update("mock", new Mock()) |
| 890 | .setUp("v = mock.nullfunc_working()") |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 891 | .testLookup("v", Runtime.NONE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 894 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 895 | public void testVoidJavaFunctionReturnsNone() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 896 | new SkylarkTest() |
| 897 | .update("mock", new Mock()) |
| 898 | .setUp("v = mock.voidfunc()") |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 899 | .testLookup("v", Runtime.NONE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 902 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 903 | public void testAugmentedAssignment() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 904 | new SkylarkTest().setUp("def f1(x):", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 905 | " x += 1", |
| 906 | " return x", |
| 907 | "", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 908 | "foo = f1(41)").testLookup("foo", 42); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 911 | @Test |
Vladimir Moskva | 23ba4a8 | 2017-02-21 15:30:54 +0000 | [diff] [blame^] | 912 | public void testAugmentedAssignmentHasNoSideEffects() throws Exception { |
| 913 | new SkylarkTest().setUp( |
| 914 | "counter = [0]", |
| 915 | "value = [1, 2]", |
| 916 | "", |
| 917 | "def f():", |
| 918 | " counter[0] = counter[0] + 1", |
| 919 | " return value", |
| 920 | "", |
| 921 | "f()[1] += 1") // `f()` should be called only once here |
| 922 | .testLookup("counter", MutableList.of(env, 1)); |
| 923 | } |
| 924 | |
| 925 | @Test |
| 926 | public void testAugmentedAssignmentNotAllowedForListLiterals() throws Exception { |
| 927 | new SkylarkTest().testIfErrorContains("Cannot perform augment assignment on a list literal", |
| 928 | "def f(a, b):", |
| 929 | " [a, b] += []", |
| 930 | "f(1, 2)"); |
| 931 | } |
| 932 | |
| 933 | @Test |
| 934 | public void testAssignmentEvaluationOrder() throws Exception { |
| 935 | new SkylarkTest().setUp( |
| 936 | "ordinary = []", |
| 937 | "augmented = []", |
| 938 | "value = [1, 2]", |
| 939 | "", |
| 940 | "def f(record):", |
| 941 | " record.append('f')", |
| 942 | " return value", |
| 943 | "", |
| 944 | "def g(record):", |
| 945 | " record.append('g')", |
| 946 | " return value", |
| 947 | "", |
| 948 | "f(ordinary)[0] = g(ordinary)[1]", |
| 949 | "f(augmented)[0] += g(augmented)[1]") |
| 950 | .testLookup("ordinary", MutableList.of(env, "g", "f")) // This order is consistent |
| 951 | .testLookup("augmented", MutableList.of(env, "f", "g")); // with Python |
| 952 | } |
| 953 | |
| 954 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 955 | public void testStaticDirectJavaCall() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 956 | new SkylarkTest().update("Mock", Mock.class).setUp("val = Mock.value_of('8')") |
| 957 | .testLookup("val", 8); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 960 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 961 | public void testStaticDirectJavaCallMethodIsNonStatic() throws Exception { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 962 | new SkylarkTest() |
| 963 | .update("Mock", Mock.class) |
| 964 | .testIfExactError("method 'is_empty' is not static", "val = Mock.is_empty('a')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 967 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 968 | public void testDictComprehensions_IterationOrder() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 969 | new SkylarkTest().setUp("def foo():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 970 | " d = {x : x for x in ['c', 'a', 'b']}", |
| 971 | " s = ''", |
| 972 | " for a in d:", |
| 973 | " s += a", |
| 974 | " return s", |
Vladimir Moskva | 76e31d1 | 2016-12-05 16:28:37 +0000 | [diff] [blame] | 975 | "s = foo()").testLookup("s", "cab"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 978 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 979 | public void testDotExpressionOnNonStructObject() throws Exception { |
Laurent Le Brun | c31f351 | 2016-12-29 21:41:33 +0000 | [diff] [blame] | 980 | new SkylarkTest() |
Laurent Le Brun | 57badf4 | 2017-01-02 15:12:24 +0000 | [diff] [blame] | 981 | .testIfExactError("object of type 'string' has no field 'field'", "x = 'a'.field"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 984 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 985 | public void testPlusEqualsOnDict() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 986 | new SkylarkTest().setUp("def func():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 987 | " d = {'a' : 1}", |
| 988 | " d += {'b' : 2}", |
| 989 | " return d", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 990 | "d = func()") |
| 991 | .testLookup("d", ImmutableMap.of("a", 1, "b", 2)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 994 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 995 | public void testDictAssignmentAsLValue() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 996 | new SkylarkTest().setUp("def func():", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 997 | " d = {'a' : 1}", |
| 998 | " d['b'] = 2", |
| 999 | " return d", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1000 | "d = func()").testLookup("d", ImmutableMap.of("a", 1, "b", 2)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1003 | @Test |
Vladimir Moskva | 1077038 | 2016-08-23 15:04:54 +0000 | [diff] [blame] | 1004 | public void testNestedDictAssignmentAsLValue() throws Exception { |
| 1005 | new SkylarkTest().setUp("def func():", |
| 1006 | " d = {'a' : 1}", |
| 1007 | " e = {'d': d}", |
| 1008 | " e['d']['b'] = 2", |
| 1009 | " return e", |
| 1010 | "e = func()").testLookup("e", ImmutableMap.of("d", ImmutableMap.of("a", 1, "b", 2))); |
| 1011 | } |
| 1012 | |
| 1013 | @Test |
| 1014 | public void testListAssignmentAsLValue() throws Exception { |
| 1015 | new SkylarkTest().setUp("def func():", |
| 1016 | " a = [1, 2]", |
| 1017 | " a[1] = 3", |
| 1018 | " a[-2] = 4", |
| 1019 | " return a", |
| 1020 | "a = str(func())").testLookup("a", "[4, 3]"); |
| 1021 | } |
| 1022 | |
| 1023 | @Test |
| 1024 | public void testNestedListAssignmentAsLValue() throws Exception { |
| 1025 | new SkylarkTest().setUp("def func():", |
| 1026 | " d = [1, 2]", |
| 1027 | " e = [3, d]", |
| 1028 | " e[1][1] = 4", |
| 1029 | " return e", |
| 1030 | "e = str(func())").testLookup("e", "[3, [1, 4]]"); |
| 1031 | } |
| 1032 | |
| 1033 | @Test |
Laurent Le Brun | d640bd3 | 2016-01-07 13:58:43 +0000 | [diff] [blame] | 1034 | public void testDictTupleAssignmentAsLValue() throws Exception { |
| 1035 | new SkylarkTest().setUp("def func():", |
| 1036 | " d = {'a' : 1}", |
| 1037 | " d['b'], d['c'] = 2, 3", |
| 1038 | " return d", |
| 1039 | "d = func()").testLookup("d", ImmutableMap.of("a", 1, "b", 2, "c", 3)); |
| 1040 | } |
| 1041 | |
| 1042 | @Test |
| 1043 | public void testDictItemPlusEqual() throws Exception { |
| 1044 | new SkylarkTest().setUp("def func():", |
| 1045 | " d = {'a' : 2}", |
| 1046 | " d['a'] += 3", |
| 1047 | " return d", |
| 1048 | "d = func()").testLookup("d", ImmutableMap.of("a", 5)); |
| 1049 | } |
| 1050 | |
| 1051 | @Test |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 1052 | public void testDictAssignmentAsLValueSideEffects() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1053 | new SkylarkTest().setUp("def func(d):", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1054 | " d['b'] = 2", |
| 1055 | "d = {'a' : 1}", |
Francois-Rene Rideau | ab049e0 | 2016-02-17 16:13:46 +0000 | [diff] [blame] | 1056 | "func(d)").testLookup("d", SkylarkDict.of(null, "a", 1, "b", 2)); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1059 | @Test |
Francois-Rene Rideau | ef7a8a5 | 2016-01-29 17:37:48 +0000 | [diff] [blame] | 1060 | public void testAssignmentToListInDictSideEffect() throws Exception { |
| 1061 | new SkylarkTest().setUp( |
| 1062 | "l = [1, 2]", |
| 1063 | "d = {0: l}", |
| 1064 | "d[0].append(3)").testLookup("l", MutableList.of(null, 1, 2, 3)); |
| 1065 | } |
| 1066 | |
| 1067 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1068 | public void testTopLevelDict() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1069 | new SkylarkTest().setUp("if 1:", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1070 | " v = 'a'", |
| 1071 | "else:", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1072 | " v = 'b'").testLookup("v", "a"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1075 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1076 | public void testUserFunctionKeywordArgs() throws Exception { |
Francois-Rene Rideau | 676905a | 2015-08-31 15:39:09 +0000 | [diff] [blame] | 1077 | new SkylarkTest().setUp("def foo(a, b, c):", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1078 | " return a + b + c", "s = foo(1, c=2, b=3)") |
| 1079 | .testLookup("s", 6); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1082 | @Test |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1083 | public void testFunctionCallOrdering() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1084 | new SkylarkTest().setUp("def func(): return foo() * 2", |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1085 | "def foo(): return 2", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1086 | "x = func()") |
| 1087 | .testLookup("x", 4); |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | @Test |
| 1091 | public void testFunctionCallBadOrdering() throws Exception { |
Florian Weikert | 3f610e8 | 2015-08-18 14:37:46 +0000 | [diff] [blame] | 1092 | new SkylarkTest().testIfErrorContains("name 'foo' is not defined", |
Laurent Le Brun | 6874316 | 2015-05-13 13:18:09 +0000 | [diff] [blame] | 1093 | "def func(): return foo() * 2", |
| 1094 | "x = func()", |
| 1095 | "def foo(): return 2"); |
| 1096 | } |
| 1097 | |
| 1098 | @Test |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 1099 | public void testTypo() throws Exception { |
| 1100 | new SkylarkTest() |
| 1101 | .testIfErrorContains( |
| 1102 | "name 'my_variable' is not defined (did you mean 'myVariable'?)", |
| 1103 | "myVariable = 2", |
| 1104 | "x = my_variable + 1"); |
| 1105 | } |
| 1106 | |
| 1107 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1108 | public void testNoneTrueFalseInSkylark() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1109 | new SkylarkTest().setUp("a = None", |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1110 | "b = True", |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1111 | "c = False") |
Francois-Rene Rideau | 0f7ba34 | 2015-08-31 16:16:21 +0000 | [diff] [blame] | 1112 | .testLookup("a", Runtime.NONE) |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1113 | .testLookup("b", Boolean.TRUE) |
| 1114 | .testLookup("c", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1117 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1118 | public void testHasattrMethods() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1119 | new SkylarkTest() |
| 1120 | .update("mock", new Mock()) |
| 1121 | .setUp("a = hasattr(mock, 'struct_field')", "b = hasattr(mock, 'function')", |
| 1122 | "c = hasattr(mock, 'is_empty')", "d = hasattr('str', 'replace')", |
| 1123 | "e = hasattr(mock, 'other')\n") |
| 1124 | .testLookup("a", Boolean.TRUE) |
| 1125 | .testLookup("b", Boolean.TRUE) |
| 1126 | .testLookup("c", Boolean.TRUE) |
| 1127 | .testLookup("d", Boolean.TRUE) |
| 1128 | .testLookup("e", Boolean.FALSE); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1131 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1132 | public void testListAnTupleConcatenationDoesNotWorkInSkylark() throws Exception { |
Francois-Rene Rideau | 93ed7f1 | 2015-10-20 15:39:33 +0000 | [diff] [blame] | 1133 | new SkylarkTest().testIfExactError("unsupported operand type(s) for +: 'list' and 'tuple'", |
Francois-Rene Rideau | 4e99410 | 2015-09-17 22:41:28 +0000 | [diff] [blame] | 1134 | "[1, 2] + (3, 4)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1137 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1138 | public void testCannotCreateMixedListInSkylark() throws Exception { |
Laurent Le Brun | e083a91 | 2015-08-10 15:13:34 +0000 | [diff] [blame] | 1139 | new SkylarkTest().testExactOrder("['a', 'b', 1, 2]", "a", "b", 1, 2); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1142 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1143 | public void testCannotConcatListInSkylarkWithDifferentGenericTypes() throws Exception { |
Laurent Le Brun | e083a91 | 2015-08-10 15:13:34 +0000 | [diff] [blame] | 1144 | new SkylarkTest().testExactOrder("[1, 2] + ['a', 'b']", 1, 2, "a", "b"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1147 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1148 | public void testConcatEmptyListWithNonEmptyWorks() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1149 | new SkylarkTest().testExactOrder("[] + ['a', 'b']", "a", "b"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1152 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1153 | public void testFormatStringWithTuple() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1154 | new SkylarkTest().setUp("v = '%s%s' % ('a', 1)").testLookup("v", "a1"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Francois-Rene Rideau | e8cfead | 2015-03-17 16:01:47 +0000 | [diff] [blame] | 1157 | @Test |
| 1158 | public void testSingletonTuple() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1159 | new SkylarkTest().testExactOrder("(1,)", 1); |
Francois-Rene Rideau | e8cfead | 2015-03-17 16:01:47 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1162 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1163 | public void testDirFindsClassObjectFields() throws Exception { |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 1164 | new SkylarkTest().update("mock", new MockClassObject()) |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1165 | .testExactOrder("dir(mock)", "field", "nset"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1168 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1169 | public void testDirFindsJavaObjectStructFieldsAndMethods() throws Exception { |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1170 | new SkylarkTest() |
| 1171 | .update("mock", new Mock()) |
| 1172 | .testExactOrder( |
| 1173 | "dir(mock)", |
| 1174 | "function", |
| 1175 | "is_empty", |
| 1176 | "nullfunc_failing", |
| 1177 | "nullfunc_working", |
| 1178 | "return_bad", |
| 1179 | "string", |
| 1180 | "string_list", |
| 1181 | "struct_field", |
Vladimir Moskva | a5b1674 | 2016-10-31 14:09:41 +0000 | [diff] [blame] | 1182 | "struct_field_callable", |
Damien Martin-Guillerez | 2d32c58 | 2016-08-04 14:29:18 +0000 | [diff] [blame] | 1183 | "value_of", |
| 1184 | "voidfunc", |
| 1185 | "with_params"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1188 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1189 | public void testPrint() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1190 | // TODO(fwe): cannot be handled by current testing suite |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1191 | setFailFast(false); |
| 1192 | eval("print('hello')"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1193 | assertContainsWarning("hello"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1194 | eval("print('a', 'b')"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1195 | assertContainsWarning("a b"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1196 | eval("print('a', 'b', sep='x')"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1197 | assertContainsWarning("axb"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1200 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1201 | public void testPrintBadKwargs() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1202 | new SkylarkTest().testIfExactError( |
Francois-Rene Rideau | 537a90b | 2015-04-22 06:47:31 +0000 | [diff] [blame] | 1203 | "unexpected keywords 'end', 'other' in call to print(*args, sep: string = \" \")", |
| 1204 | "print(end='x', other='y')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1207 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1208 | public void testSkylarkTypes() { |
| 1209 | assertEquals(TransitiveInfoCollection.class, |
| 1210 | EvalUtils.getSkylarkType(FileConfiguredTarget.class)); |
| 1211 | assertEquals(TransitiveInfoCollection.class, |
| 1212 | EvalUtils.getSkylarkType(RuleConfiguredTarget.class)); |
| 1213 | assertEquals(Artifact.class, EvalUtils.getSkylarkType(SpecialArtifact.class)); |
| 1214 | } |
| 1215 | |
| 1216 | // Override tests in EvaluationTest incompatible with Skylark |
| 1217 | |
| 1218 | @SuppressWarnings("unchecked") |
| 1219 | @Override |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1220 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1221 | public void testConcatLists() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1222 | new SkylarkTest().testExactOrder("[1,2] + [3,4]", 1, 2, 3, 4).testExactOrder("(1,2)", 1, 2) |
| 1223 | .testExactOrder("(1,2) + (3,4)", 1, 2, 3, 4); |
| 1224 | |
| 1225 | // TODO(fwe): cannot be handled by current testing suite |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1226 | // list |
| 1227 | Object x = eval("[1,2] + [3,4]"); |
| 1228 | assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1229 | |
| 1230 | // tuple |
| 1231 | x = eval("(1,2)"); |
| 1232 | assertThat((Iterable<Object>) x).containsExactly(1, 2).inOrder(); |
| 1233 | assertTrue(((SkylarkList) x).isTuple()); |
| 1234 | |
| 1235 | x = eval("(1,2) + (3,4)"); |
| 1236 | assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder(); |
| 1237 | assertTrue(((SkylarkList) x).isTuple()); |
| 1238 | } |
| 1239 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1240 | @Override |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1241 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1242 | public void testListConcatenation() throws Exception {} |
| 1243 | |
Florian Weikert | f07e544 | 2015-07-01 13:08:43 +0000 | [diff] [blame] | 1244 | @Override |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1245 | @Test |
| 1246 | public void testInFail() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1247 | new SkylarkTest().testIfExactError( |
| 1248 | "in operator only works on strings if the left operand is also a string", "1 in '123'"); |
| 1249 | new SkylarkTest().testIfExactError( |
| 1250 | "in operator only works on lists, tuples, sets, dicts and strings", "'a' in 1"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1253 | @Override |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1254 | @Test |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1255 | public void testListComprehensionsMultipleVariablesFail() throws Exception { |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 1256 | new SkylarkTest() |
| 1257 | .testIfErrorContains( |
| 1258 | "lvalue has length 3, but rvalue has has length 2", |
| 1259 | "def foo (): return [x + y for x, y, z in [(1, 2), (3, 4)]]", |
| 1260 | "foo()"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1261 | |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 1262 | new SkylarkTest() |
| 1263 | .testIfErrorContains( |
| 1264 | "type 'int' is not a collection", |
| 1265 | "def bar (): return [x + y for x, y in (1, 2)]", |
| 1266 | "bar()"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1267 | |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 1268 | new SkylarkTest() |
| 1269 | .testIfErrorContains( |
| 1270 | "lvalue has length 3, but rvalue has has length 2", |
| 1271 | "[x + y for x, y, z in [(1, 2), (3, 4)]]"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1272 | |
| 1273 | // can't reuse the same local variable twice(!) |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 1274 | new SkylarkTest() |
| 1275 | .testIfErrorContains( |
Laurent Le Brun | d4d7fca | 2017-02-14 19:12:02 +0000 | [diff] [blame] | 1276 | "Variable x is read only", "[x + y for x, y in (1, 2)]", "[x + y for x, y in (1, 2)]"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1277 | |
Florian Weikert | c1d54ec | 2015-08-26 14:06:58 +0000 | [diff] [blame] | 1278 | new SkylarkTest() |
| 1279 | .testIfErrorContains("type 'int' is not a collection", "[x2 + y2 for x2, y2 in (1, 2)]"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | @Override |
| 1283 | @Test |
| 1284 | public void testNotCallInt() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1285 | new SkylarkTest().setUp("sum = 123456").testLookup("sum", 123456) |
| 1286 | .testIfExactError("'int' object is not callable", "sum(1, 2, 3, 4, 5, 6)") |
| 1287 | .testStatement("sum", 123456); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1288 | } |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 1289 | |
| 1290 | @Test |
| 1291 | public void testConditionalExpressionAtToplevel() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1292 | 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] | 1293 | } |
| 1294 | |
| 1295 | @Test |
| 1296 | public void testConditionalExpressionInFunction() throws Exception { |
Florian Weikert | 28da365 | 2015-07-01 14:52:30 +0000 | [diff] [blame] | 1297 | new SkylarkTest().setUp("def foo(a, b, c): return a+b if c else a-b\n").testStatement( |
| 1298 | "foo(23, 5, 0)", 18); |
Francois-Rene Rideau | 6fc5ee7 | 2015-03-12 20:55:17 +0000 | [diff] [blame] | 1299 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1300 | } |