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 void Init_LogsTagHelperComponentInitialized()
        {
            // 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
            testTagHelperComponentTagHelper.Init(tagHelperContext);

            // Assert
            Assert.Equal($"Tag helper component '{typeof(TestTagHelperComponent)}' initialized.", sink.Writes[0].State.ToString(), StringComparer.Ordinal);
        }
        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);
        }