Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bricks/test_optimizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ The above command will generate a `.test_optimizer.dart` in the `test` directory
// GENERATED CODE - DO NOT MODIFY BY HAND
// Consider adding this file to your .gitignore.

import 'app/view/app_test.dart' as app_view_app_test_dart;
import 'counter/cubit/counter_cubit_test.dart' as counter_cubit_counter_cubit_test_dart;
import 'counter/view/counter_page_test.dart' as counter_view_counter_page_test_dart;
import 'app/view/app_test.dart' as _a;
import 'counter/cubit/counter_cubit_test.dart' as _b;
import 'counter/view/counter_page_test.dart' as _c;

void main() {
app_view_app_test_dart.main();
counter_cubit_counter_cubit_test_dart.main();
counter_view_counter_page_test_dart.main();
group('app/view/app_test.dart.dart', () { _a.main(); });
group('counter/cubit/counter_cubit_test.dart', () { _b.main(); });
group('counter/view/counter_page_test.dart', () { _c.main(); });
}
```

[1]: https://github.com/felangel/mason

---
---

### Note for maintainers

After changing this brick, make sure to run `./tools/generate_test_optimizer_bundle.sh` from the root of the repository to update the bundle.
After changing this brick, make sure to run `./tools/generate_test_optimizer_bundle.sh` from the root of the repository to update the bundle.
4 changes: 2 additions & 2 deletions bricks/test_optimizer/__brick__/test/.test_optimizer.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bricks/test_optimizer/brick.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ vars:
type: string
default: "."
description: The path to the package root.
prompt: Please enter the path to the package root.
prompt: Please enter the path to the package root.
35 changes: 35 additions & 0 deletions bricks/test_optimizer/hooks/lib/dart_identifier_generator.dart
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}
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);
}
}
25 changes: 14 additions & 11 deletions bricks/test_optimizer/hooks/lib/pre_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'dart:io';

import 'package:hooks/dart_identifier_generator.dart';
import 'package:mason/mason.dart';
import 'package:path/path.dart' as path;

Expand All @@ -28,17 +29,19 @@ Future<void> run(HookContext context) async {
final flutterSdkRegExp = RegExp(r'sdk:\s*flutter$', multiLine: true);
final isFlutter = flutterSdkRegExp.hasMatch(pubspecContents);

final tests = testDir
.listSync(recursive: true)
.where((entity) => entity.isTest)
.map(
(entity) => path
.relative(entity.path, from: testDir.path)
.replaceAll(r'\', '/'),
)
.toList();

context.vars = {'tests': tests, 'isFlutter': isFlutter};
final identifierGenerator = DartIdentifierGenerator();
final testIdentifierTable = <Map<String, String>>[];
for (final entity
in testDir.listSync(recursive: true).where((entity) => entity.isTest)) {
final relativePath =
path.relative(entity.path, from: testDir.path).replaceAll(r'\', '/');
testIdentifierTable.add({
'path': relativePath,
'identifier': identifierGenerator.next(),
});
}

context.vars = {'tests': testIdentifierTable, 'isFlutter': isFlutter};
}

extension on FileSystemEntity {
Expand Down
136 changes: 136 additions & 0 deletions bricks/test_optimizer/hooks/test/dart_identifier_generator_test.dart
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',
];
9 changes: 4 additions & 5 deletions bricks/test_optimizer/hooks/test/pre_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ void main() {

await pre_gen.run(context);

final tests = context.vars['tests'] as List<String>;
final tests = context.vars['tests'] as List<Map<String, String>>;

expect(
tests,
containsAll([
'test2_test.dart',
'test1_test.dart',
equals([
{'path': 'test2_test.dart', 'identifier': '_a'},
{'path': 'test1_test.dart', 'identifier': '_b'},
]),
);
expect(test, isNot(contains('no_test_here.dart')));
expect(context.vars['isFlutter'], false);
});

Expand Down
Loading