2021-03-17 23:04:36 +00:00
|
|
|
import 'package:combinator/combinator.dart';
|
|
|
|
import 'package:matcher/matcher.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
|
|
|
|
main() {
|
|
|
|
test('advance', () {
|
|
|
|
var scanner = scan('hello world');
|
|
|
|
|
|
|
|
// Casted -> dynamic just for the sake of coverage.
|
|
|
|
var parser = match('he').forward(2).castDynamic();
|
|
|
|
parser.parse(scanner);
|
|
|
|
expect(scanner.position, 4);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('change', () {
|
2021-03-18 00:00:56 +00:00
|
|
|
var parser = match('hello').change((r) => r!.change(value: 23));
|
|
|
|
expect(parser.parse(scan('helloworld'))!.value, 23);
|
2021-03-17 23:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('check', () {
|
|
|
|
var parser = match<int>(new RegExp(r'[A-Za-z]+'))
|
2021-03-18 00:00:56 +00:00
|
|
|
.value((r) => r.span!.length)
|
2021-03-17 23:04:36 +00:00
|
|
|
.check(greaterThan(3));
|
2021-03-18 00:00:56 +00:00
|
|
|
expect(parser.parse(scan('helloworld'))!.successful, isTrue);
|
|
|
|
expect(parser.parse(scan('yo'))!.successful, isFalse);
|
2021-03-17 23:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('map', () {
|
2021-03-18 00:00:56 +00:00
|
|
|
var parser =
|
|
|
|
match(new RegExp(r'[A-Za-z]+')).map<int>((r) => r.span!.length);
|
|
|
|
expect(parser.parse(scan('hello'))!.value, 5);
|
2021-03-17 23:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('negate', () {
|
|
|
|
var parser = match('hello').negate(errorMessage: 'world');
|
2021-03-18 00:00:56 +00:00
|
|
|
expect(parser.parse(scan('goodbye world'))!.successful, isTrue);
|
|
|
|
expect(parser.parse(scan('hello world'))!.successful, isFalse);
|
|
|
|
expect(parser.parse(scan('hello world'))!.errors.first.message, 'world');
|
2021-03-17 23:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('opt', () {
|
|
|
|
var single = match('hello').opt(backtrack: true);
|
|
|
|
var list = match('hel').then(match('lo')).opt();
|
|
|
|
|
|
|
|
test('succeeds if present', () {
|
2021-03-18 00:00:56 +00:00
|
|
|
expect(single.parse(scan('hello'))!.successful, isTrue);
|
|
|
|
expect(list.parse(scan('hello'))!.successful, isTrue);
|
2021-03-17 23:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('succeeds if not present', () {
|
2021-03-18 00:00:56 +00:00
|
|
|
expect(single.parse(scan('goodbye'))!.successful, isTrue);
|
|
|
|
expect(list.parse(scan('goodbye'))!.successful, isTrue);
|
2021-03-17 23:04:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('backtracks if not present', () {
|
|
|
|
for (var parser in [single, list]) {
|
|
|
|
var scanner = scan('goodbye');
|
|
|
|
var pos = scanner.position;
|
|
|
|
parser.parse(scanner);
|
|
|
|
expect(scanner.position, pos);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('safe', () {});
|
|
|
|
}
|