diff --git a/test/parser_test.dart b/test/parser_test.dart index 327ec45d..06e40ba5 100644 --- a/test/parser_test.dart +++ b/test/parser_test.dart @@ -5,14 +5,6 @@ import 'generated/sequence_test.dart' as sequence_test; import 'utils/assertions.dart'; import 'utils/matchers.dart'; -TypeMatcher> isSeparatedList({ - List elements = const [], - List separators = const [], -}) => - isA>() - .having((list) => list.elements, 'elements', elements) - .having((list) => list.separators, 'separators', separators); - void main() { group('action', () { group('cast', () { @@ -2161,24 +2153,24 @@ void main() { expect( parser, isParseSuccess('1a2b3', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']))); expect( parser, isParseSuccess('1a2b3c', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']), position: 5)); expect( parser, isParseSuccess('1a2b3c4', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']), position: 5)); expect( parser, isParseSuccess('1a2b3c4d', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']), position: 5)); }); @@ -2193,35 +2185,35 @@ void main() { expect( parser, isParseSuccess('1a2', - result: - isSeparatedList(elements: ['1', '2'], separators: ['a']))); + result: isSeparatedList( + elements: ['1', '2'], separators: ['a']))); expect( parser, isParseSuccess('1a2b', - result: - isSeparatedList(elements: ['1', '2'], separators: ['a']), + result: isSeparatedList( + elements: ['1', '2'], separators: ['a']), position: 3)); expect( parser, isParseSuccess('1a2b3', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']))); expect( parser, isParseSuccess('1a2b3c', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']), position: 5)); expect( parser, isParseSuccess('1a2b3c4', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']), position: 5)); expect( parser, isParseSuccess('1a2b3c4d', - result: isSeparatedList( + result: isSeparatedList( elements: ['1', '2', '3'], separators: ['a', 'b']), position: 5)); }); diff --git a/test/regression_test.dart b/test/regression_test.dart index bd6f3bf7..7cbfbc86 100644 --- a/test/regression_test.dart +++ b/test/regression_test.dart @@ -581,4 +581,24 @@ void main() { expect(parser, isParseSuccess('AAaaAA', result: 'fallback')); }); }); + group('https://stackoverflow.com/questions/78078779', () { + test('How to consume only as long as another parser accepts?', () { + final parser = digit().plusLazy(digit().repeat(3).not()); + expect(parser, isParseSuccess('123', result: ['1'], position: 1)); + expect(parser, isParseSuccess('1234', result: ['1', '2'], position: 2)); + }); + test('How to recognize a list of items with optional delimiters?', () { + final parser = digit().plusSeparated(char(',').optional()); + expect( + parser, + isParseSuccess('1,2,3', + result: isSeparatedList( + elements: ['1', '2', '3'], separators: [',', ',']))); + expect( + parser, + isParseSuccess('12,3', + result: isSeparatedList( + elements: ['1', '2', '3'], separators: [null, ',']))); + }); + }); } diff --git a/test/utils/matchers.dart b/test/utils/matchers.dart index 8448062a..433d167d 100644 --- a/test/utils/matchers.dart +++ b/test/utils/matchers.dart @@ -140,3 +140,12 @@ Matcher isLinterIssue({ .having((issue) => issue.parser, 'parser', parser) .having((issue) => issue.description, 'description', description) .having((issue) => issue.toString(), 'toString()', toString); + +/// Returns a [Matcher] that asserts a [SeparatedList]. +Matcher isSeparatedList({ + dynamic elements = anything, + dynamic separators = anything, +}) => + isA>() + .having((list) => list.elements, 'elements', elements) + .having((list) => list.separators, 'separators', separators);