Skip to content

Commit

Permalink
Better integration of Option<T> with string formatting
Browse files Browse the repository at this point in the history
Closes nlkl#20
  • Loading branch information
atifaziz committed Dec 23, 2016
1 parent f4668ce commit f753f1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 33 additions & 1 deletion Optional.Tests/MaybeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
Expand Down Expand Up @@ -166,7 +167,38 @@ public void Maybe_StringRepresentation()
}

[TestMethod]
public void Maybe_GetValue()
public void Maybe_Format()
{
Assert.AreEqual("", ((IFormattable) Option.None<int>()).ToString(null, null));
Assert.AreEqual("", ((IFormattable) Option.None<int?>()).ToString(null, null));
Assert.AreEqual("", ((IFormattable) Option.None<string>()).ToString(null, null));

Assert.AreEqual("", ((IFormattable) Option.Some<int?>(null)).ToString(null, null));
Assert.AreEqual("", ((IFormattable) Option.Some<string>(null)).ToString(null, null));

// Test number formatting using a culture that uses a different
// separator for grouping than the current culture.

var numberFormatProvider =
CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.First(ci => ci.NumberFormat.NumberGroupSeparator != NumberFormatInfo.CurrentInfo.NumberGroupSeparator);

Assert.AreEqual(int.MaxValue.ToString("N0", numberFormatProvider), ((IFormattable) Option.Some<int>(int.MaxValue)).ToString("N0", numberFormatProvider));
Assert.AreEqual(int.MaxValue.ToString("N0", numberFormatProvider), ((IFormattable) Option.Some<int?>(int.MaxValue)).ToString("N0", numberFormatProvider));

// Test date formatting using a culture that would yield a
// different result than the current culture.

var now = DateTime.Now;
var dateFormatProvider =
CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.First(ci => now.ToString("D", ci) != now.ToLongDateString());

Assert.AreEqual(now.ToString("D", dateFormatProvider), ((IFormattable) Option.Some<DateTime>(now)).ToString("D", dateFormatProvider));
}

[TestMethod]
public void Maybe_GetValue()
{
var noneStruct = Option.None<int>();
var noneNullable = Option.None<int?>();
Expand Down
11 changes: 9 additions & 2 deletions Optional/Option_Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Optional
[Serializable]
#endif
[DebuggerDisplay("{" + nameof(DebugString) + "}")]
public struct Option<T> : IEquatable<Option<T>>
public struct Option<T> : IEquatable<Option<T>>, IFormattable
{
private readonly bool hasValue;
private readonly T value;
Expand Down Expand Up @@ -391,5 +391,12 @@ public Option<T> Filter(Func<T, bool> predicate)
/// </summary>
/// <returns>The filtered optional.</returns>
public Option<T> NotNull() => hasValue && value == null ? Option.None<T>() : this;
}

string IFormattable.ToString(string format, IFormatProvider formatProvider)
{
return hasValue
? string.Format(formatProvider, "{0:" + format + "}", value)
: string.Empty;
}
}
}

0 comments on commit f753f1a

Please sign in to comment.