public void TestValidatePassportStrict(int iTestDataPassport, bool expectedValid) { string[] rawPassports = PassportValidator.GetInputData(FilePathTestDataStrict); Dictionary <string, string> parsedPassport = PassportValidator.ParseRawPassport(rawPassports[iTestDataPassport]); bool passportValid = PassportValidator.ValidatePassportStrict(parsedPassport); Assert.AreEqual(expectedValid, passportValid); }
public void TestParsePassport() { string[] rawPassports = PassportValidator.GetInputData(FilePathTestData); Dictionary <string, string> parsedPassport = PassportValidator.ParseRawPassport(rawPassports[0]); Assert.AreEqual(8, parsedPassport.Count); Assert.IsTrue(parsedPassport.ContainsKey("ecl")); Assert.IsTrue(parsedPassport.ContainsValue("gry")); }
public static int CountValidPassportsStrict(string[] rawPassports) { int noOfValidPasswords = 0; foreach (string rawPassport in rawPassports) { Dictionary <string, string> parsedPassport = PassportValidator.ParseRawPassport(rawPassport); if (PassportValidator.ValidatePassportStrict(parsedPassport)) { noOfValidPasswords++; } } return(noOfValidPasswords); }
public static bool ValidatePassportRegex(Dictionary <string, string> parsedPassport) { bool passportIsValid = true; // Obtain the passport rules // TODO: This sghould only be new once. Maybe make into an instance variable? string[] rawPassportTemplateRegex = InputDataHandler.ReadFileAsArray(FilePathPassportRulesRegex, string.Empty); Dictionary <string, string> passportTemplateRegex = PassportValidator.ParseRawPassport(rawPassportTemplateRegex[0]); // Compare value for a kay with the allowed Regex expression // Loop over all template keys in case of an incomplete passport being supplied as argument foreach (KeyValuePair <string, string> currentFieldTemplate in passportTemplateRegex) { // Don't care about cid, skip to checking next field // TODO: Refactor to create all passport keys when reading the passport if (currentFieldTemplate.Key == "cid") { continue; } string value = string.Empty; if (parsedPassport.TryGetValue(currentFieldTemplate.Key, out value)) { if (!Regex.IsMatch(value, currentFieldTemplate.Value)) { passportIsValid = false; // Console.WriteLine("Passport invalidated by: " + currentFieldTemplate.Key + ":" + value); } } else { // If a passport doesn't include the template key it is atomatically invalid // TODO: Handle cid // TODO: Refactor the simple invalidator to use this method? passportIsValid = false; } } return(passportIsValid); }