@@ -5,6 +5,116 @@ public sealed class FeatureResultTests
55 private const string Key = "checkout" ;
66 private const string User = "user" ;
77
8+ [ Test ]
9+ public void TargetResults_KeepTheSelectedVariantAndContextPrecedence ( )
10+ {
11+ const string secondKey = "other" ;
12+ const string variant = "targeted" ;
13+ var sharedTarget = new FeatureTarget { Key = User , Variant = variant } ;
14+ var priority = new FeatureTarget { Key = User , ContextKind = "USER" , Status = FeatureStatus . Debug } ;
15+ var checker = new FeatureCheckerEvaluator (
16+ [
17+ new FeatureDefinition
18+ {
19+ Key = Key ,
20+ Targets = [ sharedTarget ] ,
21+ Variants = [ new FeatureVariant { Name = "first" } , new FeatureVariant { Name = variant , Value = "first-value" } ]
22+ } ,
23+ new FeatureDefinition
24+ {
25+ Key = secondKey ,
26+ Targets = [ priority , sharedTarget ] ,
27+ Variants = [ new FeatureVariant { Name = variant , Value = "second-value" } ]
28+ }
29+ ] ) ;
30+ var user = FeatureEvaluationContext . ForTargetingKey ( User ) ;
31+ var tenant = user . WithContextKind ( FeatureContextKinds . Tenant ) ;
32+
33+ Parallel . For ( 0 , 32 , _ =>
34+ {
35+ var first = checker . Evaluate ( Key , user ) ;
36+ first . Value . ShouldBe ( "first-value" ) ;
37+ first . VariationIndex . ShouldBe ( 1 ) ;
38+ var second = checker . Evaluate ( secondKey , tenant ) ;
39+ second . Value . ShouldBe ( "second-value" ) ;
40+ second . VariationIndex . ShouldBe ( 0 ) ;
41+ checker . Evaluate ( secondKey , user ) . IsDebug . ShouldBeTrue ( ) ;
42+ } ) ;
43+ }
44+
45+ [ Test ]
46+ public void MissingResults_UseTheCallersDefaultEvenIfTheEvaluatorSuppliesAValue ( )
47+ {
48+ IFeatureEvaluator evaluator = new MissingValueEvaluator ( ) ;
49+ var scope = evaluator . CreateScope ( FeatureEvaluationContext . ForTargetingKey ( User ) ) ;
50+
51+ scope . GetStringValue ( Key , "default" ) . ShouldBe ( "default" ) ;
52+ scope . GetBooleanValue ( "boolean" , false ) . ShouldBeFalse ( ) ;
53+ scope . GetInt32Value ( Key , 7 ) . ShouldBe ( 7 ) ;
54+ scope . GetInt64Value ( Key , 7L ) . ShouldBe ( 7L ) ;
55+ scope . GetDoubleValue ( Key , 7d ) . ShouldBe ( 7d ) ;
56+ }
57+
58+ [ Test ]
59+ public void MissingVariationDetails_UseTheCallersDefaultAndKeepTheReason ( )
60+ {
61+ IFeatureEvaluator evaluator = new MissingValueEvaluator ( ) ;
62+
63+ evaluator . StringVariationDetail ( Key , "default" ) . Value . ShouldBe ( "default" ) ;
64+ evaluator . BoolVariationDetail ( "boolean" , false ) . Value . ShouldBeFalse ( ) ;
65+ evaluator . IntVariationDetail ( Key , 7 ) . Value . ShouldBe ( 7 ) ;
66+ var detail = evaluator . DoubleVariationDetail ( Key , 7d ) ;
67+ detail . Value . ShouldBe ( 7d ) ;
68+ detail . Exists . ShouldBeFalse ( ) ;
69+ detail . ReasonKind . ShouldBe ( FeatureEvaluationReasonKind . Missing ) ;
70+ }
71+
72+ [ Test ]
73+ public void Fallthrough_DefaultDoesNotReplaceTheWeightedChoiceForTargetedContexts ( )
74+ {
75+ var builder = new FeatureSetBuilder ( ) ;
76+ builder . Feature ( Key ) . Disabled ( "default" ) . Variant ( "treatment" , 100 , FeatureStatus . Enabled , "targeted" ) ;
77+ var checker = builder . ToChecker ( ) ;
78+
79+ var first = checker . Evaluate ( Key ) ;
80+ first . ReasonKind . ShouldBe ( FeatureEvaluationReasonKind . Default ) ;
81+ first . Value . ShouldBe ( "default" ) ;
82+ var targeted = checker . Evaluate ( Key , FeatureEvaluationContext . ForTargetingKey ( User ) ) ;
83+ targeted . ReasonKind . ShouldBe ( FeatureEvaluationReasonKind . Variant ) ;
84+ targeted . VariationIndex . ShouldBe ( 0 ) ;
85+ targeted . Value . ShouldBe ( "targeted" ) ;
86+ checker . Evaluate ( Key ) . ShouldBe ( first ) ;
87+ }
88+
89+ [ Test ]
90+ public void RuleResults_KeepTheirDefinitionAndPositionAcrossConcurrentEvaluations ( )
91+ {
92+ const string secondKey = "other" ;
93+ var sharedRule = new FeatureTargetingRule { Name = "paid" , Conditions = [ FeatureCondition . Equals ( "plan" , "paid" ) ] } ;
94+ var priorityRule = new FeatureTargetingRule { Status = FeatureStatus . Debug , Conditions = [ FeatureCondition . Equals ( "role" , "staff" ) ] } ;
95+ var checker = new FeatureCheckerEvaluator (
96+ [
97+ new FeatureDefinition { Key = Key , Value = "first" , Rules = [ sharedRule ] } ,
98+ new FeatureDefinition { Key = secondKey , Value = "second" , Rules = [ priorityRule , sharedRule ] }
99+ ] ) ;
100+ var paid = FeatureEvaluationContext . ForTargetingKey ( User ) . With ( "plan" , "paid" ) ;
101+ var staff = paid . With ( "role" , "staff" ) ;
102+
103+ Parallel . For ( 0 , 32 , _ =>
104+ {
105+ var first = checker . Evaluate ( Key , paid ) ;
106+ first . Value . ShouldBe ( "first" ) ;
107+ ( first . Rule ? . RuleIndex ) . ShouldBe ( 0 ) ;
108+ var second = checker . Evaluate ( secondKey , paid ) ;
109+ second . Value . ShouldBe ( "second" ) ;
110+ ( second . Rule ? . RuleIndex ) . ShouldBe ( 1 ) ;
111+ ( second . Rule ? . RuleName ) . ShouldBe ( "paid" ) ;
112+ var priority = checker . Evaluate ( secondKey , staff ) ;
113+ priority . IsDebug . ShouldBeTrue ( ) ;
114+ ( priority . Rule ? . RuleIndex ) . ShouldBe ( 0 ) ;
115+ } ) ;
116+ }
117+
8118 [ Test ]
9119 [ Arguments ( "NaN" , 7d ) ]
10120 [ Arguments ( "Infinity" , 7d ) ]
@@ -40,4 +150,16 @@ public void UnresolvedNamedVariations_UseTheSelectingClauseStatus()
40150 checker . IsDisabled ( "target" , context ) . ShouldBeTrue ( ) ;
41151 checker . IsEnabled ( "rule" , context ) . ShouldBeTrue ( ) ;
42152 }
153+
154+ private sealed class MissingValueEvaluator ( ) : FeatureCheckerEvaluator ( Array . Empty < FeatureDefinition > ( ) ) , IFeatureEvaluator
155+ {
156+ FeatureEvaluation IFeatureEvaluator . Evaluate ( string featureKey , FeatureEvaluationContext ? context ) =>
157+ new ( )
158+ {
159+ Key = featureKey ,
160+ Value = string . Equals ( featureKey , "boolean" , StringComparison . Ordinal ) ? "true" : "42" ,
161+ Reason = FeatureEvaluationReasons . Missing ,
162+ ReasonKind = FeatureEvaluationReasonKind . Missing
163+ } ;
164+ }
43165}
0 commit comments