How to use StringTemplateEngine class of NBi.GenbiL.Templating package

Best NBi code snippet using NBi.GenbiL.Templating.StringTemplateEngine

StringTemplateEngineTest.cs

Source:StringTemplateEngineTest.cs Github

copy

Full Screen

...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=\"&lt;value&amp;\" other-attribute=\"<value&\">"));197 }198 }199}...

Full Screen

Full Screen

TestListManager.cs

Source:TestListManager.cs Github

copy

Full Screen

...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 }...

Full Screen

Full Screen

GenerateSuiteAction.cs

Source:GenerateSuiteAction.cs Github

copy

Full Screen

...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); ...

Full Screen

Full Screen

StringTemplateEngine

Using AI Code Generation

copy

Full Screen

1var engine = new StringTemplateEngine();2var result = engine.Render("Hello, {{name}}", new {name="World"});3Console.WriteLine(result);4var engine = new StringTemplateEngine();5var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});6Console.WriteLine(result);7var engine = new StringTemplateEngine();8var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});9Console.WriteLine(result);10var engine = new StringTemplateEngine();11var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});12Console.WriteLine(result);13var engine = new StringTemplateEngine();14var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});15Console.WriteLine(result);16var engine = new StringTemplateEngine();17var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});18Console.WriteLine(result);19var engine = new StringTemplateEngine();20var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});21Console.WriteLine(result);22var engine = new StringTemplateEngine();23var result = engine.Render("Hello, {{name}}", new Dictionary<string, object> {{"name", "World"}});24Console.WriteLine(result);

Full Screen

Full Screen

StringTemplateEngine

Using AI Code Generation

copy

Full Screen

1var engine = new StringTemplateEngine();2var template = engine.GetTemplate("template.stg");3var result = template.Render(new { Name = "John", Age = 42 });4var engine = new StringTemplateEngine();5var template = engine.GetTemplate("template.stg");6var result = template.Render(new { Name = "John", Age = 42 });

Full Screen

Full Screen

StringTemplateEngine

Using AI Code Generation

copy

Full Screen

1var engine = new StringTemplateEngine();2var template = engine.LoadTemplate("template.txt");3var content = engine.Render(template, new { Name = "John Doe" });4File.WriteAllText("1.cs", content);5var engine = new StringTemplateEngine();6var template = engine.LoadTemplate("template.txt");7var content = engine.Render(template, new { Name = "John Doe" });8File.WriteAllText("1.cs", content);9var engine = new StringTemplateEngine();10var template = engine.LoadTemplate("template.txt");11var content = engine.Render(template, new { Name = "John Doe" });12File.WriteAllText("1.cs", content);13var engine = new StringTemplateEngine();14var template = engine.LoadTemplate("template.txt");15var content = engine.Render(template, new { Name = "John Doe" });16File.WriteAllText("1.cs", content);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run NBi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful