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