Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 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; |
| 17 | import static com.google.common.truth.Truth.assertWithMessage; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.junit.Assert.fail; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 22 | |
Francois-Rene Rideau | 4feb160 | 2015-03-18 19:49:13 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.events.Location; |
| 26 | import com.google.devtools.build.lib.syntax.DictionaryLiteral.DictionaryEntryLiteral; |
Miguel Alcon Pinto | 927f3b2 | 2016-08-22 14:21:30 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.syntax.SkylarkImports.SkylarkImportSyntaxException; |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.syntax.util.EvaluationTestCase; |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.vfs.PathFragment; |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 30 | import java.util.LinkedList; |
| 31 | import java.util.List; |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Tests of parser behaviour. |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 38 | */ |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 39 | @RunWith(JUnit4.class) |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 40 | public class ParserTest extends EvaluationTestCase { |
| 41 | |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 42 | private BuildFileAST parseFileWithComments(String... input) { |
Laurent Le Brun | 2c52dab | 2016-11-23 16:50:23 +0000 | [diff] [blame] | 43 | return BuildFileAST.parseBuildString(getEventHandler(), input); |
Francois-Rene Rideau | 5a94e59 | 2015-09-04 19:13:47 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 46 | /** Parses build code (not Skylark) */ |
| 47 | @Override |
| 48 | protected List<Statement> parseFile(String... input) { |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 49 | return parseFileWithComments(input).getStatements(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 52 | /** Parses Skylark code */ |
| 53 | private List<Statement> parseFileForSkylark(String... input) { |
Laurent Le Brun | 2c52dab | 2016-11-23 16:50:23 +0000 | [diff] [blame] | 54 | BuildFileAST ast = BuildFileAST.parseSkylarkString(getEventHandler(), input); |
| 55 | ast = ast.validate(new ValidationEnvironment(env), getEventHandler()); |
| 56 | return ast.getStatements(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 57 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 58 | |
| 59 | private static String getText(String text, ASTNode node) { |
| 60 | return text.substring(node.getLocation().getStartOffset(), |
| 61 | node.getLocation().getEndOffset()); |
| 62 | } |
| 63 | |
| 64 | // helper func for testListLiterals: |
| 65 | private static int getIntElem(DictionaryEntryLiteral entry, boolean key) { |
| 66 | return ((IntegerLiteral) (key ? entry.getKey() : entry.getValue())).getValue(); |
| 67 | } |
| 68 | |
| 69 | // helper func for testListLiterals: |
| 70 | private static DictionaryEntryLiteral getElem(DictionaryLiteral list, int index) { |
| 71 | return list.getEntries().get(index); |
| 72 | } |
| 73 | |
| 74 | // helper func for testListLiterals: |
| 75 | private static int getIntElem(ListLiteral list, int index) { |
| 76 | return ((IntegerLiteral) list.getElements().get(index)).getValue(); |
| 77 | } |
| 78 | |
| 79 | // helper func for testListLiterals: |
| 80 | private static Expression getElem(ListLiteral list, int index) { |
| 81 | return list.getElements().get(index); |
| 82 | } |
| 83 | |
| 84 | // helper func for testing arguments: |
| 85 | private static Expression getArg(FuncallExpression f, int index) { |
| 86 | return f.getArguments().get(index).getValue(); |
| 87 | } |
| 88 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 89 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 90 | public void testPrecedence1() throws Exception { |
| 91 | BinaryOperatorExpression e = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 92 | (BinaryOperatorExpression) parseExpression("'%sx' % 'foo' + 'bar'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 93 | |
| 94 | assertEquals(Operator.PLUS, e.getOperator()); |
| 95 | } |
| 96 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 97 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 98 | public void testPrecedence2() throws Exception { |
| 99 | BinaryOperatorExpression e = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 100 | (BinaryOperatorExpression) parseExpression("('%sx' % 'foo') + 'bar'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 101 | assertEquals(Operator.PLUS, e.getOperator()); |
| 102 | } |
| 103 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 104 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 105 | public void testPrecedence3() throws Exception { |
| 106 | BinaryOperatorExpression e = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 107 | (BinaryOperatorExpression) parseExpression("'%sx' % ('foo' + 'bar')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 108 | assertEquals(Operator.PERCENT, e.getOperator()); |
| 109 | } |
| 110 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 111 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 112 | public void testPrecedence4() throws Exception { |
| 113 | BinaryOperatorExpression e = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 114 | (BinaryOperatorExpression) parseExpression("1 + - (2 - 3)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 115 | assertEquals(Operator.PLUS, e.getOperator()); |
| 116 | } |
| 117 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 118 | @Test |
Laurent Le Brun | 7bda87e | 2015-08-24 15:13:53 +0000 | [diff] [blame] | 119 | public void testPrecedence5() throws Exception { |
| 120 | BinaryOperatorExpression e = |
| 121 | (BinaryOperatorExpression) parseExpression("2 * x | y + 1"); |
| 122 | assertEquals(Operator.PIPE, e.getOperator()); |
| 123 | } |
| 124 | |
| 125 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 126 | public void testUnaryMinusExpr() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 127 | FuncallExpression e = (FuncallExpression) parseExpression("-5"); |
| 128 | FuncallExpression e2 = (FuncallExpression) parseExpression("- 5"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 129 | |
| 130 | assertEquals("-", e.getFunction().getName()); |
| 131 | assertEquals("-", e2.getFunction().getName()); |
| 132 | |
| 133 | assertThat(e.getArguments()).hasSize(1); |
| 134 | assertEquals(1, e.getNumPositionalArguments()); |
| 135 | |
| 136 | IntegerLiteral arg0 = (IntegerLiteral) e.getArguments().get(0).getValue(); |
| 137 | assertEquals(5, (int) arg0.getValue()); |
| 138 | } |
| 139 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 140 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 141 | public void testFuncallExpr() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 142 | FuncallExpression e = (FuncallExpression) parseExpression("foo(1, 2, bar=wiz)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 143 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 144 | Identifier ident = e.getFunction(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 145 | assertEquals("foo", ident.getName()); |
| 146 | |
| 147 | assertThat(e.getArguments()).hasSize(3); |
| 148 | assertEquals(2, e.getNumPositionalArguments()); |
| 149 | |
| 150 | IntegerLiteral arg0 = (IntegerLiteral) e.getArguments().get(0).getValue(); |
| 151 | assertEquals(1, (int) arg0.getValue()); |
| 152 | |
| 153 | IntegerLiteral arg1 = (IntegerLiteral) e.getArguments().get(1).getValue(); |
| 154 | assertEquals(2, (int) arg1.getValue()); |
| 155 | |
| 156 | Argument.Passed arg2 = e.getArguments().get(2); |
| 157 | assertEquals("bar", arg2.getName()); |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 158 | Identifier arg2val = (Identifier) arg2.getValue(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 159 | assertEquals("wiz", arg2val.getName()); |
| 160 | } |
| 161 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 162 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 163 | public void testMethCallExpr() throws Exception { |
| 164 | FuncallExpression e = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 165 | (FuncallExpression) parseExpression("foo.foo(1, 2, bar=wiz)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 166 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 167 | Identifier ident = e.getFunction(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 168 | assertEquals("foo", ident.getName()); |
| 169 | |
| 170 | assertThat(e.getArguments()).hasSize(3); |
| 171 | assertEquals(2, e.getNumPositionalArguments()); |
| 172 | |
| 173 | IntegerLiteral arg0 = (IntegerLiteral) e.getArguments().get(0).getValue(); |
| 174 | assertEquals(1, (int) arg0.getValue()); |
| 175 | |
| 176 | IntegerLiteral arg1 = (IntegerLiteral) e.getArguments().get(1).getValue(); |
| 177 | assertEquals(2, (int) arg1.getValue()); |
| 178 | |
| 179 | Argument.Passed arg2 = e.getArguments().get(2); |
| 180 | assertEquals("bar", arg2.getName()); |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 181 | Identifier arg2val = (Identifier) arg2.getValue(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 182 | assertEquals("wiz", arg2val.getName()); |
| 183 | } |
| 184 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 185 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 186 | public void testChainedMethCallExpr() throws Exception { |
| 187 | FuncallExpression e = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 188 | (FuncallExpression) parseExpression("foo.replace().split(1)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 189 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 190 | Identifier ident = e.getFunction(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 191 | assertEquals("split", ident.getName()); |
| 192 | |
| 193 | assertThat(e.getArguments()).hasSize(1); |
| 194 | assertEquals(1, e.getNumPositionalArguments()); |
| 195 | |
| 196 | IntegerLiteral arg0 = (IntegerLiteral) e.getArguments().get(0).getValue(); |
| 197 | assertEquals(1, (int) arg0.getValue()); |
| 198 | } |
| 199 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 200 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 201 | public void testPropRefExpr() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 202 | DotExpression e = (DotExpression) parseExpression("foo.foo"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 203 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 204 | Identifier ident = e.getField(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 205 | assertEquals("foo", ident.getName()); |
| 206 | } |
| 207 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 208 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 209 | public void testStringMethExpr() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 210 | FuncallExpression e = (FuncallExpression) parseExpression("'foo'.foo()"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 211 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 212 | Identifier ident = e.getFunction(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 213 | assertEquals("foo", ident.getName()); |
| 214 | |
| 215 | assertThat(e.getArguments()).isEmpty(); |
| 216 | } |
| 217 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 218 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 219 | public void testStringLiteralOptimizationValue() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 220 | StringLiteral l = (StringLiteral) parseExpression("'abc' + 'def'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 221 | assertEquals("abcdef", l.value); |
| 222 | } |
| 223 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 224 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 225 | public void testStringLiteralOptimizationToString() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 226 | StringLiteral l = (StringLiteral) parseExpression("'abc' + 'def'"); |
Michajlo Matijkiw | 8c539ea | 2017-02-22 23:02:46 +0000 | [diff] [blame] | 227 | assertEquals("\"abcdef\"", l.toString()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 230 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 231 | public void testStringLiteralOptimizationLocation() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 232 | StringLiteral l = (StringLiteral) parseExpression("'abc' + 'def'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 233 | assertEquals(0, l.getLocation().getStartOffset()); |
| 234 | assertEquals(13, l.getLocation().getEndOffset()); |
| 235 | } |
| 236 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 237 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 238 | public void testStringLiteralOptimizationDifferentQuote() throws Exception { |
Michajlo Matijkiw | 8c539ea | 2017-02-22 23:02:46 +0000 | [diff] [blame] | 239 | StringLiteral l = (StringLiteral) parseExpression("'abc' + \"def\""); |
| 240 | assertEquals(0, l.getLocation().getStartOffset()); |
| 241 | assertEquals(13, l.getLocation().getEndOffset()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 244 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 245 | public void testSubstring() throws Exception { |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 246 | SliceExpression s = (SliceExpression) parseExpression("'FOO.CC'[:].lower()[1:]"); |
| 247 | assertThat(((IntegerLiteral) s.getStart()).value).isEqualTo(1); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 248 | |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 249 | FuncallExpression e = (FuncallExpression) parseExpression( |
| 250 | "'FOO.CC'.lower()[1:].startswith('oo')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 251 | assertEquals("startswith", e.getFunction().getName()); |
| 252 | assertThat(e.getArguments()).hasSize(1); |
| 253 | |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 254 | s = (SliceExpression) parseExpression("'FOO.CC'[1:][:2]"); |
| 255 | assertThat(((IntegerLiteral) s.getEnd()).value).isEqualTo(2); |
Florian Weikert | e342196 | 2015-12-17 12:46:08 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void testSlice() throws Exception { |
| 260 | evalSlice("'0123'[:]", Runtime.NONE, Runtime.NONE, 1); |
| 261 | evalSlice("'0123'[1:]", 1, Runtime.NONE, 1); |
| 262 | evalSlice("'0123'[:3]", Runtime.NONE, 3, 1); |
| 263 | evalSlice("'0123'[::]", Runtime.NONE, Runtime.NONE, 1); |
| 264 | evalSlice("'0123'[1::]", 1, Runtime.NONE, 1); |
| 265 | evalSlice("'0123'[:3:]", Runtime.NONE, 3, 1); |
| 266 | evalSlice("'0123'[::-1]", Runtime.NONE, Runtime.NONE, -1); |
| 267 | evalSlice("'0123'[1:3:]", 1, 3, 1); |
| 268 | evalSlice("'0123'[1::-1]", 1, Runtime.NONE, -1); |
| 269 | evalSlice("'0123'[:3:-1]", Runtime.NONE, 3, -1); |
| 270 | evalSlice("'0123'[1:3:-1]", 1, 3, -1); |
| 271 | } |
| 272 | |
| 273 | private void evalSlice(String statement, Object... expectedArgs) { |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 274 | SliceExpression e = (SliceExpression) parseExpression(statement); |
| 275 | |
| 276 | // There is no way to evaluate the expression here, so we rely on string comparison. |
| 277 | assertThat(e.getStart().toString()).isEqualTo(printSliceArg(expectedArgs[0])); |
| 278 | assertThat(e.getEnd().toString()).isEqualTo(printSliceArg(expectedArgs[1])); |
| 279 | assertThat(e.getStep().toString()).isEqualTo(printSliceArg(expectedArgs[2])); |
Florian Weikert | e342196 | 2015-12-17 12:46:08 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | private String printSliceArg(Object arg) { |
| 283 | // The parser sees negative integer constants as FuncallExpressions instead of negative |
| 284 | // IntegerLiterals. |
| 285 | // Consequently, the string representation of -1 is "-(1)", not "-1". |
| 286 | if (arg instanceof Integer) { |
| 287 | int value = (int) arg; |
| 288 | return value < 0 ? String.format("-(%d)", -value) : String.valueOf(value); |
| 289 | } |
| 290 | return arg.toString(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | private void assertLocation(int start, int end, Location location) |
| 294 | throws Exception { |
| 295 | int actualStart = location.getStartOffset(); |
| 296 | int actualEnd = location.getEndOffset(); |
| 297 | |
| 298 | if (actualStart != start || actualEnd != end) { |
| 299 | fail("Expected location = [" + start + ", " + end + "), found [" |
| 300 | + actualStart + ", " + actualEnd + ")"); |
| 301 | } |
| 302 | } |
| 303 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 304 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 305 | public void testErrorRecovery() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 306 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 307 | |
Laurent Le Brun | 443aaae | 2015-04-21 19:49:49 +0000 | [diff] [blame] | 308 | String expr = "f(1, [x for foo foo foo foo], 3)"; |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 309 | FuncallExpression e = (FuncallExpression) parseExpression(expr); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 310 | |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 311 | assertContainsError("syntax error at 'foo'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 312 | |
| 313 | // Test that the actual parameters are: (1, $error$, 3): |
| 314 | |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 315 | Identifier ident = e.getFunction(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 316 | assertEquals("f", ident.getName()); |
| 317 | |
| 318 | assertThat(e.getArguments()).hasSize(3); |
| 319 | assertEquals(3, e.getNumPositionalArguments()); |
| 320 | |
| 321 | IntegerLiteral arg0 = (IntegerLiteral) e.getArguments().get(0).getValue(); |
| 322 | assertEquals(1, (int) arg0.getValue()); |
| 323 | |
| 324 | Argument.Passed arg1 = e.getArguments().get(1); |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 325 | Identifier arg1val = ((Identifier) arg1.getValue()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 326 | assertEquals("$error$", arg1val.getName()); |
| 327 | |
Laurent Le Brun | 443aaae | 2015-04-21 19:49:49 +0000 | [diff] [blame] | 328 | assertLocation(5, 29, arg1val.getLocation()); |
| 329 | assertEquals("[x for foo foo foo foo]", expr.substring(5, 28)); |
| 330 | assertEquals(30, arg1val.getLocation().getEndLineAndColumn().getColumn()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 331 | |
| 332 | IntegerLiteral arg2 = (IntegerLiteral) e.getArguments().get(2).getValue(); |
| 333 | assertEquals(3, (int) arg2.getValue()); |
| 334 | } |
| 335 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 336 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 337 | public void testDoesntGetStuck() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 338 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 339 | |
| 340 | // Make sure the parser does not get stuck when trying |
| 341 | // to parse an expression containing a syntax error. |
| 342 | // This usually results in OutOfMemoryError because the |
| 343 | // parser keeps filling up the error log. |
| 344 | // We need to make sure that we will always advance |
| 345 | // in the token stream. |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 346 | parseExpression("f(1, ], 3)"); |
| 347 | parseExpression("f(1, ), 3)"); |
| 348 | parseExpression("[ ) for v in 3)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 349 | |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 350 | assertContainsError(""); // "" matches any, i.e., there were some events |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 353 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 354 | public void testSecondaryLocation() { |
| 355 | String expr = "f(1 % 2)"; |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 356 | FuncallExpression call = (FuncallExpression) parseExpression(expr); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 357 | Argument.Passed arg = call.getArguments().get(0); |
Francois-Rene Rideau | c673a82 | 2015-03-02 19:52:39 +0000 | [diff] [blame] | 358 | assertThat(arg.getLocation().getEndOffset()).isLessThan(call.getLocation().getEndOffset()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 361 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 362 | public void testPrimaryLocation() { |
| 363 | String expr = "f(1 + 2)"; |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 364 | FuncallExpression call = (FuncallExpression) parseExpression(expr); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 365 | Argument.Passed arg = call.getArguments().get(0); |
Francois-Rene Rideau | c673a82 | 2015-03-02 19:52:39 +0000 | [diff] [blame] | 366 | assertThat(arg.getLocation().getEndOffset()).isLessThan(call.getLocation().getEndOffset()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 369 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 370 | public void testAssignLocation() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 371 | List<Statement> statements = parseFile("a = b;c = d\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 372 | Statement statement = statements.get(0); |
| 373 | assertEquals(5, statement.getLocation().getEndOffset()); |
| 374 | } |
| 375 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 376 | @Test |
Laurent Le Brun | c9041bf | 2015-03-23 15:34:12 +0000 | [diff] [blame] | 377 | public void testAssignKeyword() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 378 | setFailFast(false); |
| 379 | parseExpression("with = 4"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 380 | assertContainsError("keyword 'with' not supported"); |
| 381 | assertContainsError("syntax error at 'with': expected expression"); |
Laurent Le Brun | 0ddcba2 | 2015-03-23 16:48:01 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | @Test |
Laurent Le Brun | ee991a1 | 2015-06-16 10:56:46 +0000 | [diff] [blame] | 385 | public void testBreak() { |
| 386 | setFailFast(false); |
| 387 | parseExpression("break"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 388 | assertContainsError("syntax error at 'break': expected expression"); |
Laurent Le Brun | ee991a1 | 2015-06-16 10:56:46 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Test |
Laurent Le Brun | 0ddcba2 | 2015-03-23 16:48:01 +0000 | [diff] [blame] | 392 | public void testTry() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 393 | setFailFast(false); |
| 394 | parseExpression("try: 1 + 1"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 395 | assertContainsError("'try' not supported, all exceptions are fatal"); |
| 396 | assertContainsError("syntax error at 'try': expected expression"); |
Laurent Le Brun | c9041bf | 2015-03-23 15:34:12 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Test |
Laurent Le Brun | 44ad7fa | 2016-10-11 12:09:05 +0000 | [diff] [blame] | 400 | public void testDel() { |
| 401 | setFailFast(false); |
| 402 | parseExpression("del d['a']"); |
| 403 | assertContainsError("'del' not supported, use '.pop()' to delete"); |
| 404 | } |
| 405 | |
| 406 | @Test |
Laurent Le Brun | 5609389 | 2015-03-20 13:01:58 +0000 | [diff] [blame] | 407 | public void testTupleAssign() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 408 | List<Statement> statements = parseFile("list[0] = 5; dict['key'] = value\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 409 | assertThat(statements).hasSize(2); |
Laurent Le Brun | 5609389 | 2015-03-20 13:01:58 +0000 | [diff] [blame] | 410 | assertThat(statements.get(0)).isInstanceOf(AssignmentStatement.class); |
| 411 | assertThat(statements.get(1)).isInstanceOf(AssignmentStatement.class); |
| 412 | } |
| 413 | |
| 414 | @Test |
| 415 | public void testAssign() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 416 | List<Statement> statements = parseFile("a, b = 5\n"); |
Laurent Le Brun | 5609389 | 2015-03-20 13:01:58 +0000 | [diff] [blame] | 417 | assertThat(statements).hasSize(1); |
| 418 | assertThat(statements.get(0)).isInstanceOf(AssignmentStatement.class); |
| 419 | AssignmentStatement assign = (AssignmentStatement) statements.get(0); |
| 420 | assertThat(assign.getLValue().getExpression()).isInstanceOf(ListLiteral.class); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 423 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 424 | public void testInvalidAssign() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 425 | setFailFast(false); |
| 426 | parseExpression("1 + (b = c)"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 427 | assertContainsError("syntax error"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 428 | clearEvents(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 431 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 432 | public void testAugmentedAssign() throws Exception { |
Vladimir Moskva | 7153664 | 2016-12-19 13:51:57 +0000 | [diff] [blame] | 433 | assertEquals("[x += 1\n]", parseFile("x += 1").toString()); |
| 434 | assertEquals("[x -= 1\n]", parseFile("x -= 1").toString()); |
| 435 | assertEquals("[x *= 1\n]", parseFile("x *= 1").toString()); |
| 436 | assertEquals("[x /= 1\n]", parseFile("x /= 1").toString()); |
| 437 | assertEquals("[x %= 1\n]", parseFile("x %= 1").toString()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 440 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 441 | public void testPrettyPrintFunctions() throws Exception { |
| 442 | assertEquals("[x[1:3]\n]", parseFile("x[1:3]").toString()); |
Vladimir Moskva | 8d610c6 | 2016-09-15 14:36:41 +0000 | [diff] [blame] | 443 | assertEquals("[x[1:3]\n]", parseFile("x[1:3:1]").toString()); |
| 444 | assertEquals("[x[1:3:2]\n]", parseFile("x[1:3:2]").toString()); |
| 445 | assertEquals("[x[1::2]\n]", parseFile("x[1::2]").toString()); |
| 446 | assertEquals("[x[1:]\n]", parseFile("x[1:]").toString()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 447 | assertEquals("[str[42]\n]", parseFile("str[42]").toString()); |
Michajlo Matijkiw | 8c539ea | 2017-02-22 23:02:46 +0000 | [diff] [blame] | 448 | assertEquals("[ctx.new_file(\"hello\")\n]", parseFile("ctx.new_file('hello')").toString()); |
| 449 | assertEquals("[new_file(\"hello\")\n]", parseFile("new_file(\"hello\")").toString()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 452 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 453 | public void testFuncallLocation() { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 454 | List<Statement> statements = parseFile("a(b);c = d\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 455 | Statement statement = statements.get(0); |
| 456 | assertEquals(4, statement.getLocation().getEndOffset()); |
| 457 | } |
| 458 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 459 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 460 | public void testSpecialFuncallLocation() throws Exception { |
| 461 | List<Statement> statements = parseFile("-x\n"); |
| 462 | assertLocation(0, 3, statements.get(0).getLocation()); |
| 463 | |
| 464 | statements = parseFile("arr[15]\n"); |
| 465 | assertLocation(0, 8, statements.get(0).getLocation()); |
| 466 | |
| 467 | statements = parseFile("str[1:12]\n"); |
| 468 | assertLocation(0, 10, statements.get(0).getLocation()); |
| 469 | } |
| 470 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 471 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 472 | public void testListPositions() throws Exception { |
| 473 | String expr = "[0,f(1),2]"; |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 474 | ListLiteral list = (ListLiteral) parseExpression(expr); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 475 | assertEquals("[0,f(1),2]", getText(expr, list)); |
| 476 | assertEquals("0", getText(expr, getElem(list, 0))); |
| 477 | assertEquals("f(1)", getText(expr, getElem(list, 1))); |
| 478 | assertEquals("2", getText(expr, getElem(list, 2))); |
| 479 | } |
| 480 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 481 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 482 | public void testDictPositions() throws Exception { |
| 483 | String expr = "{1:2,2:f(1),3:4}"; |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 484 | DictionaryLiteral list = (DictionaryLiteral) parseExpression(expr); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 485 | assertEquals("{1:2,2:f(1),3:4}", getText(expr, list)); |
| 486 | assertEquals("1:2", getText(expr, getElem(list, 0))); |
| 487 | assertEquals("2:f(1)", getText(expr, getElem(list, 1))); |
| 488 | assertEquals("3:4", getText(expr, getElem(list, 2))); |
| 489 | } |
| 490 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 491 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 492 | public void testArgumentPositions() throws Exception { |
| 493 | String stmt = "f(0,g(1,2),2)"; |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 494 | FuncallExpression f = (FuncallExpression) parseExpression(stmt); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 495 | assertEquals(stmt, getText(stmt, f)); |
| 496 | assertEquals("0", getText(stmt, getArg(f, 0))); |
| 497 | assertEquals("g(1,2)", getText(stmt, getArg(f, 1))); |
| 498 | assertEquals("2", getText(stmt, getArg(f, 2))); |
| 499 | } |
| 500 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 501 | @Test |
Laurent Le Brun | d412c8f | 2015-06-16 11:12:54 +0000 | [diff] [blame] | 502 | public void testForBreakContinue() throws Exception { |
| 503 | List<Statement> file = parseFileForSkylark( |
| 504 | "def foo():", |
| 505 | " for i in [1, 2]:", |
| 506 | " break", |
Laurent Le Brun | 7d6a381 | 2015-10-26 12:07:12 +0000 | [diff] [blame] | 507 | " continue", |
| 508 | " break"); |
Laurent Le Brun | d412c8f | 2015-06-16 11:12:54 +0000 | [diff] [blame] | 509 | assertThat(file).hasSize(1); |
| 510 | List<Statement> body = ((FunctionDefStatement) file.get(0)).getStatements(); |
| 511 | assertThat(body).hasSize(1); |
| 512 | |
| 513 | List<Statement> loop = ((ForStatement) body.get(0)).block(); |
Laurent Le Brun | 7d6a381 | 2015-10-26 12:07:12 +0000 | [diff] [blame] | 514 | assertThat(loop).hasSize(3); |
Laurent Le Brun | d412c8f | 2015-06-16 11:12:54 +0000 | [diff] [blame] | 515 | |
Laurent Le Brun | 7d6a381 | 2015-10-26 12:07:12 +0000 | [diff] [blame] | 516 | assertThat(((FlowStatement) loop.get(0)).getKind()).isEqualTo(FlowStatement.Kind.BREAK); |
Laurent Le Brun | a3c25a6 | 2016-10-26 10:59:09 +0000 | [diff] [blame] | 517 | assertLocation(34, 39, loop.get(0).getLocation()); |
Laurent Le Brun | d412c8f | 2015-06-16 11:12:54 +0000 | [diff] [blame] | 518 | |
Laurent Le Brun | 7d6a381 | 2015-10-26 12:07:12 +0000 | [diff] [blame] | 519 | assertThat(((FlowStatement) loop.get(1)).getKind()).isEqualTo(FlowStatement.Kind.CONTINUE); |
Laurent Le Brun | a3c25a6 | 2016-10-26 10:59:09 +0000 | [diff] [blame] | 520 | assertLocation(44, 52, loop.get(1).getLocation()); |
Laurent Le Brun | 7d6a381 | 2015-10-26 12:07:12 +0000 | [diff] [blame] | 521 | |
| 522 | assertThat(((FlowStatement) loop.get(2)).getKind()).isEqualTo(FlowStatement.Kind.BREAK); |
| 523 | assertLocation(57, 62, loop.get(2).getLocation()); |
Laurent Le Brun | d412c8f | 2015-06-16 11:12:54 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 527 | public void testListLiterals1() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 528 | ListLiteral list = (ListLiteral) parseExpression("[0,1,2]"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 529 | assertFalse(list.isTuple()); |
| 530 | assertThat(list.getElements()).hasSize(3); |
| 531 | assertFalse(list.isTuple()); |
| 532 | for (int i = 0; i < 3; ++i) { |
| 533 | assertEquals(i, getIntElem(list, i)); |
| 534 | } |
| 535 | } |
| 536 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 537 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 538 | public void testTupleLiterals2() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 539 | ListLiteral tuple = (ListLiteral) parseExpression("(0,1,2)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 540 | assertTrue(tuple.isTuple()); |
| 541 | assertThat(tuple.getElements()).hasSize(3); |
| 542 | assertTrue(tuple.isTuple()); |
| 543 | for (int i = 0; i < 3; ++i) { |
| 544 | assertEquals(i, getIntElem(tuple, i)); |
| 545 | } |
| 546 | } |
| 547 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 548 | @Test |
Laurent Le Brun | 5609389 | 2015-03-20 13:01:58 +0000 | [diff] [blame] | 549 | public void testTupleWithoutParens() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 550 | ListLiteral tuple = (ListLiteral) parseExpression("0, 1, 2"); |
Laurent Le Brun | 5609389 | 2015-03-20 13:01:58 +0000 | [diff] [blame] | 551 | assertTrue(tuple.isTuple()); |
| 552 | assertThat(tuple.getElements()).hasSize(3); |
| 553 | assertTrue(tuple.isTuple()); |
| 554 | for (int i = 0; i < 3; ++i) { |
| 555 | assertEquals(i, getIntElem(tuple, i)); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | @Test |
Laurent Le Brun | b639ca8 | 2017-01-17 11:18:23 +0000 | [diff] [blame] | 560 | public void testTupleWithTrailingComma() throws Exception { |
| 561 | setFailFast(false); |
| 562 | |
| 563 | // Unlike Python, we require parens here. |
| 564 | parseExpression("0, 1, 2, 3,"); |
| 565 | assertContainsError("Trailing comma"); |
| 566 | clearEvents(); |
| 567 | |
| 568 | parseExpression("1 + 2,"); |
| 569 | assertContainsError("Trailing comma"); |
| 570 | clearEvents(); |
| 571 | |
| 572 | ListLiteral tuple = (ListLiteral) parseExpression("(0, 1, 2, 3,)"); |
Laurent Le Brun | 5609389 | 2015-03-20 13:01:58 +0000 | [diff] [blame] | 573 | assertTrue(tuple.isTuple()); |
| 574 | assertThat(tuple.getElements()).hasSize(4); |
| 575 | assertTrue(tuple.isTuple()); |
| 576 | for (int i = 0; i < 4; ++i) { |
| 577 | assertEquals(i, getIntElem(tuple, i)); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 582 | public void testTupleLiterals3() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 583 | ListLiteral emptyTuple = (ListLiteral) parseExpression("()"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 584 | assertTrue(emptyTuple.isTuple()); |
| 585 | assertThat(emptyTuple.getElements()).isEmpty(); |
| 586 | } |
| 587 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 588 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 589 | public void testTupleLiterals4() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 590 | ListLiteral singletonTuple = (ListLiteral) parseExpression("(42,)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 591 | assertTrue(singletonTuple.isTuple()); |
| 592 | assertThat(singletonTuple.getElements()).hasSize(1); |
| 593 | assertEquals(42, getIntElem(singletonTuple, 0)); |
| 594 | } |
| 595 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 596 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 597 | public void testTupleLiterals5() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 598 | IntegerLiteral intLit = (IntegerLiteral) parseExpression("(42)"); // not a tuple! |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 599 | assertEquals(42, (int) intLit.getValue()); |
| 600 | } |
| 601 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 602 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 603 | public void testListLiterals6() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 604 | ListLiteral emptyList = (ListLiteral) parseExpression("[]"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 605 | assertFalse(emptyList.isTuple()); |
| 606 | assertThat(emptyList.getElements()).isEmpty(); |
| 607 | } |
| 608 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 609 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 610 | public void testListLiterals7() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 611 | ListLiteral singletonList = (ListLiteral) parseExpression("[42,]"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 612 | assertFalse(singletonList.isTuple()); |
| 613 | assertThat(singletonList.getElements()).hasSize(1); |
| 614 | assertEquals(42, getIntElem(singletonList, 0)); |
| 615 | } |
| 616 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 617 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 618 | public void testListLiterals8() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 619 | ListLiteral singletonList = (ListLiteral) parseExpression("[42]"); // a singleton |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 620 | assertFalse(singletonList.isTuple()); |
| 621 | assertThat(singletonList.getElements()).hasSize(1); |
| 622 | assertEquals(42, getIntElem(singletonList, 0)); |
| 623 | } |
| 624 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 625 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 626 | public void testDictionaryLiterals() throws Exception { |
| 627 | DictionaryLiteral dictionaryList = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 628 | (DictionaryLiteral) parseExpression("{1:42}"); // a singleton dictionary |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 629 | assertThat(dictionaryList.getEntries()).hasSize(1); |
| 630 | DictionaryEntryLiteral tuple = getElem(dictionaryList, 0); |
| 631 | assertEquals(1, getIntElem(tuple, true)); |
| 632 | assertEquals(42, getIntElem(tuple, false)); |
| 633 | } |
| 634 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 635 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 636 | public void testDictionaryLiterals1() throws Exception { |
| 637 | DictionaryLiteral dictionaryList = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 638 | (DictionaryLiteral) parseExpression("{}"); // an empty dictionary |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 639 | assertThat(dictionaryList.getEntries()).isEmpty(); |
| 640 | } |
| 641 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 642 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 643 | public void testDictionaryLiterals2() throws Exception { |
| 644 | DictionaryLiteral dictionaryList = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 645 | (DictionaryLiteral) parseExpression("{1:42,}"); // a singleton dictionary |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 646 | assertThat(dictionaryList.getEntries()).hasSize(1); |
| 647 | DictionaryEntryLiteral tuple = getElem(dictionaryList, 0); |
| 648 | assertEquals(1, getIntElem(tuple, true)); |
| 649 | assertEquals(42, getIntElem(tuple, false)); |
| 650 | } |
| 651 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 652 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 653 | public void testDictionaryLiterals3() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 654 | DictionaryLiteral dictionaryList = (DictionaryLiteral) parseExpression("{1:42,2:43,3:44}"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 655 | assertThat(dictionaryList.getEntries()).hasSize(3); |
| 656 | for (int i = 0; i < 3; i++) { |
| 657 | DictionaryEntryLiteral tuple = getElem(dictionaryList, i); |
| 658 | assertEquals(i + 1, getIntElem(tuple, true)); |
| 659 | assertEquals(i + 42, getIntElem(tuple, false)); |
| 660 | } |
| 661 | } |
| 662 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 663 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 664 | public void testListLiterals9() throws Exception { |
| 665 | ListLiteral singletonList = |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 666 | (ListLiteral) parseExpression("[ abi + opt_level + \'/include\' ]"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 667 | assertFalse(singletonList.isTuple()); |
| 668 | assertThat(singletonList.getElements()).hasSize(1); |
| 669 | } |
| 670 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 671 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 672 | public void testListComprehensionSyntax() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 673 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 674 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 675 | parseExpression("[x for"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 676 | assertContainsError("syntax error at 'newline'"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 677 | clearEvents(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 678 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 679 | parseExpression("[x for x"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 680 | assertContainsError("syntax error at 'newline'"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 681 | clearEvents(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 682 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 683 | parseExpression("[x for x in"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 684 | assertContainsError("syntax error at 'newline'"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 685 | clearEvents(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 686 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 687 | parseExpression("[x for x in []"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 688 | assertContainsError("syntax error at 'newline'"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 689 | clearEvents(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 690 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 691 | parseExpression("[x for x for y in ['a']]"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 692 | assertContainsError("syntax error at 'for'"); |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 693 | clearEvents(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 696 | @Test |
Laurent Le Brun | 5202166 | 2015-05-18 09:28:26 +0000 | [diff] [blame] | 697 | public void testListComprehensionEmptyList() throws Exception { |
| 698 | List<ListComprehension.Clause> clauses = ((ListComprehension) parseExpression( |
| 699 | "['foo/%s.java' % x for x in []]")).getClauses(); |
| 700 | assertThat(clauses).hasSize(1); |
| 701 | assertThat(clauses.get(0).getExpression().toString()).isEqualTo("[]"); |
| 702 | assertThat(clauses.get(0).getLValue().getExpression().toString()).isEqualTo("x"); |
| 703 | } |
| 704 | |
| 705 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 706 | public void testListComprehension() throws Exception { |
Laurent Le Brun | 5202166 | 2015-05-18 09:28:26 +0000 | [diff] [blame] | 707 | List<ListComprehension.Clause> clauses = ((ListComprehension) parseExpression( |
| 708 | "['foo/%s.java' % x for x in ['bar', 'wiz', 'quux']]")).getClauses(); |
| 709 | assertThat(clauses).hasSize(1); |
| 710 | assertThat(clauses.get(0).getLValue().getExpression().toString()).isEqualTo("x"); |
| 711 | assertThat(clauses.get(0).getExpression()).isInstanceOf(ListLiteral.class); |
| 712 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 713 | |
Laurent Le Brun | 5202166 | 2015-05-18 09:28:26 +0000 | [diff] [blame] | 714 | @Test |
| 715 | public void testForForListComprehension() throws Exception { |
| 716 | List<ListComprehension.Clause> clauses = ((ListComprehension) parseExpression( |
| 717 | "['%s/%s.java' % (x, y) for x in ['foo', 'bar'] for y in list]")).getClauses(); |
| 718 | assertThat(clauses).hasSize(2); |
| 719 | assertThat(clauses.get(0).getLValue().getExpression().toString()).isEqualTo("x"); |
| 720 | assertThat(clauses.get(0).getExpression()).isInstanceOf(ListLiteral.class); |
| 721 | assertThat(clauses.get(1).getLValue().getExpression().toString()).isEqualTo("y"); |
Florian Weikert | 6f864c3 | 2015-07-23 11:26:39 +0000 | [diff] [blame] | 722 | assertThat(clauses.get(1).getExpression()).isInstanceOf(Identifier.class); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 725 | @Test |
Laurent Le Brun | 9060e16 | 2015-04-02 10:07:28 +0000 | [diff] [blame] | 726 | public void testParserRecovery() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 727 | setFailFast(false); |
| 728 | List<Statement> statements = parseFileForSkylark( |
Laurent Le Brun | 9060e16 | 2015-04-02 10:07:28 +0000 | [diff] [blame] | 729 | "def foo():", |
Laurent Le Brun | e3f4ed7 | 2015-05-08 14:47:26 +0000 | [diff] [blame] | 730 | " a = 2 for 4", // parse error |
Laurent Le Brun | 9060e16 | 2015-04-02 10:07:28 +0000 | [diff] [blame] | 731 | " b = [3, 4]", |
| 732 | "", |
| 733 | "d = 4 ada", // parse error |
| 734 | "", |
| 735 | "def bar():", |
| 736 | " a = [3, 4]", |
| 737 | " b = 2 + + 5", // parse error |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 738 | ""); |
Laurent Le Brun | 9060e16 | 2015-04-02 10:07:28 +0000 | [diff] [blame] | 739 | |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 740 | assertThat(getEventCollector()).hasSize(3); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 741 | assertContainsError("syntax error at 'for': expected newline"); |
| 742 | assertContainsError("syntax error at 'ada': expected newline"); |
| 743 | assertContainsError("syntax error at '+': expected expression"); |
Laurent Le Brun | 9060e16 | 2015-04-02 10:07:28 +0000 | [diff] [blame] | 744 | assertThat(statements).hasSize(3); |
| 745 | } |
| 746 | |
| 747 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 748 | public void testParserContainsErrorsIfSyntaxException() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 749 | setFailFast(false); |
| 750 | parseExpression("'foo' %%"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 751 | assertContainsError("syntax error at '%'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 754 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 755 | public void testParserDoesNotContainErrorsIfSuccess() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 756 | parseExpression("'foo'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 759 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 760 | public void testParserContainsErrors() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 761 | setFailFast(false); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 762 | parseFile("+"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 763 | assertContainsError("syntax error at '+'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 766 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 767 | public void testSemicolonAndNewline() throws Exception { |
| 768 | List<Statement> stmts = parseFile( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 769 | "foo='bar'; foo(bar)", |
| 770 | "", |
| 771 | "foo='bar'; foo(bar)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 772 | assertThat(stmts).hasSize(4); |
| 773 | } |
| 774 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 775 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 776 | public void testSemicolonAndNewline2() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 777 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 778 | List<Statement> stmts = parseFile( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 779 | "foo='foo' error(bar)", |
| 780 | "", |
| 781 | ""); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 782 | assertContainsError("syntax error at 'error'"); |
Laurent Le Brun | 9060e16 | 2015-04-02 10:07:28 +0000 | [diff] [blame] | 783 | assertThat(stmts).hasSize(1); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 786 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 787 | public void testExprAsStatement() throws Exception { |
| 788 | List<Statement> stmts = parseFile( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 789 | "li = []", |
| 790 | "li.append('a.c')", |
| 791 | "\"\"\" string comment \"\"\"", |
| 792 | "foo(bar)"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 793 | assertThat(stmts).hasSize(4); |
| 794 | } |
| 795 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 796 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 797 | public void testParseBuildFileWithSingeRule() throws Exception { |
| 798 | List<Statement> stmts = parseFile( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 799 | "genrule(name = 'foo',", |
| 800 | " srcs = ['input.csv'],", |
| 801 | " outs = [ 'result.txt',", |
| 802 | " 'result.log'],", |
| 803 | " cmd = 'touch result.txt result.log')", |
| 804 | ""); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 805 | assertThat(stmts).hasSize(1); |
| 806 | } |
| 807 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 808 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 809 | public void testParseBuildFileWithMultipleRules() throws Exception { |
| 810 | List<Statement> stmts = parseFile( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 811 | "genrule(name = 'foo',", |
| 812 | " srcs = ['input.csv'],", |
| 813 | " outs = [ 'result.txt',", |
| 814 | " 'result.log'],", |
| 815 | " cmd = 'touch result.txt result.log')", |
| 816 | "", |
| 817 | "genrule(name = 'bar',", |
| 818 | " srcs = ['input.csv'],", |
| 819 | " outs = [ 'graph.svg'],", |
| 820 | " cmd = 'touch graph.svg')"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 821 | assertThat(stmts).hasSize(2); |
| 822 | } |
| 823 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 824 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 825 | public void testParseBuildFileWithComments() throws Exception { |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 826 | BuildFileAST result = parseFileWithComments( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 827 | "# Test BUILD file", |
| 828 | "# with multi-line comment", |
| 829 | "", |
| 830 | "genrule(name = 'foo',", |
| 831 | " srcs = ['input.csv'],", |
| 832 | " outs = [ 'result.txt',", |
| 833 | " 'result.log'],", |
| 834 | " cmd = 'touch result.txt result.log')"); |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 835 | assertThat(result.getStatements()).hasSize(1); |
| 836 | assertThat(result.getComments()).hasSize(2); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 839 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 840 | public void testParseBuildFileWithManyComments() throws Exception { |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 841 | BuildFileAST result = parseFileWithComments( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 842 | "# 1", |
| 843 | "# 2", |
| 844 | "", |
| 845 | "# 4 ", |
| 846 | "# 5", |
| 847 | "#", // 6 - find empty comment for syntax highlighting |
| 848 | "# 7 ", |
| 849 | "# 8", |
| 850 | "genrule(name = 'foo',", |
| 851 | " srcs = ['input.csv'],", |
| 852 | " # 11", |
| 853 | " outs = [ 'result.txt',", |
| 854 | " 'result.log'], # 13", |
| 855 | " cmd = 'touch result.txt result.log')", |
| 856 | "# 15"); |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 857 | assertThat(result.getStatements()).hasSize(1); // Single genrule |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 858 | StringBuilder commentLines = new StringBuilder(); |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 859 | for (Comment comment : result.getComments()) { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 860 | // Comments start and end on the same line |
| 861 | assertEquals(comment.getLocation().getStartLineAndColumn().getLine() + " ends on " |
| 862 | + comment.getLocation().getEndLineAndColumn().getLine(), |
| 863 | comment.getLocation().getStartLineAndColumn().getLine(), |
| 864 | comment.getLocation().getEndLineAndColumn().getLine()); |
| 865 | commentLines.append('('); |
| 866 | commentLines.append(comment.getLocation().getStartLineAndColumn().getLine()); |
| 867 | commentLines.append(','); |
| 868 | commentLines.append(comment.getLocation().getStartLineAndColumn().getColumn()); |
| 869 | commentLines.append(") "); |
| 870 | } |
| 871 | assertWithMessage("Found: " + commentLines) |
Laurent Le Brun | 8e965b8 | 2016-08-03 11:50:24 +0000 | [diff] [blame] | 872 | .that(result.getComments().size()).isEqualTo(10); // One per '#' |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 875 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 876 | public void testMissingComma() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 877 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 878 | // Regression test. |
| 879 | // Note: missing comma after name='foo' |
| 880 | parseFile("genrule(name = 'foo'\n" |
| 881 | + " srcs = ['in'])"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 882 | assertContainsError("syntax error at 'srcs'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 885 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 886 | public void testDoubleSemicolon() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 887 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 888 | // Regression test. |
| 889 | parseFile("x = 1; ; x = 2;"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 890 | assertContainsError("syntax error at ';'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 893 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 894 | public void testFunctionDefinitionErrorRecovery() throws Exception { |
| 895 | // Parser skips over entire function definitions, and reports a meaningful |
| 896 | // error. |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 897 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 898 | List<Statement> stmts = parseFile( |
| 899 | "x = 1;\n" |
| 900 | + "def foo(x, y, **z):\n" |
| 901 | + " # a comment\n" |
| 902 | + " x = 2\n" |
| 903 | + " foo(bar)\n" |
| 904 | + " return z\n" |
| 905 | + "x = 3"); |
| 906 | assertThat(stmts).hasSize(2); |
| 907 | } |
| 908 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 909 | @Test |
Laurent Le Brun | 5f67445 | 2015-03-17 19:29:13 +0000 | [diff] [blame] | 910 | public void testDefSingleLine() throws Exception { |
| 911 | List<Statement> statements = parseFileForSkylark( |
| 912 | "def foo(): x = 1; y = 2\n"); |
| 913 | FunctionDefStatement stmt = (FunctionDefStatement) statements.get(0); |
| 914 | assertThat(stmt.getStatements()).hasSize(2); |
| 915 | } |
| 916 | |
| 917 | @Test |
Laurent Le Brun | 0942ee9 | 2015-03-17 20:22:16 +0000 | [diff] [blame] | 918 | public void testPass() throws Exception { |
| 919 | List<Statement> statements = parseFileForSkylark("pass\n"); |
| 920 | assertThat(statements).isEmpty(); |
| 921 | } |
| 922 | |
| 923 | @Test |
| 924 | public void testForPass() throws Exception { |
| 925 | List<Statement> statements = parseFileForSkylark( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 926 | "def foo():", |
| 927 | " pass\n"); |
Laurent Le Brun | 0942ee9 | 2015-03-17 20:22:16 +0000 | [diff] [blame] | 928 | |
| 929 | assertThat(statements).hasSize(1); |
| 930 | FunctionDefStatement stmt = (FunctionDefStatement) statements.get(0); |
| 931 | assertThat(stmt.getStatements()).isEmpty(); |
| 932 | } |
| 933 | |
| 934 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 935 | public void testSkipIfBlockFail() throws Exception { |
| 936 | // Do not parse 'if' blocks, when parsePython is not set |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 937 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 938 | List<Statement> stmts = parseFile( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 939 | "x = 1;", |
| 940 | "if x == 1:", |
| 941 | " x = 2", |
| 942 | "x = 3;\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 943 | assertThat(stmts).hasSize(2); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 944 | assertContainsError("This is not supported in BUILD files"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 947 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 948 | public void testForLoopMultipleVariables() throws Exception { |
Laurent Le Brun | 185392d | 2015-03-20 14:41:25 +0000 | [diff] [blame] | 949 | List<Statement> stmts1 = parseFile("[ i for i, j, k in [(1, 2, 3)] ]\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 950 | assertThat(stmts1).hasSize(1); |
| 951 | |
Laurent Le Brun | 185392d | 2015-03-20 14:41:25 +0000 | [diff] [blame] | 952 | List<Statement> stmts2 = parseFile("[ i for i, j in [(1, 2, 3)] ]\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 953 | assertThat(stmts2).hasSize(1); |
| 954 | |
Laurent Le Brun | 185392d | 2015-03-20 14:41:25 +0000 | [diff] [blame] | 955 | List<Statement> stmts3 = parseFile("[ i for (i, j, k) in [(1, 2, 3)] ]\n"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 956 | assertThat(stmts3).hasSize(1); |
| 957 | } |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 958 | |
Googler | cc0d995 | 2015-08-10 12:01:34 +0000 | [diff] [blame] | 959 | @Test |
| 960 | public void testReturnNone() throws Exception { |
| 961 | List<Statement> defNone = parseFileForSkylark("def foo():", " return None\n"); |
| 962 | assertThat(defNone).hasSize(1); |
| 963 | |
| 964 | List<Statement> bodyNone = ((FunctionDefStatement) defNone.get(0)).getStatements(); |
| 965 | assertThat(bodyNone).hasSize(1); |
| 966 | |
| 967 | ReturnStatement returnNone = (ReturnStatement) bodyNone.get(0); |
| 968 | assertEquals("None", ((Identifier) returnNone.getReturnExpression()).getName()); |
| 969 | |
| 970 | int i = 0; |
| 971 | for (String end : new String[]{";", "\n"}) { |
| 972 | List<Statement> defNoExpr = parseFileForSkylark("def bar" + i + "():", " return" + end); |
| 973 | i++; |
| 974 | assertThat(defNoExpr).hasSize(1); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 975 | |
Googler | cc0d995 | 2015-08-10 12:01:34 +0000 | [diff] [blame] | 976 | List<Statement> bodyNoExpr = ((FunctionDefStatement) defNoExpr.get(0)).getStatements(); |
| 977 | assertThat(bodyNoExpr).hasSize(1); |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 978 | |
Googler | cc0d995 | 2015-08-10 12:01:34 +0000 | [diff] [blame] | 979 | ReturnStatement returnNoExpr = (ReturnStatement) bodyNoExpr.get(0); |
| 980 | Identifier none = (Identifier) returnNoExpr.getReturnExpression(); |
| 981 | assertEquals("None", none.getName()); |
| 982 | assertLocation( |
| 983 | returnNoExpr.getLocation().getStartOffset(), |
| 984 | returnNoExpr.getLocation().getEndOffset(), |
| 985 | none.getLocation()); |
| 986 | } |
| 987 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 988 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 989 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 990 | public void testForLoopBadSyntax() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 991 | setFailFast(false); |
Laurent Le Brun | 185392d | 2015-03-20 14:41:25 +0000 | [diff] [blame] | 992 | parseFile("[1 for (a, b, c in var]\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 993 | assertContainsError("syntax error"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 996 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 997 | public void testForLoopBadSyntax2() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 998 | setFailFast(false); |
Laurent Le Brun | 185392d | 2015-03-20 14:41:25 +0000 | [diff] [blame] | 999 | parseFile("[1 for in var]\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1000 | assertContainsError("syntax error"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1003 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1004 | public void testFunCallBadSyntax() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1005 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1006 | parseFile("f(1,\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1007 | assertContainsError("syntax error"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1010 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1011 | public void testFunCallBadSyntax2() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1012 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1013 | parseFile("f(1, 5, ,)\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1014 | assertContainsError("syntax error"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Florian Weikert | 308c0bf | 2015-07-10 10:46:56 +0000 | [diff] [blame] | 1017 | @Test |
Miguel Alcon Pinto | 927f3b2 | 2016-08-22 14:21:30 +0000 | [diff] [blame] | 1018 | public void testValidAbsoluteImportPath() throws SkylarkImportSyntaxException { |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1019 | String importString = "/some/skylark/file"; |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1020 | List<Statement> statements = |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1021 | parseFileForSkylark("load('" + importString + "', 'fun_test')\n"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1022 | LoadStatement stmt = (LoadStatement) statements.get(0); |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1023 | SkylarkImport imp = SkylarkImports.create(stmt.getImport().getValue()); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1024 | |
| 1025 | assertThat(imp.getImportString()).named("getImportString()").isEqualTo("/some/skylark/file"); |
| 1026 | assertThat(imp.hasAbsolutePath()).named("hasAbsolutePath()").isTrue(); |
| 1027 | assertThat(imp.getAbsolutePath()).named("getAbsolutePath()") |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame^] | 1028 | .isEqualTo(PathFragment.create("/some/skylark/file.bzl")); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1029 | |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1030 | int startOffset = stmt.getImport().getLocation().getStartOffset(); |
| 1031 | int endOffset = stmt.getImport().getLocation().getEndOffset(); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1032 | assertThat(startOffset).named("getStartOffset()").isEqualTo(5); |
| 1033 | assertThat(endOffset).named("getEndOffset()") |
| 1034 | .isEqualTo(startOffset + importString.length() + 2); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | private void validNonAbsoluteImportTest(String importString, String containingFileLabelString, |
Miguel Alcon Pinto | 927f3b2 | 2016-08-22 14:21:30 +0000 | [diff] [blame] | 1038 | String expectedLabelString) throws SkylarkImportSyntaxException { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1039 | List<Statement> statements = |
| 1040 | parseFileForSkylark("load('" + importString + "', 'fun_test')\n"); |
| 1041 | LoadStatement stmt = (LoadStatement) statements.get(0); |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1042 | SkylarkImport imp = SkylarkImports.create(stmt.getImport().getValue()); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1043 | |
| 1044 | assertThat(imp.getImportString()).named("getImportString()").isEqualTo(importString); |
| 1045 | assertThat(imp.hasAbsolutePath()).named("hasAbsolutePath()").isFalse(); |
| 1046 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1047 | Label containingFileLabel = Label.parseAbsoluteUnchecked(containingFileLabelString); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1048 | assertThat(imp.getLabel(containingFileLabel)).named("containingFileLabel()") |
Michajlo Matijkiw | 8c539ea | 2017-02-22 23:02:46 +0000 | [diff] [blame] | 1049 | .isEqualTo(Label.parseAbsoluteUnchecked(expectedLabelString)); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1050 | |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1051 | int startOffset = stmt.getImport().getLocation().getStartOffset(); |
| 1052 | int endOffset = stmt.getImport().getLocation().getEndOffset(); |
John Field | 9201fda | 2015-12-30 19:30:34 +0000 | [diff] [blame] | 1053 | assertThat(startOffset).named("getStartOffset()").isEqualTo(5); |
| 1054 | assertThat(endOffset).named("getEndOffset()") |
| 1055 | .isEqualTo(startOffset + importString.length() + 2); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | private void invalidImportTest(String importString, String expectedMsg) { |
Florian Weikert | 308c0bf | 2015-07-10 10:46:56 +0000 | [diff] [blame] | 1059 | setFailFast(false); |
Michajlo Matijkiw | 8c539ea | 2017-02-22 23:02:46 +0000 | [diff] [blame] | 1060 | parseFileForSkylark("load('" + importString + "', 'fun_test')\n"); |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 1061 | assertContainsError(expectedMsg); |
Florian Weikert | 308c0bf | 2015-07-10 10:46:56 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | @Test |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1065 | public void testValidRelativeImportPathInPackageDir() throws Exception { |
| 1066 | validNonAbsoluteImportTest("file", /*containing*/ "//some/skylark:BUILD", |
| 1067 | /*expected*/ "//some/skylark:file.bzl"); |
Florian Weikert | 308c0bf | 2015-07-10 10:46:56 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1070 | @Test |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1071 | public void testValidRelativeImportPathInPackageSubdir() throws Exception { |
| 1072 | validNonAbsoluteImportTest("file", /*containing*/ "//some/path/to:skylark/parent.bzl", |
| 1073 | /*expected*/ "//some/path/to:skylark/file.bzl"); |
| 1074 | } |
| 1075 | |
| 1076 | @Test |
| 1077 | public void testInvalidRelativePathBzlExtImplicit() throws Exception { |
Laurent Le Brun | 226321c | 2016-03-31 09:56:49 +0000 | [diff] [blame] | 1078 | invalidImportTest("file.bzl", SkylarkImports.INVALID_PATH_SYNTAX); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | @Test |
| 1082 | public void testInvalidRelativePathNoSubdirs() throws Exception { |
Laurent Le Brun | 226321c | 2016-03-31 09:56:49 +0000 | [diff] [blame] | 1083 | invalidImportTest("path/to/file", SkylarkImports.INVALID_PATH_SYNTAX); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | @Test |
| 1087 | public void testInvalidRelativePathInvalidFilename() throws Exception { |
| 1088 | invalidImportTest("\tfile", SkylarkImports.INVALID_FILENAME_PREFIX); |
| 1089 | } |
| 1090 | |
Miguel Alcon Pinto | 927f3b2 | 2016-08-22 14:21:30 +0000 | [diff] [blame] | 1091 | private void validAbsoluteImportLabelTest(String importString) |
| 1092 | throws SkylarkImportSyntaxException { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1093 | validNonAbsoluteImportTest(importString, /*irrelevant*/ "//another/path:BUILD", |
| 1094 | /*expected*/ importString); |
| 1095 | } |
| 1096 | |
| 1097 | @Test |
| 1098 | public void testValidAbsoluteImportLabel() throws Exception { |
| 1099 | validAbsoluteImportLabelTest("//some/skylark:file.bzl"); |
| 1100 | } |
| 1101 | |
| 1102 | @Test |
| 1103 | public void testValidAbsoluteImportLabelWithRepo() throws Exception { |
| 1104 | validAbsoluteImportLabelTest("@my_repo//some/skylark:file.bzl"); |
| 1105 | } |
| 1106 | |
| 1107 | @Test |
| 1108 | public void testInvalidAbsoluteImportLabel() throws Exception { |
| 1109 | invalidImportTest("//some/skylark/:file.bzl", SkylarkImports.INVALID_LABEL_PREFIX); |
| 1110 | } |
| 1111 | |
| 1112 | @Test |
| 1113 | public void testInvalidAbsoluteImportLabelWithRepo() throws Exception { |
| 1114 | invalidImportTest("@my_repo//some/skylark/:file.bzl", |
| 1115 | SkylarkImports.INVALID_LABEL_PREFIX); |
| 1116 | } |
| 1117 | |
| 1118 | @Test |
| 1119 | public void testInvalidAbsoluteImportLabelMissingBzlExt() throws Exception { |
| 1120 | invalidImportTest("//some/skylark:file", SkylarkImports.MUST_HAVE_BZL_EXT_MSG); |
| 1121 | } |
| 1122 | |
| 1123 | @Test |
| 1124 | public void testInvalidAbsoluteImportReferencesExternalPkg() throws Exception { |
| 1125 | invalidImportTest("//external:file.bzl", SkylarkImports.EXTERNAL_PKG_NOT_ALLOWED_MSG); |
| 1126 | } |
| 1127 | |
| 1128 | @Test |
| 1129 | public void testValidRelativeImportSimpleLabelInPackageDir() throws Exception { |
| 1130 | validNonAbsoluteImportTest(":file.bzl", /*containing*/ "//some/skylark:BUILD", |
| 1131 | /*expected*/ "//some/skylark:file.bzl"); |
| 1132 | } |
| 1133 | |
| 1134 | @Test |
| 1135 | public void testValidRelativeImportSimpleLabelInPackageSubdir() throws Exception { |
| 1136 | validNonAbsoluteImportTest(":file.bzl", /*containing*/ "//some/path/to:skylark/parent.bzl", |
| 1137 | /*expected*/ "//some/path/to:file.bzl"); |
| 1138 | } |
| 1139 | |
| 1140 | @Test |
| 1141 | public void testValidRelativeImportComplexLabelInPackageDir() throws Exception { |
| 1142 | validNonAbsoluteImportTest(":subdir/containing/file.bzl", /*containing*/ "//some/skylark:BUILD", |
| 1143 | /*expected*/ "//some/skylark:subdir/containing/file.bzl"); |
| 1144 | } |
| 1145 | |
| 1146 | @Test |
| 1147 | public void testValidRelativeImportComplexLabelInPackageSubdir() throws Exception { |
| 1148 | validNonAbsoluteImportTest(":subdir/containing/file.bzl", |
| 1149 | /*containing*/ "//some/path/to:skylark/parent.bzl", |
| 1150 | /*expected*/ "//some/path/to:subdir/containing/file.bzl"); |
| 1151 | } |
| 1152 | |
| 1153 | @Test |
| 1154 | public void testInvalidRelativeImportLabelMissingBzlExt() throws Exception { |
| 1155 | invalidImportTest(":file", SkylarkImports.MUST_HAVE_BZL_EXT_MSG); |
| 1156 | } |
| 1157 | |
| 1158 | @Test |
| 1159 | public void testInvalidRelativeImportLabelSyntax() throws Exception { |
| 1160 | invalidImportTest("::file.bzl", SkylarkImports.INVALID_TARGET_PREFIX); |
| 1161 | } |
| 1162 | |
| 1163 | @Test |
Laurent Le Brun | 73a9849 | 2015-03-17 15:46:19 +0000 | [diff] [blame] | 1164 | public void testLoadNoSymbol() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1165 | setFailFast(false); |
Laurent Le Brun | 73a9849 | 2015-03-17 15:46:19 +0000 | [diff] [blame] | 1166 | parseFileForSkylark("load('/foo/bar/file')\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1167 | assertContainsError("syntax error"); |
Laurent Le Brun | 73a9849 | 2015-03-17 15:46:19 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1171 | public void testLoadOneSymbol() throws Exception { |
| 1172 | List<Statement> statements = parseFileForSkylark( |
| 1173 | "load('/foo/bar/file', 'fun_test')\n"); |
| 1174 | LoadStatement stmt = (LoadStatement) statements.get(0); |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1175 | assertEquals("/foo/bar/file", stmt.getImport().getValue()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1176 | assertThat(stmt.getSymbols()).hasSize(1); |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 1177 | Identifier sym = stmt.getSymbols().get(0); |
| 1178 | int startOffset = sym.getLocation().getStartOffset(); |
| 1179 | int endOffset = sym.getLocation().getEndOffset(); |
| 1180 | assertThat(startOffset).named("getStartOffset()").isEqualTo(22); |
| 1181 | assertThat(endOffset).named("getEndOffset()").isEqualTo(startOffset + 10); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1184 | @Test |
Laurent Le Brun | 59f587a | 2015-03-16 14:51:36 +0000 | [diff] [blame] | 1185 | public void testLoadOneSymbolWithTrailingComma() throws Exception { |
| 1186 | List<Statement> statements = parseFileForSkylark( |
| 1187 | "load('/foo/bar/file', 'fun_test',)\n"); |
| 1188 | LoadStatement stmt = (LoadStatement) statements.get(0); |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1189 | assertEquals("/foo/bar/file", stmt.getImport().getValue()); |
Laurent Le Brun | 59f587a | 2015-03-16 14:51:36 +0000 | [diff] [blame] | 1190 | assertThat(stmt.getSymbols()).hasSize(1); |
| 1191 | } |
| 1192 | |
| 1193 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1194 | public void testLoadMultipleSymbols() throws Exception { |
| 1195 | List<Statement> statements = parseFileForSkylark( |
| 1196 | "load('file', 'foo', 'bar')\n"); |
| 1197 | LoadStatement stmt = (LoadStatement) statements.get(0); |
Laurent Le Brun | 7b1708c | 2016-10-13 10:05:12 +0000 | [diff] [blame] | 1198 | assertEquals("file", stmt.getImport().getValue()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1199 | assertThat(stmt.getSymbols()).hasSize(2); |
| 1200 | } |
| 1201 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1202 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1203 | public void testLoadSyntaxError() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1204 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1205 | parseFileForSkylark("load(non_quoted, 'a')\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1206 | assertContainsError("syntax error"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1209 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1210 | public void testLoadSyntaxError2() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1211 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1212 | parseFileForSkylark("load('non_quoted', a)\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1213 | assertContainsError("syntax error"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1216 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1217 | public void testLoadNotAtTopLevel() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1218 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1219 | parseFileForSkylark("if 1: load(8)\n"); |
Laurent Le Brun | e102a2d | 2017-01-02 12:06:18 +0000 | [diff] [blame] | 1220 | assertContainsError("name 'load' is not defined"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1223 | @Test |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1224 | public void testLoadAlias() throws Exception { |
Jon Brandvein | ee8b7aa | 2016-07-28 15:01:26 +0000 | [diff] [blame] | 1225 | List<Statement> statements = parseFileForSkylark( |
| 1226 | "load('/foo/bar/file', my_alias = 'lawl')\n"); |
| 1227 | LoadStatement stmt = (LoadStatement) statements.get(0); |
| 1228 | ImmutableList<Identifier> actualSymbols = stmt.getSymbols(); |
| 1229 | |
| 1230 | assertThat(actualSymbols).hasSize(1); |
| 1231 | Identifier sym = actualSymbols.get(0); |
| 1232 | assertThat(sym.getName()).isEqualTo("my_alias"); |
| 1233 | int startOffset = sym.getLocation().getStartOffset(); |
| 1234 | int endOffset = sym.getLocation().getEndOffset(); |
| 1235 | assertThat(startOffset).named("getStartOffset()").isEqualTo(22); |
| 1236 | assertThat(endOffset).named("getEndOffset()").isEqualTo(startOffset + 8); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | @Test |
| 1240 | public void testLoadAliasMultiple() throws Exception { |
| 1241 | runLoadAliasTestForSymbols( |
| 1242 | "my_alias = 'lawl', 'lol', next_alias = 'rofl'", "my_alias", "lol", "next_alias"); |
| 1243 | } |
| 1244 | |
| 1245 | private void runLoadAliasTestForSymbols(String loadSymbolString, String... expectedSymbols) { |
| 1246 | List<Statement> statements = |
| 1247 | parseFileForSkylark(String.format("load('/foo/bar/file', %s)\n", loadSymbolString)); |
| 1248 | LoadStatement stmt = (LoadStatement) statements.get(0); |
| 1249 | ImmutableList<Identifier> actualSymbols = stmt.getSymbols(); |
| 1250 | |
| 1251 | assertThat(actualSymbols).hasSize(expectedSymbols.length); |
| 1252 | |
| 1253 | List<String> actualSymbolNames = new LinkedList<>(); |
| 1254 | |
| 1255 | for (Identifier identifier : actualSymbols) { |
| 1256 | actualSymbolNames.add(identifier.getName()); |
| 1257 | } |
| 1258 | |
| 1259 | assertThat(actualSymbolNames).containsExactly((Object[]) expectedSymbols); |
| 1260 | } |
| 1261 | |
| 1262 | @Test |
| 1263 | public void testLoadAliasSyntaxError() throws Exception { |
| 1264 | setFailFast(false); |
| 1265 | parseFileForSkylark("load('/foo', test1 = )\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1266 | assertContainsError("syntax error at ')': expected string"); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1267 | |
| 1268 | parseFileForSkylark("load('/foo', test2 = 1)\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1269 | assertContainsError("syntax error at '1': expected string"); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1270 | |
| 1271 | parseFileForSkylark("load('/foo', test3 = old)\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1272 | assertContainsError("syntax error at 'old': expected string"); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1273 | } |
Han-Wen Nienhuys | ceae8c5 | 2015-09-22 16:24:45 +0000 | [diff] [blame] | 1274 | |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1275 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1276 | public void testParseErrorNotComparison() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1277 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1278 | parseFile("2 < not 3"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1279 | assertContainsError("syntax error at 'not'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1282 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1283 | public void testNotWithArithmeticOperatorsBadSyntax() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1284 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1285 | parseFile("0 + not 0"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1286 | assertContainsError("syntax error at 'not'"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1289 | @Test |
Laurent Le Brun | b326638 | 2015-05-27 16:14:43 +0000 | [diff] [blame] | 1290 | public void testKwargsForbidden() throws Exception { |
| 1291 | setFailFast(false); |
| 1292 | parseFile("func(**dict)"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1293 | assertContainsError("**kwargs arguments are not allowed in BUILD files"); |
Laurent Le Brun | b326638 | 2015-05-27 16:14:43 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | @Test |
| 1297 | public void testArgsForbidden() throws Exception { |
| 1298 | setFailFast(false); |
| 1299 | parseFile("func(*array)"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1300 | assertContainsError("*args arguments are not allowed in BUILD files"); |
Laurent Le Brun | b326638 | 2015-05-27 16:14:43 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1304 | public void testOptionalArgBeforeMandatoryArgInFuncDef() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1305 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1306 | parseFileForSkylark("def func(a, b = 'a', c):\n return 0\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1307 | assertContainsError( |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1308 | "a mandatory positional parameter must not follow an optional parameter"); |
| 1309 | } |
| 1310 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1311 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1312 | public void testKwargBeforePositionalArg() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1313 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1314 | parseFileForSkylark( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1315 | "def func(a, b): return a + b", |
| 1316 | "func(**{'b': 1}, 'a')"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1317 | assertContainsError("unexpected tokens after kwarg"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1320 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1321 | public void testDuplicateKwarg() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1322 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1323 | parseFileForSkylark( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1324 | "def func(a, b): return a + b", |
| 1325 | "func(**{'b': 1}, **{'a': 2})"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1326 | assertContainsError("unexpected tokens after kwarg"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1329 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1330 | public void testUnnamedStar() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1331 | setFailFast(false); |
Francois-Rene Rideau | 4feb160 | 2015-03-18 19:49:13 +0000 | [diff] [blame] | 1332 | List<Statement> statements = parseFileForSkylark( |
| 1333 | "def func(a, b1=2, b2=3, *, c1, d=4, c2): return a + b1 + b2 + c1 + c2 + d\n"); |
| 1334 | assertThat(statements).hasSize(1); |
| 1335 | assertThat(statements.get(0)).isInstanceOf(FunctionDefStatement.class); |
| 1336 | FunctionDefStatement stmt = (FunctionDefStatement) statements.get(0); |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 1337 | FunctionSignature sig = stmt.getSignature().getSignature(); |
Francois-Rene Rideau | 012f789 | 2015-03-31 17:27:01 +0000 | [diff] [blame] | 1338 | // Note the reordering of optional named-only at the end. |
Francois-Rene Rideau | 4feb160 | 2015-03-18 19:49:13 +0000 | [diff] [blame] | 1339 | assertThat(sig.getNames()).isEqualTo(ImmutableList.<String>of( |
Francois-Rene Rideau | 012f789 | 2015-03-31 17:27:01 +0000 | [diff] [blame] | 1340 | "a", "b1", "b2", "c1", "c2", "d")); |
Francois-Rene Rideau | 4feb160 | 2015-03-18 19:49:13 +0000 | [diff] [blame] | 1341 | FunctionSignature.Shape shape = sig.getShape(); |
| 1342 | assertThat(shape.getMandatoryPositionals()).isEqualTo(1); |
| 1343 | assertThat(shape.getOptionalPositionals()).isEqualTo(2); |
| 1344 | assertThat(shape.getMandatoryNamedOnly()).isEqualTo(2); |
| 1345 | assertThat(shape.getOptionalNamedOnly()).isEqualTo(1); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1348 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1349 | public void testTopLevelForFails() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1350 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1351 | parseFileForSkylark("for i in []: 0\n"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1352 | assertContainsError( |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1353 | "for loops are not allowed on top-level. Put it into a function"); |
| 1354 | } |
| 1355 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1356 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1357 | public void testNestedFunctionFails() throws Exception { |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1358 | setFailFast(false); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1359 | parseFileForSkylark( |
Francois-Rene Rideau | 5f3e30c | 2015-04-10 19:08:39 +0000 | [diff] [blame] | 1360 | "def func(a):", |
| 1361 | " def bar(): return 0", |
| 1362 | " return bar()", |
| 1363 | ""); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1364 | assertContainsError( |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1365 | "nested functions are not allowed. Move the function to top-level"); |
| 1366 | } |
| 1367 | |
Han-Wen Nienhuys | ccf19ea | 2015-02-27 15:53:24 +0000 | [diff] [blame] | 1368 | @Test |
Laurent Le Brun | 3bc8e9a | 2015-09-10 11:00:37 +0000 | [diff] [blame] | 1369 | public void testElseWithoutIf() throws Exception { |
| 1370 | setFailFast(false); |
| 1371 | parseFileForSkylark( |
| 1372 | "def func(a):", |
| 1373 | // no if |
| 1374 | " else: return a"); |
Yue Gan | 4866e15 | 2016-04-07 13:07:08 +0000 | [diff] [blame] | 1375 | assertContainsError("syntax error at 'else': not allowed here."); |
| 1376 | } |
| 1377 | |
| 1378 | @Test |
| 1379 | public void testForElse() throws Exception { |
| 1380 | setFailFast(false); |
| 1381 | parseFileForSkylark( |
| 1382 | "def func(a):", |
| 1383 | " for i in range(a):", |
| 1384 | " print(i)", |
| 1385 | " else: return a"); |
| 1386 | assertContainsError("syntax error at 'else': not allowed here."); |
Laurent Le Brun | 3bc8e9a | 2015-09-10 11:00:37 +0000 | [diff] [blame] | 1387 | } |
Florian Weikert | 1f004e5 | 2015-10-16 09:43:48 +0000 | [diff] [blame] | 1388 | |
| 1389 | @Test |
| 1390 | public void testTryStatementInBuild() throws Exception { |
| 1391 | setFailFast(false); |
| 1392 | parseFile("try: pass"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1393 | assertContainsError("syntax error at 'try': Try statements are not supported."); |
Florian Weikert | 1f004e5 | 2015-10-16 09:43:48 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | @Test |
| 1397 | public void testTryStatementInSkylark() throws Exception { |
| 1398 | setFailFast(false); |
| 1399 | parseFileForSkylark("try: pass"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1400 | assertContainsError("syntax error at 'try': Try statements are not supported."); |
Florian Weikert | 1f004e5 | 2015-10-16 09:43:48 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | @Test |
| 1404 | public void testClassDefinitionInBuild() throws Exception { |
| 1405 | setFailFast(false); |
| 1406 | parseFile("class test(object): pass"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1407 | assertContainsError("syntax error at 'class': Class definitions are not supported."); |
Florian Weikert | 1f004e5 | 2015-10-16 09:43:48 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | @Test |
| 1411 | public void testClassDefinitionInSkylark() throws Exception { |
| 1412 | setFailFast(false); |
| 1413 | parseFileForSkylark("class test(object): pass"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1414 | assertContainsError("syntax error at 'class': Class definitions are not supported."); |
Florian Weikert | 1f004e5 | 2015-10-16 09:43:48 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | @Test |
| 1418 | public void testDefInBuild() throws Exception { |
| 1419 | setFailFast(false); |
| 1420 | parseFile("def func(): pass"); |
Ulf Adams | c708f96 | 2015-10-22 12:02:28 +0000 | [diff] [blame] | 1421 | assertContainsError("syntax error at 'def': This is not supported in BUILD files. " |
Florian Weikert | 1f004e5 | 2015-10-16 09:43:48 +0000 | [diff] [blame] | 1422 | + "Move the block to a .bzl file and load it"); |
| 1423 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 1424 | } |