From c87de5a0fc3ef6bd356aeb16d83e406b1abab9ff Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Thu, 2 Oct 2014 17:50:58 -0700 Subject: [PATCH] Add TagHelper functional test. - Added an end-to-end test that verifies all content behaviors, interactions and functionalities of tag helpers. - Added some common user scenarios to verify that the system works how we expect. #1116 --- Mvc.sln | 13 ++++ .../TagHelpersTest.cs | 55 ++++++++++++++++ .../TagHelpersWebSite.Home.About.html | 53 +++++++++++++++ .../TagHelpersWebSite.Home.Help.html | 45 +++++++++++++ .../TagHelpersWebSite.Home.Index.html | 65 +++++++++++++++++++ .../project.json | 1 + .../Controllers/HomeController.cs | 33 ++++++++++ .../Models/WebsiteContext.cs | 18 +++++ test/WebSites/TagHelpersWebSite/Startup.cs | 23 +++++++ .../TagHelpers/ATagHelper.cs | 39 +++++++++++ .../TagHelpers/AutoLinkerTagHelper.cs | 23 +++++++ .../TagHelpers/ConditionTagHelper.cs | 25 +++++++ .../TagHelpers/PrettyTagHelper.cs | 57 ++++++++++++++++ .../TagCloudViewComponentTagHelper.cs | 64 ++++++++++++++++++ .../TagHelpers/WebsiteInformationTagHelper.cs | 30 +++++++++ .../TagHelpersWebSite/TagHelpersWebSite.kproj | 21 ++++++ .../TagHelpersWebSite/Views/Home/About.cshtml | 19 ++++++ .../TagHelpersWebSite/Views/Home/Help.cshtml | 9 +++ .../TagHelpersWebSite/Views/Home/Index.cshtml | 32 +++++++++ .../Views/Shared/_Layout.cshtml | 28 ++++++++ .../TagHelpersWebSite/Views/_ViewStart.cshtml | 3 + test/WebSites/TagHelpersWebSite/project.json | 11 ++++ 22 files changed, 667 insertions(+) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html create mode 100644 test/WebSites/TagHelpersWebSite/Controllers/HomeController.cs create mode 100644 test/WebSites/TagHelpersWebSite/Models/WebsiteContext.cs create mode 100644 test/WebSites/TagHelpersWebSite/Startup.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpers/ATagHelper.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpers/AutoLinkerTagHelper.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpers/ConditionTagHelper.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpers/WebsiteInformationTagHelper.cs create mode 100644 test/WebSites/TagHelpersWebSite/TagHelpersWebSite.kproj create mode 100644 test/WebSites/TagHelpersWebSite/Views/Home/About.cshtml create mode 100644 test/WebSites/TagHelpersWebSite/Views/Home/Help.cshtml create mode 100644 test/WebSites/TagHelpersWebSite/Views/Home/Index.cshtml create mode 100644 test/WebSites/TagHelpersWebSite/Views/Shared/_Layout.cshtml create mode 100644 test/WebSites/TagHelpersWebSite/Views/_ViewStart.cshtml create mode 100644 test/WebSites/TagHelpersWebSite/project.json diff --git a/Mvc.sln b/Mvc.sln index ab9caeeb12..0285f207dd 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -83,6 +83,8 @@ EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApiExplorerWebSite", "test\WebSites\ApiExplorerWebSite\ApiExplorerWebSite.kproj", "{61061528-071E-424E-965A-07BCC2F02672}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "VersioningWebSite", "test\WebSites\VersioningWebSite\VersioningWebSite.kproj", "{C6304029-78C8-4604-99BE-2078DCA1DD36}" +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ReflectedModelWebSite", "test\WebSites\ReflectedModelWebSite\ReflectedModelWebSite.kproj", "{C2EF54F8-8886-4260-A322-44F76245F95D}" +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelpersWebSite", "test\WebSites\TagHelpersWebSite\TagHelpersWebSite.kproj", "{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FilesWebSite", "test\WebSites\FilesWebSite\FilesWebSite.kproj", "{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}" EndProject @@ -470,6 +472,16 @@ Global {CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.Build.0 = Release|Any CPU {CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|x86.ActiveCfg = Release|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|x86.ActiveCfg = Debug|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.Build.0 = Release|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -513,5 +525,6 @@ Global {0EF9860B-10D7-452F-B0F4-A405B88BEBB3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {2B2B9876-903C-4065-8D62-2EE832BBA106} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {CAE52CB7-0FAC-4B5B-8251-B0FF837DB657} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} + {6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} EndGlobalSection EndGlobal diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs new file mode 100644 index 0000000000..962755d3f0 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/TagHelpersTest.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Net; +using System.Net.Http.Headers; +using System.Reflection; +using System.Threading.Tasks; +using BasicWebSite; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.TestHost; +using Xunit; + +namespace Microsoft.AspNet.Mvc.FunctionalTests +{ + public class TagHelpersTests + { + private readonly IServiceProvider _provider = TestHelper.CreateServices("TagHelpersWebSite"); + private readonly Action _app = new Startup().Configure; + + // Some tests require comparing the actual response body against an expected response baseline + // so they require a reference to the assembly on which the resources are located, in order to + // make the tests less verbose, we get a reference to the assembly with the resources and we + // use it on all the rest of the tests. + private readonly Assembly _resourcesAssembly = typeof(TagHelpersTests).GetTypeInfo().Assembly; + + [Theory] + [InlineData("Index")] + [InlineData("About")] + [InlineData("Help")] + public async Task CanRenderViewsWithTagHelpers(string action) + { + // Arrange + var server = TestServer.Create(_provider, _app); + var client = server.CreateClient(); + var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8"); + + // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name + // as the file name, in order to update a baseline you just need to change the file in that folder. + var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( + "compiler/resources/TagHelpersWebSite.Home." + action + ".html"); + + // 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/Home/" + action); + var responseContent = await response.Content.ReadAsStringAsync(); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expectedMediaType, response.Content.Headers.ContentType); + Assert.Equal(expectedContent, responseContent); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html new file mode 100644 index 0000000000..9b9a96357b --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.About.html @@ -0,0 +1,53 @@ + + + + + + + + About - My ASP.NET Application + + + + +

ASP.NET vNext - About

+

| My Home + | My About + | My Help |

+
+ + + + +
+

Hello, you've reached the about page.

+ +

Information about our website (outdated):

+

Version: 1.1

+

Copyright Year: 1990

+

Approved: True

+

Number of tags to show: 30

+
+
+
+
+ +
+
+ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html new file mode 100644 index 0000000000..6b4eb8c191 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Help.html @@ -0,0 +1,45 @@ + + + + + + + + Help - My ASP.NET Application + + + + +

ASP.NET vNext - Help

+

| My Home + | My About + | My Help |

+
+ + + +
+

Hello, you've reached the help page. If you're having troubles try visiting our My Approved Home Page

+
+
+
+ +
+
+ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html new file mode 100644 index 0000000000..ce0fcbfdf6 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/TagHelpersWebSite.Home.Index.html @@ -0,0 +1,65 @@ + + + + + + + + Home Page - My ASP.NET Application + + + + + + +

ASP.NET vNext - Home Page

+

| My Home + | My About + | My Help |

+
+ + + + +
+

This website has not been approved yet. Visit www.contoso.com for more information.

+
+ +
+

Current Tag Cloud from Tag Helper

+
["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore","et","dolore","magna","aliquaUt","enim"]
+

Current Tag Cloud from ViewComponentHelper:

+
["Lorem","ipsum","dolor","sit","amet","consectetur","adipisicing","elit","sed","do","eiusmod","tempor","incididunt","ut","labore"]
+
+ + +
+
+ + + +
+
+ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index fd90cc931c..df4cc05cb8 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -33,6 +33,7 @@ "Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Runtime.Interfaces": "1.0.0-*", "Microsoft.AspNet.PipelineCore": "1.0.0-*", + "TagHelpersWebSite": "1.0.0", "Xunit.KRunner": "1.0.0-*" }, "commands": { diff --git a/test/WebSites/TagHelpersWebSite/Controllers/HomeController.cs b/test/WebSites/TagHelpersWebSite/Controllers/HomeController.cs new file mode 100644 index 0000000000..11a64028cf --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Controllers/HomeController.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Mvc; +using TagHelpersWebSite.Models; + +namespace TagHelpersWebSite.Controllers +{ + public class HomeController : Controller + { + public IActionResult Index(bool approved = false) + { + return View(new WebsiteContext + { + Approved = approved, + CopyrightYear = 2015, + Version = new Version(1, 3, 3, 7), + TagsToShow = 20 + }); + } + + public IActionResult About() + { + return View(); + } + + public IActionResult Help() + { + return View(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Models/WebsiteContext.cs b/test/WebSites/TagHelpersWebSite/Models/WebsiteContext.cs new file mode 100644 index 0000000000..49b47b08d8 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Models/WebsiteContext.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace TagHelpersWebSite.Models +{ + public class WebsiteContext + { + public Version Version { get; set; } + + public int CopyrightYear { get; set; } + + public bool Approved { get; set; } + + public int TagsToShow { get; set; } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Startup.cs b/test/WebSites/TagHelpersWebSite/Startup.cs new file mode 100644 index 0000000000..6a967400cf --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Startup.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Builder; +using Microsoft.Framework.DependencyInjection; + +namespace TagHelpersWebSite +{ + public class Startup + { + public void Configure(IApplicationBuilder app) + { + var configuration = app.GetTestConfiguration(); + + app.UseServices(services => + { + services.AddMvc(configuration); + }); + + app.UseMvc(); + } + } +} diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/ATagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/ATagHelper.cs new file mode 100644 index 0000000000..13f807bbd1 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/ATagHelper.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Linq; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; + +namespace TagHelpersWebSite.TagHelpers +{ + [ContentBehavior(ContentBehavior.Prepend)] + public class ATagHelper : TagHelper + { + [Activate] + public IUrlHelper UrlHelper { get; set; } + + public string Controller { get; set; } + + public string Action { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if (Controller != null && Action != null) + { + var methodParameters = output.Attributes.ToDictionary(attribute => attribute.Key, + attribute => (object)attribute.Value); + + // We remove all attributes from the resulting HTML element because they're supposed to + // be parameters to our final href value. + output.Attributes.Clear(); + + output.Attributes["href"] = UrlHelper.Action(Action, Controller, methodParameters); + + output.Content = "My "; + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/AutoLinkerTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/AutoLinkerTagHelper.cs new file mode 100644 index 0000000000..0487a7b012 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/AutoLinkerTagHelper.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Text.RegularExpressions; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; + +namespace TagHelpersWebSite.TagHelpers +{ + [TagName("p")] + [ContentBehavior(ContentBehavior.Modify)] + public class AutoLinkerTagHelper : TagHelper + { + public override void Process(TagHelperContext context, TagHelperOutput output) + { + // Find Urls in the content and replace them with their anchor tag equivalent. + output.Content = Regex.Replace( + output.Content, + @"\b(?:https?://|www\.)(\S+)\b", + "$0"); + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/ConditionTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/ConditionTagHelper.cs new file mode 100644 index 0000000000..b9a0027c82 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/ConditionTagHelper.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; + +namespace TagHelpersWebSite.TagHelpers +{ + [TagName("div", "style", "p")] + [ContentBehavior(ContentBehavior.Modify)] + public class ConditionTagHelper : TagHelper + { + public bool? Condition { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + // If a condition is set and evaluates to false, don't render the tag. + if (Condition.HasValue && !Condition.Value) + { + output.TagName = null; + output.Content = null; + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs new file mode 100644 index 0000000000..b521b8d2d4 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; + +namespace TagHelpersWebSite.TagHelpers +{ + [TagName("*")] + public class PrettyTagHelper : TagHelper + { + private static readonly Dictionary PrettyTagStyles = + new Dictionary(StringComparer.OrdinalIgnoreCase) + { + { "a", @"background-color: gray; + color: white; + border-radius: 3px; + border: 1px solid black; + padding: 3px; + font-family: cursive;" }, + { "strong", @"font-size: 1.25em; + text-decoration: underline;" }, + { "h1", @"font-family: cursive;" }, + { "h3", @"font-family: cursive;" } + }; + + public bool? MakePretty { get; set; } + + [Activate] + public ViewContext ViewContext { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if (MakePretty.HasValue && !MakePretty.Value) + { + return; + } + + string prettyStyle; + + if (PrettyTagStyles.TryGetValue(output.TagName, out prettyStyle)) + { + var style = string.Empty; + + if (output.Attributes.TryGetValue("style", out style)) + { + style += ";"; + } + + output.Attributes["style"] = style + prettyStyle; + } + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs new file mode 100644 index 0000000000..20fdd79662 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/TagCloudViewComponentTagHelper.cs @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; + +namespace MvcSample.Web.Components +{ + [TagName("tag-cloud")] + [ViewComponent(Name = "Tags")] + [ContentBehavior(ContentBehavior.Replace)] + public class TagCloudViewComponentTagHelper : ITagHelper + { + private static readonly string[] Tags = + ("Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua" + + "Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat Duis aute irure " + + "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat" + + "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum") + .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) + .ToArray(); + + public int Count { get; set; } + + [Activate] + public ViewContext ViewContext { get; set; } + + public async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) + { + var result = await InvokeAsync(Count); + var writer = new StringWriter(); + + await result.ExecuteAsync( + new ViewComponentContext(typeof(TagCloudViewComponentTagHelper).GetTypeInfo(), + ViewContext, + writer)); + + output.TagName = null; + output.Content = writer.ToString(); + } + + public async Task InvokeAsync(int count) + { + var tags = await GetTagsAsync(count); + return new JsonViewComponentResult(tags); + } + + private Task GetTagsAsync(int count) + { + return Task.FromResult(GetTags(count)); + } + + private string[] GetTags(int count) + { + return Tags.Take(count).ToArray(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/WebsiteInformationTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/WebsiteInformationTagHelper.cs new file mode 100644 index 0000000000..c98e1f152a --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/WebsiteInformationTagHelper.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.AspNet.Razor.TagHelpers; +using TagHelpersWebSite.Models; + +namespace TagHelpersWebSite.TagHelpers +{ + [ContentBehavior(ContentBehavior.Append)] + public class WebsiteInformationTagHelper : TagHelper + { + public WebsiteContext Info { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + output.TagName = "section"; + output.Content = string.Format( + "

Version: {0}

" + Environment.NewLine + + "

Copyright Year: {1}

" + Environment.NewLine + + "

Approved: {2}

" + Environment.NewLine + + "

Number of tags to show: {3}

" + Environment.NewLine, + Info.Version.ToString(), + Info.CopyrightYear.ToString(), + Info.Approved.ToString(), + Info.TagsToShow.ToString()); + } + } +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/TagHelpersWebSite.kproj b/test/WebSites/TagHelpersWebSite/TagHelpersWebSite.kproj new file mode 100644 index 0000000000..3a55f65cb0 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/TagHelpersWebSite.kproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 6db9b8d0-80f7-4e70-bbb0-0b4c04d79a47 + Web + + + + + + + 2.0 + 45493 + + + \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Views/Home/About.cshtml b/test/WebSites/TagHelpersWebSite/Views/Home/About.cshtml new file mode 100644 index 0000000000..93f679a046 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Views/Home/About.cshtml @@ -0,0 +1,19 @@ +@using TagHelpersWebSite.Models + +@{ + ViewBag.Title = "About"; +} + +@addtaghelper "TagHelpersWebSite.TagHelpers.ATagHelper, TagHelpersWebSite" +@addtaghelper "TagHelpersWebSite.TagHelpers.WebsiteInformationTagHelper, TagHelpersWebSite" + +
+

Hello, you've reached the about page.

+ +

Information about our website (outdated):

+ +
\ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Views/Home/Help.cshtml b/test/WebSites/TagHelpersWebSite/Views/Home/Help.cshtml new file mode 100644 index 0000000000..4794c1d293 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Views/Home/Help.cshtml @@ -0,0 +1,9 @@ +@{ + ViewBag.Title = "Help"; +} + +@addtaghelper "TagHelpersWebSite.TagHelpers.ATagHelper, TagHelpersWebSite" + +
+

Hello, you've reached the help page. If you're having troubles try visiting our Approved Home Page

+
\ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Views/Home/Index.cshtml b/test/WebSites/TagHelpersWebSite/Views/Home/Index.cshtml new file mode 100644 index 0000000000..6bc1da9313 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Views/Home/Index.cshtml @@ -0,0 +1,32 @@ +@using TagHelpersWebSite.Models +@model WebsiteContext + +@{ + ViewBag.Title = "Home Page"; +} + +@addtaghelper "TagHelpersWebSite" + +@section css { + +} + +
+

This website has not been approved yet. Visit www.contoso.com for more information.

+
+ +
+

Current Tag Cloud from Tag Helper

+
+

Current Tag Cloud from ViewComponentHelper:

+
@await Component.InvokeAsync("Tags", 15)
+
+ +@section footerContent { +

© @Model.CopyrightYear - My ASP.NET Application

+} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Views/Shared/_Layout.cshtml b/test/WebSites/TagHelpersWebSite/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000000..6b418b769e --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Views/Shared/_Layout.cshtml @@ -0,0 +1,28 @@ +@using TagHelpersWebSite.Controllers + +@addtaghelper "TagHelpersWebSite.TagHelpers.ATagHelper, TagHelpersWebSite" +@addtaghelper "TagHelpersWebSite.TagHelpers.PrettyTagHelper, TagHelpersWebSite" + + + + + + + @ViewBag.Title - My ASP.NET Application + + @RenderSection("css", required: false) + + +

ASP.NET vNext - @ViewBag.Title

+

| Home + | About + | Help |

+
+ @RenderBody() +
+ +
+ + \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/Views/_ViewStart.cshtml b/test/WebSites/TagHelpersWebSite/Views/_ViewStart.cshtml new file mode 100644 index 0000000000..ab23e9a239 --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "/Views/Shared/_Layout.cshtml"; +} \ No newline at end of file diff --git a/test/WebSites/TagHelpersWebSite/project.json b/test/WebSites/TagHelpersWebSite/project.json new file mode 100644 index 0000000000..538c221b5c --- /dev/null +++ b/test/WebSites/TagHelpersWebSite/project.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "Microsoft.AspNet.Mvc": "", + "Microsoft.AspNet.Server.IIS": "1.0.0-*", + "Microsoft.AspNet.Mvc.TestConfiguration": "" + }, + "frameworks": { + "aspnet50": {}, + "aspnetcore50": {} + } +} \ No newline at end of file