public void Init_GetsTagHelperComponentPropertyActivator_FromRequestServices()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                "head",
                allAttributes: new TagHelperAttributeList(
                    Enumerable.Empty <TagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var testTagHelperComponentManager = new TagHelperComponentManager(new[]
            {
                new TestTagHelperComponent()
            });

            var testTagHelperComponentTagHelper = new TestTagHelperComponentTagHelper(
                testTagHelperComponentManager,
                NullLoggerFactory.Instance);

            // Act
            testTagHelperComponentTagHelper.Init(tagHelperContext);

            // Assert
            Assert.NotNull(testTagHelperComponentTagHelper.PropertyActivator);
        }
        public async Task ProcessAsync_LogsTagHelperComponentProcessed()
        {
            // Arrange
            var sink             = new TestSink();
            var loggerFactory    = new TestLoggerFactory(sink, enabled: true);
            var tagHelperContext = new TagHelperContext(
                "head",
                allAttributes: new TagHelperAttributeList(
                    Enumerable.Empty <TagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "head",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) => Task.FromResult <TagHelperContent>(
                    new DefaultTagHelperContent()));

            var testTagHelperComponentManager = new TagHelperComponentManager(new[]
            {
                new TestTagHelperComponent()
            });

            var testTagHelperComponentTagHelper = new TestTagHelperComponentTagHelper(testTagHelperComponentManager, loggerFactory);

            // Act
            await testTagHelperComponentTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal($"Tag helper component '{typeof(TestTagHelperComponent)}' processed.", sink.Writes[0].State.ToString(), StringComparer.Ordinal);
        }
        public async Task ProcessAsync_InvokesTagHelperComponentProcessAsync_WithAddedTagHelperComponents()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                "head",
                allAttributes: new TagHelperAttributeList(
                    Enumerable.Empty <TagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "head",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) => Task.FromResult <TagHelperContent>(
                    new DefaultTagHelperContent()));

            var testTagHelperComponentManager = new TagHelperComponentManager(new[]
            {
                new TestTagHelperComponent()
            });

            testTagHelperComponentManager.Components.Add(new TestAddTagHelperComponent(0));
            testTagHelperComponentManager.Components.Add(new TestAddTagHelperComponent(2));
            var testTagHelperComponentTagHelper = new TestTagHelperComponentTagHelper(testTagHelperComponentManager, NullLoggerFactory.Instance);

            // Act
            await testTagHelperComponentTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal("Processed0Processed1Processed2", output.PostContent.GetContent());
        }
        public void Init_InvokesTagHelperComponentInit()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                "head",
                allAttributes: new TagHelperAttributeList(
                    Enumerable.Empty <TagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "head",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) => Task.FromResult <TagHelperContent>(
                    new DefaultTagHelperContent()));

            var testTagHelperComponentManager = new TagHelperComponentManager(new[]
            {
                new TestTagHelperComponent()
            });

            var testTagHelperComponentTagHelper = new TestTagHelperComponentTagHelper(testTagHelperComponentManager, NullLoggerFactory.Instance);

            // Act
            testTagHelperComponentTagHelper.Init(tagHelperContext);

            // Assert
            Assert.Equal("Value", tagHelperContext.Items["Key"]);
        }
        public async void ProcessAsync_InvokesComponentsProcessAsyncInCorrectOrder()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                "head",
                allAttributes: new TagHelperAttributeList(
                    Enumerable.Empty <TagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "head",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) => Task.FromResult <TagHelperContent>(
                    new DefaultTagHelperContent()));

            var incrementer = 0;
            var testTagHelperComponentManager = new TagHelperComponentManager(new[]
            {
                new CallbackTagHelperComponent(
                    order: 2,
                    initCallback: () => { },
                    processAsyncCallback: () =>
                {
                    Assert.Equal(1, incrementer);
                    incrementer++;
                }),
                new CallbackTagHelperComponent(
                    order: 3,
                    initCallback: () => { },
                    processAsyncCallback: () =>
                {
                    Assert.Equal(2, incrementer);
                    incrementer++;
                }),
                new CallbackTagHelperComponent(
                    order: 1,
                    initCallback: () => { },
                    processAsyncCallback: () =>
                {
                    Assert.Equal(0, incrementer);
                    incrementer++;
                }),
            });

            var testTagHelperComponentTagHelper = new TestTagHelperComponentTagHelper(testTagHelperComponentManager, NullLoggerFactory.Instance);

            // Act
            testTagHelperComponentTagHelper.Init(tagHelperContext);
            await testTagHelperComponentTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(3, incrementer);
        }
        public void Init_InvokesComponentsInitInCorrectOrder()
        {
            // Arrange
            var tagHelperContext = new TagHelperContext(
                "head",
                allAttributes: new TagHelperAttributeList(
                    Enumerable.Empty <TagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var incrementer = 0;
            var testTagHelperComponentManager = new TagHelperComponentManager(new[]
            {
                new CallbackTagHelperComponent(
                    order: 2,
                    initCallback: () =>
                {
                    Assert.Equal(1, incrementer);
                    incrementer++;
                },
                    processAsyncCallback: null),
                new CallbackTagHelperComponent(
                    order: 3,
                    initCallback: () =>
                {
                    Assert.Equal(2, incrementer);
                    incrementer++;
                },
                    processAsyncCallback: null),
                new CallbackTagHelperComponent(
                    order: 1,
                    initCallback: () =>
                {
                    Assert.Equal(0, incrementer);
                    incrementer++;
                },
                    processAsyncCallback: null),
            });

            var testTagHelperComponentTagHelper = new TestTagHelperComponentTagHelper(testTagHelperComponentManager, NullLoggerFactory.Instance);

            // Act
            testTagHelperComponentTagHelper.Init(tagHelperContext);

            // Assert
            Assert.Equal(3, incrementer);
        }