|
5 | 5 | import 'package:analyzer/dart/ast/ast.dart'; |
6 | 6 | import 'package:analyzer/dart/ast/token.dart'; |
7 | 7 | import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 8 | +import 'package:analyzer/src/error/codes.dart'; |
8 | 9 | import 'package:test/test.dart'; |
9 | 10 | import 'package:test_reflective_loader/test_reflective_loader.dart'; |
10 | 11 |
|
@@ -143,15 +144,143 @@ class ClassMemberParserTest extends FastaParserTestCase |
143 | 144 | var method = parser.parseClassMember('C') as MethodDeclaration; |
144 | 145 | expect(method, isNotNull); |
145 | 146 | listener.assertErrors([ |
146 | | - expectedError(ParserErrorCode.UNEXPECTED_TOKEN, 13, 5), |
147 | | - expectedError(ParserErrorCode.UNEXPECTED_TOKEN, 23, 5) |
| 147 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 13, 5), |
| 148 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 23, 5) |
148 | 149 | ]); |
149 | 150 | FunctionBody body = method.body; |
150 | 151 | expect(body, isBlockFunctionBody); |
151 | 152 | Statement statement = (body as BlockFunctionBody).block.statements[0]; |
152 | 153 | expect(statement, isReturnStatement); |
153 | 154 | Expression expression = (statement as ReturnStatement).expression!; |
154 | 155 | expect(expression, isBinaryExpression); |
| 156 | + expect((expression as BinaryExpression).leftOperand, isAwaitExpression); |
| 157 | + expect(expression.rightOperand, isAwaitExpression); |
| 158 | + } |
| 159 | + |
| 160 | + void test_parseAwaitExpression_inSync_v1_49116() { |
| 161 | + createParser('m() { await returnsFuture(); }'); |
| 162 | + var method = parser.parseClassMember('C') as MethodDeclaration; |
| 163 | + expect(method, isNotNull); |
| 164 | + listener.assertErrors([ |
| 165 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 6, 5), |
| 166 | + ]); |
| 167 | + FunctionBody body = method.body; |
| 168 | + expect(body, isBlockFunctionBody); |
| 169 | + Statement statement = (body as BlockFunctionBody).block.statements[0]; |
| 170 | + expect(statement, isExpressionStatement); |
| 171 | + Expression expression = (statement as ExpressionStatement).expression; |
| 172 | + expect(expression, isAwaitExpression); |
| 173 | + } |
| 174 | + |
| 175 | + void test_parseAwaitExpression_inSync_v2_49116() { |
| 176 | + createParser('''m() { |
| 177 | + if (await returnsFuture()) {} |
| 178 | + else if (!await returnsFuture()) {} |
| 179 | + }'''); |
| 180 | + var method = parser.parseClassMember('C') as MethodDeclaration; |
| 181 | + expect(method, isNotNull); |
| 182 | + listener.assertErrors([ |
| 183 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 16, 5), |
| 184 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 58, 5), |
| 185 | + ]); |
| 186 | + FunctionBody body = method.body; |
| 187 | + expect(body, isBlockFunctionBody); |
| 188 | + Statement statement = (body as BlockFunctionBody).block.statements[0]; |
| 189 | + expect(statement, isIfStatement); |
| 190 | + Expression expression = (statement as IfStatement).condition; |
| 191 | + expect(expression, isAwaitExpression); |
| 192 | + expect(statement.elseStatement, isNotNull); |
| 193 | + Statement elseStatement = statement.elseStatement!; |
| 194 | + expect(elseStatement, isIfStatement); |
| 195 | + expression = (elseStatement as IfStatement).condition; |
| 196 | + expect(expression, isPrefixExpression); |
| 197 | + expect((expression as PrefixExpression).operator.lexeme, '!'); |
| 198 | + expression = expression.operand; |
| 199 | + expect(expression, isAwaitExpression); |
| 200 | + } |
| 201 | + |
| 202 | + void test_parseAwaitExpression_inSync_v3_49116() { |
| 203 | + createParser('m() { print(await returnsFuture()); }'); |
| 204 | + var method = parser.parseClassMember('C') as MethodDeclaration; |
| 205 | + expect(method, isNotNull); |
| 206 | + listener.assertErrors([ |
| 207 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 12, 5), |
| 208 | + ]); |
| 209 | + FunctionBody body = method.body; |
| 210 | + expect(body, isBlockFunctionBody); |
| 211 | + Statement statement = (body as BlockFunctionBody).block.statements[0]; |
| 212 | + expect(statement, isExpressionStatement); |
| 213 | + Expression expression = (statement as ExpressionStatement).expression; |
| 214 | + expect(expression, isMethodInvocation); |
| 215 | + expression = (expression as MethodInvocation).argumentList.arguments.single; |
| 216 | + expect(expression, isAwaitExpression); |
| 217 | + } |
| 218 | + |
| 219 | + void test_parseAwaitExpression_inSync_v4_49116() { |
| 220 | + createParser('''m() { |
| 221 | + xor(await returnsFuture(), await returnsFuture(), await returnsFuture()); |
| 222 | + }'''); |
| 223 | + var method = parser.parseClassMember('C') as MethodDeclaration; |
| 224 | + expect(method, isNotNull); |
| 225 | + listener.assertErrors([ |
| 226 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 16, 5), |
| 227 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 39, 5), |
| 228 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 62, 5), |
| 229 | + ]); |
| 230 | + FunctionBody body = method.body; |
| 231 | + expect(body, isBlockFunctionBody); |
| 232 | + Statement statement = (body as BlockFunctionBody).block.statements[0]; |
| 233 | + expect(statement, isExpressionStatement); |
| 234 | + Expression expression = (statement as ExpressionStatement).expression; |
| 235 | + expect(expression, isMethodInvocation); |
| 236 | + expect((expression as MethodInvocation).argumentList.arguments.length, 3); |
| 237 | + expect(expression.argumentList.arguments[0], isAwaitExpression); |
| 238 | + expect(expression.argumentList.arguments[1], isAwaitExpression); |
| 239 | + expect(expression.argumentList.arguments[2], isAwaitExpression); |
| 240 | + } |
| 241 | + |
| 242 | + void test_parseAwaitExpression_inSync_v5_49116() { |
| 243 | + createParser('''m() { |
| 244 | + await returnsFuture() ^ await returnsFuture(); |
| 245 | + }'''); |
| 246 | + var method = parser.parseClassMember('C') as MethodDeclaration; |
| 247 | + expect(method, isNotNull); |
| 248 | + listener.assertErrors([ |
| 249 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 12, 5), |
| 250 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 36, 5), |
| 251 | + ]); |
| 252 | + FunctionBody body = method.body; |
| 253 | + expect(body, isBlockFunctionBody); |
| 254 | + Statement statement = (body as BlockFunctionBody).block.statements[0]; |
| 255 | + expect(statement, isExpressionStatement); |
| 256 | + Expression expression = (statement as ExpressionStatement).expression; |
| 257 | + expect(expression, isBinaryExpression); |
| 258 | + expect((expression as BinaryExpression).leftOperand, isAwaitExpression); |
| 259 | + expect(expression.rightOperand, isAwaitExpression); |
| 260 | + expect(expression.operator.lexeme, '^'); |
| 261 | + } |
| 262 | + |
| 263 | + void test_parseAwaitExpression_inSync_v6_49116() { |
| 264 | + createParser('''m() { |
| 265 | + print(await returnsFuture() ^ await returnsFuture()); |
| 266 | + }'''); |
| 267 | + var method = parser.parseClassMember('C') as MethodDeclaration; |
| 268 | + expect(method, isNotNull); |
| 269 | + listener.assertErrors([ |
| 270 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 18, 5), |
| 271 | + expectedError(CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, 42, 5), |
| 272 | + ]); |
| 273 | + FunctionBody body = method.body; |
| 274 | + expect(body, isBlockFunctionBody); |
| 275 | + Statement statement = (body as BlockFunctionBody).block.statements[0]; |
| 276 | + expect(statement, isExpressionStatement); |
| 277 | + Expression expression = (statement as ExpressionStatement).expression; |
| 278 | + expect(expression, isMethodInvocation); |
| 279 | + expression = (expression as MethodInvocation).argumentList.arguments.single; |
| 280 | + expect(expression, isBinaryExpression); |
| 281 | + expect((expression as BinaryExpression).leftOperand, isAwaitExpression); |
| 282 | + expect(expression.rightOperand, isAwaitExpression); |
| 283 | + expect(expression.operator.lexeme, '^'); |
155 | 284 | } |
156 | 285 |
|
157 | 286 | void test_parseClassMember_constructor_initializers_conditional() { |
|
0 commit comments