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