-
Notifications
You must be signed in to change notification settings - Fork 235
fix: test optimizer to import files as valid dart names #658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7de8342
fix: avoid invalid import names
alestiago 494899b
test: updated pregen test
alestiago 74d22ff
feat: altered StringIdGenerator
alestiago bca443b
refactor: renamed and documented dart_indentifier_generator
alestiago 8299730
feat: updated brick to have groups as snake cased
alestiago 86adec3
test: updated test
alestiago 0952d4f
test: refactored test name
alestiago e4a351c
docs: updated readme
alestiago 536fb27
docs: readme typo
alestiago 88dfbf9
test: readme typo
alestiago 7359740
docs: phrasing
alestiago d4a703b
refactor: removed not needed const
alestiago efa058a
test: removed not required expect
alestiago e679eb2
test: included _test suffix
alestiago 2ee65f6
Merge branch 'main' into fix/square-brackets-not-allowed-as-imports
alestiago e68c52a
docs: minor documentation changes
alestiago 3104401
Merge branch 'fix/square-brackets-not-allowed-as-imports' of https://…
alestiago c877ec0
Merge branch 'main' into fix/square-brackets-not-allowed-as-imports
alestiago File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
bricks/test_optimizer/hooks/lib/dart_identifier_generator.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /// {@template DartIdentifierGenerator} | ||
| /// A class that generates valid Dart identifiers. | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * Section 17.37 from [Dart Language Specification](https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf) | ||
| /// {@endtemplate} | ||
| class DartIdentifierGenerator { | ||
| /// {@macro DartIdentifierGenerator} | ||
alestiago marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| DartIdentifierGenerator([ | ||
| this._chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', | ||
| ]) : _nextId = [0]; | ||
|
|
||
| final String _chars; | ||
| final List<int> _nextId; | ||
|
|
||
| /// Generate the next short identifier. | ||
| String next() { | ||
| final r = <String>['_', for (final char in _nextId) _chars[char]]; | ||
| _increment(); | ||
| return r.join(); | ||
| } | ||
|
|
||
| void _increment() { | ||
| for (var i = 0; i < _nextId.length; i++) { | ||
| final val = ++_nextId[i]; | ||
| if (val >= _chars.length) { | ||
| _nextId[i] = 0; | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
| _nextId.add(0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
bricks/test_optimizer/hooks/test/dart_identifier_generator_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import 'package:hooks/dart_identifier_generator.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| group('$DartIdentifierGenerator', () { | ||
| test('can be instantiated', () { | ||
| expect(DartIdentifierGenerator.new, returnsNormally); | ||
| }); | ||
|
|
||
| group('next', () { | ||
| test('returns normally', () { | ||
| final generator = DartIdentifierGenerator(); | ||
| expect(generator.next, returnsNormally); | ||
| }); | ||
|
|
||
| test('generates unique strings', () { | ||
| final generator = DartIdentifierGenerator(); | ||
| final ids = <String>{}; | ||
| const count = 1000; | ||
| for (var i = 0; i < count; i++) { | ||
| final id = generator.next(); | ||
| ids.add(id); | ||
| } | ||
| expect(ids.length, count); | ||
| }); | ||
|
|
||
| test('generates valid dart identifiers', () { | ||
| // For a full specification of valid dart identifiers, read | ||
| // Section 17.37 from the [Dart Language Specification](https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf). | ||
| final generator = DartIdentifierGenerator(); | ||
| final ids = <String>[]; | ||
| for (var i = 0; i < 1000; i++) { | ||
| final id = generator.next(); | ||
| ids.add(id); | ||
| } | ||
|
|
||
| expect( | ||
| ids.where((id) => _dartReservedKeywords.contains(id)), | ||
| isEmpty, | ||
| ); | ||
| expect( | ||
| ids.every((id) { | ||
| final idStart = id.codeUnitAt(0); | ||
| final isAlphabetic = (idStart >= 65 && idStart <= 90) || | ||
| (idStart >= 97 && idStart <= 122); | ||
| final isUnderscore = idStart == 95; | ||
| final isDollarSign = idStart == 36; | ||
| return isAlphabetic || isUnderscore || isDollarSign; | ||
| }), | ||
| true, | ||
| ); | ||
| expect( | ||
| ids.every((id) { | ||
| final idPart = id.codeUnits.skip(1); | ||
| return idPart.every((ascii) { | ||
| final isAlphabetic = | ||
| (ascii >= 65 && ascii <= 90) || (ascii >= 97 && ascii <= 122); | ||
| final isUnderscore = ascii == 95; | ||
| final isDollarSign = ascii == 36; | ||
| final isDigit = ascii >= 48 && ascii <= 57; | ||
| return isAlphabetic || isUnderscore || isDollarSign || isDigit; | ||
| }); | ||
| }), | ||
| true, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // All reserved keywords in [Dart 2.19.2](https://dart.dev/guides/language/language-tour#keywords). | ||
| const _dartReservedKeywords = [ | ||
| 'abstract', | ||
| 'as', | ||
| 'assert', | ||
| 'async', | ||
| 'await', | ||
| 'break', | ||
| 'case', | ||
| 'catch', | ||
| 'class', | ||
| 'const', | ||
| 'continue', | ||
| 'covariant', | ||
| 'default', | ||
| 'deferred', | ||
| 'do', | ||
| 'dynamic', | ||
| 'else', | ||
| 'enum', | ||
| 'export', | ||
| 'extends', | ||
| 'extension', | ||
| 'external', | ||
| 'factory', | ||
| 'false', | ||
| 'final', | ||
| 'finally', | ||
| 'for', | ||
| 'Function', | ||
| 'get', | ||
| 'hide', | ||
| 'if', | ||
| 'implements', | ||
| 'import', | ||
| 'in', | ||
| 'interface', | ||
| 'is', | ||
| 'late', | ||
| 'library', | ||
| 'mixin', | ||
| 'new', | ||
| 'null', | ||
| 'on', | ||
| 'operator', | ||
| 'part', | ||
| 'required', | ||
| 'rethrow', | ||
| 'return', | ||
| 'set', | ||
| 'show', | ||
| 'static', | ||
| 'super', | ||
| 'switch', | ||
| 'sync', | ||
| 'this', | ||
| 'throw', | ||
| 'true', | ||
| 'try', | ||
| 'typedef', | ||
| 'var', | ||
| 'void', | ||
| 'while', | ||
| 'with', | ||
| 'yield', | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.