public static void IsSuccess <TValue>(IEither <TValue, ParseError> result) { if (result.IsError) { Assert.Fail("Expected Success, got: " + result.FromError()); } }
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())); } }
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); } }
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)); }
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())); } }
public void FromError_Error_ReturnsValue() { IEither <bool, int> either = Either.Error <bool, int>(42); Assert.AreEqual(42, either.FromError()); }
public void FromError_Success_ThrowsException() { IEither <bool, int> either = Either.Success <bool, int>(true); either.FromError(); }
public static void ErrorEquals <TValue>(string expected, IEither <TValue, ParseError> result) { IsError(result); Assert.AreEqual(expected, result.FromError().Message, "Error message"); }