drift/drift_dev/test/analyzer/utils.dart

83 lines
2.2 KiB
Dart
Raw Normal View History

2019-12-21 13:44:52 -08:00
import 'package:build/build.dart';
2021-10-08 09:10:48 -07:00
import 'package:drift_dev/moor_generator.dart';
import 'package:drift_dev/src/analyzer/errors.dart';
2021-10-08 09:10:48 -07:00
import 'package:drift_dev/src/analyzer/options.dart';
import 'package:drift_dev/src/analyzer/runner/file_graph.dart';
import 'package:drift_dev/src/analyzer/runner/task.dart';
import 'package:drift_dev/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,
2022-04-22 14:38:41 -07:00
{MoorOptions? options}) {
final backend = TestBackend(
{
for (final entry in content.entries)
AssetId.parse(entry.key): entry.value,
},
);
2019-12-21 13:44:52 -08:00
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
2021-07-26 10:48:26 -07:00
Object? featureValueOf(dynamic actual) {
if (actual is! SqlSelectQuery) {
return actual;
}
2021-07-26 10:48:26 -07:00
final resultSet = actual.resultSet;
return {for (final column in resultSet.columns) column.name: column.type};
}
}
extension ExpectErrors on FoundFile {
void expectDartError(dynamic matcher, String lexeme) {
expect(
errors.errors,
contains(isA<ErrorInDartCode>()
.having((e) => e.message, 'message', matcher)
.having((e) => e.span?.text, 'span.text', lexeme)));
}
}