/// <summary> /// Attempt to parse an input with a given parser. /// </summary> public static Either <T, ParseError> TryParse <T, S> (this Parser <T, S> parser, IParserInput <S> input) { var res = parser(input); return(res ? Either <T, ParseError> .Create(res.Result) : Either <T, ParseError> .Create(ParseError.FromParseResult(res))); }
public void Sequence_NotOK() { var input = Enumerable.Range(1, 100).Select(Either <int, string> .Create) .Append(Either <int, string> .Create("Failed")); var result = input.Sequence(); Assert.False(result.IsSuccess); }
public void MapIsANoopForError() { var error = "fail"; var value = Either <int, string> .Create(error) .Map(i => i + 1); Assert.True(value.IsError); Assert.Equal(error, value.AsError.Value); }
public void WhereNot_NotOk() { var input = Either <int, string> .Create(100); var expected = "Fail"; var result = input.WhereNot(i => i > 10, () => expected); Assert.False(result.IsSuccess); Assert.Equal(expected, result.AsError.Value); }
public void WhereNot_Ok() { var expected = 10; var input = Either <int, string> .Create(expected); var result = input.WhereNot(i => i > 10, () => "Fail"); Assert.True(result.IsSuccess); Assert.Equal(expected, result.AsSuccess.Value); }
public void MapIsApplied() { var givenValue = 42; var expectedValue = givenValue + 1; var value = Either <int, string> .Create(givenValue) .Map(i => i + 1); Assert.True(value.IsSuccess); Assert.Equal(expectedValue, value.AsSuccess.Value); }
public void CreateErrorCase() { var errorMessage = "fail"; var value = Either <int, string> .Create(errorMessage); Assert.True(value.IsError); Assert.False(value.IsSuccess); Assert.Equal(errorMessage, value.AsError.Value); }
public void CreateSuccessCase() { var expected = 42; var value = Either <int, string> .Create(expected); Assert.True(value.IsSuccess); Assert.False(value.IsError); Assert.Equal(expected, value.AsSuccess.Value); }
public void TestMakeEither(int?left, string right, bool hasLeft, bool hasRight) { var e = left != null?Either.Create <int, string>(() => left.Value) : Either.Create <int, string>(() => right); Assert.AreEqual(hasLeft, e().HasLeft); Assert.AreEqual(hasRight, e().HasRight); if (hasLeft) { Assert.AreEqual(left.Value, e().Left); } if (hasRight) { Assert.AreEqual(right, e().Right); } }
public SimpleUndoPair ClearLocalizationAction(Id <LocalizedText> guid) { Either <LocalizationElement, Null> oldValue = Either.Create(m_data.LocalizationExists(guid), () => m_data.GetLocalized(guid), Null.Func); return(InnerSetLocalizationAction(guid, Null.Func(), oldValue)); }
public SimpleUndoPair SetLocalizationAction(Id <LocalizedText> guid, string p) { Either <LocalizationElement, Null> oldValue = Either.Create(m_data.LocalizationExists(guid), () => m_data.GetLocalized(guid), Null.Func); return(InnerSetLocalizationAction(guid, new LocalizationElement(DateTime.Now, p), oldValue)); }
public static IEither <IEnumerable <ICommandError>, IEnumerable <IEvent> > Failed(IEnumerable <ICommandError> errors) => Either.Create <IEnumerable <ICommandError>, IEnumerable <IEvent> >(errors);
public static IEither <IEnumerable <ICommandError>, IEnumerable <IEvent> > Handled(IEnumerable <IEvent> events) => Either.Create <IEnumerable <ICommandError>, IEnumerable <IEvent> >(events);