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