Last active
March 20, 2019 21:37
-
-
Save beeleebow/9a1093d625d02723196e02260d6388ac to your computer and use it in GitHub Desktop.
Lifting functions that deal with non-optional values into an Optional context.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Blackbaud.Registration.Tests.Common.Extensions; | |
using LanguageExt.UnitTesting; | |
using Xunit; | |
using static LanguageExt.Prelude; | |
namespace Blackbaud.Registration.MessageHandlers.UnitTests.CommandHandlers | |
{ | |
public class UsingApplyOnOption | |
{ | |
private class NonFictionBook | |
{ | |
public string Title { get; } | |
public string Author { get; } | |
public NonFictionBook(string title, string author) | |
{ | |
Title = title; | |
Author = author; | |
} | |
} | |
private class FictionBook | |
{ | |
public string BookTitle { get; } | |
public string AuthorName { get; } | |
public FictionBook(string bookTitle, string authorName) | |
{ | |
BookTitle = bookTitle; | |
AuthorName = authorName; | |
} | |
} | |
[Fact] | |
public void LiftingNonOptionalFunctionsIntoOptionExample() | |
{ | |
Func<NonFictionBook, FictionBook, bool> writtenBySameAuthor = | |
(nonFictionBook, fictionBook) => nonFictionBook.Author == fictionBook.AuthorName; | |
writtenBySameAuthor.Apply( | |
Some(new NonFictionBook("Cooking for Dummies", "Sarah Smith")), | |
Some(new FictionBook("1984", "George Orwell")) | |
).ShouldBeSome(Assert.False); | |
writtenBySameAuthor.Apply( | |
Some(new NonFictionBook("The Age of Mass Surveillance", "George Orwell")), | |
Some(new FictionBook("1984", "George Orwell")) | |
).ShouldBeSome(Assert.True); | |
writtenBySameAuthor.Apply( | |
Some(new NonFictionBook("The Age of Mass Surveillance", "George Orwell")), | |
None | |
).ShouldBeNone(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment