public void RecursivelyParses(string s, bool expectation) { var dict = new Dictionary <int, RuleParser>(); dict[0] = new ChoiceParser(new RuleParser[] { new SequenceParser(new RuleParser[] { new LookupParser(1, dict), new ConstantParser('b') }), new ConstantParser('c') }); dict[1] = new ChoiceParser(new List <RuleParser> { new ConstantParser('a'), new SequenceParser(new List <RuleParser> { new ConstantParser('a'), new LookupParser(1, dict) }) }); Accepts(dict[0], s).Should().Be(expectation); }
public void Parse_Test() { #region Arrange const int ItemsCount = 2; const string ChoiceName = "Choice"; const double Score = 0.0d; const ChoiceInfo.AcceptanceCriterias Criteria = ChoiceInfo.AcceptanceCriterias.Favorable; const bool CommReq = false; const bool NewItemReq = true; const string NewItemProcName = "test_process_"; const int NewItemProcId = 0; var sb = new StringBuilder(); sb.AppendLine(@"<ChoiceInfo IsGlobalChoice=""false"" GlobalChoiceId=""0"">"); for (var i = 0; i < ItemsCount; i++) { sb.AppendLine(); sb.AppendFormat(@" <Choice Choice=""{0}"" Score=""{1}"" AcceptanceCriteria=""{2}"" IsCommentRequired=""{3}"" IsNewItemRequired=""{4}"" NewItemProcessName=""{5}"" NewItemProcessId=""{6}"" />", ChoiceName + i, //0 Score + i, //1 Criteria, //2 CommReq, //3 NewItemReq, //4 NewItemProcName + i, //5 NewItemProcId + i); //6 sb.AppendLine(); } sb.AppendLine(@"</ChoiceInfo>"); var input = sb.ToString(); #endregion Arrange #region Act var result = new ChoiceParser().Parse(input); #endregion Act #region Assert for (var i = 0; i < ItemsCount; i++) { Assert.AreEqual(result[i].Choice, ChoiceName + i); Assert.AreEqual(result[i].Score, Score + i); Assert.AreEqual(result[i].AcceptanceCriteria, Criteria); Assert.AreEqual(result[i].IsCommentRequired, CommReq); Assert.AreEqual(result[i].IsNewItemRequired, NewItemReq); Assert.AreEqual(result[i].NewItemProcessName, NewItemProcName + i); Assert.AreEqual(result[i].NewItemProcessId, NewItemProcId + i); } #endregion Assert }
public void RecursivelyParsesBalanced(string s, bool expectation) { var dict = new Dictionary <int, RuleParser>(); dict[0] = new ChoiceParser(new RuleParser[] { new SequenceParser(new RuleParser[] { new ConstantParser('o'), new LookupParser(0, dict), new ConstantParser('o') }), new ConstantParser('x') }); Accepts(dict[0], s).Should().Be(expectation); }
/// <summary> /// Business or validation rule implementation. /// </summary> /// <param name="context">Rule context object.</param> protected override void Execute(RuleContext context) { if (context == null) return; var choiceXml = (string)context.InputPropertyValues[PrimaryProperty]; if (string.IsNullOrEmpty(choiceXml)) return; var choiceInfoList = new ChoiceParser().Parse(choiceXml).ToList(); var foundScores = new HashSet<double?>(); foreach (var choice in choiceInfoList.Where(choice => !foundScores.Contains(choice.Score) && choiceInfoList.Any(x => x != choice && x.Score == choice.Score))) { foundScores.Add(choice.Score); context.AddErrorResult(PrimaryProperty, "All choices should have unique scores! You have more than one choice with score \"" + choice.Score + "\"."); } }
/// <summary> /// Business or validation rule implementation. /// </summary> /// <param name="context">Rule context object.</param> protected override void Execute(RuleContext context) { if (context == null) { return; } var choiceXml = (string)context.InputPropertyValues[PrimaryProperty]; if (string.IsNullOrEmpty(choiceXml)) { return; } var choiceInfoList = new ChoiceParser().Parse(choiceXml).ToList(); foreach (var choice in choiceInfoList) { if (choice.IsNewItemRequired && string.IsNullOrEmpty(choice.NewItemProcessName)) { context.AddErrorResult(this.PrimaryProperty, string.Format(CultureInfo.InvariantCulture, LanguageService.Translate("Error_NewItemProcessRequired"), choice.Choice)); } } }
private int[] Execute(string txt, int count) { return(ChoiceParser.Parse(txt, count)); }
private static void LoginUserMethod(ElectionDbContext context, WebserviceRawCommunication webservice) { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("----- Login to system -------------------------------------------------------"); LoginCredentials loginCredentials = GetCredentialsFromConsole(); User user = new Domain.Models.User(loginCredentials, context, webservice); LoginValidation loginValidation = user.Login(); if (loginValidation.Error) { Console.ForegroundColor = ConsoleColor.Red; foreach (var error in loginValidation.LoginErrors) { Console.WriteLine($"- Error: {LoginErrorDescription.GetDescription(error)}"); } } if (loginValidation.Warning) { Console.ForegroundColor = ConsoleColor.DarkYellow; foreach (var warning in loginValidation.LoginWarnings) { Console.WriteLine($"- Warning: {LoginWarningDescription.GetDescription(warning)}"); } } if (!loginValidation.Error) { if (user.Logged) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"- User logged : {user.FirstName} / {user.LastName} / {user.Pesel}"); if (!loginValidation.LoginWarnings.Contains(LoginWarning.UserAlreadyVoted) && !loginValidation.LoginWarnings.Contains(LoginWarning.UserIsDisallowedToVote)) { IList <Candidate> candidates = context.Candidates.ToList(); IList <Party> parties = context.Parties.ToList(); Console.ForegroundColor = ConsoleColor.Magenta; for (int i = 0; i < candidates.Count; i++) { Console.WriteLine( $"{i + 1}] {candidates[i].Name} / {parties.Single(x => x.PartyId == candidates[i].PartyId).Name}"); } int[] parsedChoice; do { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Please enter you candidate (or candidates) no [eg. \"1,5,10\"]: "); Console.ForegroundColor = ConsoleColor.White; string candidatesRead = Console.ReadLine(); parsedChoice = ChoiceParser.Parse(candidatesRead, candidates.Count); if (parsedChoice == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"- Error: Illegal characters in your response"); } } while (parsedChoice == null); Console.ForegroundColor = ConsoleColor.Yellow; for (int i = candidates.Count; i > 0; i--) { if (!parsedChoice.Any(x => x == i)) { candidates.RemoveAt(i - 1); } } Console.ForegroundColor = ConsoleColor.DarkGray; if (candidates.Count == 0) { Console.WriteLine($"You voted to nobody"); } foreach (var c in candidates) { Console.WriteLine( $"You voted to {c.Name} / {parties.Single(x => x.PartyId == c.PartyId).Name}"); } user.Vote(candidates); } // Get Vote statistics VoteStatistics(context); user.Logout(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("- User logout"); } } }