| // Copyright (c) 2026, 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:checks/checks.dart'; | |
| import 'package:path/path.dart' as p; | |
| import 'package:snippets/src/update.dart'; | |
| import 'package:test/scaffolding.dart'; | |
| import 'checks_ext.dart'; | |
| /// Runs the driver over [source] with [files] on the "disk". | |
| /// | |
| /// Paths in [files] are relative to the package root; the document is | |
| /// `/pkg/README.md` unless [document] says otherwise. | |
| UpdateResult update( | |
| String source, { | |
| Map<String, String> files = const {}, | |
| String document = 'README.md', | |
| }) { | |
| final root = p.normalize(p.absolute('/pkg')); | |
| return updateDocument( | |
| source: source, | |
| documentPath: p.join(root, document), | |
| packageRoot: root, | |
| readFile: (path) => files[p.posix.joinAll(p.split(p.relative( | |
| path, | |
| from: root, | |
| )))], | |
| ); | |
| } | |
| const _hello = ''' | |
| // #region greeting | |
| void main() { | |
| print('hello'); | |
| } | |
| // #endregion | |
| '''; | |
| void main() { | |
| test('fills in an empty block', () { | |
| check(update( | |
| '```dart @example example/hello.dart#greeting\n```\n', | |
| files: {'example/hello.dart': _hello}, | |
| )) | |
| ..diagnostics.isEmpty() | |
| ..stale.length.equals(1) | |
| ..updated.equals(''' | |
| ```dart @example example/hello.dart#greeting | |
| void main() { | |
| print('hello'); | |
| } | |
| ``` | |
| '''); | |
| }); | |
| test('a block that is already correct is left alone', () { | |
| const source = ''' | |
| ```dart @example example/hello.dart#greeting | |
| void main() { | |
| print('hello'); | |
| } | |
| ``` | |
| '''; | |
| check(update(source, files: {'example/hello.dart': _hello})) | |
| ..diagnostics.isEmpty() | |
| ..stale.isEmpty() | |
| ..updated.isNull(); | |
| }); | |
| test('is idempotent', () { | |
| final once = update( | |
| '```dart @example example/hello.dart#greeting\nstale;\n```\n', | |
| files: {'example/hello.dart': _hello}, | |
| ); | |
| final twice = update(once.updated!, files: {'example/hello.dart': _hello}); | |
| check(twice.updated).isNull(); | |
| }); | |
| test('leaves the rest of the document byte-identical', () { | |
| final result = update(''' | |
| # Title | |
| Some prose with a ``` in it. | |
| ```dart @example example/hello.dart#greeting | |
| ``` | |
| an indented code block | |
| > a quote | |
| ''', files: {'example/hello.dart': _hello}); | |
| check(result.updated).isNotNull() | |
| ..startsWith('# Title\n\nSome prose') | |
| ..endsWith('> a quote\n'); | |
| }); | |
| test('writes the container prefix on every line', () { | |
| final result = update( | |
| '> ```dart @example example/hello.dart#greeting\n> ```\n', | |
| files: {'example/hello.dart': _hello}, | |
| ); | |
| check(result.updated).equals(''' | |
| > ```dart @example example/hello.dart#greeting | |
| > void main() { | |
| > print('hello'); | |
| > } | |
| > ``` | |
| '''); | |
| }); | |
| test('indents into a list item', () { | |
| final result = update(''' | |
| 1. Run this: | |
| ```dart @example example/hello.dart#greeting | |
| ``` | |
| ''', files: {'example/hello.dart': _hello}); | |
| check(result.updated).equals(''' | |
| 1. Run this: | |
| ```dart @example example/hello.dart#greeting | |
| void main() { | |
| print('hello'); | |
| } | |
| ``` | |
| '''); | |
| }); | |
| test('blank lines carry no trailing whitespace', () { | |
| final result = update( | |
| '> ```dart @example a.dart\n> ```\n', | |
| files: {'a.dart': 'one;\n\ntwo;\n'}, | |
| ); | |
| check(result.updated) | |
| .equals('> ```dart @example a.dart\n> one;\n>\n> two;\n> ```\n'); | |
| }); | |
| test('preserves CRLF', () { | |
| final result = update( | |
| '```dart @example a.dart\r\n```\r\n', | |
| files: {'a.dart': 'x;\n'}, | |
| ); | |
| check(result.updated).equals('```dart @example a.dart\r\nx;\r\n```\r\n'); | |
| }); | |
| test('lengthens the fence when the content contains one', () { | |
| final result = update( | |
| '```markdown @example a.md\n```\n', | |
| files: {'a.md': '```dart\nx;\n```\n'}, | |
| ); | |
| check(result.updated).equals(''' | |
| ````markdown @example a.md | |
| ```dart | |
| x; | |
| ``` | |
| ```` | |
| '''); | |
| }); | |
| test('updates several blocks at once', () { | |
| final result = update(''' | |
| ```dart @example a.dart | |
| ``` | |
| ```dart @example b.dart | |
| ``` | |
| ''', files: {'a.dart': 'a;\n', 'b.dart': 'b;\n'}); | |
| check(result.updated).equals(''' | |
| ```dart @example a.dart | |
| a; | |
| ``` | |
| ```dart @example b.dart | |
| b; | |
| ``` | |
| '''); | |
| }); | |
| test('updates regions from YAML and JS files', () { | |
| final result = update(''' | |
| ```yaml @example config.yaml#deps | |
| ``` | |
| ```js @example web/app.js#init | |
| ``` | |
| ''', files: { | |
| 'config.yaml': '# #region deps\npath: ^1.9.0\n# #endregion\n', | |
| 'web/app.js': '// #region init\nmount();\n// #endregion\n', | |
| }); | |
| check(result.diagnostics).isEmpty(); | |
| check(result.updated).equals(''' | |
| ```yaml @example config.yaml#deps | |
| path: ^1.9.0 | |
| ``` | |
| ```js @example web/app.js#init | |
| mount(); | |
| ``` | |
| '''); | |
| }); | |
| test('supports indent=strip|keep option', () { | |
| const file = ' a;\n'; | |
| check( | |
| update('```dart @example a.dart\n```\n', files: {'a.dart': file}).updated, | |
| ).equals('```dart @example a.dart\na;\n```\n'); | |
| check( | |
| update( | |
| '```dart @example a.dart indent=keep\n```\n', | |
| files: {'a.dart': file}, | |
| ).updated, | |
| ).equals('```dart @example a.dart indent=keep\n a;\n```\n'); | |
| }); | |
| test('rejects unknown options and invalid indent= values', () { | |
| for (final bad in ['indnet=keep', 'indent=bogus']) { | |
| final result = update( | |
| '```dart @example a.dart $bad\n```\n', | |
| files: {'a.dart': 'a;\n'}, | |
| ); | |
| check(result.diagnostics).single.message.contains(bad); | |
| } | |
| }); | |
| group('errors', () { | |
| test('a missing file is an error, and nothing is written', () { | |
| check(update('```dart @example missing.dart\n```\n')) | |
| ..updated.isNull() | |
| ..diagnostics.single.which((d) => d | |
| ..line.equals(0) | |
| ..message.contains('missing.dart')); | |
| }); | |
| test('a missing region is an error at the reference', () { | |
| check(update( | |
| 'intro\n\n```dart @example example/hello.dart#nope\n```\n', | |
| files: {'example/hello.dart': _hello}, | |
| )) | |
| ..updated.isNull() | |
| ..diagnostics.single.which((d) => d | |
| ..line.equals(2) | |
| ..span | |
| .isNotNull() | |
| .sourceUrl | |
| .isNotNull() | |
| .has((u) => u.path, 'path') | |
| .endsWith('README.md')); | |
| }); | |
| test('one broken reference does not stop the others', () { | |
| check(update(''' | |
| ```dart @example missing.dart | |
| ``` | |
| ```dart @example a.dart | |
| ``` | |
| ''', files: {'a.dart': 'a;\n'})) | |
| ..diagnostics.isNotEmpty() | |
| ..updated.isNotNull().contains('a;'); | |
| }); | |
| test('an unclosed block is an error, even if its content matches', () { | |
| for (final source in [ | |
| '```dart @example a.dart\n', | |
| '```dart @example a.dart\na;\n', | |
| ]) { | |
| check(update(source, files: {'a.dart': 'a;\n'})) | |
| ..updated.isNull() | |
| ..diagnostics.single.message.contains('closing fence'); | |
| } | |
| }); | |
| test('a reference outside the package is an error', () { | |
| check(update('```dart @example x%2F..%2F..%2F..%2Fetc/hostname\n```\n')) | |
| ..updated.isNull() | |
| ..diagnostics.single.message.contains('outside the package'); | |
| }); | |
| test('a malformed example file is an error', () { | |
| check(update( | |
| '```dart @example a.dart#r\n```\n', | |
| files: {'a.dart': '// #region r\na;\n'}, | |
| )) | |
| ..updated.isNull() | |
| ..diagnostics.single.message.contains('never closed'); | |
| }); | |
| }); | |
| } |