Skip to content

Commit

Permalink
Return string value of Some or blank in Option<T>.ToString()
Browse files Browse the repository at this point in the history
This renders Option<T>.ToString() consistent with Nullable<T>.ToString() and easier to substitute for.

Closes nlkl#17
  • Loading branch information
atifaziz committed Dec 23, 2016
1 parent 376982b commit 2c08dec
Show file tree
Hide file tree
Showing 2 changed files with 10 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
12 changes: 1 addition & 11 deletions Optional/Option_Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,7 @@ public override int GetHashCode()
/// <returns>A string that represents the current optional.</returns>
public override string ToString()
{
if (hasValue)
{
if (value == null)
{
return "Some(null)";
}

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

return "None";
return hasValue && value != null ? value.ToString() : string.Empty;
}

/// <summary>
Expand Down

0 comments on commit 2c08dec

Please sign in to comment.