Esempio n. 1
0
        public void GivenAGenericList_WhenRenderingUsingTypeTemplate_ThenTheExpectedOutcomeIsRendered()
        {
            // Arrange
            var parser   = new TemplateParser();
            var template = parser.Parse("[foreach]{}[/]");

            var source = new List <int>()
            {
                1, 2, 3
            };

            // Act
            var testProxy = new TestProxy();

            testProxy.WriteTemplate(template, source);
            var preTemplateResult = testProxy.ToString();

            parser.AddTypeTemplate <int>("{v}", t => new{ v = (10 - t).ToString() });

            var testProxy2 = new TestProxy();

            testProxy2.WriteTemplate(template, source);
            var postTemplateResult = testProxy2.ToString();

            // Assert
            Assert.Equal("123", preTemplateResult);
            Assert.Equal("987", postTemplateResult);
        }
        public void GivenAPropertyNameWithFormatting_WhenRendering_ThenSubstituteWithFormattedValue()
        {
            // Arrange
            var substitutionRenderer = new SubstitutionRenderer("Value:D5", new Template(new TemplateParser()));
            var consoleProxy         = new TestProxy();

            // Act
            substitutionRenderer.Render(consoleProxy, new { Value = 1 }, CultureInfo.CurrentUICulture);

            // Assert
            Assert.Equal("00001", consoleProxy.ToString());
        }
        public void GivenANullValue_WhenSubstituting_ThenReplaceWithEmptyString()
        {
            // Arrange
            var substitutionRenderer = new SubstitutionRenderer("Value", new Template(new TemplateParser()));
            var consoleProxy         = new TestProxy();

            // Act
            substitutionRenderer.Render(consoleProxy, new { Value = (string)null }, CultureInfo.CurrentUICulture);

            // Assert
            Assert.Equal(consoleProxy.ToString(), string.Empty);
        }
        public void GivenANestedPropertyName_WhenRendering_ThenSubstituteWithValue()
        {
            // Arrange
            var substitutionRenderer = new SubstitutionRenderer("Value.Length", new Template(new TemplateParser()));
            var consoleProxy         = new TestProxy();

            // Act
            substitutionRenderer.Render(consoleProxy, new { Value = "This is the value" }, CultureInfo.CurrentUICulture);

            // Assert
            Assert.Equal("17", consoleProxy.ToString());
        }
        public void GivenASimplePropertyNameWithWrongCasing_WhenRendering_ThenSubstituteWithEmptyString()
        {
            // Arrange
            var substitutionRenderer = new SubstitutionRenderer("value", new Template(new TemplateParser()));
            var consoleProxy         = new TestProxy();

            // Act
            substitutionRenderer.Render(consoleProxy, new { Value = "This is the value" }, CultureInfo.CurrentUICulture);

            // Assert
            Assert.Equal(string.Empty, consoleProxy.ToString());
        }
        public void GivenAController_WhenCallingAMethod_ThenStandardConvertersAreUsed()
        {
            // Arrange
            var consoleProxy = new TestProxy();
            var setupCalled  = false;
            var controller   = new Controller(new MyClass(), consoleProxy, setup => setupCalled = true);

            controller.Run("Render", "true", "Some string", "12", "-Uppercase");

            // Act
            var actual = consoleProxy.ToString();

            // Assert
            Assert.Equal("TRUE : SOME STRING : 12", actual);
            Assert.True(setupCalled);
        }
Esempio n. 7
0
        public void GivenATypeTemplateWithTypeConverter_WhenRenderingAValue_ThenTheValueIsConverted()
        {
            // Arrange
            var parser   = new TemplateParser();
            var template = parser.Parse("{}");

            // Act
            var testProxy = new TestProxy();

            testProxy.WriteTemplate(template, this);
            var preTemplateResult = testProxy.ToString();

            parser.AddTypeTemplate <SubTemplateTests>("{}", tests => new string(tests.GetType().Name.Reverse().ToArray()));
            var testProxy2 = new TestProxy();

            testProxy2.WriteTemplate(template, this);
            var postTemplateResult = testProxy2.ToString();

            // Assert
            Assert.Equal("ConsoleExtensions.Templating.Tests.SubTemplateTests", preTemplateResult);
            Assert.Equal("stseTetalpmeTbuS", postTemplateResult);
        }
Esempio n. 8
0
        public void GivenAObjectWithNoSubTemplate_WhenRegisteringATemplate_ThenTheRegisteredTemplateShouldBeUsed()
        {
            // Arrange
            var parser   = new TemplateParser();
            var template = parser.Parse("{}");

            // Act
            var testProxy = new TestProxy();

            testProxy.WriteTemplate(template, this);
            var preTemplateResult = testProxy.ToString();

            parser.AddTypeTemplate <SubTemplateTests>("Replaced with template");
            var testProxy2 = new TestProxy();

            testProxy2.WriteTemplate(template, this);
            var postTemplateResult = testProxy2.ToString();

            // Assert
            Assert.Equal("ConsoleExtensions.Templating.Tests.SubTemplateTests", preTemplateResult);
            Assert.Equal("Replaced with template", postTemplateResult);
        }