Skip to content

Commit

Permalink
Fixed bug with implicit conversion of Outcome<TObservation> and Resul…
Browse files Browse the repository at this point in the history
…t<T, TObservation> when source TObservation type is Unit...
  • Loading branch information
iSynaptic committed Feb 6, 2013
1 parent 76a64bf commit 5a08eb1
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Application/iSynaptic.Commons/Outcome.cs
Expand Up @@ -310,6 +310,11 @@ public static Outcome<TObservation> ObserveMany<TObservation>(this Outcome<TObse
return new Outcome<TObservation>(() => new Outcome<TObservation>(self.WasSuccessful, self.Observations.Concat(selector(self.WasSuccessful))));
}

public static Outcome<T> Fail<T>(this Outcome<T> @this)
{
return new Outcome<T>(false, @this.Observations);
}

public static Outcome<Unit> FailIf(bool predicate)
{
return new Outcome<Unit>(!predicate);
Expand Down
9 changes: 7 additions & 2 deletions Application/iSynaptic.Commons/Result.cs
Expand Up @@ -254,7 +254,7 @@ public Outcome<TObservation> ToOutcome()

public static implicit operator Result<T, TObservation>(Result<T, Unit> result)
{
return new Result<T, TObservation>(result.ToMaybe(), new Outcome<TObservation>());
return new Result<T, TObservation>(result.ToMaybe(), new Outcome<TObservation>(result.WasSuccessful, null));
}

public static implicit operator Result<T, TObservation>(Result<Unit, TObservation> result)
Expand All @@ -264,7 +264,7 @@ public Outcome<TObservation> ToOutcome()

public static implicit operator Result<T, TObservation>(Result<Unit, Unit> result)
{
return new Result<T, TObservation>();
return new Result<T, TObservation>(result.WasSuccessful, null);
}
}

Expand Down Expand Up @@ -528,6 +528,11 @@ public static class Result
return new Result<T, TObservation>(() => new Result<T, TObservation>(self.ToMaybe(), self.ToOutcome().Combine(outcomes)));
}

public static Result<T, TObservation> Fail<T, TObservation>(this Result<T, TObservation> @this)
{
return new Result<T, TObservation>(@this.ToMaybe(), new Outcome<TObservation>(false, @this.Observations));
}

public static Result<Unit, Unit> FailIf(bool predicate)
{
return new Result<Unit, Unit>(Maybe.NoValue, Outcome.FailIf(predicate));
Expand Down
4 changes: 2 additions & 2 deletions CommonAssemblyInfo.cs
Expand Up @@ -36,5 +36,5 @@
[assembly: ComVisible(false)]
[assembly: CLSCompliant(true)]

[assembly: AssemblyVersion("0.4.15.0")]
[assembly: AssemblyFileVersion("0.4.15.0")]
[assembly: AssemblyVersion("0.4.16.0")]
[assembly: AssemblyFileVersion("0.4.16.0")]
9 changes: 7 additions & 2 deletions Testing/iSynaptic.Commons.UnitTests/OutcomeTests.cs
Expand Up @@ -21,9 +21,7 @@
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace iSynaptic.Commons
Expand Down Expand Up @@ -255,6 +253,13 @@ public void Run_ForcesEvaluation()
outcome.Run();
Assert.IsTrue(executed);
}

[Test]
public void ImplicitConversion_WhenObservationTypeIsUnit_DoesNotLooseFailures()
{
Outcome<String> outcome = Outcome.Failure();
Assert.IsFalse(outcome.WasSuccessful);
}
}

public class Observation
Expand Down
10 changes: 10 additions & 0 deletions Testing/iSynaptic.Commons.UnitTests/ResultTests.cs
Expand Up @@ -242,6 +242,16 @@ public void ImplicitConversionOf_ResultUnitAndT_ToAnyOtherResultType()
Assert.IsTrue(intStringResult.Observations.SequenceEqual(new[]{"Hello, World!"}));
}

[Test]
public void ImplicitConversion_WhenObservationTypeIsUnit_DoesNotLooseFailures()
{
Result<int, string> intStringResult = Result.Failure();
Assert.IsFalse(intStringResult.WasSuccessful);

intStringResult = 42.ToResult().Fail();
Assert.IsFalse(intStringResult.WasSuccessful);
}

[Test]
public void Success_Default()
{
Expand Down
3 changes: 2 additions & 1 deletion Work Items.txt
@@ -1,14 +1,15 @@
== v0.5 Changes ==
- Remove ".Commons" from all namespaces
- Rename Maybe<T> to Optional<T>...
- Make Outcome eagerly evaluated...
- Consider making Outcome eagerly evaluated...
- Consider tying .WasSuccessful to each observation in Outcome<TObservation> and Result<T, TObservation>...
- Eliminate all optional arguments from methods
- Change all Guard.NotNull(@this, "@this") calls to Guard.NotNull(@this, "this") - argument name shouldn't have '@' sign
- Rename Guard to Argument
- Refactor CompositeDisposable to dispose enlisted items in reverse order...
- Fix SystemClock to not allow changes to default strategy after prevent is called; disallow returning times that are not in UTC.
- Evaluate targeting Client profile...
- Consider better how Unit interacts with Maybe<T>, Outcome<TObservation>, and Result<T, TObservation>

== Potential Future Changes ==
- Create experimental fork
Expand Down
8 changes: 8 additions & 0 deletions iSynaptic.Commons.ReleaseNotes.txt
Expand Up @@ -2,6 +2,14 @@
RELEASE NOTES: iSynaptic.Commons
======================================

==== v0.4.16 (February 6, 2012) ====

---- Features ----

* Fixed bug where failures are lost in implicit conversion to Outcome<TObservation>
and Result<T, TObservation> when the source TObservation type is Unit.


==== v0.4.15 (February 4, 2012) ====

---- Features ----
Expand Down
2 changes: 1 addition & 1 deletion iSynaptic.Commons.nuspec
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>iSynaptic.Commons</id>
<version>0.4.15</version>
<version>0.4.16</version>
<authors>Jordan Terrell</authors>
<description>Library of common functionality in functional programming style.</description>
<language>en-US</language>
Expand Down

0 comments on commit 5a08eb1

Please sign in to comment.