Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum parser for enums that are mapped to chars #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/SuccincT/Parsers/EnumCharParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using SuccincT.Options;
using System;

namespace SuccincT.Parsers
{
/// <summary>
/// An enum parser that handles enums that are mapped to chars
/// </summary>
public static class EnumCharParser
{
/// <summary>
/// Parses an enum that is mapped to a char
/// </summary>
/// <typeparam name="T">Enum type</typeparam>
/// <param name="source">source char</param>
/// <returns>Option for the parsed enum</returns>
/// <exception cref="T:System.ArgumentExeption">If T is not of an Enum Type</exception>
public static Option<T> TryParsEnum<T>(this char source) where T : struct
{
var sourceAsInt = (int)source;

// Since TryParse will return true even for ints that don't
// correspond to valid enum values, need to check using IsDefined first
if (!Enum.IsDefined(typeof(T), sourceAsInt))
{
return Option<T>.None();
}

return sourceAsInt.ToString().TryParseEnum<T>();
}
}
}
80 changes: 80 additions & 0 deletions tests/SuccincT.Tests/SuccincT/EnumParsers/EnumCharParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using NUnit.Framework;
using System;
using SuccincT.Options;
using SuccincT.Parsers;
using static NUnit.Framework.Assert;

namespace SuccincTTests.SuccincT.EnumParsers
{
[TestFixture]
public class EnumCharParserTests
{
private enum TestType
{
Foo = 'F',
Bar = 'B'
}

private enum NotCharType
{
Foo,
Bar
}

private struct JustAStruct
{

}

[Test]
public void NonEnumShouldThrowException()
{
// GIVEN a char

// WHEN it is parsed into a type that is not an enum
Action action = () =>
{
'F'.TryParsEnum<JustAStruct>();
};

// THEN an argument exception should be thrown
Throws<ArgumentException>(new TestDelegate(action), "an argument exception should have been thrown");
}

[Test]
public void EnumNotKeyedToCharShouldReturnNone()
{
// GIVEN a char
// WHEN it is parsed into an enum type that is not keyed to chars
var result = 'C'.TryParsEnum<NotCharType>();

// THEN it should return option none
AreEqual(Option<NotCharType>.None(), result, "if the enum is not keyed to chars, it should return none");
}

[Test]
public void InvalidValueReturnsNone()
{
// GIVEN a char
// WHEN it is parsed into an enum type that doesn't have a corresponding value
var result = 'C'.TryParsEnum<TestType>();

// THEN it should return option none
AreEqual(Option<TestType>.None(),result, "if the char doesn't correspond to an enum value, it should return none");
}

[Test]
public void ValidCharReturnsProperEnumValue()
{
// GIVEN a char
// WHEN it is parsed into an enum type with the corresponding char value
var result = 'F'.TryParsEnum<TestType>();

// THEN the corresponding enum value option should be returned
AreEqual(Option<TestType>.Some(TestType.Foo), result,
"result should have returned some with the correct enum value");
}


}
}