From 8e66248765fb821e7542f4b6f50efa575b7883a5 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 25 Jul 2023 00:15:41 +0000 Subject: [PATCH] Refactor doc comment-parsing tests I suspect we ought to just remove many/all of the remaining simpler parser tests. I glanced at a few, and they do not seem specific to analyzer, so their covered features should be covered by parser tests in _fe_analyzer_shared. https://github.com/dart-lang/sdk/issues/50702 Change-Id: I0e524562f6584145db5e80a154e72ea66090d924 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/316042 Reviewed-by: Konstantin Shcheglov Commit-Queue: Samuel Rawlins --- .../test/generated/simple_parser_test.dart | 495 --------------- .../src/dart/parser/doc_comment_test.dart | 569 ++++++++++++++++++ .../test/src/dart/parser/test_all.dart | 2 + 3 files changed, 571 insertions(+), 495 deletions(-) create mode 100644 pkg/analyzer/test/src/dart/parser/doc_comment_test.dart diff --git a/pkg/analyzer/test/generated/simple_parser_test.dart b/pkg/analyzer/test/generated/simple_parser_test.dart index c90bf9dc864b..ed327920be57 100644 --- a/pkg/analyzer/test/generated/simple_parser_test.dart +++ b/pkg/analyzer/test/generated/simple_parser_test.dart @@ -567,187 +567,6 @@ class C {} expect(declaration.metadata, isEmpty); } - void test_parseCommentReference_new_prefixed() { - createParser(''); - var reference = parseCommentReference('new a.b', 7)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isPrefixedIdentifier); - var prefixedIdentifier = reference.expression as PrefixedIdentifier; - SimpleIdentifier prefix = prefixedIdentifier.prefix; - expect(prefix.token, isNotNull); - expect(prefix.name, "a"); - expect(prefix.offset, 11); - expect(prefixedIdentifier.period, isNotNull); - SimpleIdentifier identifier = prefixedIdentifier.identifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "b"); - expect(identifier.offset, 13); - } - - void test_parseCommentReference_new_simple() { - createParser(''); - var reference = parseCommentReference('new a', 5)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isSimpleIdentifier); - var identifier = reference.expression as SimpleIdentifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "a"); - expect(identifier.offset, 9); - } - - void test_parseCommentReference_operator_withKeyword_notPrefixed() { - createParser(''); - var reference = parseCommentReference('operator ==', 5)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isSimpleIdentifier); - var identifier = reference.expression as SimpleIdentifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "=="); - expect(identifier.offset, 14); - } - - void test_parseCommentReference_operator_withKeyword_prefixed() { - createParser(''); - var reference = parseCommentReference('Object.operator==', 7)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isPrefixedIdentifier); - var prefixedIdentifier = reference.expression as PrefixedIdentifier; - SimpleIdentifier prefix = prefixedIdentifier.prefix; - expect(prefix.token, isNotNull); - expect(prefix.name, "Object"); - expect(prefix.offset, 7); - expect(prefixedIdentifier.period, isNotNull); - SimpleIdentifier identifier = prefixedIdentifier.identifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "=="); - expect(identifier.offset, 22); - } - - void test_parseCommentReference_operator_withoutKeyword_notPrefixed() { - createParser(''); - var reference = parseCommentReference('==', 5)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isSimpleIdentifier); - var identifier = reference.expression as SimpleIdentifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "=="); - expect(identifier.offset, 5); - } - - void test_parseCommentReference_operator_withoutKeyword_prefixed() { - createParser(''); - var reference = parseCommentReference('Object.==', 7)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isPrefixedIdentifier); - var prefixedIdentifier = reference.expression as PrefixedIdentifier; - SimpleIdentifier prefix = prefixedIdentifier.prefix; - expect(prefix.token, isNotNull); - expect(prefix.name, "Object"); - expect(prefix.offset, 7); - expect(prefixedIdentifier.period, isNotNull); - SimpleIdentifier identifier = prefixedIdentifier.identifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "=="); - expect(identifier.offset, 14); - } - - void test_parseCommentReference_prefixed() { - createParser(''); - var reference = parseCommentReference('a.b', 7)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isPrefixedIdentifier); - var prefixedIdentifier = reference.expression as PrefixedIdentifier; - SimpleIdentifier prefix = prefixedIdentifier.prefix; - expect(prefix.token, isNotNull); - expect(prefix.name, "a"); - expect(prefix.offset, 7); - expect(prefixedIdentifier.period, isNotNull); - SimpleIdentifier identifier = prefixedIdentifier.identifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "b"); - expect(identifier.offset, 9); - } - - void test_parseCommentReference_simple() { - createParser(''); - var reference = parseCommentReference('a', 5)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isSimpleIdentifier); - var identifier = reference.expression as SimpleIdentifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "a"); - expect(identifier.offset, 5); - } - - void test_parseCommentReference_synthetic() { - createParser(''); - var reference = parseCommentReference('', 5)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - expect(reference.expression, isSimpleIdentifier); - var identifier = reference.expression as SimpleIdentifier; - expect(identifier, isNotNull); - expect(identifier.isSynthetic, isTrue); - expect(identifier.token, isNotNull); - expect(identifier.name, ""); - expect(identifier.offset, 5); - // Should end with EOF token. - Token nextToken = identifier.token.next!; - expect(nextToken, isNotNull); - expect(nextToken.type, TokenType.EOF); - } - - @failingTest - void test_parseCommentReference_this() { - // This fails because we are returning null from the method and asserting - // that the return value is not null. - createParser(''); - var reference = parseCommentReference('this', 5)!; - expectNotNullIfNoErrors(reference); - assertNoErrors(); - var identifier = reference.expression as SimpleIdentifier; - expect(identifier.token, isNotNull); - expect(identifier.name, "a"); - expect(identifier.offset, 5); - } - - void test_parseCommentReferences_multiLine() { - DocumentationCommentToken token = DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, "/** xxx [a] yyy [bb] zzz */", 3); - List tokens = [token]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(2)); - { - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 12); - Token referenceToken = reference.expression.beginToken; - expect(referenceToken.offset, 12); - expect(referenceToken.lexeme, 'a'); - } - { - CommentReference reference = references[1]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 20); - Token referenceToken = reference.expression.beginToken; - expect(referenceToken.offset, 20); - expect(referenceToken.lexeme, 'bb'); - } - } - void test_parseCommentReferences_notClosed_noIdentifier() { DocumentationCommentToken docToken = DocumentationCommentToken( TokenType.MULTI_LINE_COMMENT, "/** [ some text", 5); @@ -770,320 +589,6 @@ class C {} expect(nextToken.type, TokenType.EOF); } - void test_parseCommentReferences_notClosed_withIdentifier() { - DocumentationCommentToken docToken = DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, "/** [namePrefix some text", 5); - createParser(''); - List references = - parser.parseCommentReferences([docToken]); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - Token referenceToken = reference.expression.beginToken; - expect(reference, isNotNull); - expect(referenceToken, same(reference.beginToken)); - expect(reference.expression, isNotNull); - var identifier = reference.expression as Identifier; - expect(identifier.isSynthetic, isFalse); - expect(identifier.name, "namePrefix"); - // Should end with EOF token. - Token nextToken = referenceToken.next!; - expect(nextToken, isNotNull); - expect(nextToken.type, TokenType.EOF); - } - - void test_parseCommentReferences_notCodeBlock_4spaces_afterText() { - List tokens = [ - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// comment:", 0), - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// a[i] == b[i]", 0) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(2)); - } - - void test_parseCommentReferences_singleLine() { - List tokens = [ - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// xxx [a] yyy [b] zzz", 3), - DocumentationCommentToken(TokenType.SINGLE_LINE_COMMENT, "/// x [c]", 28) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(3)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 12); - reference = references[1]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 20); - reference = references[2]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 35); - } - - void test_parseCommentReferences_skipCodeBlock_4spaces_afterEmptyComment() { - List tokens = [ - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// Code block:", 0), - DocumentationCommentToken(TokenType.SINGLE_LINE_COMMENT, "///", 0), - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// a[i] == b[i]", 0) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, isEmpty); - } - - void test_parseCommentReferences_skipCodeBlock_4spaces_afterEmptyLine() { - List tokens = [ - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// Code block:", 0), - DocumentationCommentToken(TokenType.SINGLE_LINE_COMMENT, "", 0), - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// a[i] == b[i]", 0) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, isEmpty); - } - - void test_parseCommentReferences_skipCodeBlock_4spaces_block() { - List tokens = [ - DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT, - "/**\n * a[i]\n * non-code line\n */", 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, isEmpty); - } - - void test_parseCommentReferences_skipCodeBlock_4spaces_first() { - List tokens = [ - DocumentationCommentToken( - TokenType.SINGLE_LINE_COMMENT, "/// a[i] == b[i]", 0) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, isEmpty); - } - - void test_parseCommentReferences_skipCodeBlock_bracketed() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, "/** [:xxx [a] yyy:] [b] zzz */", 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 24); - } - - void test_parseCommentReferences_skipCodeBlock_gitHub() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, "/** `a[i]` and [b] */", 0) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 16); - } - - void test_parseCommentReferences_skipCodeBlock_gitHub_multiLine() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, - r''' -/** - * First. - * ```dart - * Some [int] reference. - * ``` - * Last. - */ -''', - 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, isEmpty); - } - - void test_parseCommentReferences_skipCodeBlock_gitHub_multiLine_lines() { - String commentText = r''' -/// First. -/// ```dart -/// Some [int] reference. -/// ``` -/// Last. -'''; - List tokens = commentText - .split('\n') - .map((line) => - DocumentationCommentToken(TokenType.SINGLE_LINE_COMMENT, line, 0)) - .toList(); - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, isEmpty); - } - - void test_parseCommentReferences_skipCodeBlock_gitHub_notTerminated() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, "/** `a[i] and [b] */", 0) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(2)); - } - - void test_parseCommentReferences_skipCodeBlock_spaces() { - List tokens = [ - DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT, - "/**\n * a[i]\n * xxx [i] zzz\n */", 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 27); - } - - @failingTest - void test_parseCommentReferences_skipLink_direct_multiLine() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, - ''' -/** - * [a link split across multiple - * lines](http://www.google.com) [b] zzz - */ -''', - 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 74); - } - - void test_parseCommentReferences_skipLink_direct_singleLine() { - List tokens = [ - DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT, - "/** [a](http://www.google.com) [b] zzz */", 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 35); - } - - @failingTest - void test_parseCommentReferences_skipLink_reference_multiLine() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, - ''' -/** - * [a link split across multiple - * lines][c] [b] zzz - */ -''', - 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 54); - } - - void test_parseCommentReferences_skipLink_reference_singleLine() { - List tokens = [ - DocumentationCommentToken( - TokenType.MULTI_LINE_COMMENT, "/** [a][c] [b] zzz */", 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 15); - } - - void test_parseCommentReferences_skipLinkDefinition() { - List tokens = [ - DocumentationCommentToken(TokenType.MULTI_LINE_COMMENT, - "/** [a]: http://www.google.com (Google) [b] zzz */", 3) - ]; - createParser(''); - List references = parser.parseCommentReferences(tokens); - expectNotNullIfNoErrors(references); - assertNoErrors(); - expect(references, hasLength(1)); - CommentReference reference = references[0]; - expect(reference, isNotNull); - expect(reference.expression, isNotNull); - expect(reference.offset, 44); - } - void test_parseConfiguration_noOperator_dottedIdentifier() { createParser("if (a.b) 'c.dart'"); Configuration configuration = parser.parseConfiguration(); diff --git a/pkg/analyzer/test/src/dart/parser/doc_comment_test.dart b/pkg/analyzer/test/src/dart/parser/doc_comment_test.dart new file mode 100644 index 000000000000..3015ead089ef --- /dev/null +++ b/pkg/analyzer/test/src/dart/parser/doc_comment_test.dart @@ -0,0 +1,569 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import '../../diagnostics/parser_diagnostics.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(DocCommentParserTest); + }); +} + +@reflectiveTest +class DocCommentParserTest extends ParserDiagnosticsTest { + test_code() { + final parseResult = parseStringWithErrors(r''' +/// `a[i]` and [b]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a[i]'); + // TODO(srawlins): Parse code into its own node. + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: b + tokens + /// `a[i]` and [b]. +'''); + } + + test_code_legacy_block() { + // TODO(srawlins): I believe we should drop support for `[:` `:]`. + final parseResult = parseStringWithErrors(r''' +/** [:xxx [a] yyy:] [b] zzz */ +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: b + tokens + /** [:xxx [a] yyy:] [b] zzz */ +'''); + } + + test_code_unterminated() { + final parseResult = parseStringWithErrors(r''' +/** `a[i] and [b] */ +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a['); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: i + CommentReference + expression: SimpleIdentifier + token: b + tokens + /** `a[i] and [b] */ +'''); + } + + test_codeBlock_backticks() { + final parseResult = parseStringWithErrors(r''' +/// First. +/// ```dart +/// a[i] = b[i]; +/// ``` +/// Last. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a[i]'); + // TODO(srawlins): Parse a backtick code block into its own node. + assertParsedNodeText(node, r''' +Comment + tokens + /// First. + /// ```dart + /// a[i] = b[i]; + /// ``` + /// Last. +'''); + } + + test_codeBlock_backticks_block() { + final parseResult = parseStringWithErrors(r''' +/** + * First. + * ```dart + * a[i] = b[i]; + * ``` + * Last. + */ +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a[i]'); + // TODO(srawlins): Parse a backtick code block into its own node. + assertParsedNodeText(node, r''' +Comment + tokens + /** + * First. + * ```dart + * a[i] = b[i]; + * ``` + * Last. + */ +'''); + } + + test_codeBlock_indented_afterBlankLine() { + final parseResult = parseStringWithErrors(r''' +/// Text. +/// +/// a[i] = b[i]; +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('Text'); + // TODO(srawlins): Parse a backtick code block into its own node. + assertParsedNodeText(node, r''' +Comment + tokens + /// Text. + /// + /// a[i] = b[i]; +'''); + } + + test_codeBlock_indented_afterTextLine_notCodeBlock() { + final parseResult = parseStringWithErrors(r''' +/// Text. +/// a[i] = b[i]; +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('Text'); + // TODO(srawlins): Parse an indented code block into its own node. + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: i + CommentReference + expression: SimpleIdentifier + token: i + tokens + /// Text. + /// a[i] = b[i]; +'''); + } + + test_codeBlock_indented_firstLine() { + final parseResult = parseStringWithErrors(r''' +/// a[i] = b[i]; +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a[i]'); + // TODO(srawlins): Parse an indented code block into its own node. + assertParsedNodeText(node, r''' +Comment + tokens + /// a[i] = b[i]; +'''); + } + + test_codeBlock_indented_firstLine_block() { + final parseResult = parseStringWithErrors(r''' +/** + * a[i] = b[i]; + * [c]. + */ +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a[i]'); + // TODO(srawlins): Parse an indented code block into its own node. + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: c + tokens + /** + * a[i] = b[i]; + * [c]. + */ +'''); + } + + test_commentReference_block() { + final parseResult = parseStringWithErrors(r''' +/** [a]. */ +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: a + tokens + /** [a]. */ +'''); + } + + test_commentReference_empty() { + final parseResult = parseStringWithErrors(r''' +/// []. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: + tokens + /// []. +'''); + } + + test_commentReference_multiple() { + final parseResult = parseStringWithErrors(r''' +/// [a] and [b]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: a + CommentReference + expression: SimpleIdentifier + token: b + tokens + /// [a] and [b]. +'''); + } + + test_commentReference_multiple_block() { + final parseResult = parseStringWithErrors(r''' +/** [a] and [b]. */ +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: a + CommentReference + expression: SimpleIdentifier + token: b + tokens + /** [a] and [b]. */ +'''); + } + + test_commentReference_new_prefixed() { + final parseResult = parseStringWithErrors(r''' +/// [new a.A]. +class B {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('new'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + newKeyword: new + expression: PrefixedIdentifier + prefix: SimpleIdentifier + token: a + period: . + identifier: SimpleIdentifier + token: A + tokens + /// [new a.A]. +'''); + } + + test_commentReference_new_simple() { + final parseResult = parseStringWithErrors(r''' +/// [new A]. +class B {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('new'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + newKeyword: new + expression: SimpleIdentifier + token: A + tokens + /// [new A]. +'''); + } + + test_commentReference_operator_withKeyword_notPrefixed() { + final parseResult = parseStringWithErrors(r''' +/// [operator ==]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('=='); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: == + tokens + /// [operator ==]. +'''); + } + + test_commentReference_operator_withKeyword_prefixed() { + final parseResult = parseStringWithErrors(r''' +/// [Object.operator ==]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('=='); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: PrefixedIdentifier + prefix: SimpleIdentifier + token: Object + period: . + identifier: SimpleIdentifier + token: == + tokens + /// [Object.operator ==]. +'''); + } + + test_commentReference_operator_withoutKeyword_notPrefixed() { + final parseResult = parseStringWithErrors(r''' +/// [==]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('=='); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: == + tokens + /// [==]. +'''); + } + + test_commentReference_operator_withoutKeyword_prefixed() { + final parseResult = parseStringWithErrors(r''' +/// [Object.==]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('=='); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: PrefixedIdentifier + prefix: SimpleIdentifier + token: Object + period: . + identifier: SimpleIdentifier + token: == + tokens + /// [Object.==]. +'''); + } + + test_commentReference_prefixedIdentifier() { + final parseResult = parseStringWithErrors(r''' +/// [a.b]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('a.b'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: PrefixedIdentifier + prefix: SimpleIdentifier + token: a + period: . + identifier: SimpleIdentifier + token: b + tokens + /// [a.b]. +'''); + } + + test_commentReference_simpleIdentifier() { + final parseResult = parseStringWithErrors(r''' +/// [a]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: a + tokens + /// [a]. +'''); + } + + test_commentReference_this() { + final parseResult = parseStringWithErrors(r''' +/// [this]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('this'); + // TODO(srawlins): I think there is an intention to parse this as a comment + // reference. + assertParsedNodeText(node, r''' +Comment + tokens + /// [this]. +'''); + } + + test_inlineLink() { + final parseResult = parseStringWithErrors(r''' +/// [a](http://www.google.com) [b]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: b + tokens + /// [a](http://www.google.com) [b]. +'''); + } + + test_linkReference() { + final parseResult = parseStringWithErrors(r''' +/// [a]: http://www.google.com Google [b] +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a]'); + // TODO(srawlins): Ideally this should not parse `[b]` as a comment + // reference. + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: b + tokens + /// [a]: http://www.google.com Google [b] +'''); + } + + test_referenceLink() { + final parseResult = parseStringWithErrors(r''' +/// [a link][c] [b]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: b + tokens + /// [a link][c] [b]. +'''); + } + + test_referenceLink_multiline() { + final parseResult = parseStringWithErrors(r''' +/// [a link split across multiple +/// lines][c] [b]. +class A {} +'''); + parseResult.assertNoErrors(); + + final node = parseResult.findNode.comment('[a'); + assertParsedNodeText(node, r''' +Comment + references + CommentReference + expression: SimpleIdentifier + token: a + CommentReference + expression: SimpleIdentifier + token: b + tokens + /// [a link split across multiple + /// lines][c] [b]. +'''); + } +} diff --git a/pkg/analyzer/test/src/dart/parser/test_all.dart b/pkg/analyzer/test/src/dart/parser/test_all.dart index 322dc798b497..7ea6902bbf0d 100644 --- a/pkg/analyzer/test/src/dart/parser/test_all.dart +++ b/pkg/analyzer/test/src/dart/parser/test_all.dart @@ -4,11 +4,13 @@ import 'package:test_reflective_loader/test_reflective_loader.dart'; +import 'doc_comment_test.dart' as doc_comment; import 'extension_type_test.dart' as extension_type; /// Utility for manually running all tests. main() { defineReflectiveSuite(() { + doc_comment.main(); extension_type.main(); }, name: 'parser'); }