diff --git a/script/tool/lib/src/update_excerpts_command.dart b/script/tool/lib/src/update_excerpts_command.dart index 6904dfdf02f..24381bf6d33 100644 --- a/script/tool/lib/src/update_excerpts_command.dart +++ b/script/tool/lib/src/update_excerpts_command.dart @@ -146,6 +146,12 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case '.cpp': language = 'c++'; break; + case '.m': + language = 'objectivec'; + break; + case '.gradle': + language = 'groovy'; + break; default: language = extension.substring(1); break; @@ -239,6 +245,9 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case 'js': case 'kotlin': case 'rfwtxt': + case 'java': + case 'groovy': + case 'objectivec': case 'swift': prefix = '// '; break; @@ -255,6 +264,9 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case 'yaml': prefix = '# '; break; + case 'sh': + prefix = '# '; + break; } final String startRegionMarker = '$prefix#docregion $section$suffix'; final String endRegionMarker = '$prefix#enddocregion $section$suffix'; diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index 71cf836616b..187f0d8d598 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -33,12 +33,16 @@ void runAllTests(MockPlatform platform) { )); }); - Future testInjection(String before, String source, String after, - {bool failOnChange = false}) async { + Future testInjection( + {required String before, + required String source, + required String after, + required String filename, + bool failOnChange = false}) async { final RepositoryPackage package = createFakePackage('a_package', packagesDir); package.readmeFile.writeAsStringSync(before); - package.directory.childFile('main.dart').writeAsStringSync(source); + package.directory.childFile(filename).writeAsStringSync(source); Object? errorObject; final List output = await runCapturingPrint( runner, @@ -57,10 +61,12 @@ void runAllTests(MockPlatform platform) { } test('succeeds when nothing has changed', () async { + const String filename = 'main.dart'; + const String readme = ''' Example: - + ```dart A B C ``` @@ -72,7 +78,8 @@ A B C // #enddocregion SomeSection FAIL '''; - await testInjection(readme, source, readme); + await testInjection( + before: readme, source: source, after: readme, filename: filename); }); test('fails if example injection fails', () async { @@ -112,31 +119,36 @@ FAIL }); test('updates files', () async { - await testInjection( - ''' + const String filename = 'main.dart'; + + const String before = ''' Example: - + ```dart X Y Z ``` -''', - ''' +'''; + + const String source = ''' FAIL // #docregion SomeSection A B C // #enddocregion SomeSection FAIL -''', - ''' +'''; + + const String after = ''' Example: - + ```dart A B C ``` -''', - ); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('fails if READMEs are changed with --fail-on-change', () async { @@ -174,151 +186,177 @@ FAIL test('does not fail if READMEs are not changed with --fail-on-change', () async { + const String filename = 'main.dart'; + const String readme = ''' Example: - + ```dart A ``` - + ```dart B ``` '''; - await testInjection( - readme, - ''' + + const String source = ''' // #docregion aa A // #enddocregion aa // #docregion bb B // #enddocregion bb -''', - readme, +'''; + + await testInjection( + before: readme, + source: source, + after: readme, + filename: filename, failOnChange: true, ); }); test('indents the plaster', () async { - await testInjection( - ''' + const String filename = 'main.dart'; + + const String before = ''' Example: - + ```dart ``` -''', - ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +'''; + + const String after = ''' Example: - + ```dart A // ··· B ``` -''', - ); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('does not unindent blocks if plaster will not unindent', () async { - await testInjection( - ''' + const String filename = 'main.dart'; + + const String before = ''' Example: - + ```dart ``` -''', - ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +'''; + + const String after = ''' Example: - + ```dart A // ··· B ``` -''', - ); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('unindents blocks', () async { - await testInjection( - ''' + const String filename = 'main.dart'; + + const String before = ''' Example: - + ```dart ``` -''', - ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +'''; + + const String after = ''' Example: - + ```dart A // ··· B ``` -''', - ); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('unindents blocks and plaster', () async { - await testInjection( - ''' + const String filename = 'main.dart'; + + const String before = ''' Example: - + ```dart ``` -''', - ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +'''; + + const String after = ''' Example: - + ```dart A // ··· B ``` -''', - ); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('relative path bases', () async { @@ -447,6 +485,78 @@ FAIL ]), ); }); + + group('File type tests', () { + const List> testCases = >[ + {'filename': 'main.cc', 'language': 'c++'}, + {'filename': 'main.cpp', 'language': 'c++'}, + {'filename': 'main.dart'}, + {'filename': 'main.js'}, + {'filename': 'main.kt', 'language': 'kotlin'}, + {'filename': 'main.java'}, + {'filename': 'main.gradle', 'language': 'groovy'}, + {'filename': 'main.m', 'language': 'objectivec'}, + {'filename': 'main.swift'}, + { + 'filename': 'main.css', + 'prefix': '/* ', + 'suffix': ' */' + }, + { + 'filename': 'main.html', + 'prefix': '' + }, + { + 'filename': 'main.xml', + 'prefix': '' + }, + {'filename': 'main.yaml', 'prefix': '# '}, + {'filename': 'main.sh', 'prefix': '# '}, + {'filename': 'main', 'language': 'txt', 'prefix': ''}, + ]; + + void runTest(Map testCase) { + test('updates ${testCase['filename']} files', () async { + final String filename = testCase['filename']!; + final String language = testCase['language'] ?? filename.split('.')[1]; + final String prefix = testCase['prefix'] ?? '// '; + final String suffix = testCase['suffix'] ?? ''; + + final String before = ''' +Example: + + +```$language +X Y Z +``` +'''; + + final String source = ''' +FAIL +$prefix#docregion SomeSection$suffix +A B C +$prefix#enddocregion SomeSection$suffix +FAIL +'''; + + final String after = ''' +Example: + + +```$language +A B C +``` +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); + }); + } + + testCases.forEach(runTest); + }); } void main() {