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