Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianEdwards committed Jan 16, 2015
1 parent d968d06 commit ab6690a
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
4 changes: 4 additions & 0 deletions samples/TagHelperSample.Web/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@using TagHelperSample.Web.Models
@model IList<User>

<environment names="development">
Hello, you're in Development
</environment>

<h2>Index</h2>
@if (Model != null && Model.Count() != 0)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class EnvironmentTagHelper : TagHelper
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing.
[Activate]
protected internal IHostingEnvironment HostingEnvironment { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
// Always strip the outer tag name as we never want <environment> to render
Expand All @@ -36,7 +36,9 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
return;
}

var environments = Names.Split(_nameSeparator, StringSplitOptions.RemoveEmptyEntries);
var environments = Names.Split(_nameSeparator, StringSplitOptions.RemoveEmptyEntries)
.Where(name => !string.IsNullOrWhiteSpace(name))
.ToArray();

if (environments.Length == 0)
{
Expand Down
124 changes: 124 additions & 0 deletions test/Microsoft.AspNet.Mvc.TagHelpers.Test/EnvironmentTagHelperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 System.Threading.Tasks;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using Moq;
using Xunit;

namespace Microsoft.AspNet.Mvc.TagHelpers.Test
{
public class EnvironmentTagHelperTest
{
[Theory]
[InlineData("Development")]
[InlineData("development")]
[InlineData("DEVELOPMENT")]
[InlineData(" development")]
[InlineData("development ")]
[InlineData(" development ")]
[InlineData("Development,Production")]
[InlineData("Production,Development")]
[InlineData("Development , Production")]
[InlineData(" Development,Production ")]
[InlineData("Development,Staging,Production")]
[InlineData("Staging,Development,Production")]
[InlineData("Staging,Production,Development")]
public void ShowsContentWhenCurrentEnvironmentIsSpecified(string namesAttribute)
{
ShouldShowContent(namesAttribute);
}

[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData(" ")]
[InlineData(", ")]
[InlineData(" , ")]
[InlineData(",")]
[InlineData(",,")]
[InlineData(",,,")]
[InlineData(",,, ")]
public void ShowsContentWhenNoEnvironmentIsSpecified(string namesAttribute)
{
ShouldShowContent(namesAttribute);
}

[Theory]
[InlineData("NotDevelopment")]
[InlineData("NOTDEVELOPMENT")]
[InlineData("NotDevelopment,AlsoNotDevelopment")]
[InlineData("Doesn'tMatchAtAll")]
[InlineData("Development and a space")]
[InlineData("Development and a space,SomethingElse")]
public void DoesNotShowContentWhenCurrentEnvironmentIsNotSpecified(string namesAttribute)
{
var content = "content";
var context = MakeTagHelperContext(
attributes: new Dictionary<string, object> { { "names", namesAttribute } },
content: content);
var output = MakeTagHelperOutput("environment");
var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName);
hostingEnvironment.Object.EnvironmentName = "Development";

var helper = new EnvironmentTagHelper
{
HostingEnvironment = hostingEnvironment.Object,
Names = namesAttribute
};
helper.Process(context, output);

Assert.Null(output.TagName);
Assert.Null(output.PreContent);
Assert.Null(output.Content);
Assert.Null(output.PostContent);
Assert.True(output.ContentSet);
}

private void ShouldShowContent(string namesAttribute)
{
var content = "content";
var context = MakeTagHelperContext(
attributes: new Dictionary<string, object> { { "names", namesAttribute } },
content: content);
var output = MakeTagHelperOutput("environment");
var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName);
hostingEnvironment.Object.EnvironmentName = "Development";

var helper = new EnvironmentTagHelper
{
HostingEnvironment = hostingEnvironment.Object,
Names = namesAttribute
};
helper.Process(context, output);

Assert.Null(output.TagName);
Assert.False(output.ContentSet);
}

private TagHelperContext MakeTagHelperContext(IDictionary<string, object> attributes = null, string content = null)
{
if (attributes == null)
{
attributes = new Dictionary<string, object>();
}

return new TagHelperContext(attributes, Guid.NewGuid().ToString("N"), () => Task.FromResult(content));
}

private TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary<string, string> attributes = null)
{
if (attributes == null)
{
attributes = new Dictionary<string, string>();
}

return new TagHelperOutput(tagName, attributes);
}
}
}

0 comments on commit ab6690a

Please sign in to comment.