csharp/alkampfergit/AzureDevopsWordPlayground/src/WordExporter.Tests/Templates/Parser/BasicParserTests.cs

BasicParserTests.cs
using NUnit.Framework;
using System;
using System.Linq;
using WordExporter.Core.Templates;
using WordExporter.Core.Templates.Parser;

namespace WordExporter.Tests.Templates.Parser
{
    [TestFixture]
    public clast BasicParserTests
    {
        [Test]
        public void Basic_Parsing_of_Parameters_returns_parameter_section()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[parameters]]
    parama
    paramb");
            astert.That(def.ParameterSection, Is.Not.Null);
        }

        [Test]
        public void Basic_Parsing_of_definition_for_excel_Export()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[definition]]
  type=excel
[[parameters]]
  TargetDateStart=2019-06-01
  TargetDateEnd=2019-10-01
[[parameterDefinition]]");
            astert.That(def.Type, Is.EqualTo(TemplateType.Excel));
        }

        [Test]
        public void Default_template_type_is_word()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[parameters]]
  TargetDateStart=2019-06-01
  TargetDateEnd=2019-10-01
[[parameterDefinition]]");
            astert.That(def.Type, Is.EqualTo(TemplateType.Word));
        }

        [Test]
        public void Support_for_base_template()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[definition]]
  type=excel
  baseTemplate=test.xlsx
[[parameters]]
  TargetDateStart=2019-06-01
  TargetDateEnd=2019-10-01
[[parameterDefinition]]");
            astert.That(def.BaseTemplate, Is.EqualTo ("test.xlsx"));
        }

        [Test]
        public void Basic_Parsing_of_Parameters_with_default_value_returns_parameter_section()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[parameters]]
    parama=2017-01-01
    paramb=2019-04-31");
            astert.That(def.ParameterSection, Is.Not.Null);
            var paramA = def.ParameterSection.Parameters["parama"];
            astert.That(paramA, Is.EqualTo("2017-01-01"));

            var paramB = def.ParameterSection.Parameters["paramb"];
            astert.That(paramB, Is.EqualTo("2019-04-31"));
        }

        [Test]
        public void Basic_Parsing_of_Parameters_with_allowed_values()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[parameterDefinition]]
    parama=string/A|B|C
    paramb=datetime");
            astert.That(def.ParameterDefinition, Is.Not.Null);
            var paramA = def.ParameterDefinition["parama"];
            astert.That(paramA.Type, Is.EqualTo("string"));
            astert.That(paramA.AllowedValues, Is.EquivalentTo(new[] { "A", "B", "C" }));

            var paramb = def.ParameterDefinition["paramb"];
            astert.That(paramb.Type, Is.EqualTo("datetime"));
        }

        [Test]
        public void Basic_Parsing_of_array_parameter()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[arrayParameters]]
    tags");
            astert.That(def.ArrayParameterSection.ArrayParameters.Count, Is.EqualTo(1));
        }

        [Test]
        public void Basic_Parsing_of_array_parameter_followed_by_other_params()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[arrayParameters]]
    tags=te,tr,ty
[[parameters]]
	TargetDateStart=2019-01-01");
            astert.That(def.ArrayParameterSection.ArrayParameters.Count, Is.EqualTo(1));
        }

        [Test]
        public void Parsing_two_Sections()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[parameters]]
    parama
    paramb
[[static]]
    filename: bla.txt");
            astert.That(def.AllSections.Length, Is.EqualTo(2));
            astert.That(def.AllSections[0], Is.InstanceOf());
            astert.That(def.AllSections[1], Is.InstanceOf());
        }

        [Test]
        public void Valid_static_section()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[static]]
    filename: bla.docx
    pageBreak: true");
            var staticSection = def.AllSections.Single() as StaticWordSection;
            astert.That(staticSection.FileName, Is.EqualTo("bla.docx"));
            astert.That(staticSection.PageBreak, Is.EqualTo(true));
        }

        [Test]
        public void Valid_query_section()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    name: TestQueryName
    query: ""SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'""
    template/Product Backlog Item: pbix.docx
    template/Bug: bugaa.docx
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.Name, Is.EqualTo("TestQueryName"));
            astert.That(querySection.Query, Is.EqualTo("SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'"));
        }

        [Test]
        public void Query_section_parse_specific_workItem_templates()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'""
    template/Product Backlog Item: pbix.docx
    template/Bug: bugaa.docx
    limit: 1
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.GetTemplateForWorkItem("Product Backlog Item"), Is.EqualTo("pbix.docx"));
            astert.That(querySection.GetTemplateForWorkItem("Task"), Is.EqualTo(null));
            astert.That(querySection.Limit, Is.EqualTo(1));
        }

        [Test]
        public void Query_section_parse_filter_work_item_by_type()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'""
    template/Product Backlog Item: pbix.docx
    template/Bug: bugaa.docx
    limit: 1
    workItemTypes: Product Backlog Item,Feature
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.GetTemplateForWorkItem("Product Backlog Item"), Is.EqualTo("pbix.docx"));
            astert.That(querySection.GetTemplateForWorkItem("Task"), Is.EqualTo(null));
            astert.That(querySection.WorkItemTypes, Is.EquivalentTo(new[] { "Product Backlog Item", "Feature" }));
        }

        [Test]
        public void Query_section_without_limit_default_to_max_value()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'""
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.Limit, Is.EqualTo(Int32.MaxValue));
        }

        [Test]
        public void Basic_Parsing_of_custom_Parameters()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[parameters]]
    parama
    paramb
");
            astert.That(def.ParameterSection.Parameters.Keys, Is.EquivalentTo(new[] { "parama", "paramb" }));
        }

        [Test]
        public void Valid_query_section_with_table_file()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'""
    tableTemplate: table.docx
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.TableTemplate, Is.EqualTo("table.docx"));
        }

        [Test]
        public void Valid_query_iteration_parameter()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT * FROM WorkItems Where [System.AreaPath] UNDER '{areaPath}' AND [System.IterationPath] UNDER '{iterationPath}'""
    tableTemplate: table.docx
    repeatForEachIteration: true
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.RepeatForEachIteration, Is.True);
        }

        [Test]
        public void Valid_parametric_query()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT
        * 
        FROM workitems
        WHERE
            [System.TeamProject] = @project
            AND [System.WorkItemType] = 'Product Backlog Item'
            AND [System.IterationPath] = '{iterationPath}'""
    parameterSet: iterationPath=Zoalord Insurance\Release 1\Sprint 4
    parameterSet: iterationPath=Zoalord Insurance\Release 1\Sprint 5  
    parameterSet: iterationPath=Zoalord Insurance\Release 1\Sprint 6
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.QueryParameters.Count, Is.EqualTo(3));
            astert.That(querySection.QueryParameters[0]["iterationPath"], Is.EqualTo(@"Zoalord Insurance\Release 1\Sprint 4"));
            astert.That(querySection.QueryParameters[1]["iterationPath"], Is.EqualTo(@"Zoalord Insurance\Release 1\Sprint 5"));
            astert.That(querySection.QueryParameters[2]["iterationPath"], Is.EqualTo(@"Zoalord Insurance\Release 1\Sprint 6"));
        }

        [Test]
        public void Multiline_query()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
query: ""SELECT
    * 
    FROM workitems
    WHERE
        [System.TeamProject] = @project
        AND [System.WorkItemType] = 'Product Backlog Item'
        AND [System.IterationPath] = '{iterationPath}'""
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.Query.Contains("AND [System.IterationPath] = '{iterationPath}'"));
            astert.That(querySection.Query.Contains("[System.TeamProject] = @project"));
        }

        [Test]
        public void Multiline_query_complex()
        {
            var sut = new ConfigurationParser();
            TemplateDefinition def = sut.ParseTemplateDefinition(
@"[[query]]
    query: ""SELECT
        *
        FROM
            workitems
        WHERE
            [System.WorkItemType] = 'Feature' AND
            [Microsoft.VSTS.Scheduling.TargetDate] >= '{TargetDateStart}' AND
            [Microsoft.VSTS.Scheduling.TargetDate]  '2000-02-02T00:00:00.0000000'
    )
    AND (
        [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
    )
    AND (
        [Target].[System.TeamProject] = @project
        AND [Target].[System.WorkItemType]  ''
    )
MODE (Recursive)""
    hierarchyMode: task,feature,requirement,epic
");
            var querySection = def.AllSections.Single() as QuerySection;
            astert.That(querySection.HierarchyMode, Is.EquivalentTo(new[] { "task", "feature", "requirement", "epic" }));
        }
    }
}