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