From 2c08dec917e0518a17d9475137e082478fb5dfcc Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 23 Dec 2016 11:04:57 +0100 Subject: [PATCH] Return string value of Some or blank in Option.ToString() This renders Option.ToString() consistent with Nullable.ToString() and easier to substitute for. Closes #17 --- Optional.Tests/MaybeTests.cs | 18 +++++++++--------- Optional/Option_Maybe.cs | 12 +----------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Optional.Tests/MaybeTests.cs b/Optional.Tests/MaybeTests.cs index 93f30c8..29d12e3 100644 --- a/Optional.Tests/MaybeTests.cs +++ b/Optional.Tests/MaybeTests.cs @@ -150,19 +150,19 @@ public void Maybe_Hashing() [TestMethod] public void Maybe_StringRepresentation() { - Assert.AreEqual(Option.None().ToString(), "None"); - Assert.AreEqual(Option.None().ToString(), "None"); - Assert.AreEqual(Option.None().ToString(), "None"); + Assert.AreEqual(Option.None().ToString(), ""); + Assert.AreEqual(Option.None().ToString(), ""); + Assert.AreEqual(Option.None().ToString(), ""); - Assert.AreEqual(Option.Some(null).ToString(), "Some(null)"); - Assert.AreEqual(Option.Some(null).ToString(), "Some(null)"); + Assert.AreEqual(Option.Some(null).ToString(), ""); + Assert.AreEqual(Option.Some(null).ToString(), ""); - Assert.AreEqual(Option.Some(1).ToString(), "Some(1)"); - Assert.AreEqual(Option.Some(1).ToString(), "Some(1)"); - Assert.AreEqual(Option.Some("1").ToString(), "Some(1)"); + Assert.AreEqual(Option.Some(1).ToString(), "1"); + Assert.AreEqual(Option.Some(1).ToString(), "1"); + Assert.AreEqual(Option.Some("1").ToString(), "1"); var now = DateTime.Now; - Assert.AreEqual(Option.Some(now).ToString(), "Some(" + now.ToString() + ")"); + Assert.AreEqual(Option.Some(now).ToString(), now.ToString()); } [TestMethod] diff --git a/Optional/Option_Maybe.cs b/Optional/Option_Maybe.cs index 8785e1f..71a1581 100644 --- a/Optional/Option_Maybe.cs +++ b/Optional/Option_Maybe.cs @@ -97,17 +97,7 @@ public override int GetHashCode() /// A string that represents the current optional. 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; } ///