diff --git a/moor_generator/test/services/ide/highlights_test.dart b/moor_generator/test/services/ide/highlights_test.dart new file mode 100644 index 00000000..f4e1ea4a --- /dev/null +++ b/moor_generator/test/services/ide/highlights_test.dart @@ -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 data; + List results; + + setUp(() { + data = {}; + moorIde = spawnIde(data); + }); + + Future 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); + }); +} diff --git a/moor_generator/test/services/ide/utils.dart b/moor_generator/test/services/ide/utils.dart new file mode 100644 index 00000000..d53d6e14 --- /dev/null +++ b/moor_generator/test/services/ide/utils.dart @@ -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 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 waitUntilParsed(String path) async { + final task = session.startTask(backend.startTask(fsPathToUri(path))); + await task.runTask(); + } +} diff --git a/moor_generator/test/utils/test_backend.dart b/moor_generator/test/utils/test_backend.dart index 829f8366..6e6ceb13 100644 --- a/moor_generator/test/utils/test_backend.dart +++ b/moor_generator/test/utils/test_backend.dart @@ -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() {