From b04ebc9e8c30c57aef738e840e2acc38f19ee4fc Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Sat, 18 Jan 2020 20:51:09 +0100 Subject: [PATCH] Utils to make testing autocomplete easier --- .../test/engine/autocomplete/static_test.dart | 25 +++------- sqlparser/test/engine/autocomplete/utils.dart | 48 +++++++++++++++++++ 2 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 sqlparser/test/engine/autocomplete/utils.dart diff --git a/sqlparser/test/engine/autocomplete/static_test.dart b/sqlparser/test/engine/autocomplete/static_test.dart index 9b8ca8c5..b4bab009 100644 --- a/sqlparser/test/engine/autocomplete/static_test.dart +++ b/sqlparser/test/engine/autocomplete/static_test.dart @@ -1,39 +1,28 @@ -import 'package:sqlparser/sqlparser.dart'; import 'package:sqlparser/src/engine/autocomplete/engine.dart'; import 'package:test/test.dart'; -final _moorOptions = EngineOptions(useMoorExtensions: true); +import 'utils.dart'; void main() { test('suggests a CREATE an empty file', () { - final engine = SqlEngine.withOptions(_moorOptions); - final parseResult = engine.parseMoorFile(''); - - final suggestions = parseResult.autoCompleteEngine.suggestCompletions(0); + final suggestions = completionsFor('^'); expect(suggestions.anchor, 0); - expect(suggestions.suggestions, contains(hasCode('CREATE'))); - expect(suggestions.suggestions, contains(hasCode('CREATE'))); + expect(suggestions, suggests('CREATE')); }); test('suggests CREATE TABLE completion after CREATE', () async { - final engine = SqlEngine.withOptions(_moorOptions); - final parseResult = engine.parseMoorFile('CREATE '); - - final suggestions = parseResult.autoCompleteEngine.suggestCompletions(7); + final suggestions = completionsFor('CREATE ^'); expect(suggestions.anchor, 7); - expect(suggestions.suggestions, contains(hasCode('TABLE'))); + expect(suggestions, suggests('TABLE')); }); test('suggests completions for started keywords', () { - final engine = SqlEngine.withOptions(_moorOptions); - final parseResult = engine.parseMoorFile('creat'); - - final suggestions = parseResult.autoCompleteEngine.suggestCompletions(0); + final suggestions = completionsFor('creat^'); expect(suggestions.anchor, 0); - expect(suggestions.suggestions, contains(hasCode('CREATE'))); + expect(suggestions, suggests('CREATE')); }); } diff --git a/sqlparser/test/engine/autocomplete/utils.dart b/sqlparser/test/engine/autocomplete/utils.dart new file mode 100644 index 00000000..02084772 --- /dev/null +++ b/sqlparser/test/engine/autocomplete/utils.dart @@ -0,0 +1,48 @@ +import 'package:sqlparser/sqlparser.dart'; +import 'package:sqlparser/src/engine/autocomplete/engine.dart'; +import 'package:test/test.dart'; + +/// Parses the [moorFile] and computes available autocomplete suggestions at the +/// position of a `^` character in the source. +ComputedSuggestions completionsFor(String moorFile) { + final position = moorFile.indexOf('^'); + final engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true)); + + final result = engine.parseMoorFile(moorFile.replaceFirst('^', '')); + return result.autoCompleteEngine.suggestCompletions(position - 1); +} + +Matcher hasCode(String code) => _SuggestionWithCode(code); +Matcher suggests(String code) => _SuggestsMatcher(contains(hasCode(code))); +Matcher suggestsAll(List codes) { + return _SuggestsMatcher(containsAll(codes.map(hasCode))); +} + +class _SuggestionWithCode extends Matcher { + final Matcher codeMatcher; + + _SuggestionWithCode(dynamic code) : codeMatcher = wrapMatcher(code); + + @override + Description describe(Description description) { + return description.add('suggests ').addDescriptionOf(codeMatcher); + } + + @override + bool matches(dynamic item, Map matchState) { + return item is Suggestion && codeMatcher.matches(item.code, matchState); + } +} + +class _SuggestsMatcher extends CustomMatcher { + _SuggestsMatcher(matcher) + : super('Suggestions containing', 'suggestions', matcher); + + @override + List featureValueOf(dynamic actual) { + if (actual is ComputedSuggestions) { + return actual.suggestions; + } + return null; + } +}