@@ -33,9 +33,9 @@ public void FormatWithSingleSimpleToken()
3333 [ Test ]
3434 public void FormatWithMultipleTokensAndVerbatimText ( )
3535 {
36- var propertyObject = new { SomeProperty = "SomeValue " , AnotherProperty = "Other Value" } ;
36+ var propertyObject = new { SomeProperty = "AValue " , AnotherProperty = "Other Value" } ;
3737 const string target = "{SomeProperty} some text {AnotherProperty}" ;
38- const string expected = "SomeValue some text Other Value" ;
38+ const string expected = "AValue some text Other Value" ;
3939 var actual = target . FormatWith ( propertyObject , this . environment ) ;
4040 Assert . That ( actual , Is . EqualTo ( expected ) ) ;
4141 }
@@ -56,7 +56,7 @@ public void FormatWithEnvVarTokenWithFallback()
5656 {
5757 this . environment . SetEnvironmentVariable ( "GIT_VERSION_TEST_VAR" , "Env Var Value" ) ;
5858 var propertyObject = new { } ;
59- const string target = "{env:GIT_VERSION_TEST_VAR ?? fallback}" ;
59+ const string target = "{env:GIT_VERSION_TEST_VAR ?? \" fallback\" }" ;
6060 const string expected = "Env Var Value" ;
6161 var actual = target . FormatWith ( propertyObject , this . environment ) ;
6262 Assert . That ( actual , Is . EqualTo ( expected ) ) ;
@@ -67,7 +67,7 @@ public void FormatWithUnsetEnvVarToken_WithFallback()
6767 {
6868 this . environment . SetEnvironmentVariable ( "GIT_VERSION_UNSET_TEST_VAR" , null ) ;
6969 var propertyObject = new { } ;
70- const string target = "{env:GIT_VERSION_UNSET_TEST_VAR ?? fallback}" ;
70+ const string target = "{env:GIT_VERSION_UNSET_TEST_VAR ?? \" fallback\" }" ;
7171 const string expected = "fallback" ;
7272 var actual = target . FormatWith ( propertyObject , this . environment ) ;
7373 Assert . That ( actual , Is . EqualTo ( expected ) ) ;
@@ -98,44 +98,139 @@ public void FormatWithMultipleEnvVars()
9898 public void FormatWithMultipleEnvChars ( )
9999 {
100100 var propertyObject = new { } ;
101- //Test the greediness of the regex in matching env: char
102- const string target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ?? fallback}" ;
103- const string expected = "{env:env:GIT_VERSION_TEST_VAR_1} and fallback" ;
104- var actual = target . FormatWith ( propertyObject , this . environment ) ;
105- Assert . That ( actual , Is . EqualTo ( expected ) ) ;
101+ const string target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ?? \" fallback\" }" ;
102+ Assert . Throws < ArgumentException > ( ( ) => target . FormatWith ( propertyObject , this . environment ) ) ;
106103 }
107104
108105 [ Test ]
109106 public void FormatWithMultipleFallbackChars ( )
110107 {
111108 var propertyObject = new { } ;
112- //Test the greediness of the regex in matching env: and ?? chars
113- const string target = "{env:env:GIT_VERSION_TEST_VAR_1} and {env:DUMMY_VAR ??? fallback}" ;
114- var actual = target . FormatWith ( propertyObject , this . environment ) ;
115- Assert . That ( actual , Is . EqualTo ( target ) ) ;
109+ const string target = " and {env:DUMMY_VAR ??? \" fallback\" }" ;
110+ Assert . Throws < FormatException > ( ( ) => target . FormatWith ( propertyObject , this . environment ) ) ;
116111 }
117112
118113 [ Test ]
119114 public void FormatWithSingleFallbackChar ( )
120115 {
121- this . environment . SetEnvironmentVariable ( "DUMMY_ENV_VAR" , "Dummy-Val " ) ;
116+ this . environment . SetEnvironmentVariable ( "DUMMY_ENV_VAR" , "DummyVal " ) ;
122117 var propertyObject = new { } ;
123- //Test the sanity of the regex when there is a grammar mismatch
124- const string target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR??fallback}" ;
125- var actual = target . FormatWith ( propertyObject , this . environment ) ;
126- Assert . That ( actual , Is . EqualTo ( target ) ) ;
118+ const string target = "{en:DUMMY_ENV_VAR} and {env:DUMMY_ENV_VAR??\" fallback\" }" ;
119+ Assert . Throws < ArgumentException > ( ( ) => target . FormatWith ( propertyObject , this . environment ) ) ;
127120 }
128121
129122 [ Test ]
130- public void FormatWIthNullPropagationWithMultipleSpaces ( )
123+ public void FormatWithNullPropagationWithMultipleSpaces ( )
131124 {
132125 var propertyObject = new { SomeProperty = "Some Value" } ;
133- const string target = "{SomeProperty} and {env:DUMMY_ENV_VAR ?? fallback}" ;
126+ const string target = "{SomeProperty} and {env:DUMMY_ENV_VAR ?? \" fallback\" }" ;
134127 const string expected = "Some Value and fallback" ;
135128 var actual = target . FormatWith ( propertyObject , this . environment ) ;
136129 Assert . That ( actual , Is . EqualTo ( expected ) ) ;
137130 }
138131
132+ [ Test ]
133+ public void FormatWithMissingPropertyAndEnvFallback ( )
134+ {
135+ this . environment . SetEnvironmentVariable ( "DUMMY_ENV_VAR" , "Dummy-Value" ) ;
136+ var propertyObject = new { } ;
137+ const string target = "{SomeProperty ?? env:DUMMY_ENV_VAR}" ;
138+ const string expected = "Dummy-Value" ;
139+ var actual = target . FormatWith ( propertyObject , this . environment ) ;
140+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
141+ }
142+
143+ [ Test ]
144+ public void FormatWithMissingEnvAndPropertyFallback ( )
145+ {
146+ var propertyObject = new { SomeProperty = "Some Value" } ;
147+ const string target = "{env:DUMMY_ENV_VAR ?? SomeProperty}" ;
148+ const string expected = "Some Value" ;
149+ var actual = target . FormatWith ( propertyObject , this . environment ) ;
150+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
151+ }
152+
153+ [ Test ]
154+ public void FormatWithMultiplePropertiesAndNoFallback ( )
155+ {
156+ var propertyObject = new { } ;
157+ const string target = "{SomeProperty ?? SomeOtherProperty ?? MissingProp}" ;
158+ Assert . Throws < ArgumentException > ( ( ) => target . FormatWith ( propertyObject , this . environment ) ) ;
159+ }
160+
161+ [ Test ]
162+ public void FormatWithMultiplePropertiesAndQuotedFallback ( )
163+ {
164+ var propertyObject = new { } ;
165+ const string target = "{SomeProperty ?? SomeOtherProperty ?? \" fallback\" }" ;
166+ const string expected = "fallback" ;
167+ var actual = target . FormatWith ( propertyObject , this . environment ) ;
168+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
169+ }
170+
171+ [ Test ]
172+ public void FormatWithMultipleAvailablePropertiesAndFallback ( )
173+ {
174+ var propertyObject = new { SomeOtherProperty = "Some-Value" } ;
175+ const string target = "{SomeProperty ?? SomeOtherProperty ?? \" fallback\" }" ;
176+ const string expected = "Some-Value" ;
177+ var actual = target . FormatWith ( propertyObject , this . environment ) ;
178+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
179+ }
180+
181+ [ Test ]
182+ public void FormatWithMissingPropertiesAndIntegerFallback ( )
183+ {
184+ var propertyObject = new { } ;
185+ const string target = "{SomeProperty ?? SomeOtherProperty ?? 47}" ;
186+ const string expected = "47" ;
187+ var actual = target . FormatWith ( propertyObject , this . environment ) ;
188+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
189+ }
190+
191+ [ Test ]
192+ public void FormatWithPropertyAndEnvAndFormatters ( )
193+ {
194+ this . environment . SetEnvironmentVariable ( "DUMMY_ENV_VAR" , "DummyVal" ) ;
195+ var propertyObject = new { SomeProperty = "TheValue" } ;
196+ const string target = "{SomeProperty:l} and {env:DUMMY_ENV_VAR:l}" ;
197+ const string expected = "thevalue and dummyval" ;
198+ var actual = target . FormatWith ( propertyObject , this . environment ) ;
199+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
200+ }
201+
202+ [ TestCase ( "{env:VARIABLE ?? \" \" }" , null , null , "" ) ]
203+ [ TestCase ( "{env:MISSING ?? env:VARIABLE}" , "Var" , null , "Var" ) ]
204+ [ TestCase ( "{Property ?? \" \" }" , null , null , "" ) ]
205+ [ TestCase ( "{Property ?? 47}" , null , null , "47" ) ]
206+ [ TestCase ( "{Property ?? env:VARIABLE}" , null , "Branch" , "Branch" ) ]
207+ [ TestCase ( "{Property ?? env:VARIABLE ?? \" \" }" , null , null , "" ) ]
208+ [ TestCase ( "{Property ?? env:VARIABLE ?? 42}" , null , null , "42" ) ]
209+ public void FormatWith_EnvVarAndPropertyAndFallback_DoesNotThrow ( string input , string ? envVar , string ? property , string expected )
210+ {
211+ if ( envVar != null )
212+ {
213+ this . environment . SetEnvironmentVariable ( "VARIABLE" , envVar ) ;
214+ }
215+
216+ object propertyObject = property != null
217+ ? new { Property = property }
218+ : new { } ;
219+
220+ var actual = input . FormatWith ( propertyObject , this . environment ) ;
221+ Assert . That ( actual , Is . EqualTo ( expected ) ) ;
222+ }
223+
224+ [ TestCase ( "{env:VARIABLE}" ) ]
225+ [ TestCase ( "{Property}" ) ]
226+ [ TestCase ( "{Property ?? env:VARIABLE}" ) ]
227+ [ TestCase ( "{Property ?? Property}" ) ]
228+ public void FormatWith_MissingEnvVarOrPropertyAndNoFallback_Throws ( string input )
229+ {
230+ object propertyObject = new { } ;
231+ Assert . Throws < ArgumentException > ( ( ) => input . FormatWith ( propertyObject , this . environment ) ) ;
232+ }
233+
139234 [ Test ]
140235 public void FormatEnvVar_WithFallback_QuotedAndEmpty ( )
141236 {
@@ -186,7 +281,7 @@ public void FormatProperty_NullInteger()
186281 public void FormatProperty_String_WithFallback ( )
187282 {
188283 var propertyObject = new { Property = "Value" } ;
189- const string target = "{Property ?? fallback}" ;
284+ const string target = "{Property ?? \" fallback\" }" ;
190285 var actual = target . FormatWith ( propertyObject , this . environment ) ;
191286 Assert . That ( actual , Is . EqualTo ( "Value" ) ) ;
192287 }
@@ -195,7 +290,7 @@ public void FormatProperty_String_WithFallback()
195290 public void FormatProperty_Integer_WithFallback ( )
196291 {
197292 var propertyObject = new { Property = 42 } ;
198- const string target = "{Property ?? fallback}" ;
293+ const string target = "{Property ?? \" fallback\" }" ;
199294 var actual = target . FormatWith ( propertyObject , this . environment ) ;
200295 Assert . That ( actual , Is . EqualTo ( "42" ) ) ;
201296 }
@@ -204,7 +299,7 @@ public void FormatProperty_Integer_WithFallback()
204299 public void FormatProperty_NullObject_WithFallback ( )
205300 {
206301 var propertyObject = new { Property = ( object ? ) null } ;
207- const string target = "{Property ?? fallback}" ;
302+ const string target = "{Property ?? \" fallback\" }" ;
208303 var actual = target . FormatWith ( propertyObject , this . environment ) ;
209304 Assert . That ( actual , Is . EqualTo ( "fallback" ) ) ;
210305 }
@@ -213,18 +308,18 @@ public void FormatProperty_NullObject_WithFallback()
213308 public void FormatProperty_NullInteger_WithFallback ( )
214309 {
215310 var propertyObject = new { Property = ( int ? ) null } ;
216- const string target = "{Property ?? fallback }" ;
311+ const string target = "{Property ?? 43 }" ;
217312 var actual = target . FormatWith ( propertyObject , this . environment ) ;
218- Assert . That ( actual , Is . EqualTo ( "fallback " ) ) ;
313+ Assert . That ( actual , Is . EqualTo ( "43 " ) ) ;
219314 }
220315
221316 [ Test ]
222317 public void FormatProperty_NullObject_WithFallback_Quoted ( )
223318 {
224319 var propertyObject = new { Property = ( object ? ) null } ;
225- const string target = "{Property ?? \" fallback \" }" ;
320+ const string target = "{Property ?? \" literal \" }" ;
226321 var actual = target . FormatWith ( propertyObject , this . environment ) ;
227- Assert . That ( actual , Is . EqualTo ( "fallback " ) ) ;
322+ Assert . That ( actual , Is . EqualTo ( "literal " ) ) ;
228323 }
229324
230325 [ Test ]
0 commit comments