public string Parse(string input) { var finder = new ControlStringFinder('{', ':', '}', '[', ']'); var controlStrings = finder.FindAllControlStrings(input); string result = input; foreach (var controlString in controlStrings) { if (matcher.Matches(controlString)) { var originalString = input.Substring(controlString.Index, controlString.Length); var specialPrepending = finder.FindPrependingSpecial(originalString.Substring(1, originalString.Length - 2)); var specialPostpending = finder.FindPostpendingSpecial(originalString.Substring(1, originalString.Length - 2)); var newString = matcher.Match(controlString); newString = string.IsNullOrEmpty(newString) ? newString : specialPrepending + newString + specialPostpending; result = result.Replace(originalString, newString); } } return result; }
public void FindAllControlStrings_SplitsFoundControlStringByControlStringSeparator() { const char ControlStringStarter = '{'; const char ControlStringTerminator = '}'; const char ControlStringSeparator = ':'; var input = $"{ControlStringStarter}foo{ControlStringSeparator}bar{ControlStringTerminator}"; var unit = new ControlStringFinder(ControlStringStarter, ControlStringSeparator, ControlStringTerminator, 'b', 'c', 'd'); var valueQueue = new Queue <string>(); valueQueue.Enqueue("foo"); valueQueue.Enqueue("bar"); var expected = new ControlString(0, 9, valueQueue, new Queue <string>()); // Act var result = unit.FindAllControlStrings(input).Single(); result.Length.ShouldBe(expected.Length); result.Values.Dequeue(); expected.Values.Dequeue(); result.Values.Peek().ShouldBe(expected.Values.Peek()); }
public void FindPrependingSpecial_WhenInputDoesNotStartWithSpecialStringStarter_ReturnsEmptyString() { const char SpecialStringStarter = '['; const char SpecialStringTerminator = ']'; var input = $"foo{SpecialStringStarter}bar{SpecialStringTerminator}"; var unit = new ControlStringFinder('{', ':', '}', SpecialStringStarter, SpecialStringTerminator, 'd'); var expected = string.Empty; // Act var result = unit.FindPrependingSpecial(input); result.ShouldBe(expected); }
public void FindPostpendingSpecial_IgnoresLeadingCharactersOutsideSpecialStringStarter() { const char SpecialStringStarter = '['; const char SpecialStringTerminator = ']'; var input = $"foo{SpecialStringStarter}bar{SpecialStringTerminator}"; var unit = new ControlStringFinder('{', ':', '}', SpecialStringStarter, SpecialStringTerminator, 'd'); var expected = "bar"; // Act var result = unit.FindPostpendingSpecial(input); result.ShouldBe(expected); }
public void FindPrependingSpecial_FindsInternalString() { const char SpecialStringStarter = '['; const char SpecialStringTerminator = ']'; var input = $"{SpecialStringStarter}bar{SpecialStringTerminator}"; var unit = new ControlStringFinder('{', ':', '}', SpecialStringStarter, SpecialStringTerminator, 'd'); var expected = "bar"; // Act var result = unit.FindPrependingSpecial(input); result.ShouldBe(expected); }
public void FindPostpendingSpecial_WhenInputIsNull_ThrowsArgumentNullException(ControlStringFinder unit) { Should.Throw <ArgumentNullException>(() => unit.FindPostpendingSpecial(null)); }
public void FindAllControlStrings_WhenMessageIsNull_ThrowsArgumentNullException(ControlStringFinder unit) { Should.Throw <ArgumentNullException>(() => unit.FindAllControlStrings(null)); }
public void FindPostpendingSpecial_WhenInputIsEmptyString_ReturnsEmptyString(ControlStringFinder unit) { var actual = unit.FindPostpendingSpecial(string.Empty); actual.ShouldBe(string.Empty); }