public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { VariableString varStr = value as VariableString; string convertedValue = null; if (varStr != null) { convertedValue = varStr?.GetFormattedString(); } return(convertedValue); }
public void VariableString_GetFormattedStringTest() { var testCases = new[] { new { FormattedString = "{global:var1}{global:var2}{channel:var1}{channel:var2}{global:var3}{channel:var3}?", ExpectedString = "{global:var1}{global:var2}{channel:var1}{channel:var2}{global:var3}{channel:var3}?", ExpectException = false }, new { FormattedString = "{var1}{var2}{channel:var1}{channel:var2}{var3}{channel:var3}?", ExpectedString = "{global:var1}{global:var2}{channel:var1}{channel:var2}{global:var3}{channel:var3}?", ExpectException = false }, new { FormattedString = "{{var1}}{{{{var1}}}}{{channel:var1}}{{channel:var1}}{{var1}}{{channel:var1}}?", ExpectedString = "{var1}{{var1}}{channel:var1}{channel:var1}{var1}{channel:var1}?", ExpectException = false }, new { FormattedString = "{global:var1}{global:var1}{channel:var1}{local:var1}{global:var1}{channel:var1}?", ExpectedString = (string)null, ExpectException = true }, new { FormattedString = "{global:var1?", ExpectedString = (string)null, ExpectException = true }, new { FormattedString = "global}:var1?", ExpectedString = (string)null, ExpectException = true }, new { FormattedString = "glo{bal{:var1?", ExpectedString = (string)null, ExpectException = true } }; foreach (var testCase in testCases) { if (testCase.ExpectException) { try { VariableString varString = VariableString.GetAsVariableString(testCase.FormattedString); Assert.Fail("Expected an exception. No exception thrown"); } catch (FormatException) { } } else { VariableString varString = VariableString.GetAsVariableString(testCase.FormattedString); Assert.AreEqual(testCase.ExpectedString, varString.GetFormattedString()); } } }