From 8af458d70b0fe1d37175a53fee2c27c2416eb6e1 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 4 Dec 2023 11:35:56 -0800 Subject: [PATCH 1/9] [tool] Add support for `.java`, `.gradle`, `.sh`, and `.m` files to `update-excerpts` tool --- script/tool/lib/src/update_excerpts_command.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/script/tool/lib/src/update_excerpts_command.dart b/script/tool/lib/src/update_excerpts_command.dart index 6904dfdf02f..9b1c569381b 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,10 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case 'js': case 'kotlin': case 'rfwtxt': + case 'java': + case 'groovy': + case 'objectivec': + case 'gradle': case 'swift': prefix = '// '; break; @@ -255,6 +265,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'; From 576fc7b2e03c762a5af8cad4dcc9513a1005d1fa Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 4 Dec 2023 14:37:34 -0800 Subject: [PATCH 2/9] Fix 'gradle' should not be listed as a language --- script/tool/lib/src/update_excerpts_command.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/script/tool/lib/src/update_excerpts_command.dart b/script/tool/lib/src/update_excerpts_command.dart index 9b1c569381b..24381bf6d33 100644 --- a/script/tool/lib/src/update_excerpts_command.dart +++ b/script/tool/lib/src/update_excerpts_command.dart @@ -248,7 +248,6 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case 'java': case 'groovy': case 'objectivec': - case 'gradle': case 'swift': prefix = '// '; break; From 83a76b7be02374eab3dbc9ac7077a53c0b68393f Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 5 Dec 2023 09:41:38 -0800 Subject: [PATCH 3/9] Add tests to ensure different file types are updated properly Update test helper function to accept fileName parameter so that tests may use arbitrary file types Remove extra line in tool file that prevents language from being properly set in cc files --- .../tool/lib/src/update_excerpts_command.dart | 1 - .../test/update_excerpts_command_test.dart | 513 ++++++++++++++++-- 2 files changed, 457 insertions(+), 57 deletions(-) diff --git a/script/tool/lib/src/update_excerpts_command.dart b/script/tool/lib/src/update_excerpts_command.dart index 24381bf6d33..edd40fd424c 100644 --- a/script/tool/lib/src/update_excerpts_command.dart +++ b/script/tool/lib/src/update_excerpts_command.dart @@ -142,7 +142,6 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case '.kt': language = 'kotlin'; break; - case '.cc': case '.cpp': language = 'c++'; break; diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index 71cf836616b..675e6ef5131 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -33,12 +33,13 @@ void runAllTests(MockPlatform platform) { )); }); - Future testInjection(String before, String source, String after, + Future testInjection( + String before, String source, String after, 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 +58,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 +75,7 @@ A B C // #enddocregion SomeSection FAIL '''; - await testInjection(readme, source, readme); + await testInjection(readme, source, readme, fileName); }); test('fails if example injection fails', () async { @@ -112,31 +115,29 @@ FAIL }); test('updates files', () async { - await testInjection( - ''' + const String fileName = 'main.dart'; + + await testInjection(''' Example: - + ```dart X Y Z ``` -''', - ''' +''', ''' FAIL // #docregion SomeSection A B C // #enddocregion SomeSection FAIL -''', - ''' +''', ''' Example: - + ```dart A B C ``` -''', - ); +''', fileName); }); test('fails if READMEs are changed with --fail-on-change', () async { @@ -174,14 +175,16 @@ 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 ``` @@ -197,128 +200,121 @@ B // #enddocregion bb ''', readme, + fileName, failOnChange: true, ); }); test('indents the plaster', () async { - await testInjection( - ''' + const String fileName = 'main.dart'; + + await testInjection(''' Example: - + ```dart ``` -''', - ''' +''', ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +''', ''' Example: - + ```dart A // ··· B ``` -''', - ); +''', fileName); }); test('does not unindent blocks if plaster will not unindent', () async { - await testInjection( - ''' + const String fileName = 'main.dart'; + + await testInjection(''' Example: - + ```dart ``` -''', - ''' +''', ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +''', ''' Example: - + ```dart A // ··· B ``` -''', - ); +''', fileName); }); test('unindents blocks', () async { - await testInjection( - ''' + const String fileName = 'main.dart'; + + await testInjection(''' Example: - + ```dart ``` -''', - ''' +''', ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +''', ''' Example: - + ```dart A // ··· B ``` -''', - ); +''', fileName); }); test('unindents blocks and plaster', () async { - await testInjection( - ''' + const String fileName = 'main.dart'; + + await testInjection(''' Example: - + ```dart ``` -''', - ''' +''', ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', - ''' +''', ''' Example: - + ```dart A // ··· B ``` -''', - ); +''', fileName); }); test('relative path bases', () async { @@ -447,6 +443,411 @@ FAIL ]), ); }); + + test('updates gradle files', () async { + const String fileName = 'main.gradle'; + const String language = 'groovy'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates java files', () async { + const String fileName = 'main.java'; + const String language = 'java'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates sh files', () async { + const String fileName = 'main.sh'; + const String language = 'sh'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +# #docregion SomeSection +A B C +# #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates cc files', () async { + const String fileName = 'main.cc'; + const String language = 'cc'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates cpp files', () async { + const String fileName = 'main.cpp'; + const String language = 'c++'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates files with no extension', () async { + const String fileName = 'main'; + const String language = 'txt'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +#docregion SomeSection +A B C +#enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates kotlin files', () async { + const String fileName = 'main.kt'; + const String language = 'kotlin'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates m files', () async { + const String fileName = 'main.m'; + const String language = 'objectivec'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates js files', () async { + const String fileName = 'main.js'; + const String language = 'js'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates java files', () async { + const String fileName = 'main.java'; + const String language = 'java'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates swift files', () async { + const String fileName = 'main.swift'; + const String language = 'swift'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +// #docregion SomeSection +A B C +// #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates css files', () async { + const String fileName = 'main.css'; + const String language = 'css'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +/* #docregion SomeSection */ +A B C +/* #enddocregion SomeSection */ +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates html files', () async { + const String fileName = 'main.html'; + const String language = 'html'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL + +A B C + +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates xml files', () async { + const String fileName = 'main.xml'; + const String language = 'xml'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL + +A B C + +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); + + test('updates yaml files', () async { + const String fileName = 'main.yaml'; + const String language = 'yaml'; + + await testInjection(''' +Example: + + +```$language +X Y Z +``` +''', ''' +FAIL +# #docregion SomeSection +A B C +# #enddocregion SomeSection +FAIL +''', ''' +Example: + + +```$language +A B C +``` +''', fileName); + }); } void main() { From c13043e762f63380b9697d391533136abc719174 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 5 Dec 2023 19:58:24 -0800 Subject: [PATCH 4/9] Run tests in a 'for' loop Group file type tests --- .../test/update_excerpts_command_test.dart | 427 ++---------------- 1 file changed, 45 insertions(+), 382 deletions(-) diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index 675e6ef5131..d662f2f67e2 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -444,362 +444,45 @@ FAIL ); }); - test('updates gradle files', () async { - const String fileName = 'main.gradle'; - const String language = 'groovy'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates java files', () async { - const String fileName = 'main.java'; - const String language = 'java'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates sh files', () async { - const String fileName = 'main.sh'; - const String language = 'sh'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -# #docregion SomeSection -A B C -# #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates cc files', () async { - const String fileName = 'main.cc'; - const String language = 'cc'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates cpp files', () async { - const String fileName = 'main.cpp'; - const String language = 'c++'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates files with no extension', () async { - const String fileName = 'main'; - const String language = 'txt'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -#docregion SomeSection -A B C -#enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates kotlin files', () async { - const String fileName = 'main.kt'; - const String language = 'kotlin'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates m files', () async { - const String fileName = 'main.m'; - const String language = 'objectivec'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates js files', () async { - const String fileName = 'main.js'; - const String language = 'js'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates java files', () async { - const String fileName = 'main.java'; - const String language = 'java'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates swift files', () async { - const String fileName = 'main.swift'; - const String language = 'swift'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -// #docregion SomeSection -A B C -// #enddocregion SomeSection -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates css files', () async { - const String fileName = 'main.css'; - const String language = 'css'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -/* #docregion SomeSection */ -A B C -/* #enddocregion SomeSection */ -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); - - test('updates html files', () async { - const String fileName = 'main.html'; - const String language = 'html'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL - -A B C - -FAIL -''', ''' -Example: - - -```$language -A B C -``` -''', fileName); - }); + group('File type tests', () { + const List> testCases = >[ + {'fileName': 'main.cc'}, + {'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': ''}, + ]; - test('updates xml files', () async { - const String fileName = 'main.xml'; - const String language = 'xml'; + 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'] ?? ''; - await testInjection(''' + await testInjection(''' Example: @@ -808,9 +491,9 @@ X Y Z ``` ''', ''' FAIL - +$prefix#docregion SomeSection$suffix A B C - +$prefix#enddocregion SomeSection$suffix FAIL ''', ''' Example: @@ -820,33 +503,13 @@ Example: A B C ``` ''', fileName); - }); - - test('updates yaml files', () async { - const String fileName = 'main.yaml'; - const String language = 'yaml'; - - await testInjection(''' -Example: - - -```$language -X Y Z -``` -''', ''' -FAIL -# #docregion SomeSection -A B C -# #enddocregion SomeSection -FAIL -''', ''' -Example: + }); + } - -```$language -A B C -``` -''', fileName); + // ignore: prefer_foreach + for (final Map testCase in testCases) { + runTest(testCase); + } }); } From bc628be43436f40af941ae31f6f9003785560620 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Dec 2023 14:53:13 -0800 Subject: [PATCH 5/9] Re-add recognition of '.cc' files as 'c++' language --- script/tool/lib/src/update_excerpts_command.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/script/tool/lib/src/update_excerpts_command.dart b/script/tool/lib/src/update_excerpts_command.dart index edd40fd424c..24381bf6d33 100644 --- a/script/tool/lib/src/update_excerpts_command.dart +++ b/script/tool/lib/src/update_excerpts_command.dart @@ -142,6 +142,7 @@ class UpdateExcerptsCommand extends PackageLoopingCommand { case '.kt': language = 'kotlin'; break; + case '.cc': case '.cpp': language = 'c++'; break; From 2a0a18a28e6f99505a3060a43f7c2f2f5f78aa03 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Dec 2023 14:58:59 -0800 Subject: [PATCH 6/9] Replace 'fileName' with 'filename' --- .../test/update_excerpts_command_test.dart | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index d662f2f67e2..3a710dea933 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -34,12 +34,12 @@ void runAllTests(MockPlatform platform) { }); Future testInjection( - String before, String source, String after, String fileName, + String before, String source, String after, String filename, {bool failOnChange = false}) async { final RepositoryPackage package = createFakePackage('a_package', packagesDir); package.readmeFile.writeAsStringSync(before); - package.directory.childFile(fileName).writeAsStringSync(source); + package.directory.childFile(filename).writeAsStringSync(source); Object? errorObject; final List output = await runCapturingPrint( runner, @@ -58,12 +58,12 @@ void runAllTests(MockPlatform platform) { } test('succeeds when nothing has changed', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; const String readme = ''' Example: - + ```dart A B C ``` @@ -75,7 +75,7 @@ A B C // #enddocregion SomeSection FAIL '''; - await testInjection(readme, source, readme, fileName); + await testInjection(readme, source, readme, filename); }); test('fails if example injection fails', () async { @@ -115,12 +115,12 @@ FAIL }); test('updates files', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; await testInjection(''' Example: - + ```dart X Y Z ``` @@ -133,11 +133,11 @@ FAIL ''', ''' Example: - + ```dart A B C ``` -''', fileName); +''', filename); }); test('fails if READMEs are changed with --fail-on-change', () async { @@ -175,16 +175,16 @@ FAIL test('does not fail if READMEs are not changed with --fail-on-change', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; const String readme = ''' Example: - + ```dart A ``` - + ```dart B ``` @@ -200,18 +200,18 @@ B // #enddocregion bb ''', readme, - fileName, + filename, failOnChange: true, ); }); test('indents the plaster', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; await testInjection(''' Example: - + ```dart ``` ''', ''' @@ -224,22 +224,22 @@ B ''', ''' Example: - + ```dart A // ··· B ``` -''', fileName); +''', filename); }); test('does not unindent blocks if plaster will not unindent', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; await testInjection(''' Example: - + ```dart ``` ''', ''' @@ -252,22 +252,22 @@ Example: ''', ''' Example: - + ```dart A // ··· B ``` -''', fileName); +''', filename); }); test('unindents blocks', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; await testInjection(''' Example: - + ```dart ``` ''', ''' @@ -280,22 +280,22 @@ Example: ''', ''' Example: - + ```dart A // ··· B ``` -''', fileName); +''', filename); }); test('unindents blocks and plaster', () async { - const String fileName = 'main.dart'; + const String filename = 'main.dart'; await testInjection(''' Example: - + ```dart ``` ''', ''' @@ -308,13 +308,13 @@ Example: ''', ''' Example: - + ```dart A // ··· B ``` -''', fileName); +''', filename); }); test('relative path bases', () async { @@ -446,46 +446,46 @@ FAIL group('File type tests', () { const List> testCases = >[ - {'fileName': 'main.cc'}, - {'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.cc'}, + {'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', + 'filename': 'main.css', 'prefix': '/* ', 'suffix': ' */' }, { - 'fileName': 'main.html', + 'filename': 'main.html', 'prefix': '' }, { - 'fileName': 'main.xml', + 'filename': 'main.xml', 'prefix': '' }, - {'fileName': 'main.yaml', 'prefix': '# '}, - {'fileName': 'main.sh', 'prefix': '# '}, - {'fileName': 'main', 'language': 'txt', '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]; + 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'] ?? ''; await testInjection(''' Example: - + ```$language X Y Z ``` @@ -498,11 +498,11 @@ FAIL ''', ''' Example: - + ```$language A B C ``` -''', fileName); +''', filename); }); } From 0ec0bd61b8cde21385ebdf16012a49b232cbfa11 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Dec 2023 15:03:29 -0800 Subject: [PATCH 7/9] Fix test case for extracting excerpts from '.cc' files --- script/tool/test/update_excerpts_command_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index 3a710dea933..e1e267b7e3a 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -446,7 +446,7 @@ FAIL group('File type tests', () { const List> testCases = >[ - {'filename': 'main.cc'}, + {'filename': 'main.cc', 'language': 'c++'}, {'filename': 'main.cpp', 'language': 'c++'}, {'filename': 'main.dart'}, {'filename': 'main.js'}, From 9c5ff635d00145b8cc03350466db6b8da8753f0b Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Dec 2023 15:32:44 -0800 Subject: [PATCH 8/9] Convert 'testInjection' function arguments to named arguments --- .../test/update_excerpts_command_test.dart | 115 +++++++++++++----- 1 file changed, 82 insertions(+), 33 deletions(-) diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index e1e267b7e3a..d8df4298e9d 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -34,8 +34,11 @@ void runAllTests(MockPlatform platform) { }); Future testInjection( - String before, String source, String after, String filename, - {bool failOnChange = false}) async { + {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); @@ -75,7 +78,8 @@ A B C // #enddocregion SomeSection FAIL '''; - await testInjection(readme, source, readme, filename); + await testInjection( + before: readme, source: source, after: readme, filename: filename); }); test('fails if example injection fails', () async { @@ -117,27 +121,34 @@ FAIL test('updates files', () async { const String filename = 'main.dart'; - await testInjection(''' + 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 ``` -''', filename); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('fails if READMEs are changed with --fail-on-change', () async { @@ -189,18 +200,21 @@ A B ``` '''; - await testInjection( - readme, - ''' + + const String source = ''' // #docregion aa A // #enddocregion aa // #docregion bb B // #enddocregion bb -''', - readme, - filename, +'''; + + await testInjection( + before: readme, + source: source, + after: readme, + filename: filename, failOnChange: true, ); }); @@ -208,20 +222,24 @@ B test('indents the plaster', () async { const String filename = 'main.dart'; - await testInjection(''' + const String before = ''' Example: ```dart ``` -''', ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', ''' +'''; + + const String after = ''' Example: @@ -230,26 +248,33 @@ A // ··· B ``` -''', filename); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('does not unindent blocks if plaster will not unindent', () async { const String filename = 'main.dart'; - await testInjection(''' + const String before = ''' Example: ```dart ``` -''', ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', ''' +'''; + + const String after = ''' Example: @@ -258,26 +283,33 @@ Example: // ··· B ``` -''', filename); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('unindents blocks', () async { const String filename = 'main.dart'; - await testInjection(''' + const String before = ''' Example: ```dart ``` -''', ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', ''' +'''; + + const String after = ''' Example: @@ -286,26 +318,33 @@ A // ··· B ``` -''', filename); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('unindents blocks and plaster', () async { const String filename = 'main.dart'; - await testInjection(''' + const String before = ''' Example: ```dart ``` -''', ''' +'''; + + const String source = ''' // #docregion SomeSection A // #enddocregion SomeSection // #docregion SomeSection B // #enddocregion SomeSection -''', ''' +'''; + + const String after = ''' Example: @@ -314,7 +353,10 @@ A // ··· B ``` -''', filename); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); test('relative path bases', () async { @@ -482,27 +524,34 @@ FAIL final String prefix = testCase['prefix'] ?? '// '; final String suffix = testCase['suffix'] ?? ''; - await testInjection(''' + 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 ``` -''', filename); +'''; + + await testInjection( + before: before, source: source, after: after, filename: filename); }); } From dde5ebf5a4bad528882837eeb9fab1cf978947d8 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Dec 2023 15:35:23 -0800 Subject: [PATCH 9/9] Change 'for...in' loop to 'forEach' --- script/tool/test/update_excerpts_command_test.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index d8df4298e9d..187f0d8d598 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -555,10 +555,7 @@ A B C }); } - // ignore: prefer_foreach - for (final Map testCase in testCases) { - runTest(testCase); - } + testCases.forEach(runTest); }); }