Exemplo n.º 1
0
 public static void IsError <TValue>(IEither <TValue, ParseError> result)
 {
     if (result.IsSuccess)
     {
         Assert.Fail("Expected Error, got: " + result.FromSuccess());
     }
 }
Exemplo 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()));
     }
 }
Exemplo 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);
            }
        }
Exemplo 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));
        }
Exemplo 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()));
            }
        }
Exemplo n.º 6
0
        public void FromSuccess_Error_ThrowsException()
        {
            IEither <bool, int> either = Either.Error <bool, int>(42);

            either.FromSuccess();
        }
Exemplo n.º 7
0
        public void FromSuccess_Success_ReturnsValue()
        {
            IEither <bool, int> either = Either.Success <bool, int>(true);

            Assert.AreEqual(true, either.FromSuccess());
        }
Exemplo n.º 8
0
 public static void ValueEquals <TValue>(IEnumerable <TValue> expected, IEither <IEnumerable <TValue>, ParseError> result)
 {
     IsSuccess(result);
     CollectionAssert.AreEqual(expected.ToArray(), result.FromSuccess().ToArray());
 }
Exemplo n.º 9
0
 public static void ValueEquals(double expected, IEither <double, ParseError> result)
 {
     IsSuccess(result);
     Assert.AreEqual(expected, result.FromSuccess(), 0.0001);
 }
Exemplo n.º 10
0
 public static void ValueEquals <T, TValue>(TValue expected, IEither <T, ParseError> result, Func <T, TValue> valueSelector)
 {
     IsSuccess(result);
     Assert.AreEqual(expected, valueSelector(result.FromSuccess()), "Parsed value");
 }