Utils to make testing autocomplete easier

This commit is contained in:
Simon Binder 2020-01-18 20:51:09 +01:00
parent eb77d06cac
commit b04ebc9e8c
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 55 additions and 18 deletions

View File

@ -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'));
});
}

View File

@ -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<String> 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<Suggestion> featureValueOf(dynamic actual) {
if (actual is ComputedSuggestions) {
return actual.suggestions;
}
return null;
}
}