Best NBi code snippet using NBi.GenbiL.Templating.StringTemplateEngine.StringTemplateEngine
StringTemplateEngineTest.cs
Source:StringTemplateEngineTest.cs
...13using NBi.Xml;14namespace NBi.Testing.GenbiL.Templating15{16 [TestFixture]17 public class StringTemplateEngineTest18 {19 private string ReadTemplateFile(string filename)20 {21 var template = string.Empty;22 // A Stream is needed to read the nbitt document.23 using (Stream stream = Assembly.GetExecutingAssembly()24 .GetManifestResourceStream(25 $"{GetType().Namespace}.Resources.{filename}.nbitt"26 ))27 using (StreamReader reader = new StreamReader(stream))28 {29 template = reader.ReadToEnd();30 }31 return template;32 }33 private List<List<object>> BuildCase(IEnumerable<string> inlineCase)34 {35 var caseBuilt = new List<List<object>>();36 foreach (var item in inlineCase)37 caseBuilt.Add(new List<object>() { item });38 return caseBuilt;39 }40 [Test]41 public void Build_OrderedLightTemplate_CorrectTest()42 {43 var template = ReadTemplateFile("OrderedLight");44 var variables = new string[] { "perspective", "dimension", "hierarchy", "order" };45 var data = new List<List<List<object>>>()46 {47 BuildCase(new string[] { "myPerspective", "myDimension", "myHierarchy", "numerical" })48 };49 var engine = new StringTemplateEngine(template, variables);50 var testSuite = engine.Build<TestStandaloneXml>(data, null);51 var test = testSuite.ElementAt(0);52 //Test the object53 var members = test.Systems[0] as MembersXml;54 Assert.That(members.Exclude.Items, Is.Null.Or.Empty);55 Assert.That(members.Exclude.Patterns, Is.Null.Or.Empty);56 var ordered = test.Constraints[0] as OrderedXml;57 Assert.That(ordered.Rule, Is.EqualTo(OrderedXml.Order.Numerical));58 Assert.That(ordered.Descending, Is.EqualTo(false));59 Assert.That(ordered.Definition, Is.Null.Or.Empty);60 //Test the content serialized61 var content = test.Content;62 Assert.That(content, Does.Contain("rule=\"numerical\""));63 Assert.That(content, Does.Not.Contain("descending=\"false\""));64 Assert.That(content, Does.Not.Contain("<rule-definition"));65 Assert.That(content, Does.Not.Contain("<exclude"));66 67 }68 [Test]69 public void Build_OrderedLightTemplate_ConditionSetupCleanupAreNotAvailable()70 {71 var template = ReadTemplateFile("OrderedLight");72 var variables = new string[] { "perspective", "dimension", "hierarchy", "order" };73 var data = new List<List<List<object>>>()74 {75 BuildCase(new string[] { "myPerspective", "myDimension", "myHierarchy", "numerical" })76 };77 var engine = new StringTemplateEngine(template, variables);78 var testSuite = engine.Build<TestStandaloneXml>(data, null);79 var test = testSuite.ElementAt(0);80 //Test the content serialized81 var content = test.Content;82 Assert.That(content, Does.Not.Contain("<setup"));83 Assert.That(content, Does.Not.Contain("<condition"));84 Assert.That(content, Does.Not.Contain("<cleanup"));85 }86 [Test]87 public void Build_OrderedFullTemplate_CorrectTest()88 {89 var template = ReadTemplateFile("OrderedFull");90 var variables = new string[] { "perspective", "dimension", "hierarchy", "order", "descending", "exclude" };91 var data = new List<List<List<object>>>()92 {93 BuildCase(new string[] { "myPerspective", "myDimension", "myHierarchy", "specific", "true", "Unknown" })94 };95 var engine = new StringTemplateEngine(template, variables);96 var testSuite = engine.Build<TestStandaloneXml>(data, null);97 var test = testSuite.ElementAt(0);98 //Test the object99 var members = test.Systems[0] as MembersXml;100 Assert.That(members.Exclude.Items, Is.Not.Null.And.Not.Empty);101 var ordered = test.Constraints[0] as OrderedXml;102 Assert.That(ordered.Rule, Is.EqualTo(OrderedXml.Order.Specific));103 Assert.That(ordered.Descending, Is.EqualTo(true));104 Assert.That(ordered.Definition, Is.Not.Null.And.Not.Empty);105 //Test the content serialized106 var content = test.Content;107 Assert.That(content, Does.Contain("rule=\"specific\""));108 Assert.That(content, Does.Contain("descending=\"true\""));109 Assert.That(content, Does.Contain("<rule-definition"));110 Assert.That(content, Does.Contain("<exclude"));111 }112 [Test]113 public void BuildTestString_OneSimpleRow_CorrectRendering()114 {115 var template = "<dimension caption='$dimension$' perspective='$perspective$'/>";116 var engine = new StringTemplateEngine(template, new string[] {"dimension", "perspective"});117 var values = new List<List<object>>();118 var firstCell = new List<object>() {"myDim"};119 var secondCell = new List<object>() {"myPersp"};120 values.Add(firstCell);121 values.Add(secondCell);122 engine.InitializeTemplate(null);123 var result = engine.RenderTemplate(values);124 Assert.That(result, Is.EqualTo("<dimension caption='myDim' perspective='myPersp'/>"));125 }126 [Test]127 public void BuildTestString_OneRowWithMultipleItems_CorrectRendering()128 {129 var template = "$dimension$ ... <subsetOf>\r\n\t<item>$items; separator=\"</item>\r\n\t<item>\"$</item>\r\n</subsetOf>";130 var engine = new StringTemplateEngine(template, new string[] { "dimension", "items" });131 var values = new List<List<object>>();132 var firstCell = new List<object>() { "myDim" };133 var secondCell = new List<object>() { "item A", "item B" };134 values.Add(firstCell);135 values.Add(secondCell);136 engine.InitializeTemplate(null);137 var result = engine.RenderTemplate(values);138 Assert.That(result, Is.EqualTo("myDim ... <subsetOf>\r\n\t<item>item A</item>\r\n\t<item>item B</item>\r\n</subsetOf>"));139 }140 [Test]141 public void BuildTestString_OneRowWithNoneVariable_CorrectRenderingTextIsIgnored()142 {143 var template = "$dimension$ ... $if(ignore)$<ignore>$ignore$</ignore>$endif$";144 var engine = new StringTemplateEngine(template, new string[] { "dimension", "ignore" });145 var values = new List<List<object>>();146 var firstCell = new List<object>() { "myDim" };147 var secondCell = new List<object>() { "(none)" };148 values.Add(firstCell);149 values.Add(secondCell);150 engine.InitializeTemplate(null);151 var result = engine.RenderTemplate(values);152 Assert.That(result, Is.EqualTo("myDim ... "));153 }154 [Test]155 public void BuildTestString_OneRowWithEmptyVariable_CorrectRenderingTextIsIgnoredAndVariablePlaceHolderIsEmpty()156 {157 var template = "$dimension$ ->$empty$<- ... $if(empty)$<ignore>$empty$</ignore>$endif$";158 var engine = new StringTemplateEngine(template, new string[] { "dimension", "empty" });159 var values = new List<List<object>>();160 var firstCell = new List<object>() { "myDim" };161 var secondCell = new List<object>() { string.Empty };162 values.Add(firstCell);163 values.Add(secondCell);164 engine.InitializeTemplate(null);165 var result = engine.RenderTemplate(values);166 Assert.That(result, Is.EqualTo("myDim -><- ... "));167 }168 [Test]169 public void BuildTestString_OneRowWithNotIgnoredVariable_CorrectRenderingTextIsDisplayed()170 {171 var template = "$dimension$ ... $if(ignore)$<ignore>$ignore$</ignore>$endif$";172 var engine = new StringTemplateEngine(template, new string[] { "dimension", "ignore" });173 var values = new List<List<object>>();174 var firstCell = new List<object>() { "myDim" };175 var secondCell = new List<object>() { "reason to ignore" };176 values.Add(firstCell);177 values.Add(secondCell);178 engine.InitializeTemplate(null);179 var result = engine.RenderTemplate(values);180 Assert.That(result, Is.EqualTo("myDim ... <ignore>reason to ignore</ignore>"));181 }182 183 184 [Test]185 public void BuildTestString_EncodeXml_CorrectEncoding()186 {187 var template = "<element attribute=\"$value; format=\"xml-encode\"$\" other-attribute=\"$other$\">";188 var engine = new StringTemplateEngine(template, new string[] { "value", "other" });189 var values = new List<List<object>>();190 var firstCell = new List<object>() { "<value&"};191 var secondCell = new List<object>() { "<value&" };192 values.Add(firstCell);193 values.Add(secondCell);194 engine.InitializeTemplate(null);195 var result = engine.RenderTemplate(values);196 Assert.That(result, Is.EqualTo("<element attribute=\"<value&\" other-attribute=\"<value&\">"));197 }198 }199}...
TestListManager.cs
Source:TestListManager.cs
...21 lastGeneration = new List<TestStandaloneXml>();22 }23 public void Build(string template, string[] variables, DataTable dataTable, bool useGrouping, IDictionary<string, object> globalVariables)24 {25 var generator = new StringTemplateEngine(template, variables);26 var cases = GetCases(dataTable, useGrouping);27 generator.Progressed += new EventHandler<ProgressEventArgs>(this.OnTestGenerated);28 lastGeneration = generator.BuildTests(cases, globalVariables).ToList();29 generator.Progressed -= new EventHandler<ProgressEventArgs>(this.OnTestGenerated);30 Tests = Tests.Concat(lastGeneration).ToList();31 }32 public void Build(IEnumerable<string> templates, string[] variables, DataTable dataTable, bool useGrouping, IDictionary<string, object> globalVariables)33 {34 if (templates.Count() == 0)35 throw new ArgumentException("No template was specified. You must at least define a template before generating a test suite.");36 if (templates.Count() == 1)37 Build(templates.ElementAt(0), variables, dataTable, useGrouping, globalVariables);38 else39 {40 41 var cases = GetCases(dataTable, useGrouping);42 foreach (var indiv in cases)43 {44 foreach (var template in templates)45 {46 var generator = new StringTemplateEngine(template, variables);47 generator.Progressed += new EventHandler<ProgressEventArgs>(this.OnTestGenerated);48 lastGeneration = generator.BuildTests(new List<List<List<object>>>() { indiv }, globalVariables).ToList();49 generator.Progressed -= new EventHandler<ProgressEventArgs>(this.OnTestGenerated);50 Tests = Tests.Concat(lastGeneration).ToList();51 }52 }53 }54 }55 public void OnTestGenerated(object sender, ProgressEventArgs e)56 {57 InvokeProgress(e);58 }59 public event EventHandler<ProgressEventArgs> Progressed;60 public void InvokeProgress(ProgressEventArgs e)61 {62 Progressed?.Invoke(this, e);63 }64 public bool CanUndo65 {66 get { return lastGeneration.Count != 0; }67 }68 public void Undo()69 {70 foreach (var test in lastGeneration)71 {72 Tests.Remove(test);73 }74 lastGeneration.Clear();75 }76 public IList<Test> GetTests()77 {78 var value = new List<Test>();79 foreach (var test in Tests)80 {81 var t = new Test82 {83 Content = test.Content,84 Title = test.Name,85 Reference = test86 };87 value.Add(t);88 }89 return value;90 }91 protected List<List<List<object>>> GetCases(DataTable dataTable, bool useGrouping)92 {93 if (dataTable.Rows.Count==0)94 return new List<List<List<object>>>();95 int groupedColumn = dataTable.Rows[0].ItemArray.Length - 1;96 var variableTests = new List<List<List<object>>>();97 for (int i = 0; i < dataTable.Rows.Count; i++)98 {99 var isIdentical = (i != 0) && useGrouping;100 var grouping = dataTable.Rows[i].ItemArray.ToList();101 grouping.RemoveAt(groupedColumn);102 var k = 0;103 while (k < grouping.Count && isIdentical)104 {105 if (grouping[k].ToString() != variableTests[variableTests.Count - 1][k][0].ToString())106 isIdentical = false;107 k++;108 }109 if (!isIdentical)110 {111 variableTests.Add(new List<List<object>>());112 for (int j = 0; j < dataTable.Rows[i].ItemArray.Length; j++)113 {114 variableTests[variableTests.Count - 1].Add(new List<object>());115 if (dataTable.Rows[i].ItemArray[j] is IEnumerable<string>)116 {117 foreach (var item in (IEnumerable<string>)dataTable.Rows[i].ItemArray[j])118 variableTests[variableTests.Count - 1][j].Add(item);119 }120 else121 variableTests[variableTests.Count - 1][j].Add(dataTable.Rows[i].ItemArray[j].ToString());122 }123 }124 else125 variableTests[variableTests.Count - 1][groupedColumn].Add(dataTable.Rows[i].ItemArray[groupedColumn].ToString());126 }127 return variableTests;128 }129 public void Clear()130 {131 Tests.Clear();132 lastGeneration.Clear();133 }134 public void RemoveAt(int index)135 {136 Tests.RemoveAt(index);137 }138 public void Remove(Test test)139 {140 Tests.Remove(test.Reference);141 }142 public void SetTests(IEnumerable<Test> tests)143 {144 Tests.Clear();145 foreach (var testDto in tests)146 Tests.Add(testDto.Reference);147 }148 149 public void AddCategory(Test test, string categoryName)150 {151 var categories = test.Reference.Categories;152 if (!categories.Contains(categoryName))153 {154 categories.Add(categoryName);155 //test.Reference.Content = StringTemplateEngine.XmlSerializeFrom<TestStandaloneXml>((TestStandaloneXml)test.Reference);156 }157 }158 public IEnumerable<char> GetCategoryForbiddenChars() => new List<char>() { '+', '-' };159 public IEnumerable<string> GetExistingCategories()160 {161 var categories = new List<string>();162 foreach (var test in Tests)163 {164 foreach (var category in test.Categories)165 {166 if (!categories.Contains(category))167 categories.Add(category);168 }169 }...
GenerateSuiteAction.cs
Source:GenerateSuiteAction.cs
...94 protected abstract TreeNode BuildNode(T content);9596 protected IEnumerable<T> Build(string template, string[] variables, DataTable dataTable, bool useGrouping, IDictionary<string, object> globalVariables)97 {98 var generator = new StringTemplateEngine(template, variables);99 var cases = GetCases(dataTable, useGrouping);100 generator.Progressed += new EventHandler<ProgressEventArgs>(this.OnTestGenerated);101 var lastGeneration = generator.Build<T>(cases, globalVariables).ToList();102 generator.Progressed -= new EventHandler<ProgressEventArgs>(this.OnTestGenerated);103 return lastGeneration;104 }105106 protected IEnumerable<T> Build(IEnumerable<string> templates, string[] variables, DataTable dataTable, bool useGrouping, IDictionary<string, object> globalVariables)107 {108 if (templates.Count() == 0)109 throw new ArgumentException("No template was specified. You must at least define a template before generating a test suite.");110111 if (templates.Count() == 1)112 return Build(templates.ElementAt(0), variables, dataTable, useGrouping, globalVariables);113 else114 {115 var lastGeneration = new List<T>();116 var cases = GetCases(dataTable, useGrouping);117 foreach (var indiv in cases)118 {119 foreach (var template in templates)120 {121 var engine = new StringTemplateEngine(template, variables);122 engine.Progressed += new EventHandler<ProgressEventArgs>(this.OnTestGenerated);123 lastGeneration.AddRange(engine.Build<T>(new List<List<List<object>>>() { indiv }, globalVariables).ToList());124 engine.Progressed -= new EventHandler<ProgressEventArgs>(this.OnTestGenerated);125 }126 }127 return lastGeneration;128 }129 }130131 protected void GenerateBranches(RootNode rootNode, IEnumerable<string[]> groupNames)132 {133 foreach (var groupName in groupNames)134 {135 BranchNode parentNode = rootNode;136 foreach (var nodeName in groupName)137 {138 var groupNode = (parentNode.Children.FirstOrDefault(x => x.Name == nodeName) as GroupNode) ?? new GroupNode(nodeName);139 if (!parentNode.Children.Any(x => x == groupNode))140 parentNode.AddChild(groupNode);141 parentNode = groupNode;142 }143 }144 }145146 protected IEnumerable<string> RenderGroupNames(IEnumerable<string> templates, string[] variables, DataTable dataTable, IDictionary<string, object> globalVariables)147 {148 var cases = GetCases(dataTable, false);149 var names = new List<string>();150 foreach (var template in templates)151 {152 var engine = new StringTemplateEngine(template, variables);153 foreach (var indiv in cases)154 {155 var newNames = engine.Build<string>(new List<List<List<object>>>() { indiv }, globalVariables).ToList();156 names.AddRange(newNames);157 }158159 }160 return names;161 }162163164 public void OnTestGenerated(object sender, ProgressEventArgs e) => InvokeProgress(e);165 public event EventHandler<ProgressEventArgs> Progressed;166 public void InvokeProgress(ProgressEventArgs e) => Progressed?.Invoke(this, e);
...
StringTemplateEngine
Using AI Code Generation
1var template = "Hello $name$";2var parameters = new Dictionary<string, object> { { "name", "world" } };3var result = StringTemplateEngine.Evaluate(template, parameters);4Console.WriteLine(result);5var template = "Hello $name$";6var parameters = new Dictionary<string, object> { { "name", "world" } };7var result = StringTemplateEngine.Evaluate(template, parameters);8Console.WriteLine(result);9var template = "Hello $name$";10var parameters = new Dictionary<string, object> { { "name", "world" } };11var result = StringTemplateEngine.Evaluate(template, parameters);12Console.WriteLine(result);13var template = "Hello $name$";14var parameters = new Dictionary<string, object> { { "name", "world" } };15var result = StringTemplateEngine.Evaluate(template, parameters);16Console.WriteLine(result);17var template = "Hello $name$";18var parameters = new Dictionary<string, object> { { "name", "world" } };19var result = StringTemplateEngine.Evaluate(template, parameters);20Console.WriteLine(result);21var template = "Hello $name$";22var parameters = new Dictionary<string, object> { { "name", "world" } };23var result = StringTemplateEngine.Evaluate(template, parameters);24Console.WriteLine(result);25var template = "Hello $name$";26var parameters = new Dictionary<string, object> { { "name", "world" } };27var result = StringTemplateEngine.Evaluate(template, parameters);28Console.WriteLine(result);
StringTemplateEngine
Using AI Code Generation
1var template = @"Hello, $name$!";2var parameters = new Dictionary<string, object>();3parameters.Add("name", "World");4var result = StringTemplateEngine.Evaluate(template, parameters);5System.Console.WriteLine(result);6var template = @"Hello, $name$!";7var parameters = new Dictionary<string, object>();8parameters.Add("name", "World");9var result = StringTemplateEngine.Evaluate(template, parameters);10System.Console.WriteLine(result);11var template = @"Hello, $name$!";12var parameters = new Dictionary<string, object>();13parameters.Add("name", "World");14var result = StringTemplateEngine.Evaluate(template, parameters);15System.Console.WriteLine(result);16var template = @"Hello, $name$!";17var parameters = new Dictionary<string, object>();18parameters.Add("name", "World");19var result = StringTemplateEngine.Evaluate(template, parameters);20System.Console.WriteLine(result);21var template = @"Hello, $name$!";22var parameters = new Dictionary<string, object>();23parameters.Add("name", "World");24var result = StringTemplateEngine.Evaluate(template, parameters);25System.Console.WriteLine(result);26var template = @"Hello, $name$!";27var parameters = new Dictionary<string, object>();28parameters.Add("name", "World");29var result = StringTemplateEngine.Evaluate(template, parameters);30System.Console.WriteLine(result);31var template = @"Hello, $name$!";32var parameters = new Dictionary<string, object>();33parameters.Add("name", "World");34var result = StringTemplateEngine.Evaluate(template, parameters);35System.Console.WriteLine(result);
StringTemplateEngine
Using AI Code Generation
1{2 {3 public string Template { get; set; }4 public string Destination { get; set; }5 public string Content { get; set; }6 public StringTemplateEngineAction(string template, string destination)7 {8 Template = template;9 Destination = destination;10 }11 public void Execute(GenerationState state)12 {13 Content = new StringTemplateEngine().Execute(Template, state);14 }15 public void WriteReport(GenerationState state)16 {17 state.LogTemplate(Content, Destination);18 }19 }20}21{22 {23 public string Template { get; set; }24 public string Destination { get; set; }25 public string Content { get; set; }26 public StringTemplateEngineAction(string template, string destination)27 {28 Template = template;29 Destination = destination;30 }31 public void Execute(GenerationState state)32 {33 Content = new StringTemplateEngine().Execute(Template, state);34 }35 public void WriteReport(GenerationState state)36 {37 state.LogTemplate(Content, Destination);38 }39 }40}41{42 {43 public string Template { get; set; }44 public string Destination { get; set; }45 public string Content { get; set; }46 public StringTemplateEngineAction(string template, string destination)47 {48 Template = template;49 Destination = destination;50 }51 public void Execute(GenerationState state)52 {53 Content = new StringTemplateEngine().Execute(Template, state);54 }55 public void WriteReport(GenerationState state)56 {57 state.LogTemplate(Content, Destination);58 }59 }60}
StringTemplateEngine
Using AI Code Generation
1var template = "This is a template with $1$ and $2$";2var templateEngine = new NBi.GenbiL.Templating.StringTemplateEngine();3var result = templateEngine.ApplyTemplate(template, new List<string> {"one", "two"});4Console.WriteLine(result);5var template = "This is a template with $1$ and $2$";6var templateEngine = new NBi.GenbiL.Templating.StringTemplateEngine();7var result = templateEngine.ApplyTemplate(template, new List<string> {"one", "two"});8Console.WriteLine(result);9var template = "This is a template with $1$ and $2$";10var templateEngine = new NBi.GenbiL.Templating.StringTemplateEngine();11var result = templateEngine.ApplyTemplate(template, new List<string> {"one", "two"});12Console.WriteLine(result);13var template = "This is a template with $1$ and $2$";14var templateEngine = new NBi.GenbiL.Templating.StringTemplateEngine();15var result = templateEngine.ApplyTemplate(template, new List<string> {"one", "two"});16Console.WriteLine(result);17var template = "This is a template with $1$ and $2$";18var templateEngine = new NBi.GenbiL.Templating.StringTemplateEngine();19var result = templateEngine.ApplyTemplate(template, new List<string> {"one", "two"});20Console.WriteLine(result);21var template = "This is a template with $1$ and $2$";22var templateEngine = new NBi.GenbiL.Templating.StringTemplateEngine();23var result = templateEngine.ApplyTemplate(template, new List<string
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!