1+ using System . Diagnostics ;
12using Microsoft . Build . Utilities . ProjectCreation ;
23using Microsoft . Extensions . Logging ;
34
@@ -8,16 +9,22 @@ public class SdkTests : MSBuildTestBase
89 [ Fact ]
910 public void NoOverridingProperties_CanCompile ( )
1011 {
11- ProjectCreator . Templates . SdkProject ( out var result , out var buildOutput )
12- . TryGetConstant ( "BIT_INCLUDE_TELEMETRY" , out var hasTelementryConstant )
13- . TryGetConstant ( "BIT_INCLUDE_FEATURES" , out var hasFeaturesConstant )
14- . TryGetConstant ( "BIT_INCLUDE_AUTHENTICATION" , out var hasAuthenticationConstant ) ;
12+ IEnumerable < ( string Feature , bool DefaultValue ) > featuresAndDefaults = [
13+ ( "TELEMETRY" , true ) ,
14+ ( "FEATURES" , true ) ,
15+ ( "AUTHENTICATION" , true ) ,
16+ ( "CACHING" , false ) ,
17+ ] ;
18+
19+ var project = ProjectCreator . Templates . SdkProject ( out var result , out var buildOutput ) ;
1520
1621 Assert . True ( result , buildOutput . GetConsoleLog ( ) ) ;
1722
18- Assert . True ( hasTelementryConstant ) ;
19- Assert . True ( hasFeaturesConstant ) ;
20- Assert . True ( hasAuthenticationConstant ) ;
23+ foreach ( var ( feature , expectedDefault ) in featuresAndDefaults )
24+ {
25+ project . TryGetConstant ( $ "BIT_INCLUDE_{ feature } ", out var actualValue ) ;
26+ Assert . Equal ( expectedDefault , actualValue ) ;
27+ }
2128 }
2229
2330 [ Fact ]
@@ -123,6 +130,24 @@ public void FeaturesTurnedOn_CanUseFeatureService()
123130 Assert . True ( result , buildOutput . GetConsoleLog ( ) ) ;
124131 }
125132
133+ [ Fact ]
134+ public void CachingTurnedOn_CanUseFusionCache ( )
135+ {
136+ ProjectCreator . Templates . SdkProject (
137+ out var result ,
138+ out var buildOutput ,
139+ customAction : ( project ) =>
140+ {
141+ project . Property ( "BitIncludeCaching" , bool . TrueString ) ;
142+ } ,
143+ additional : """
144+ app.MapGet("/test", ([FromKeyedServices("Test")] ZiggyCreatures.Caching.Fusion.IFusionCache cache) => cache.GetOrSetAsync("Key", true));
145+ """
146+ ) ;
147+
148+ Assert . True ( result , buildOutput . GetConsoleLog ( ) ) ;
149+ }
150+
126151 [ Fact ]
127152 public void AuthenticationTurnedOff_CanCompile ( )
128153 {
@@ -138,34 +163,59 @@ public void AuthenticationTurnedOff_CanCompile()
138163 Assert . True ( result , buildOutput . GetConsoleLog ( ) ) ;
139164 }
140165
141- public static TheoryData < bool , bool , bool > MatrixData
142- => new MatrixTheoryData < bool , bool , bool > ( [ true , false ] , [ true , false ] , [ true , false ] ) ;
166+ public static TheoryData < string > PossibleVariantData ( )
167+ {
168+ string [ ] features = [ "Telemetry" , "Features" , "Authentication" , "Caching" ] ;
169+ var totalCombinations = ( int ) Math . Pow ( 2 , features . Length ) ;
143170
144- // There will be some variants that disallow the use of feature Y if feature X is not also enabled.
145- // Use this set to exclude those known variants from being tested.
146- public static HashSet < ( bool , bool , bool ) > ExcludedVariants => [ ] ;
171+ var theory = new TheoryData < string > ( ) ;
147172
148- [ Theory , MemberData ( nameof ( MatrixData ) ) ]
149- public void AllVariants_Work ( bool includeTelemetry , bool includeFeatures , bool includeAuthentication )
150- {
151- if ( ExcludedVariants . Contains ( ( includeTelemetry , includeFeatures , includeAuthentication ) ) )
173+ for ( var i = 0 ; i < totalCombinations ; i ++ )
174+ {
175+ var variant = new Dictionary < string , bool > ( ) ;
176+
177+ for ( var j = 0 ; j < features . Length ; j ++ )
178+ {
179+ // Check if the j-th bit is set in i
180+ variant [ features [ j ] ] = ( i & ( 1 << j ) ) != 0 ;
181+ }
182+
183+ // TODO: When there are variants that need to be skipped do so here but still add
184+ // a row with a skip message
185+ theory . Add ( Serialize ( variant ) ) ;
186+ }
187+
188+ return theory ;
189+
190+ // We serialize it into a simple string so that it can be easily viewed in test explorer
191+ static string Serialize ( Dictionary < string , bool > features )
152192 {
153- Assert . Skip ( $ """
154- Excluded Variant Skipped:
155- IncludeTelemetry = { includeTelemetry }
156- IncludeFeatures = { includeFeatures }
157- IncludeAuthentication = { includeAuthentication }
158- """ ) ;
193+ return string . Join ( ',' , features . Select ( feature => $ "{ feature . Key } ={ feature . Value } ") ) ;
159194 }
195+ }
196+
197+ [ Theory , MemberData ( nameof ( PossibleVariantData ) ) ]
198+ public void AllVariants_Work ( string featureSets )
199+ {
200+ // Deserialize from simple string into dictionary
201+ var features = featureSets . Split ( "," )
202+ . Select ( featureSet =>
203+ {
204+ var split = featureSet . Split ( "=" ) ;
205+ Debug . Assert ( split . Length == 2 , "Invalid format" ) ;
206+ return new KeyValuePair < string , bool > ( split [ 0 ] , bool . Parse ( split [ 1 ] ) ) ;
207+ } )
208+ . ToDictionary ( ) ;
160209
161210 ProjectCreator . Templates . SdkProject (
162211 out var result ,
163212 out var buildOutput ,
164213 customAction : ( project ) =>
165214 {
166- project . Property ( "BitIncludeTelemetry" , includeTelemetry . ToString ( ) ) ;
167- project . Property ( "BitIncludeFeatures" , includeFeatures . ToString ( ) ) ;
168- project . Property ( "BitIncludeAuthentication" , includeAuthentication . ToString ( ) ) ;
215+ foreach ( var feature in features )
216+ {
217+ project . Property ( $ "BitInclude{ feature . Key } ", feature . Value . ToString ( ) ) ;
218+ }
169219 }
170220 ) ;
171221
0 commit comments