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

Commit 92843b1

Browse files
committed
Added unit tests
- #1553
1 parent b3cf4a3 commit 92843b1

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

samples/TagHelperSample.Web/Views/Home/Index.cshtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
@using TagHelperSample.Web.Models
33
@model IList<User>
44

5+
<environment names="development">
6+
Hello, you're in Development
7+
</environment>
8+
59
<h2>Index</h2>
610
@if (Model != null && Model.Count() != 0)
711
{

src/Microsoft.AspNet.Mvc.TagHelpers/EnvironmentTagHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class EnvironmentTagHelper : TagHelper
2424
// Protected to ensure subclasses are correctly activated. Internal for ease of use when testing.
2525
[Activate]
2626
protected internal IHostingEnvironment HostingEnvironment { get; set; }
27-
27+
2828
public override void Process(TagHelperContext context, TagHelperOutput output)
2929
{
3030
// Always strip the outer tag name as we never want <environment> to render
@@ -36,7 +36,9 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
3636
return;
3737
}
3838

39-
var environments = Names.Split(_nameSeparator, StringSplitOptions.RemoveEmptyEntries);
39+
var environments = Names.Split(_nameSeparator, StringSplitOptions.RemoveEmptyEntries)
40+
.Where(name => !string.IsNullOrWhiteSpace(name))
41+
.ToArray();
4042

4143
if (environments.Length == 0)
4244
{
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNet.Hosting;
8+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
9+
using Moq;
10+
using Xunit;
11+
12+
namespace Microsoft.AspNet.Mvc.TagHelpers.Test
13+
{
14+
public class EnvironmentTagHelperTest
15+
{
16+
[Theory]
17+
[InlineData("Development")]
18+
[InlineData("development")]
19+
[InlineData("DEVELOPMENT")]
20+
[InlineData(" development")]
21+
[InlineData("development ")]
22+
[InlineData(" development ")]
23+
[InlineData("Development,Production")]
24+
[InlineData("Production,Development")]
25+
[InlineData("Development , Production")]
26+
[InlineData(" Development,Production ")]
27+
[InlineData("Development,Staging,Production")]
28+
[InlineData("Staging,Development,Production")]
29+
[InlineData("Staging,Production,Development")]
30+
public void ShowsContentWhenCurrentEnvironmentIsSpecified(string namesAttribute)
31+
{
32+
ShouldShowContent(namesAttribute);
33+
}
34+
35+
[Theory]
36+
[InlineData("")]
37+
[InlineData(null)]
38+
[InlineData(" ")]
39+
[InlineData(", ")]
40+
[InlineData(" , ")]
41+
[InlineData(",")]
42+
[InlineData(",,")]
43+
[InlineData(",,,")]
44+
[InlineData(",,, ")]
45+
public void ShowsContentWhenNoEnvironmentIsSpecified(string namesAttribute)
46+
{
47+
ShouldShowContent(namesAttribute);
48+
}
49+
50+
[Theory]
51+
[InlineData("NotDevelopment")]
52+
[InlineData("NOTDEVELOPMENT")]
53+
[InlineData("NotDevelopment,AlsoNotDevelopment")]
54+
[InlineData("Doesn'tMatchAtAll")]
55+
[InlineData("Development and a space")]
56+
[InlineData("Development and a space,SomethingElse")]
57+
public void DoesNotShowContentWhenCurrentEnvironmentIsNotSpecified(string namesAttribute)
58+
{
59+
var content = "content";
60+
var context = MakeTagHelperContext(
61+
attributes: new Dictionary<string, object> { { "names", namesAttribute } },
62+
content: content);
63+
var output = MakeTagHelperOutput("environment");
64+
var hostingEnvironment = new Mock<IHostingEnvironment>();
65+
hostingEnvironment.SetupProperty(h => h.EnvironmentName);
66+
hostingEnvironment.Object.EnvironmentName = "Development";
67+
68+
var helper = new EnvironmentTagHelper
69+
{
70+
HostingEnvironment = hostingEnvironment.Object,
71+
Names = namesAttribute
72+
};
73+
helper.Process(context, output);
74+
75+
Assert.Null(output.TagName);
76+
Assert.Null(output.PreContent);
77+
Assert.Null(output.Content);
78+
Assert.Null(output.PostContent);
79+
Assert.True(output.ContentSet);
80+
}
81+
82+
private void ShouldShowContent(string namesAttribute)
83+
{
84+
var content = "content";
85+
var context = MakeTagHelperContext(
86+
attributes: new Dictionary<string, object> { { "names", namesAttribute } },
87+
content: content);
88+
var output = MakeTagHelperOutput("environment");
89+
var hostingEnvironment = new Mock<IHostingEnvironment>();
90+
hostingEnvironment.SetupProperty(h => h.EnvironmentName);
91+
hostingEnvironment.Object.EnvironmentName = "Development";
92+
93+
var helper = new EnvironmentTagHelper
94+
{
95+
HostingEnvironment = hostingEnvironment.Object,
96+
Names = namesAttribute
97+
};
98+
helper.Process(context, output);
99+
100+
Assert.Null(output.TagName);
101+
Assert.False(output.ContentSet);
102+
}
103+
104+
private TagHelperContext MakeTagHelperContext(IDictionary<string, object> attributes = null, string content = null)
105+
{
106+
if (attributes == null)
107+
{
108+
attributes = new Dictionary<string, object>();
109+
}
110+
111+
return new TagHelperContext(attributes, Guid.NewGuid().ToString("N"), () => Task.FromResult(content));
112+
}
113+
114+
private TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary<string, string> attributes = null)
115+
{
116+
if (attributes == null)
117+
{
118+
attributes = new Dictionary<string, string>();
119+
}
120+
121+
return new TagHelperOutput(tagName, attributes);
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)