Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Mar 1, 2024
1 parent 249c55a commit 15dd9e0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
32 changes: 12 additions & 20 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import 'generated/sequence_test.dart' as sequence_test;
import 'utils/assertions.dart';
import 'utils/matchers.dart';

TypeMatcher<SeparatedList<R, S>> isSeparatedList<R, S>({
List<R> elements = const [],
List<S> separators = const [],
}) =>
isA<SeparatedList<R, S>>()
.having((list) => list.elements, 'elements', elements)
.having((list) => list.separators, 'separators', separators);

void main() {
group('action', () {
group('cast', () {
Expand Down Expand Up @@ -2161,24 +2153,24 @@ void main() {
expect(
parser,
isParseSuccess('1a2b3',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b'])));
expect(
parser,
isParseSuccess('1a2b3c',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b']),
position: 5));
expect(
parser,
isParseSuccess('1a2b3c4',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b']),
position: 5));
expect(
parser,
isParseSuccess('1a2b3c4d',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b']),
position: 5));
});
Expand All @@ -2193,35 +2185,35 @@ void main() {
expect(
parser,
isParseSuccess('1a2',
result:
isSeparatedList(elements: ['1', '2'], separators: ['a'])));
result: isSeparatedList<String, String>(
elements: ['1', '2'], separators: ['a'])));
expect(
parser,
isParseSuccess('1a2b',
result:
isSeparatedList(elements: ['1', '2'], separators: ['a']),
result: isSeparatedList<String, String>(
elements: ['1', '2'], separators: ['a']),
position: 3));
expect(
parser,
isParseSuccess('1a2b3',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b'])));
expect(
parser,
isParseSuccess('1a2b3c',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b']),
position: 5));
expect(
parser,
isParseSuccess('1a2b3c4',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b']),
position: 5));
expect(
parser,
isParseSuccess('1a2b3c4d',
result: isSeparatedList(
result: isSeparatedList<String, String>(
elements: ['1', '2', '3'], separators: ['a', 'b']),
position: 5));
});
Expand Down
20 changes: 20 additions & 0 deletions test/regression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String?>(
elements: ['1', '2', '3'], separators: [',', ','])));
expect(
parser,
isParseSuccess('12,3',
result: isSeparatedList<String, String?>(
elements: ['1', '2', '3'], separators: [null, ','])));
});
});
}
9 changes: 9 additions & 0 deletions test/utils/matchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<R, S>({
dynamic elements = anything,
dynamic separators = anything,
}) =>
isA<SeparatedList<R, S>>()
.having((list) => list.elements, 'elements', elements)
.having((list) => list.separators, 'separators', separators);

0 comments on commit 15dd9e0

Please sign in to comment.