Skip to content

Commit 6303884

Browse files
authored
Add FileInfo / DirectoryInfo parser (#39)
1 parent 9057f9b commit 6303884

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ namespace Example
520520

521521
# 💡 Example: Parsing and validation
522522
There are already parsers for many data types implemented: all types of `int`, `char`, `bool`,
523-
`DateTime`, `DateTimeOffset`, `decimal`, `double`, `float`, `byte`, `Uri`, `string` (which is
524-
trivial) and enums
523+
`DateTime`, `DateTimeOffset`, `decimal`, `double`, `float`, `byte`, `Uri`, `FileInfo`,
524+
`DirectoryInfo`, `string` (which is trivial) and enums
525525

526526
It might happen that you have to implement your own parser. It is always possible to define a
527527
parser to any definition. There are even cases where not every value in the parsed domain is

doc/README.md.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ A blocking example:
185185

186186
# 💡 Example: Parsing and validation
187187
There are already parsers for many data types implemented: all types of `int`, `char`, `bool`,
188-
`DateTime`, `DateTimeOffset`, `decimal`, `double`, `float`, `byte`, `Uri`, `string` (which is
189-
trivial) and enums
188+
`DateTime`, `DateTimeOffset`, `decimal`, `double`, `float`, `byte`, `Uri`, `FileInfo`,
189+
`DirectoryInfo`, `string` (which is trivial) and enums
190190

191191
It might happen that you have to implement your own parser. It is always possible to define a
192192
parser to any definition. There are even cases where not every value in the parsed domain is

source/FluentArgs.Test/Parsing/DefaultParserTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.IO;
56
using System.Threading.Tasks;
67
using FluentAssertions;
78
using Xunit;
@@ -294,6 +295,42 @@ public static void DefaultNullableUriParser_ShouldExist(string value)
294295
.Should().Be(new Uri(value));
295296
}
296297

298+
[Theory]
299+
[InlineData("file.txt")]
300+
[InlineData("directory/file.txt")]
301+
public static void DefaultFileInfoParser_ShouldExist(string value)
302+
{
303+
ParseValueWithDefaultParser<FileInfo>(value)
304+
.Should().NotBeNull();
305+
}
306+
307+
[Theory]
308+
[InlineData("file.txt")]
309+
[InlineData("directory/file.txt")]
310+
public static void DefaultNullableFileInfoParser_ShouldExist(string value)
311+
{
312+
ParseValueWithDefaultParser<FileInfo?>(value)
313+
.Should().NotBeNull();
314+
}
315+
316+
[Theory]
317+
[InlineData("./")]
318+
[InlineData("directoryA/directoryB")]
319+
public static void DefaultDirectoryInfoParser_ShouldExist(string value)
320+
{
321+
ParseValueWithDefaultParser<DirectoryInfo>(value)
322+
.Should().NotBeNull();
323+
}
324+
325+
[Theory]
326+
[InlineData("./")]
327+
[InlineData("directoryA/directoryB")]
328+
public static void DefaultNullableDirectoryInfoParser_ShouldExist(string value)
329+
{
330+
ParseValueWithDefaultParser<DirectoryInfo?>(value)
331+
.Should().NotBeNull();
332+
}
333+
297334
[Theory]
298335
[InlineData("")]
299336
[InlineData(" ")]

source/FluentArgs/Parser/DefaultStringParsers.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.Immutable;
66
using System.Globalization;
7+
using System.IO;
78
using System.Linq;
89

910
internal static class DefaultStringParsers
@@ -33,7 +34,10 @@ internal static class DefaultStringParsers
3334
[typeof(DateTimeOffset)] = s => DateTimeOffset.Parse(s, CultureInfo.InvariantCulture),
3435
[typeof(TimeSpan)] = s => TimeSpan.Parse(s, CultureInfo.InvariantCulture),
3536

36-
[typeof(Uri)] = s => new Uri(s)
37+
[typeof(Uri)] = s => new Uri(s),
38+
39+
[typeof(FileInfo)] = s => new FileInfo(s),
40+
[typeof(DirectoryInfo)] = s => new DirectoryInfo(s)
3741
};
3842

3943
private delegate T ParseNumberFunc<out T>(string input);

0 commit comments

Comments
 (0)