/// <summary> /// Tries to parse a start direction from given location description. Some locations, e.g. /// from the DHV Geländedatenbank, have the start directions in the description, after a /// certain text. /// </summary> /// <param name="description">location description</param> /// <param name="takeoffDirections">parsed takeoff directions</param> /// <returns>true when a takeoff direction could be parsed, or false when not</returns> private static bool TryParseStartDirectionFromDescription(string description, out TakeoffDirections takeoffDirections) { takeoffDirections = TakeoffDirections.None; const string StartDirectionText = "Startrichtung "; int posStartDirection = description.IndexOf(StartDirectionText); if (posStartDirection == -1) { return(false); } int posLineBreak = description.IndexOf("<br", posStartDirection); if (posLineBreak == -1) { posLineBreak = description.Length; } posStartDirection += StartDirectionText.Length; string direction = description.Substring(posStartDirection, posLineBreak - posStartDirection); return(TakeoffDirectionsHelper.TryParse(direction, out takeoffDirections)); }
public void TestTryParseCorrectTakeoffTexts() { // set up string[] correctTakeoffTexts = new string[] { "N", "SSW", "N-E", "N-SSE", "N-S", "N-SSW", "E-WSW", "E-W", "E-WNW", "N-NNW", "S-SW,NW-W", "S-SW, NW-W", "O-NO", "West takeoff (W-SW)", "Takeoff (N, O, S)", "Takeoff (NO, NW)", }; foreach (var text in correctTakeoffTexts) { // run bool result = TakeoffDirectionsHelper.TryParse(text, out TakeoffDirections takeoffDirections); Debug.WriteLine($"text={text} directions={takeoffDirections}"); // check Assert.IsTrue(result, "text must have been parsed correctly"); Assert.IsTrue(takeoffDirections != TakeoffDirections.None, "takeoff direction must not be empty"); } }
/// <summary> /// Adds takeoff directions value to every location in the list /// </summary> /// <param name="locationList">location list to modify</param> private static void AddTakeoffDirections(List <Location> locationList) { foreach (var location in locationList) { if (TakeoffDirectionsHelper.TryParse(location.Name, out TakeoffDirections takeoffDirections)) { location.TakeoffDirections = takeoffDirections; } else if (TryParseStartDirectionFromDescription(location.Description, out TakeoffDirections takeoffDirections2)) { location.TakeoffDirections = takeoffDirections2; } } }
public void TestTryParseInvalidTakeoffTexts() { // set up string[] invalidTakeoffTexts = new string[] { "SR", "SSW-", "N-NNW-", "S-SW NW-W", }; foreach (var text in invalidTakeoffTexts) { // run bool result = TakeoffDirectionsHelper.TryParse(text, out TakeoffDirections takeoffDirections); // check Assert.IsFalse(result, "text parsing must have failed"); Assert.IsTrue(takeoffDirections == TakeoffDirections.None, "takeoff direction must be None"); } }