Esempio n. 1
0
 public static void IsSuccess <TValue>(IEither <TValue, ParseError> result)
 {
     if (result.IsError)
     {
         Assert.Fail("Expected Success, got: " + result.FromError());
     }
 }
Esempio n. 2
0
 public static IEither <TResult, E> Select <S, E, TResult>(this IEither <S, E> either, Func <S, IEither <TResult, E> > func)
 {
     if (either.IsSuccess)
     {
         return(func(either.FromSuccess()));
     }
     else
     {
         return(Either.Error <TResult, E>(either.FromError()));
     }
 }
Esempio n. 3
0
        private static void GeneratePassword(string pwdRegex)
        {
            IEither <IGenerator, ParseError> generator = Parser.GeneratorParser.Parse(pwdRegex);

            if (generator.IsError)
            {
                Console.WriteLine(generator.FromError());
            }
            else
            {
                string password = generator.FromSuccess().Generate(new Random());
                Console.WriteLine(password);
                ValidatePassword(pwdRegex, password);
            }
        }
Esempio n. 4
0
        public IEither <IEnumerable <T>, ParseError> Parse(IInputReader input)
        {
            List <T> acc = new List <T>(parsers.Count());

            foreach (IParser <T> parser in parsers)
            {
                IEither <T, ParseError> result = parser.Parse(input);
                if (result.IsSuccess)
                {
                    acc.Add(result.FromSuccess());
                }
                else
                {
                    return(ParseResult.Error <IEnumerable <T> >(result.FromError()));
                }
            }

            return(ParseResult.Success(acc));
        }
Esempio n. 5
0
        public IEither <TResult, ParseError> Parse(IInputReader input)
        {
            TAccum acc = this.seed();

            IEither <T, ParseError> result = null;
            Position position = input.GetPosition();

            while ((result = this.parser.Parse(input)).IsSuccess)
            {
                acc      = this.func(acc, result.FromSuccess());
                position = input.GetPosition();
            }

            if (input.GetPosition() == position)
            {
                return(ParseResult.Success(this.resultSelector(acc)));
            }
            else
            {
                return(ParseResult.Error <TResult>(result.FromError()));
            }
        }
Esempio n. 6
0
        public void FromError_Error_ReturnsValue()
        {
            IEither <bool, int> either = Either.Error <bool, int>(42);

            Assert.AreEqual(42, either.FromError());
        }
Esempio n. 7
0
        public void FromError_Success_ThrowsException()
        {
            IEither <bool, int> either = Either.Success <bool, int>(true);

            either.FromError();
        }
Esempio n. 8
0
 public static void ErrorEquals <TValue>(string expected, IEither <TValue, ParseError> result)
 {
     IsError(result);
     Assert.AreEqual(expected, result.FromError().Message, "Error message");
 }