Tests for syntax highlighting

This commit is contained in:
Simon Binder 2020-02-25 21:54:12 +01:00
parent d7bb4f51e6
commit 3e691a72a6
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 91 additions and 2 deletions

View File

@ -0,0 +1,52 @@
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
import 'package:build/build.dart';
import 'package:moor_generator/src/services/ide/moor_ide.dart';
import 'package:test/test.dart';
import 'utils.dart';
final _asset = AssetId('foo', 'lib/bar.moor');
void main() {
MoorIde moorIde;
Map<AssetId, String> data;
List<HighlightRegion> results;
setUp(() {
data = {};
moorIde = spawnIde(data);
});
Future<void> highlight(String source) async {
data[_asset] = source;
results = await moorIde.highlight('/foo/lib/bar.moor');
}
void expectRegion(String content, HighlightRegionType type) {
final failureBuffer = StringBuffer();
for (final region in results) {
final regionType = region.type;
final lexeme =
data[_asset].substring(region.offset, region.offset + region.length);
if (regionType == type && lexeme == content) return;
failureBuffer.write('$regionType with $lexeme \n');
}
fail(
'Expected region of type $type with text $content. Got $failureBuffer');
}
test('import statement', () async {
await highlight("import 'package:bar/baz.moor'");
expectRegion('import', HighlightRegionType.BUILT_IN);
expectRegion("'package:bar/baz.moor'", HighlightRegionType.LITERAL_STRING);
});
test('string literals', () async {
await highlight("query: SELECT 'foo';");
expectRegion("'foo'", HighlightRegionType.LITERAL_STRING);
});
}

View File

@ -0,0 +1,33 @@
import 'package:build/build.dart';
import 'package:moor_generator/src/analyzer/session.dart';
import 'package:moor_generator/src/services/ide/moor_ide.dart';
import 'package:path/path.dart';
import '../../utils/test_backend.dart';
MoorIde spawnIde(Map<AssetId, String> content) {
final backend = TestBackend(content, enableDartAnalyzer: false);
final session = MoorSession(backend);
return MoorIde(session, _FileManagementForTesting(session, backend));
}
class _FileManagementForTesting implements IdeFileManagement {
final TestBackend backend;
final MoorSession session;
_FileManagementForTesting(this.session, this.backend);
@override
Uri fsPathToUri(String path) {
final segments = posix.split(path).skip(1);
final asset = AssetId(segments.first, segments.skip(1).join('/'));
return asset.uri;
}
@override
Future<void> waitUntilParsed(String path) async {
final task = session.startTask(backend.startTask(fsPathToUri(path)));
await task.runTask();
}
}

View File

@ -17,8 +17,12 @@ class TestBackend extends Backend {
/// input files have been parsed and analyzed by the Dart analyzer.
Future get _ready => _initCompleter.future;
TestBackend(this.fakeContent) {
_init();
TestBackend(this.fakeContent, {bool enableDartAnalyzer = false}) {
if (enableDartAnalyzer) {
_init();
} else {
_initCompleter.complete();
}
}
void _init() {