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

SingleSectionParserTests.cs
using NUnit.Framework;
using Sprache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WordExporter.Core.Templates.Parser;

namespace WordExporter.Tests.Templates.Parser
{
    [TestFixture]
    public clast SingleSectionParserTests
    {
        [Test]
        public void Identify_basic_parameters_section()
        {
            var section = ConfigurationParser.SectionParser.Parse(@"[[parameters]]
section");
            astert.That(section, Is.InstanceOf());
        }

        [Test]
        public void Unknown_section_Will_throw()
        {
            astert.Throws(() => ConfigurationParser.SectionParser.Parse("[[unknown section]]"));
        }

        [Test]
        public void Basic_KeyValue()
        {
            var keyValue = ConfigurationParser.KeyValue.Parse("key: value");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("value"));
        }

        [Test]
        public void Can_have_more_than_value_with_Single_key()
        {
            var keyValues = ConfigurationParser.KeyValueList.Parse("key: value\nkey: value2").ToArray();
            astert.That(keyValues.Length, Is.EqualTo(2));
            astert.That(keyValues[0].Key, Is.EqualTo("key"));
            astert.That(keyValues[0].Value, Is.EqualTo("value"));
            astert.That(keyValues[1].Key, Is.EqualTo("key"));
            astert.That(keyValues[1].Value, Is.EqualTo("value2"));
        }

        [Test]
        public void Basic_Multiple_KeyValue()
        {
            var keyValues = ConfigurationParser.KeyValueList.Parse("key: value\nkey2: value2").ToArray();
            astert.That(keyValues.Length, Is.EqualTo(2));
            astert.That(keyValues[0].Key, Is.EqualTo("key"));
            astert.That(keyValues[0].Value, Is.EqualTo("value"));
            astert.That(keyValues[1].Key, Is.EqualTo("key2"));
            astert.That(keyValues[1].Value, Is.EqualTo("value2"));
        }

        [Test]
        public void Basic_KeyValue_resilient_to_spaces()
        {
            var keyValue = ConfigurationParser.KeyValue.Parse("key  : value  ");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("value"));
        }

        [Test]
        public void SingleLine_with_semicolon()
        {
            var keyValue = ConfigurationParser.KeyValue.Parse("key: value:with:semicolon");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("value:with:semicolon"));
        }

        [Test]
        public void MultiLine_key_value()
        {
            var keyValue = ConfigurationParser.MultiLineKeyValue.Parse(@"key: ""value

value2

value3""");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("value\r\n\r\nvalue2\r\n\r\nvalue3"));
        }

        [Test]
        public void MultiLine_key_value_query()
        {
            var keyValue = ConfigurationParser.MultiLineKeyValue.Parse(
      @"query: ""SELECT
        * 
        FROM workitems
        WHERE
            [System.TeamProject] = @project
            AND [System.WorkItemType] = 'Product Backlog Item'
            AND [System.IterationPath] = '{iterationPath}'""");
            astert.That(keyValue.Key, Is.EqualTo("query"));
            astert.That(keyValue.Value.Contains("[System.TeamProject] = @project"));
        }

        [Test]
        public void MultiLine_key_value_query_plus_parameter()
        {
            var keyValue = ConfigurationParser.MultiLineKeyValue.Parse(
      @"query: ""SELECT
        * 
        FROM workitems
        WHERE
            [System.TeamProject] = @project
            AND [System.WorkItemType] = 'Product Backlog Item'
            AND [System.IterationPath] = '{iterationPath}'""
        queryParameter: test");
            astert.That(keyValue.Key, Is.EqualTo("query"));
            astert.That(keyValue.Value.Contains("[System.TeamProject] = @project"));
        }

        [Test]
        public void MultiLine_key_value_can_contain_square_bracket()
        {
            var keyValue = ConfigurationParser.MultiLineKeyValue.Parse(@"key: ""value
[value2]
value3""");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("value\r\n[value2]\r\nvalue3"));
        }

        [Test]
        public void MultiLine_key_value_can_contain_semicolon()
        {
            var keyValue = ConfigurationParser.MultiLineKeyValue.Parse(@"key: ""value
value:[with]:semicolon
:value3""");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("value\r\nvalue:[with]:semicolon\r\n:value3"));
        }

        [Test]
        public void Single_parameter_set()
        {
            var parameterList = ConfigurationParser.ParameterSetList.Parse(@"param1=a|param2=b").ToArray();
            astert.That(parameterList.Length, Is.EqualTo(2));
            astert.That(parameterList[0].Key, Is.EqualTo("param1"));
            astert.That(parameterList[0].Value, Is.EqualTo("a"));
            astert.That(parameterList[1].Key, Is.EqualTo("param2"));
            astert.That(parameterList[1].Value, Is.EqualTo("b"));
        }

        [Test]
        public void Parameter_with_default_value()
        {
            var keyValue = ParameterSection.Parameter.Parse("key=defValue");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("defValue"));
        }

        [Test]
        public void Parameter_with_default_value_and_spaces()
        {
            var keyValue = ParameterSection.Parameter.Parse("key  =  defValue");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo("defValue"));
        }

        [Test]
        public void Parameter_without_default_value_but_equalSign()
        {
            var keyValue = ParameterSection.Parameter.Parse("key=");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo(""));
        }

        [Test]
        public void Parameter_without_default_value()
        {
            var keyValue = ParameterSection.Parameter.Parse("key");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo(""));
        }

        [Test]
        public void Parameter_without_default_value_and_other_line()
        {
            var keyValue = ParameterSection.Parameter.Parse(@"key
otherline");
            astert.That(keyValue.Key, Is.EqualTo("key"));
            astert.That(keyValue.Value, Is.EqualTo(""));
        }

        [Test]
        public void Parameter_without_default_value_and_other_line_many_parse()
        {
            var keyValue = ParameterSection.Parser.Parse(@"key
otherline");
            astert.That(keyValue.Parameters.Count, Is.EqualTo(2));
            astert.That(keyValue.Parameters.Keys, Is.EquivalentTo(new[] { "key", "otherline"}));
        }
    }
}