@@ -14,14 +14,14 @@ namespace Microsoft.AspNet.Routing
1414 public class ConstraintMatcherTest
1515 {
1616#if ASPNET50
17+ private const string _name = "name" ;
18+
1719 [ Fact ]
1820 public void MatchUrlGeneration_DoesNotLogData ( )
1921 {
2022 // Arrange
21- var name = "name" ;
22-
2323 var sink = new TestSink ( ) ;
24- var logger = new TestLogger ( name , sink ) ;
24+ var logger = new TestLogger ( _name , sink , enabled : true ) ;
2525
2626 var routeValueDictionary = new RouteValueDictionary ( new { a = "value" , b = "value" } ) ;
2727 var constraints = new Dictionary < string , IRouteConstraint >
@@ -41,62 +41,40 @@ public void MatchUrlGeneration_DoesNotLogData()
4141
4242 // Assert
4343 // There are no BeginScopes called.
44- Assert . Equal ( 0 , sink . Scopes . Count ) ;
44+ Assert . Empty ( sink . Scopes ) ;
4545
4646 // There are no WriteCores called.
47- Assert . Equal ( 0 , sink . Writes . Count ) ;
47+ Assert . Empty ( sink . Writes ) ;
4848 }
4949
5050 [ Fact ]
5151 public void MatchFail_LogsCorrectData ( )
5252 {
53- // Arrange
54- var name = "name" ;
55-
56- var sink = new TestSink ( ) ;
57- var logger = new TestLogger ( name , sink ) ;
58-
59- var routeValueDictionary = new RouteValueDictionary ( new { a = "value" , b = "value" } ) ;
53+ // Arrange & Act
6054 var constraints = new Dictionary < string , IRouteConstraint >
6155 {
6256 { "a" , new PassConstraint ( ) } ,
6357 { "b" , new FailConstraint ( ) }
6458 } ;
65-
66- // Act
67- RouteConstraintMatcher . Match (
68- constraints : constraints ,
69- routeValues : routeValueDictionary ,
70- httpContext : new Mock < HttpContext > ( ) . Object ,
71- route : new Mock < IRouter > ( ) . Object ,
72- routeDirection : RouteDirection . IncomingRequest ,
73- logger : logger ) ;
59+ var sink = SetUpMatch ( constraints , true ) ;
7460
7561 // Assert
7662 // There are no begin scopes called.
77- Assert . Equal ( 0 , sink . Scopes . Count ) ;
78-
79- // There are two records for IsEnabled and two for WriteCore.
80- Assert . Equal ( 4 , sink . Writes . Count ) ;
63+ Assert . Empty ( sink . Scopes ) ;
8164
82- var enabled = sink . Writes [ 0 ] ;
83- Assert . Equal ( name , enabled . LoggerName ) ;
84- Assert . Null ( enabled . State ) ;
65+ // There are two records for WriteCore.
66+ Assert . Equal ( 2 , sink . Writes . Count ) ;
8567
86- var write = sink . Writes [ 1 ] ;
87- Assert . Equal ( name , write . LoggerName ) ;
68+ var write = sink . Writes [ 0 ] ;
69+ Assert . Equal ( _name , write . LoggerName ) ;
8870 var values = Assert . IsType < RouteConstraintMatcherMatchValues > ( write . State ) ;
8971 Assert . Equal ( "RouteConstraintMatcher.Match" , values . Name ) ;
9072 Assert . Equal ( "a" , values . ConstraintKey ) ;
9173 Assert . Equal ( constraints [ "a" ] , values . Constraint ) ;
9274 Assert . Equal ( true , values . Matched ) ;
9375
94- enabled = sink . Writes [ 2 ] ;
95- Assert . Equal ( name , enabled . LoggerName ) ;
96- Assert . Null ( enabled . State ) ;
97-
98- write = sink . Writes [ 3 ] ;
99- Assert . Equal ( name , write . LoggerName ) ;
76+ write = sink . Writes [ 1 ] ;
77+ Assert . Equal ( _name , write . LoggerName ) ;
10078 values = Assert . IsType < RouteConstraintMatcherMatchValues > ( write . State ) ;
10179 Assert . Equal ( "RouteConstraintMatcher.Match" , values . Name ) ;
10280 Assert . Equal ( "b" , values . ConstraintKey ) ;
@@ -105,62 +83,78 @@ public void MatchFail_LogsCorrectData()
10583 }
10684
10785 [ Fact ]
108- public void MatchSuccess_LogsCorrectData ( )
86+ public void MatchFail_DisabledLoggerDoesNotLog ( )
10987 {
110- // Arrange
111- var name = "name" ;
88+ // Arrange & Act
89+ var constraints = new Dictionary < string , IRouteConstraint >
90+ {
91+ { "a" , new PassConstraint ( ) } ,
92+ { "b" , new FailConstraint ( ) }
93+ } ;
94+ var sink = SetUpMatch ( constraints , false ) ;
11295
113- var sink = new TestSink ( ) ;
114- var logger = new TestLogger ( name , sink ) ;
96+ // Assert
97+ // There are no begin scopes called.
98+ Assert . Empty ( sink . Scopes ) ;
11599
116- var routeValueDictionary = new RouteValueDictionary ( new { a = "value" , b = "value" } ) ;
100+ // Logger is disabled so it should not write
101+ Assert . Empty ( sink . Writes ) ;
102+ }
103+
104+ [ Fact ]
105+ public void MatchSuccess_LogsCorrectData ( )
106+ {
107+ // Arrange & Act
117108 var constraints = new Dictionary < string , IRouteConstraint >
118109 {
119110 { "a" , new PassConstraint ( ) } ,
120111 { "b" , new PassConstraint ( ) }
121112 } ;
122-
123- // Act
124- RouteConstraintMatcher . Match (
125- constraints : constraints ,
126- routeValues : routeValueDictionary ,
127- httpContext : new Mock < HttpContext > ( ) . Object ,
128- route : new Mock < IRouter > ( ) . Object ,
129- routeDirection : RouteDirection . IncomingRequest ,
130- logger : logger ) ;
113+ var sink = SetUpMatch ( constraints , true ) ;
131114
132115 // Assert
133116 // There are no begin scopes called.
134- Assert . Equal ( 0 , sink . Scopes . Count ) ;
135-
136- // There are two records for IsEnabled and two for WriteCore.
137- Assert . Equal ( 4 , sink . Writes . Count ) ;
117+ Assert . Empty ( sink . Scopes ) ;
138118
139- var enabled = sink . Writes [ 0 ] ;
140- Assert . Equal ( name , enabled . LoggerName ) ;
141- Assert . Null ( enabled . State ) ;
119+ // There are two records WriteCore.
120+ Assert . Equal ( 2 , sink . Writes . Count ) ;
142121
143- var write = sink . Writes [ 1 ] ;
144- Assert . Equal ( name , write . LoggerName ) ;
122+ var write = sink . Writes [ 0 ] ;
123+ Assert . Equal ( _name , write . LoggerName ) ;
145124 var values = Assert . IsType < RouteConstraintMatcherMatchValues > ( write . State ) ;
146125 Assert . Equal ( "RouteConstraintMatcher.Match" , values . Name ) ;
147126 Assert . Equal ( "a" , values . ConstraintKey ) ;
148127 Assert . Equal ( constraints [ "a" ] , values . Constraint ) ;
149128 Assert . Equal ( true , values . Matched ) ;
150129
151- enabled = sink . Writes [ 2 ] ;
152- Assert . Equal ( name , enabled . LoggerName ) ;
153- Assert . Null ( enabled . State ) ;
154-
155- write = sink . Writes [ 3 ] ;
156- Assert . Equal ( name , write . LoggerName ) ;
130+ write = sink . Writes [ 1 ] ;
131+ Assert . Equal ( _name , write . LoggerName ) ;
157132 values = Assert . IsType < RouteConstraintMatcherMatchValues > ( write . State ) ;
158133 Assert . Equal ( "RouteConstraintMatcher.Match" , values . Name ) ;
159134 Assert . Equal ( "b" , values . ConstraintKey ) ;
160135 Assert . Equal ( constraints [ "b" ] , values . Constraint ) ;
161136 Assert . Equal ( true , values . Matched ) ;
162137 }
163138
139+ [ Fact ]
140+ public void MatchSuccess_DisabledLoggerDoesNotLog ( )
141+ {
142+ // Arrange & Act
143+ var constraints = new Dictionary < string , IRouteConstraint >
144+ {
145+ { "a" , new PassConstraint ( ) } ,
146+ { "b" , new PassConstraint ( ) }
147+ } ;
148+ var sink = SetUpMatch ( constraints , false ) ;
149+
150+ // Assert
151+ // There are no begin scopes called.
152+ Assert . Empty ( sink . Scopes ) ;
153+
154+ // Disabled Logger should not write
155+ Assert . Empty ( sink . Writes ) ;
156+ }
157+
164158 [ Fact ]
165159 public void ReturnsTrueOnValidConstraints ( )
166160 {
@@ -272,6 +266,25 @@ public void ReturnsTrueOnNullInput()
272266 routeDirection : RouteDirection . IncomingRequest ,
273267 logger : NullLogger . Instance ) ) ;
274268 }
269+
270+ private TestSink SetUpMatch ( Dictionary < string , IRouteConstraint > constraints , bool enabled )
271+ {
272+ // Arrange
273+ var sink = new TestSink ( ) ;
274+ var logger = new TestLogger ( _name , sink , enabled ) ;
275+
276+ var routeValueDictionary = new RouteValueDictionary ( new { a = "value" , b = "value" } ) ;
277+
278+ // Act
279+ RouteConstraintMatcher . Match (
280+ constraints : constraints ,
281+ routeValues : routeValueDictionary ,
282+ httpContext : new Mock < HttpContext > ( ) . Object ,
283+ route : new Mock < IRouter > ( ) . Object ,
284+ routeDirection : RouteDirection . IncomingRequest ,
285+ logger : logger ) ;
286+ return sink ;
287+ }
275288#endif
276289
277290 private class PassConstraint : IRouteConstraint
0 commit comments