Skip to content

Commit

Permalink
Add FileInfo / DirectoryInfo parser (kutoga#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kutoga authored Oct 23, 2019
1 parent 9057f9b commit 6303884
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ namespace Example

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

It might happen that you have to implement your own parser. It is always possible to define a
parser to any definition. There are even cases where not every value in the parsed domain is
Expand Down
4 changes: 2 additions & 2 deletions doc/README.md.template
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ A blocking example:

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

It might happen that you have to implement your own parser. It is always possible to define a
parser to any definition. There are even cases where not every value in the parsed domain is
Expand Down
37 changes: 37 additions & 0 deletions source/FluentArgs.Test/Parsing/DefaultParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -294,6 +295,42 @@ public static void DefaultNullableUriParser_ShouldExist(string value)
.Should().Be(new Uri(value));
}

[Theory]
[InlineData("file.txt")]
[InlineData("directory/file.txt")]
public static void DefaultFileInfoParser_ShouldExist(string value)
{
ParseValueWithDefaultParser<FileInfo>(value)
.Should().NotBeNull();
}

[Theory]
[InlineData("file.txt")]
[InlineData("directory/file.txt")]
public static void DefaultNullableFileInfoParser_ShouldExist(string value)
{
ParseValueWithDefaultParser<FileInfo?>(value)
.Should().NotBeNull();
}

[Theory]
[InlineData("./")]
[InlineData("directoryA/directoryB")]
public static void DefaultDirectoryInfoParser_ShouldExist(string value)
{
ParseValueWithDefaultParser<DirectoryInfo>(value)
.Should().NotBeNull();
}

[Theory]
[InlineData("./")]
[InlineData("directoryA/directoryB")]
public static void DefaultNullableDirectoryInfoParser_ShouldExist(string value)
{
ParseValueWithDefaultParser<DirectoryInfo?>(value)
.Should().NotBeNull();
}

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down
6 changes: 5 additions & 1 deletion source/FluentArgs/Parser/DefaultStringParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;

internal static class DefaultStringParsers
Expand Down Expand Up @@ -33,7 +34,10 @@ internal static class DefaultStringParsers
[typeof(DateTimeOffset)] = s => DateTimeOffset.Parse(s, CultureInfo.InvariantCulture),
[typeof(TimeSpan)] = s => TimeSpan.Parse(s, CultureInfo.InvariantCulture),

[typeof(Uri)] = s => new Uri(s)
[typeof(Uri)] = s => new Uri(s),

[typeof(FileInfo)] = s => new FileInfo(s),
[typeof(DirectoryInfo)] = s => new DirectoryInfo(s)
};

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

0 comments on commit 6303884

Please sign in to comment.