Skip to content

Commit

Permalink
Merge pull request #19 from atifaziz/option-maybe-val-tostr
Browse files Browse the repository at this point in the history
Return string value of Some or blank in Option<T>.ToString()
  • Loading branch information
nlkl authored Jan 10, 2017
2 parents 97d1617 + f4668ce commit 8ddb72d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
18 changes: 9 additions & 9 deletions Optional.Tests/MaybeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,19 @@ public void Maybe_Hashing()
[TestMethod]
public void Maybe_StringRepresentation()
{
Assert.AreEqual(Option.None<int>().ToString(), "None");
Assert.AreEqual(Option.None<int?>().ToString(), "None");
Assert.AreEqual(Option.None<string>().ToString(), "None");
Assert.AreEqual(Option.None<int>().ToString(), "");
Assert.AreEqual(Option.None<int?>().ToString(), "");
Assert.AreEqual(Option.None<string>().ToString(), "");

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

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

var now = DateTime.Now;
Assert.AreEqual(Option.Some<DateTime>(now).ToString(), "Some(" + now.ToString() + ")");
Assert.AreEqual(Option.Some<DateTime>(now).ToString(), now.ToString());
}

[TestMethod]
Expand Down
30 changes: 20 additions & 10 deletions Optional/Option_Maybe.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

Expand All @@ -12,6 +13,7 @@ namespace Optional
#if !NETSTANDARD
[Serializable]
#endif
[DebuggerDisplay("{" + nameof(DebugString) + "}")]
public struct Option<T> : IEquatable<Option<T>>
{
private readonly bool hasValue;
Expand Down Expand Up @@ -91,23 +93,31 @@ public override int GetHashCode()
return 0;
}

/// <summary>
/// Returns a string that represents the current optional.
/// </summary>
/// <returns>A string that represents the current optional.</returns>
public override string ToString()
string DebugString
{
if (hasValue)
get
{
if (value == null)
if (hasValue)
{
return "Some(null)";
if (value == null)
{
return "Some(null)";
}

return string.Format("Some({0})", value);
}

return string.Format("Some({0})", value);
return "None";
}
}

return "None";
/// <summary>
/// Returns a string that represents the current optional.
/// </summary>
/// <returns>A string that represents the current optional.</returns>
public override string ToString()
{
return hasValue && value != null ? value.ToString() : string.Empty;
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Portable.Optional/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"Microsoft.NETCore.Platforms": "1.0.1",
"System.Collections": "4.0.11"
"System.Collections": "4.0.11",
"System.Diagnostics.Debug": "4.0.11"
},
"frameworks": {
"netstandard1.0": {}
Expand Down

0 comments on commit 8ddb72d

Please sign in to comment.