public void TestTemplateBuilder_ReturnValidTemplate() { IStringTemplateBuilder builder = new StringTemplateBuilder(); IStringTemplate template = builder .SetFormat(templateData) .AddParameter(templateVarName) .Build(); IStringTemplateProcessor processor = new StringTemplateProcessor(); var a = processor.Process(template, templateVarValue); var argB = template.CreateArgument(0, templateVarValue); var b = processor.Process(template, argB); var argC = template.CreateArgument(templateVarName, templateVarValue); var c = processor.Process(template, argC); var argD = template.CreateArgument(template.Parameters[0], templateVarValue); var d = processor.Process(template, argD); Assert.AreEqual(expected_result, a); Assert.AreEqual(expected_result, b); Assert.AreEqual(expected_result, c); Assert.AreEqual(expected_result, d); }
public string Process(IStringTemplate template, params IStringTemplateArgument[] arguments) { if (template == null) { throw new ArgumentNullException(nameof(template)); } return(ProcessTemplateImpl(template, null, arguments)); }
public void TestTemplateParser_MessyData_ReturnFixedTemplate() { IStringTemplateParser parser = new StringTemplateParser(); IStringTemplate template = parser.Parse(messyTemplateData, StringTemplateParserOption.FixMalformedTemplate); Assert.AreEqual(templateData, template.Template); Assert.AreEqual(1, template.Parameters.Length); Assert.AreEqual(templateVarName, template.Parameters[0].Name); IStringTemplateProcessor processor = new StringTemplateProcessor(); var a = processor.Process(template, templateVarValue); Assert.AreEqual(expected_result, a); }
public void TestTemplateParser_ReturnValidTemplate() { IStringTemplateParser parser = new StringTemplateParser(); IStringTemplate template = parser.Parse(templateData); Assert.AreEqual(templateData, template.Template); Assert.AreEqual(1, template.Parameters.Length); Assert.AreEqual(templateVarName, template.Parameters[0].Name); IStringTemplateProcessor processor = new StringTemplateProcessor(); var a = processor.Process(template, templateVarValue); Assert.AreEqual(expected_result, a); }
public string Process(IStringTemplate template, params object[] arguments) { if (template == null) { throw new ArgumentNullException(nameof(template)); } var args = new IStringTemplateArgument[template.Parameters.Length]; for (var i = 0; i < template.Parameters.Length; ++i) { var argValue = i < arguments.Length ? arguments[i] : null; args[i] = new StringTemplateArgument(template.Parameters[i], argValue); } return(ProcessTemplateImpl(template, null, args)); }
private string ProcessTemplateImpl(IStringTemplate template, IStringTemplate templateOverride, IStringTemplateArgument[] arguments) { var output = FixMalformedTemplate(templateOverride?.Template ?? template.Template); foreach (var arg in arguments) { AssertArgumentType(arg); if (arg.BoxedValue != null) { if (output.IndexOf("{" + arg.Parameter.Name + " ") >= 0) { output = output.Replace("{" + arg.Parameter.Name, arg.BoxedValue.ToString()); } else { output = output.Replace("{" + arg.Parameter.Name + "}", arg.BoxedValue.ToString()); } } } return(output.Replace("{", "").Replace("}", "")); // clear up malformed variables }