/// <summary> /// Returns true if the aircraft passes the airport filter. /// </summary> /// <param name="filterString"></param> /// <param name="aircraft"></param> /// <returns></returns> private bool PassesAirportFilter(FilterString filterString, IAircraft aircraft) { Func <string, string> extractAirportCode = (string airportDescription) => { var code = airportDescription; if (!String.IsNullOrEmpty(code)) { var separatorIndex = code.IndexOf(' '); if (separatorIndex != -1) { code = code.Substring(0, separatorIndex); } } return(code); }; var airportCodes = new List <string>(); airportCodes.Add(extractAirportCode(aircraft.Origin)); airportCodes.Add(extractAirportCode(aircraft.Destination)); foreach (var stopover in aircraft.Stopovers) { airportCodes.Add(extractAirportCode(stopover)); } return(filterString.Passes(airportCodes)); }
public void FilterString_Passes_Single_String_Returns_Correct_Results() { foreach (FilterCondition condition in Enum.GetValues(typeof(FilterCondition))) { foreach (var reverseCondition in new bool[] { false, true }) { foreach (var value in new string[] { null, "", "abc", "ABC", }) { foreach (var testValue in new string[] { null, "", "abc", "ABC", "a", "A", "b", "B", "c", "C", "d", "D" }) { var filter = new FilterString() { Condition = condition, ReverseCondition = reverseCondition, Value = value }; var result = filter.Passes(testValue); var expectedResult = true; if (condition != FilterCondition.Between && condition != FilterCondition.Missing) { if (String.IsNullOrEmpty(value)) { if (condition != FilterCondition.Equals) { expectedResult = true; } else { expectedResult = String.IsNullOrEmpty(testValue); if (reverseCondition) { expectedResult = !expectedResult; } } } else { var ucaseValue = (value ?? "").ToUpper(); var ucaseTestValue = (testValue ?? "").ToUpper(); switch (condition) { case FilterCondition.Contains: expectedResult = ucaseTestValue.Contains(ucaseValue); break; case FilterCondition.EndsWith: expectedResult = ucaseTestValue.EndsWith(ucaseValue); break; case FilterCondition.Equals: expectedResult = ucaseTestValue == ucaseValue; break; case FilterCondition.StartsWith: expectedResult = ucaseTestValue.StartsWith(ucaseValue); break; default: throw new NotImplementedException(); } if (reverseCondition) { expectedResult = !expectedResult; } } } Assert.AreEqual(expectedResult, result, "{0}/{1}/{2}/{3}", condition, reverseCondition, value, testValue); } } } } }
public void FilterString_Passes_String_Collection_Returns_Correct_Results() { foreach (FilterCondition condition in Enum.GetValues(typeof(FilterCondition))) { foreach (var reverseCondition in new bool[] { false, true }) { foreach (var value in new string[] { null, "", "abc", "ABC", }) { foreach (var testValue in new string[][] { null, new string[] { null }, new string[] { "" }, new string[] { "abc" }, new string[] { "a", "c" }, new string[] { "ABC" }, new string[] { "A", "C" }, new string[] { null, "A" }, new string[] { "", "A" }, new string[] { "D" }, }) { var filter = new FilterString() { Condition = condition, ReverseCondition = reverseCondition, Value = value }; var result = filter.Passes(testValue); var expectedResult = true; if (condition != FilterCondition.Between && condition != FilterCondition.Missing) { if (String.IsNullOrEmpty(value)) { if (condition != FilterCondition.Equals) { expectedResult = true; } else { expectedResult = testValue == null || testValue.Contains(null) || testValue.Contains(""); if (reverseCondition) { expectedResult = !expectedResult; } } } else { var ucaseValue = (value ?? "").ToUpper(); var ucaseTestValue = (testValue ?? new string[0]).Select(r => (r ?? "").ToUpper()).ToArray(); switch (condition) { case FilterCondition.Contains: expectedResult = ucaseTestValue.Any(r => r.Contains(ucaseValue)); break; case FilterCondition.EndsWith: expectedResult = ucaseTestValue.Any(r => r.EndsWith(ucaseValue)); break; case FilterCondition.Equals: expectedResult = ucaseTestValue.Any(r => r == ucaseValue); break; case FilterCondition.StartsWith: expectedResult = ucaseTestValue.Any(r => r.StartsWith(ucaseValue)); break; default: throw new NotImplementedException(); } if (reverseCondition) { expectedResult = !expectedResult; } } } Assert.AreEqual(expectedResult, result, "{0}/{1}/{2}/{3}", condition, reverseCondition, value == "" ? "empty string" : value == null ? "null string" : value, testValue == null ? "null array" : String.Join(",", testValue.Select(r => r ?? "null").ToArray())); } } } } }