From 1dd1669582a13618fe3fff65c910971ae12a37c6 Mon Sep 17 00:00:00 2001 From: Robert Virkus Date: Mon, 6 Sep 2021 16:34:56 +0200 Subject: [PATCH] forward customParser argument This fixes #4 --- lib/src/components.dart | 9 ++++++--- test/extension_test.dart | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 test/extension_test.dart diff --git a/lib/src/components.dart b/lib/src/components.dart index e5fe31c..3426833 100644 --- a/lib/src/components.dart +++ b/lib/src/components.dart @@ -160,6 +160,7 @@ abstract class VComponent { /// The [text] can either contain `\r\n` (`CRLF`) or `\n` linebreaks, when both linebreak types are present in the [text], `CRLF` linebreaks are assumed. /// Folded lines are unfolded automatically. /// When you have a custom line delimiter, use [parseLines] instead. + /// Define the [customParser] if you want to support specific properties. Note that unknown properties will be made available with [getProperty], e.g. `final value = component.getProperty('X-NAME')?.textValue;` static VComponent parse(String text, {Property? Function(String name, String definition)? customParser}) { final containsStandardCompliantLineBreaks = text.contains('\r\n'); @@ -173,13 +174,14 @@ abstract class VComponent { if (lines.isEmpty) { throw FormatException('Invalid input: [$text]'); } - return parseLines(lines); + return parseLines(lines, customParser: customParser); } /// Parses the component from the specified text [lines]. /// /// Compare [parse] for details. - static VComponent parseLines(List lines) { + static VComponent parseLines(List lines, + {Property? Function(String name, String definition)? customParser}) { VComponent root = _createComponent(lines.first); VComponent current = root; for (var i = 1; i < lines.length; i++) { @@ -199,7 +201,8 @@ abstract class VComponent { current = parent; } } else { - final property = Property.parseProperty(line); + final property = + Property.parseProperty(line, customParser: customParser); current.properties.add(property); } } diff --git a/test/extension_test.dart b/test/extension_test.dart new file mode 100644 index 0000000..cfaf916 --- /dev/null +++ b/test/extension_test.dart @@ -0,0 +1,31 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:enough_icalendar/enough_icalendar.dart'; + +void main() { + test('Calendar simple', () { + final text = '''BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//hacksw/handcal//NONSGML v1.0//EN +X-XXX-NUMBER:123 +BEGIN:VEVENT +UID:19970610T172345Z-AF23B2@example.com +DTSTAMP:19970610T172345Z +DTSTART:19970714T170000Z +DTEND:19970715T040000Z +SUMMARY:Bastille Day Party +END:VEVENT +END:VCALENDAR'''; + // final calendar = VComponent.parse(text); + final calendar = VComponent.parse(text, customParser: (name, definition) { + if (name == 'X-XXX-NUMBER') { + return IntegerProperty(definition); + } + }); + expect(calendar, isInstanceOf()); + expect(calendar.children, isNotEmpty); + expect( + calendar.getProperty('X-XXX-NUMBER'), isInstanceOf()); + expect( + calendar.getProperty('X-XXX-NUMBER')?.intValue, 123); + }); +}