public void Activate_InitializesTagHelpersWithMultipleInitializers()
        {
            // Arrange
            var services = new ServiceCollection();
            var builder = new MvcCoreBuilder(services);
            builder.InitializeTagHelper<TestTagHelper>((h, vc) =>
            {
                h.Name = "Test 1";
                h.Number = 100;
            });
            builder.InitializeTagHelper<TestTagHelper>((h, vc) =>
            {
                h.Name += ", Test 2";
                h.Number += 100;
            });
            var httpContext = MakeHttpContext(services.BuildServiceProvider());
            var viewContext = MakeViewContext(httpContext);
            var activator = new DefaultTagHelperActivator();
            var helper = new TestTagHelper();

            // Act
            activator.Activate(helper, viewContext);

            // Assert
            Assert.Equal("Test 1, Test 2", helper.Name);
            Assert.Equal(200, helper.Number);
        }
        public void Activate_InitializesTagHelpers(string name, int number)
        {
            // Arrange
            var services = new ServiceCollection();
            var builder = new MvcCoreBuilder(services);
            builder.InitializeTagHelper<TestTagHelper>((h, vc) =>
            {
                h.Name = name;
                h.Number = number;
                h.ViewDataValue = vc.ViewData["TestData"];
            });
            var httpContext = MakeHttpContext(services.BuildServiceProvider());
            var viewContext = MakeViewContext(httpContext);
            var viewDataValue = new object();
            viewContext.ViewData.Add("TestData", viewDataValue);
            var activator = new DefaultTagHelperActivator();
            var helper = new TestTagHelper();

            // Act
            activator.Activate(helper, viewContext);

            // Assert
            Assert.Equal(name, helper.Name);
            Assert.Equal(number, helper.Number);
            Assert.Same(viewDataValue, helper.ViewDataValue);
        }
        public void Activate_InitializesTagHelpersAfterActivatingProperties()
        {
            // Arrange
            var services = new ServiceCollection();
            var builder = new MvcCoreBuilder(services);
            builder.InitializeTagHelper<TestTagHelper>((h, _) => h.ViewContext = MakeViewContext(MakeHttpContext()));
            var httpContext = MakeHttpContext(services.BuildServiceProvider());
            var viewContext = MakeViewContext(httpContext);
            var activator = new DefaultTagHelperActivator();
            var helper = new TestTagHelper();

            // Act
            activator.Activate(helper, viewContext);

            // Assert
            Assert.NotSame(viewContext, helper.ViewContext);
        }
        public async Task FormTagHelper_GeneratesExpectedContent(bool? optionsAntiforgery)
        {
            // Arrange
            var newServices = new ServiceCollection();
            var builder = new MvcCoreBuilder(newServices);
            builder.InitializeTagHelper<FormTagHelper>((helper, _) => helper.Antiforgery = optionsAntiforgery);
            var server = TestHelper.CreateServer(_app, SiteName,
                services =>
                {
                    services.Add(newServices);
                    _configureServices(services);
                });
            var client = server.CreateClient();
            var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");

            var outputFile = string.Format(
                "compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.Options.Antiforgery.{0}.html",
                optionsAntiforgery?.ToString() ?? "null");
            var expectedContent =
                await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);

            // Act
            // The host is not important as everything runs in memory and tests are isolated from each other.
            var response = await client.GetAsync("http://localhost/HtmlGeneration_Home/Form");
            var responseContent = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);

            responseContent = responseContent.Trim();
            var forgeryTokens = AntiforgeryTestHelper.RetrieveAntiforgeryTokens(responseContent).ToArray();

#if GENERATE_BASELINES
            // Reverse usual substitutions and insert format items into the new file content.
            for (var index = 0; index < forgeryTokens.Length; index++)
            {
                responseContent = responseContent.Replace(forgeryTokens[index], $"{{{ index }}}");
            }

            ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
            expectedContent = string.Format(expectedContent, forgeryTokens);
            Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
#endif
        }
        public void Activate_InitializesTagHelpersWithCorrectInitializers()
        {
            // Arrange
            var services = new ServiceCollection();
            var builder = new MvcCoreBuilder(services);
            builder.InitializeTagHelper<TestTagHelper>((h, vc) =>
            {
                h.Name = "Test 1";
                h.Number = 100;
            });
            builder.InitializeTagHelper<AnotherTestTagHelper>((h, vc) =>
            {
                h.Name = "Test 2";
                h.Number = 102;
            });
            var httpContext = MakeHttpContext(services.BuildServiceProvider());
            var viewContext = MakeViewContext(httpContext);
            var activator = new DefaultTagHelperActivator();
            var testTagHelper = new TestTagHelper();
            var anotherTestTagHelper = new AnotherTestTagHelper();

            // Act
            activator.Activate(testTagHelper, viewContext);
            activator.Activate(anotherTestTagHelper, viewContext);

            // Assert
            Assert.Equal("Test 1", testTagHelper.Name);
            Assert.Equal(100, testTagHelper.Number);
            Assert.Equal("Test 2", anotherTestTagHelper.Name);
            Assert.Equal(102, anotherTestTagHelper.Number);
        }