Example #1
0
        /// <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)));
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        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);
        }
Example #7
0
        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);
        }
Example #8
0
        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);
        }
Example #9
0
        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);
            }
        }
Example #10
0
        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));
        }
Example #11
0
        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));
        }
Example #12
0
 public static IEither <IEnumerable <ICommandError>, IEnumerable <IEvent> > Failed(IEnumerable <ICommandError> errors)
 => Either.Create <IEnumerable <ICommandError>, IEnumerable <IEvent> >(errors);
Example #13
0
 public static IEither <IEnumerable <ICommandError>, IEnumerable <IEvent> > Handled(IEnumerable <IEvent> events)
 => Either.Create <IEnumerable <ICommandError>, IEnumerable <IEvent> >(events);