drift/moor_generator/test/analyzer/utils.dart

72 lines
1.9 KiB
Dart
Raw Normal View History

//@dart=2.9
2019-12-21 13:44:52 -08:00
import 'package:build/build.dart';
import 'package:moor_generator/moor_generator.dart';
import 'package:moor_generator/src/analyzer/options.dart';
2019-12-21 13:44:52 -08:00
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
import 'package:moor_generator/src/analyzer/runner/task.dart';
import 'package:moor_generator/src/analyzer/session.dart';
import 'package:test/test.dart';
2019-12-21 13:44:52 -08:00
import '../utils/test_backend.dart';
2019-12-21 13:44:52 -08:00
class TestState {
TestBackend backend;
MoorSession session;
TestState(this.backend, this.session);
factory TestState.withContent(Map<String, String> content,
{MoorOptions options}) {
2019-12-21 13:44:52 -08:00
final backend = TestBackend({
for (final entry in content.entries)
AssetId.parse(entry.key): entry.value,
});
final session = MoorSession(backend);
if (options != null) {
session.options = options;
}
2019-12-21 13:44:52 -08:00
return TestState(backend, session);
}
Future<Task> runTask(String entrypointUri) async {
final backendTask = backend.startTask(Uri.parse(entrypointUri));
final task = session.startTask(backendTask);
await task.runTask();
return task;
}
FoundFile file(String uri) {
return session.registerFile(Uri.parse(uri));
}
2019-12-31 06:07:08 -08:00
Future<FoundFile> analyze(String uri) async {
await runTask(uri);
return file(uri);
}
void close() {
backend.finish();
}
2019-12-21 13:44:52 -08:00
}
// Matchers
Matcher returnsColumns(Map<String, ColumnType> columns) {
return _HasInferredColumnTypes(columns);
}
class _HasInferredColumnTypes extends CustomMatcher {
_HasInferredColumnTypes(dynamic expected)
: super('Select query with inferred columns', 'columns', expected);
@override
Object featureValueOf(dynamic actual) {
if (actual is! SqlSelectQuery) {
return actual;
}
final query = actual as SqlSelectQuery;
final resultSet = query.resultSet;
return {for (final column in resultSet.columns) column.name: column.type};
}
}