2021-03-09 12:27:25 -08:00
|
|
|
//@dart=2.9
|
2019-12-21 13:44:52 -08:00
|
|
|
import 'package:build/build.dart';
|
2020-01-15 13:10:18 -08:00
|
|
|
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';
|
2020-01-15 13:10:18 -08:00
|
|
|
import 'package:test/test.dart';
|
2019-12-21 13:44:52 -08:00
|
|
|
|
2019-12-25 12:07:08 -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);
|
|
|
|
|
2020-01-15 13:10:18 -08:00
|
|
|
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);
|
2020-01-15 13:10:18 -08:00
|
|
|
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);
|
|
|
|
}
|
2020-06-28 09:48:32 -07:00
|
|
|
|
|
|
|
void close() {
|
|
|
|
backend.finish();
|
|
|
|
}
|
2019-12-21 13:44:52 -08:00
|
|
|
}
|
2020-01-15 13:10:18 -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};
|
|
|
|
}
|
|
|
|
}
|