/// <summary> /// Splits Coordinate test string to get value to parse and expected output. /// Checks proper parsing and value /// </summary> /// <param name="val">Test string value</param> private void TryParse_Check(string val) { var vals = val.Split('#'); //First part of string is value to parse, seconds is expected string value after parse string coordString = vals[0]; string expected = vals[1]; CoordinatePart c; Assert.IsTrue(CoordinatePart.TryParse(coordString, out c), $"{coordString} cannot be parsed."); //CHECK STRING COMPARISON, BUT REPLACE , and . with * to avoid cultural mismatch Assert.AreEqual(expected.Replace(",", "*").Replace(".", "*"), c.ToString().Replace(",", "*").Replace(".", "*"), $"{vals} parsed as {c.ToString().Replace(",", "*").Replace(".", "*")} but {expected.Replace(",", "*").Replace(".", "*")} was expected."); }
private static void CoordinatePart_Parses_Test() { //Parse CoordinatePart Formats Console.WriteLine("CoordinatePart Parses..."); string[] coordStrings = File.ReadAllLines("CoordinateData\\CoordinateParts.txt"); CoordinatePart cp; bool pass = true; string lastType = ""; foreach (string c in coordStrings) { c.Trim(); if (c.Contains("\\")) { if (lastType != "") { Pass.Write(lastType.Split('#')[0], pass); } lastType = ""; pass = true; lastType = c; } else { string[] cc = c.Split('#'); if (!CoordinatePart.TryParse(cc[0], out cp)) { pass = false; } else { if (cp.ToString().Replace(",", "*").Replace(".", "*") != cc[1].Replace(",", "*").Replace(".", "*")) { Debug.WriteLine("...MISMATCH: " + cp.ToString() + " - " + cc[1]); pass = false; } } } } if (lastType != "") { Pass.Write(lastType.Split(',')[0], pass); } //Attempt Forces Param pass = true; try { if (CoordinatePart.TryParse("95", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("E181", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("N 95 45", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("95", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("WD24 45", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail } catch { pass = false; } Console.WriteLine(); Pass.Write("\\\\Intentional Fails", pass); }
static void Coordinate_Parsers_Tests() { CoordinatePart cp; Coordinate coordinate; //Parse Coordinate Formats string[] coordStrings = File.ReadAllLines("Coordinates.txt"); bool pass = true; string lastType = ""; Console.WriteLine("Coordinate Parses..."); foreach (string c in coordStrings) { if (c.Contains("\\")) { if (lastType != "") { Write_Pass(lastType.Split('#')[0], pass); } lastType = ""; pass = true; lastType = c; } else { string[] cc = c.Split('#'); if (!Coordinate.TryParse(cc[0], out coordinate)) { pass = false; } else { if (coordinate.ToString() != cc[1]) { Debug.WriteLine("...MISMATCH: " + coordinate.ToString() + " - " + cc[1]); pass = false; } } } } if (lastType != "") { Write_Pass(lastType.Split(',')[0], pass); } //Attempt Forces Param pass = true; try { if (Coordinate.TryParse("95F, 54", out coordinate)) { pass = false; } //Intentional Fail if (Coordinate.TryParse("E 181 30, 56 76", out coordinate)) { pass = false; } //Intentional Fail if (Coordinate.TryParse("N 95 45, E 45", out coordinate)) { pass = false; } //Intentional Fail if (Coordinate.TryParse("95 87 46 78 D", out coordinate)) { pass = false; } //Intentional Fail if (Coordinate.TryParse("W24 45, W45", out coordinate)) { pass = false; } //Intentional Fail } catch { pass = false; } Console.WriteLine(); Write_Pass("\\\\Intentional Fails", pass); Console.WriteLine(); //Parse CoordinatePart Formats Console.WriteLine("CoordinatePart Parses..."); coordStrings = File.ReadAllLines("CoordinateParts.txt"); pass = true; lastType = ""; foreach (string c in coordStrings) { c.Trim(); if (c.Contains("\\")) { if (lastType != "") { Write_Pass(lastType.Split('#')[0], pass); } lastType = ""; pass = true; lastType = c; } else { string[] cc = c.Split('#'); if (!CoordinatePart.TryParse(cc[0], out cp)) { pass = false; } else { if (cp.ToString() != cc[1]) { Debug.WriteLine("...MISMATCH: " + cp.ToString() + " - " + cc[1]); pass = false; } } } } if (lastType != "") { Write_Pass(lastType.Split(',')[0], pass); } //Attempt Forces Param pass = true; try { if (CoordinatePart.TryParse("95", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("E181", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("N 95 45", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("95", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail if (CoordinatePart.TryParse("WD24 45", CoordinateType.Lat, out cp)) { pass = false; } //Intentional Fail } catch { pass = false; } Console.WriteLine(); Write_Pass("\\\\Intentional Fails", pass); }