From f97d0228c7d9f8946494dfeb3df82ded680a41c9 Mon Sep 17 00:00:00 2001 From: Todd Baert Date: Fri, 9 Sep 2022 13:55:35 -0400 Subject: [PATCH 1/2] feat!: structure->value, object value constructor Signed-off-by: Todd Baert --- src/OpenFeature.SDK/FeatureProvider.cs | 4 +- src/OpenFeature.SDK/IFeatureClient.cs | 4 +- src/OpenFeature.SDK/Model/Value.cs | 27 ++++++++++ src/OpenFeature.SDK/NoOpProvider.cs | 2 +- src/OpenFeature.SDK/OpenFeatureClient.cs | 4 +- .../FeatureProviderTests.cs | 10 ++-- .../OpenFeatureClientTests.cs | 16 +++--- .../TestImplementations.cs | 4 +- test/OpenFeature.SDK.Tests/ValueTests.cs | 52 ++++++++++++++++++- 9 files changed, 100 insertions(+), 23 deletions(-) diff --git a/src/OpenFeature.SDK/FeatureProvider.cs b/src/OpenFeature.SDK/FeatureProvider.cs index 26f51793..648f5db1 100644 --- a/src/OpenFeature.SDK/FeatureProvider.cs +++ b/src/OpenFeature.SDK/FeatureProvider.cs @@ -72,13 +72,13 @@ public abstract Task> ResolveDoubleValue(string flagKe EvaluationContext context = null); /// - /// Resolves a structure feature flag + /// Resolves a structured feature flag /// /// Feature flag key /// Default value /// /// - public abstract Task> ResolveStructureValue(string flagKey, Structure defaultValue, + public abstract Task> ResolveStructureValue(string flagKey, Value defaultValue, EvaluationContext context = null); } } diff --git a/src/OpenFeature.SDK/IFeatureClient.cs b/src/OpenFeature.SDK/IFeatureClient.cs index 722c5927..99af15c3 100644 --- a/src/OpenFeature.SDK/IFeatureClient.cs +++ b/src/OpenFeature.SDK/IFeatureClient.cs @@ -21,7 +21,7 @@ internal interface IFeatureClient Task GetDoubleValue(string flagKey, double defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null); Task> GetDoubleDetails(string flagKey, double defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null); - Task GetObjectValue(string flagKey, Structure defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null); - Task> GetObjectDetails(string flagKey, Structure defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null); + Task GetObjectValue(string flagKey, Value defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null); + Task> GetObjectDetails(string flagKey, Value defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null); } } diff --git a/src/OpenFeature.SDK/Model/Value.cs b/src/OpenFeature.SDK/Model/Value.cs index 2746a858..b805df98 100644 --- a/src/OpenFeature.SDK/Model/Value.cs +++ b/src/OpenFeature.SDK/Model/Value.cs @@ -17,6 +17,27 @@ public class Value /// public Value() => this._innerValue = null; + /// + /// Creates a Value with the inner set to the object + /// + /// The object to set as the inner value + public Value(Object value) + { + // integer is a special case, convert those. + this._innerValue = value is int ? Convert.ToDouble(value) : value; + if (!(this.IsNull() + || this.IsBoolean() + || this.IsString() + || this.IsNumber() + || this.IsStructure() + || this.IsList() + || this.IsDateTime())) + { + throw new ArgumentException("Invalid value type: " + value.GetType()); + } + } + + /// /// Creates a Value with the inner value to the inner value of the value param /// @@ -107,6 +128,12 @@ public class Value /// True if value is DateTime public bool IsDateTime() => this._innerValue is DateTime; + /// + /// Returns the underlying inner value as an object. Returns null if the inner value is null. + /// + /// Value as object + public object AsObject() => this._innerValue; + /// /// Returns the underlying int value /// Value will be null if it isn't a integer diff --git a/src/OpenFeature.SDK/NoOpProvider.cs b/src/OpenFeature.SDK/NoOpProvider.cs index bd5dd3ea..a406a994 100644 --- a/src/OpenFeature.SDK/NoOpProvider.cs +++ b/src/OpenFeature.SDK/NoOpProvider.cs @@ -33,7 +33,7 @@ public override Task> ResolveDoubleValue(string flagKe return Task.FromResult(NoOpResponse(flagKey, defaultValue)); } - public override Task> ResolveStructureValue(string flagKey, Structure defaultValue, EvaluationContext context = null) + public override Task> ResolveStructureValue(string flagKey, Value defaultValue, EvaluationContext context = null) { return Task.FromResult(NoOpResponse(flagKey, defaultValue)); } diff --git a/src/OpenFeature.SDK/OpenFeatureClient.cs b/src/OpenFeature.SDK/OpenFeatureClient.cs index d45ec675..04125fec 100644 --- a/src/OpenFeature.SDK/OpenFeatureClient.cs +++ b/src/OpenFeature.SDK/OpenFeatureClient.cs @@ -188,7 +188,7 @@ await this.EvaluateFlag(this._featureProvider.ResolveDoubleValue, FlagValueType. /// Evaluation Context /// Flag Evaluation Options /// Resolved flag details - public async Task GetObjectValue(string flagKey, Structure defaultValue, EvaluationContext context = null, + public async Task GetObjectValue(string flagKey, Value defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null) => (await this.GetObjectDetails(flagKey, defaultValue, context, config)).Value; @@ -200,7 +200,7 @@ public async Task GetObjectValue(string flagKey, Structure defaultVal /// Evaluation Context /// Flag Evaluation Options /// Resolved flag details - public async Task> GetObjectDetails(string flagKey, Structure defaultValue, + public async Task> GetObjectDetails(string flagKey, Value defaultValue, EvaluationContext context = null, FlagEvaluationOptions config = null) => await this.EvaluateFlag(this._featureProvider.ResolveStructureValue, FlagValueType.Object, flagKey, defaultValue, context, config); diff --git a/test/OpenFeature.SDK.Tests/FeatureProviderTests.cs b/test/OpenFeature.SDK.Tests/FeatureProviderTests.cs index ef6e5e89..8965eff0 100644 --- a/test/OpenFeature.SDK.Tests/FeatureProviderTests.cs +++ b/test/OpenFeature.SDK.Tests/FeatureProviderTests.cs @@ -36,7 +36,7 @@ public async Task Provider_Must_Resolve_Flag_Values() var defaultStringValue = fixture.Create(); var defaultIntegerValue = fixture.Create(); var defaultDoubleValue = fixture.Create(); - var defaultStructureValue = fixture.Create(); + var defaultStructureValue = fixture.Create(); var provider = new NoOpFeatureProvider(); var boolResolutionDetails = new ResolutionDetails(flagName, defaultBoolValue, ErrorType.None, NoOpProvider.ReasonNoOp, NoOpProvider.Variant); @@ -51,7 +51,7 @@ public async Task Provider_Must_Resolve_Flag_Values() var stringResolutionDetails = new ResolutionDetails(flagName, defaultStringValue, ErrorType.None, NoOpProvider.ReasonNoOp, NoOpProvider.Variant); (await provider.ResolveStringValue(flagName, defaultStringValue)).Should().BeEquivalentTo(stringResolutionDetails); - var structureResolutionDetails = new ResolutionDetails(flagName, defaultStructureValue, ErrorType.None, NoOpProvider.ReasonNoOp, NoOpProvider.Variant); + var structureResolutionDetails = new ResolutionDetails(flagName, defaultStructureValue, ErrorType.None, NoOpProvider.ReasonNoOp, NoOpProvider.Variant); (await provider.ResolveStructureValue(flagName, defaultStructureValue)).Should().BeEquivalentTo(structureResolutionDetails); } @@ -66,7 +66,7 @@ public async Task Provider_Must_ErrorType() var defaultStringValue = fixture.Create(); var defaultIntegerValue = fixture.Create(); var defaultDoubleValue = fixture.Create(); - var defaultStructureValue = fixture.Create(); + var defaultStructureValue = fixture.Create(); var providerMock = new Mock(MockBehavior.Strict); providerMock.Setup(x => x.ResolveBooleanValue(flagName, defaultBoolValue, It.IsAny())) @@ -82,10 +82,10 @@ public async Task Provider_Must_ErrorType() .ReturnsAsync(new ResolutionDetails(flagName, defaultStringValue, ErrorType.TypeMismatch, NoOpProvider.ReasonNoOp, NoOpProvider.Variant)); providerMock.Setup(x => x.ResolveStructureValue(flagName, defaultStructureValue, It.IsAny())) - .ReturnsAsync(new ResolutionDetails(flagName, defaultStructureValue, ErrorType.FlagNotFound, NoOpProvider.ReasonNoOp, NoOpProvider.Variant)); + .ReturnsAsync(new ResolutionDetails(flagName, defaultStructureValue, ErrorType.FlagNotFound, NoOpProvider.ReasonNoOp, NoOpProvider.Variant)); providerMock.Setup(x => x.ResolveStructureValue(flagName2, defaultStructureValue, It.IsAny())) - .ReturnsAsync(new ResolutionDetails(flagName, defaultStructureValue, ErrorType.ProviderNotReady, NoOpProvider.ReasonNoOp, NoOpProvider.Variant)); + .ReturnsAsync(new ResolutionDetails(flagName, defaultStructureValue, ErrorType.ProviderNotReady, NoOpProvider.ReasonNoOp, NoOpProvider.Variant)); var provider = providerMock.Object; diff --git a/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs b/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs index 3d9b9fbd..ac32c42c 100644 --- a/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs +++ b/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs @@ -68,7 +68,7 @@ public async Task OpenFeatureClient_Should_Allow_Flag_Evaluation() var defaultStringValue = fixture.Create(); var defaultIntegerValue = fixture.Create(); var defaultDoubleValue = fixture.Create(); - var defaultStructureValue = fixture.Create(); + var defaultStructureValue = fixture.Create(); var emptyFlagOptions = new FlagEvaluationOptions(new List(), new Dictionary()); OpenFeature.Instance.SetProvider(new NoOpFeatureProvider()); @@ -114,7 +114,7 @@ public async Task OpenFeatureClient_Should_Allow_Details_Flag_Evaluation() var defaultStringValue = fixture.Create(); var defaultIntegerValue = fixture.Create(); var defaultDoubleValue = fixture.Create(); - var defaultStructureValue = fixture.Create(); + var defaultStructureValue = fixture.Create(); var emptyFlagOptions = new FlagEvaluationOptions(new List(), new Dictionary()); OpenFeature.Instance.SetProvider(new NoOpFeatureProvider()); @@ -140,7 +140,7 @@ public async Task OpenFeatureClient_Should_Allow_Details_Flag_Evaluation() (await client.GetStringDetails(flagName, defaultStringValue, new EvaluationContext())).Should().BeEquivalentTo(stringFlagEvaluationDetails); (await client.GetStringDetails(flagName, defaultStringValue, new EvaluationContext(), emptyFlagOptions)).Should().BeEquivalentTo(stringFlagEvaluationDetails); - var structureFlagEvaluationDetails = new FlagEvaluationDetails(flagName, defaultStructureValue, ErrorType.None, NoOpProvider.ReasonNoOp, NoOpProvider.Variant); + var structureFlagEvaluationDetails = new FlagEvaluationDetails(flagName, defaultStructureValue, ErrorType.None, NoOpProvider.ReasonNoOp, NoOpProvider.Variant); (await client.GetObjectDetails(flagName, defaultStructureValue)).Should().BeEquivalentTo(structureFlagEvaluationDetails); (await client.GetObjectDetails(flagName, defaultStructureValue, new EvaluationContext())).Should().BeEquivalentTo(structureFlagEvaluationDetails); (await client.GetObjectDetails(flagName, defaultStructureValue, new EvaluationContext(), emptyFlagOptions)).Should().BeEquivalentTo(structureFlagEvaluationDetails); @@ -159,7 +159,7 @@ public async Task OpenFeatureClient_Should_Return_DefaultValue_When_Type_Mismatc var clientName = fixture.Create(); var clientVersion = fixture.Create(); var flagName = fixture.Create(); - var defaultValue = fixture.Create(); + var defaultValue = fixture.Create(); var mockedFeatureProvider = new Mock(MockBehavior.Strict); var mockedLogger = new Mock>(MockBehavior.Default); @@ -302,12 +302,12 @@ public async Task Should_Resolve_StructureValue() var clientName = fixture.Create(); var clientVersion = fixture.Create(); var flagName = fixture.Create(); - var defaultValue = fixture.Create(); + var defaultValue = fixture.Create(); var featureProviderMock = new Mock(MockBehavior.Strict); featureProviderMock .Setup(x => x.ResolveStructureValue(flagName, defaultValue, It.IsAny())) - .ReturnsAsync(new ResolutionDetails(flagName, defaultValue)); + .ReturnsAsync(new ResolutionDetails(flagName, defaultValue)); featureProviderMock.Setup(x => x.GetMetadata()) .Returns(new Metadata(fixture.Create())); featureProviderMock.Setup(x => x.GetProviderHooks()) @@ -316,7 +316,7 @@ public async Task Should_Resolve_StructureValue() OpenFeature.Instance.SetProvider(featureProviderMock.Object); var client = OpenFeature.Instance.GetClient(clientName, clientVersion); - (await client.GetObjectValue(flagName, defaultValue)).Should().Equal(defaultValue); + (await client.GetObjectValue(flagName, defaultValue)).Should().Be(defaultValue); featureProviderMock.Verify(x => x.ResolveStructureValue(flagName, defaultValue, It.IsAny()), Times.Once); } @@ -328,7 +328,7 @@ public async Task When_Exception_Occurs_During_Evaluation_Should_Return_Error() var clientName = fixture.Create(); var clientVersion = fixture.Create(); var flagName = fixture.Create(); - var defaultValue = fixture.Create(); + var defaultValue = fixture.Create(); var featureProviderMock = new Mock(MockBehavior.Strict); featureProviderMock diff --git a/test/OpenFeature.SDK.Tests/TestImplementations.cs b/test/OpenFeature.SDK.Tests/TestImplementations.cs index 59392685..1d0539aa 100644 --- a/test/OpenFeature.SDK.Tests/TestImplementations.cs +++ b/test/OpenFeature.SDK.Tests/TestImplementations.cs @@ -70,10 +70,10 @@ public override Task> ResolveDoubleValue(string flagKe return Task.FromResult(new ResolutionDetails(flagKey, defaultValue)); } - public override Task> ResolveStructureValue(string flagKey, Structure defaultValue, + public override Task> ResolveStructureValue(string flagKey, Value defaultValue, EvaluationContext context = null) { - return Task.FromResult(new ResolutionDetails(flagKey, defaultValue)); + return Task.FromResult(new ResolutionDetails(flagKey, defaultValue)); } } } diff --git a/test/OpenFeature.SDK.Tests/ValueTests.cs b/test/OpenFeature.SDK.Tests/ValueTests.cs index fde2573a..1129b808 100644 --- a/test/OpenFeature.SDK.Tests/ValueTests.cs +++ b/test/OpenFeature.SDK.Tests/ValueTests.cs @@ -7,6 +7,8 @@ namespace OpenFeature.SDK.Tests { public class ValueTests { + class Foo { } + [Fact] public void No_Arg_Should_Contain_Null() { @@ -14,6 +16,55 @@ public void No_Arg_Should_Contain_Null() Assert.True(value.IsNull()); } + [Fact] + public void Object_Arg_Should_Contain_Object() + { + try + { + // int is a special case, see Int_Object_Arg_Should_Contain_Object() + IList list = new List(){ + true, "val", .5, new Structure(), new List(), DateTime.Now + }; + + int i = 0; + foreach (Object l in list) + { + Value value = new Value(l); + Assert.Equal(list[i], value.AsObject()); + i++; + } + } + catch (Exception) + { + Assert.True(false, "Expected no exception."); + } + } + + [Fact] + public void Int_Object_Arg_Should_Contain_Object() + { + try + { + int innerValue = 1; + Value value = new Value(innerValue); + Assert.True(value.IsNumber()); + Assert.Equal(innerValue, value.AsInteger()); + } + catch (Exception) + { + Assert.True(false, "Expected no exception."); + } + } + + [Fact] + public void Invalid_Object_Should_Throw() + { + Assert.Throws(() => + { + return new Value(new Foo()); + }); + } + [Fact] public void Bool_Arg_Should_Contain_Bool() { @@ -48,7 +99,6 @@ public void String_Arg_Should_Contain_String() Assert.Equal(innerValue, value.AsString()); } - [Fact] public void DateTime_Arg_Should_Contain_DateTime() { From 9f8bdf580cb3d7a9d804e84a1573cfda20145e77 Mon Sep 17 00:00:00 2001 From: Todd Baert Date: Mon, 12 Sep 2022 08:48:18 -0400 Subject: [PATCH 2/2] Convert arg-less methods to props Signed-off-by: Todd Baert --- src/OpenFeature.SDK/Model/Value.cs | 52 ++++++++--------- .../OpenFeatureClientTests.cs | 2 +- .../OpenFeatureEvaluationContextTests.cs | 34 +++++------ .../OpenFeatureHookTests.cs | 16 +++--- test/OpenFeature.SDK.Tests/StructureTests.cs | 22 ++++---- test/OpenFeature.SDK.Tests/ValueTests.cs | 56 +++++++++---------- 6 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/OpenFeature.SDK/Model/Value.cs b/src/OpenFeature.SDK/Model/Value.cs index b805df98..941eecba 100644 --- a/src/OpenFeature.SDK/Model/Value.cs +++ b/src/OpenFeature.SDK/Model/Value.cs @@ -21,23 +21,23 @@ public class Value /// Creates a Value with the inner set to the object /// /// The object to set as the inner value - public Value(Object value) + public Value(Object value) { // integer is a special case, convert those. this._innerValue = value is int ? Convert.ToDouble(value) : value; - if (!(this.IsNull() - || this.IsBoolean() - || this.IsString() - || this.IsNumber() - || this.IsStructure() - || this.IsList() - || this.IsDateTime())) + if (!(this.IsNull + || this.IsBoolean + || this.IsString + || this.IsNumber + || this.IsStructure + || this.IsList + || this.IsDateTime)) { throw new ArgumentException("Invalid value type: " + value.GetType()); } - } - - + } + + /// /// Creates a Value with the inner value to the inner value of the value param /// @@ -90,97 +90,97 @@ public Value(Object value) /// Determines if inner value is null /// /// True if value is null - public bool IsNull() => this._innerValue is null; + public bool IsNull => this._innerValue is null; /// /// Determines if inner value is bool /// /// True if value is bool - public bool IsBoolean() => this._innerValue is bool; + public bool IsBoolean => this._innerValue is bool; /// /// Determines if inner value is numeric /// /// True if value is double - public bool IsNumber() => this._innerValue is double; + public bool IsNumber => this._innerValue is double; /// /// Determines if inner value is string /// /// True if value is string - public bool IsString() => this._innerValue is string; + public bool IsString => this._innerValue is string; /// /// Determines if inner value is Structure /// /// True if value is Structure - public bool IsStructure() => this._innerValue is Structure; + public bool IsStructure => this._innerValue is Structure; /// /// Determines if inner value is list /// /// True if value is list - public bool IsList() => this._innerValue is IList; + public bool IsList => this._innerValue is IList; /// /// Determines if inner value is DateTime /// /// True if value is DateTime - public bool IsDateTime() => this._innerValue is DateTime; + public bool IsDateTime => this._innerValue is DateTime; /// /// Returns the underlying inner value as an object. Returns null if the inner value is null. /// /// Value as object - public object AsObject() => this._innerValue; + public object AsObject => this._innerValue; /// /// Returns the underlying int value /// Value will be null if it isn't a integer /// /// Value as int - public int? AsInteger() => this.IsNumber() ? (int?)Convert.ToInt32((double?)this._innerValue) : null; + public int? AsInteger => this.IsNumber ? (int?)Convert.ToInt32((double?)this._innerValue) : null; /// /// Returns the underlying bool value /// Value will be null if it isn't a bool /// /// Value as bool - public bool? AsBoolean() => this.IsBoolean() ? (bool?)this._innerValue : null; + public bool? AsBoolean => this.IsBoolean ? (bool?)this._innerValue : null; /// /// Returns the underlying double value /// Value will be null if it isn't a double /// /// Value as int - public double? AsDouble() => this.IsNumber() ? (double?)this._innerValue : null; + public double? AsDouble => this.IsNumber ? (double?)this._innerValue : null; /// /// Returns the underlying string value /// Value will be null if it isn't a string /// /// Value as string - public string AsString() => this.IsString() ? (string)this._innerValue : null; + public string AsString => this.IsString ? (string)this._innerValue : null; /// /// Returns the underlying Structure value /// Value will be null if it isn't a Structure /// /// Value as Structure - public Structure AsStructure() => this.IsStructure() ? (Structure)this._innerValue : null; + public Structure AsStructure => this.IsStructure ? (Structure)this._innerValue : null; /// /// Returns the underlying List value /// Value will be null if it isn't a List /// /// Value as List - public IList AsList() => this.IsList() ? (IList)this._innerValue : null; + public IList AsList => this.IsList ? (IList)this._innerValue : null; /// /// Returns the underlying DateTime value /// Value will be null if it isn't a DateTime /// /// Value as DateTime - public DateTime? AsDateTime() => this.IsDateTime() ? (DateTime?)this._innerValue : null; + public DateTime? AsDateTime => this.IsDateTime ? (DateTime?)this._innerValue : null; } } diff --git a/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs b/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs index ac32c42c..5902908e 100644 --- a/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs +++ b/test/OpenFeature.SDK.Tests/OpenFeatureClientTests.cs @@ -362,7 +362,7 @@ public void Should_Get_And_Set_Context() var VAL = 1; FeatureClient client = OpenFeature.Instance.GetClient(); client.SetContext(new EvaluationContext().Add(KEY, VAL)); - Assert.Equal(VAL, client.GetContext().GetValue(KEY).AsInteger()); + Assert.Equal(VAL, client.GetContext().GetValue(KEY).AsInteger); } } } diff --git a/test/OpenFeature.SDK.Tests/OpenFeatureEvaluationContextTests.cs b/test/OpenFeature.SDK.Tests/OpenFeatureEvaluationContextTests.cs index 808ffc8c..ff913c13 100644 --- a/test/OpenFeature.SDK.Tests/OpenFeatureEvaluationContextTests.cs +++ b/test/OpenFeature.SDK.Tests/OpenFeatureEvaluationContextTests.cs @@ -21,8 +21,8 @@ public void Should_Merge_Two_Contexts() context1.Merge(context2); Assert.Equal(2, context1.Count); - Assert.Equal("value1", context1.GetValue("key1").AsString()); - Assert.Equal("value2", context1.GetValue("key2").AsString()); + Assert.Equal("value1", context1.GetValue("key1").AsString); + Assert.Equal("value2", context1.GetValue("key2").AsString); } [Fact] @@ -39,8 +39,8 @@ public void Should_Merge_TwoContexts_And_Override_Duplicates_With_RightHand_Cont context1.Merge(context2); Assert.Equal(2, context1.Count); - Assert.Equal("overriden_value", context1.GetValue("key1").AsString()); - Assert.Equal("value2", context1.GetValue("key2").AsString()); + Assert.Equal("overriden_value", context1.GetValue("key1").AsString); + Assert.Equal("value2", context1.GetValue("key2").AsString); context1.Remove("key1"); Assert.Throws(() => context1.GetValue("key1")); @@ -63,28 +63,28 @@ public void EvaluationContext_Should_All_Types() .Add("key6", 1.0); var value1 = context.GetValue("key1"); - value1.IsString().Should().BeTrue(); - value1.AsString().Should().Be("value"); + value1.IsString.Should().BeTrue(); + value1.AsString.Should().Be("value"); var value2 = context.GetValue("key2"); - value2.IsNumber().Should().BeTrue(); - value2.AsInteger().Should().Be(1); + value2.IsNumber.Should().BeTrue(); + value2.AsInteger.Should().Be(1); var value3 = context.GetValue("key3"); - value3.IsBoolean().Should().Be(true); - value3.AsBoolean().Should().Be(true); + value3.IsBoolean.Should().Be(true); + value3.AsBoolean.Should().Be(true); var value4 = context.GetValue("key4"); - value4.IsDateTime().Should().BeTrue(); - value4.AsDateTime().Should().Be(now); + value4.IsDateTime.Should().BeTrue(); + value4.AsDateTime.Should().Be(now); var value5 = context.GetValue("key5"); - value5.IsStructure().Should().BeTrue(); - value5.AsStructure().Should().Equal(structure); + value5.IsStructure.Should().BeTrue(); + value5.AsStructure.Should().Equal(structure); var value6 = context.GetValue("key6"); - value6.IsNumber().Should().BeTrue(); - value6.AsDouble().Should().Be(1.0); + value6.IsNumber.Should().BeTrue(); + value6.AsDouble.Should().Be(1.0); } [Fact] @@ -112,7 +112,7 @@ public void Should_Be_Able_To_Get_All_Values() var count = 0; foreach (var keyValue in context) { - context.GetValue(keyValue.Key).AsString().Should().Be(keyValue.Value.AsString()); + context.GetValue(keyValue.Key).AsString.Should().Be(keyValue.Value.AsString); count++; } diff --git a/test/OpenFeature.SDK.Tests/OpenFeatureHookTests.cs b/test/OpenFeature.SDK.Tests/OpenFeatureHookTests.cs index a673a006..7b470be5 100644 --- a/test/OpenFeature.SDK.Tests/OpenFeatureHookTests.cs +++ b/test/OpenFeature.SDK.Tests/OpenFeatureHookTests.cs @@ -185,7 +185,7 @@ public async Task Evaluation_Context_Must_Be_Mutable_Before_Hook() new FlagEvaluationOptions(new[] { hook1.Object, hook2.Object }, new Dictionary())); hook1.Verify(x => x.Before(It.IsAny>(), It.IsAny>()), Times.Once); - hook2.Verify(x => x.Before(It.Is>(a => a.EvaluationContext.GetValue("test").AsString() == "test"), It.IsAny>()), Times.Once); + hook2.Verify(x => x.Before(It.Is>(a => a.EvaluationContext.GetValue("test").AsString == "test"), It.IsAny>()), Times.Once); } [Fact] @@ -245,13 +245,13 @@ public async Task Evaluation_Context_Must_Be_Merged_In_Correct_Order() // after proper merging, all properties should equal true provider.Verify(x => x.ResolveBooleanValue(It.IsAny(), It.IsAny(), It.Is(y => - (y.GetValue(propGlobal).AsBoolean() ?? false) - && (y.GetValue(propClient).AsBoolean() ?? false) - && (y.GetValue(propGlobalToOverwrite).AsBoolean() ?? false) - && (y.GetValue(propInvocation).AsBoolean() ?? false) - && (y.GetValue(propClientToOverwrite).AsBoolean() ?? false) - && (y.GetValue(propHook).AsBoolean() ?? false) - && (y.GetValue(propInvocationToOverwrite).AsBoolean() ?? false) + (y.GetValue(propGlobal).AsBoolean ?? false) + && (y.GetValue(propClient).AsBoolean ?? false) + && (y.GetValue(propGlobalToOverwrite).AsBoolean ?? false) + && (y.GetValue(propInvocation).AsBoolean ?? false) + && (y.GetValue(propClientToOverwrite).AsBoolean ?? false) + && (y.GetValue(propHook).AsBoolean ?? false) + && (y.GetValue(propInvocationToOverwrite).AsBoolean ?? false) )), Times.Once); } diff --git a/test/OpenFeature.SDK.Tests/StructureTests.cs b/test/OpenFeature.SDK.Tests/StructureTests.cs index 63949030..e16757e8 100644 --- a/test/OpenFeature.SDK.Tests/StructureTests.cs +++ b/test/OpenFeature.SDK.Tests/StructureTests.cs @@ -23,7 +23,7 @@ public void Dictionary_Arg_Should_Contain_New_Dictionary() { KEY, new Value(KEY) } }; Structure structure = new Structure(dictionary); - Assert.Equal(KEY, structure.AsDictionary()[KEY].AsString()); + Assert.Equal(KEY, structure.AsDictionary()[KEY].AsString); Assert.NotSame(structure.AsDictionary(), dictionary); // should be a copy } @@ -58,14 +58,14 @@ public void Add_And_Get_Add_And_Return_Values() structure.Add(LIST_KEY, LIST_VAL); structure.Add(VALUE_KEY, VALUE_VAL); - Assert.Equal(BOOL_VAL, structure.GetValue(BOOL_KEY).AsBoolean()); - Assert.Equal(STRING_VAL, structure.GetValue(STRING_KEY).AsString()); - Assert.Equal(INT_VAL, structure.GetValue(INT_KEY).AsInteger()); - Assert.Equal(DOUBLE_VAL, structure.GetValue(DOUBLE_KEY).AsDouble()); - Assert.Equal(DATE_VAL, structure.GetValue(DATE_KEY).AsDateTime()); - Assert.Equal(STRUCT_VAL, structure.GetValue(STRUCT_KEY).AsStructure()); - Assert.Equal(LIST_VAL, structure.GetValue(LIST_KEY).AsList()); - Assert.True(structure.GetValue(VALUE_KEY).IsNull()); + Assert.Equal(BOOL_VAL, structure.GetValue(BOOL_KEY).AsBoolean); + Assert.Equal(STRING_VAL, structure.GetValue(STRING_KEY).AsString); + Assert.Equal(INT_VAL, structure.GetValue(INT_KEY).AsInteger); + Assert.Equal(DOUBLE_VAL, structure.GetValue(DOUBLE_KEY).AsDouble); + Assert.Equal(DATE_VAL, structure.GetValue(DATE_KEY).AsDateTime); + Assert.Equal(STRUCT_VAL, structure.GetValue(STRUCT_KEY).AsStructure); + Assert.Equal(LIST_VAL, structure.GetValue(LIST_KEY).AsList); + Assert.True(structure.GetValue(VALUE_KEY).IsNull); } [Fact] @@ -91,7 +91,7 @@ public void TryGetValue_Should_Return_Value() structure.Add(KEY, VAL); Value value; Assert.True(structure.TryGetValue(KEY, out value)); - Assert.Equal(VAL, value.AsString()); + Assert.Equal(VAL, value.AsString); } [Fact] @@ -127,7 +127,7 @@ public void GetEnumerator_Should_Return_Enumerator() structure.Add(KEY, VAL); IEnumerator> enumerator = structure.GetEnumerator(); enumerator.MoveNext(); - Assert.Equal(VAL, enumerator.Current.Value.AsString()); + Assert.Equal(VAL, enumerator.Current.Value.AsString); } } } diff --git a/test/OpenFeature.SDK.Tests/ValueTests.cs b/test/OpenFeature.SDK.Tests/ValueTests.cs index 1129b808..47d4876a 100644 --- a/test/OpenFeature.SDK.Tests/ValueTests.cs +++ b/test/OpenFeature.SDK.Tests/ValueTests.cs @@ -13,13 +13,13 @@ class Foo { } public void No_Arg_Should_Contain_Null() { Value value = new Value(); - Assert.True(value.IsNull()); + Assert.True(value.IsNull); } [Fact] public void Object_Arg_Should_Contain_Object() { - try + try { // int is a special case, see Int_Object_Arg_Should_Contain_Object() IList list = new List(){ @@ -27,14 +27,14 @@ public void Object_Arg_Should_Contain_Object() }; int i = 0; - foreach (Object l in list) + foreach (Object l in list) { Value value = new Value(l); - Assert.Equal(list[i], value.AsObject()); + Assert.Equal(list[i], value.AsObject); i++; } - } - catch (Exception) + } + catch (Exception) { Assert.True(false, "Expected no exception."); } @@ -43,14 +43,14 @@ public void Object_Arg_Should_Contain_Object() [Fact] public void Int_Object_Arg_Should_Contain_Object() { - try + try { int innerValue = 1; Value value = new Value(innerValue); - Assert.True(value.IsNumber()); - Assert.Equal(innerValue, value.AsInteger()); - } - catch (Exception) + Assert.True(value.IsNumber); + Assert.Equal(innerValue, value.AsInteger); + } + catch (Exception) { Assert.True(false, "Expected no exception."); } @@ -59,7 +59,7 @@ public void Int_Object_Arg_Should_Contain_Object() [Fact] public void Invalid_Object_Should_Throw() { - Assert.Throws(() => + Assert.Throws(() => { return new Value(new Foo()); }); @@ -70,8 +70,8 @@ public void Bool_Arg_Should_Contain_Bool() { bool innerValue = true; Value value = new Value(innerValue); - Assert.True(value.IsBoolean()); - Assert.Equal(innerValue, value.AsBoolean()); + Assert.True(value.IsBoolean); + Assert.Equal(innerValue, value.AsBoolean); } [Fact] @@ -79,15 +79,15 @@ public void Numeric_Arg_Should_Return_Double_Or_Int() { double innerDoubleValue = .75; Value doubleValue = new Value(innerDoubleValue); - Assert.True(doubleValue.IsNumber()); - Assert.Equal(1, doubleValue.AsInteger()); // should be rounded - Assert.Equal(.75, doubleValue.AsDouble()); + Assert.True(doubleValue.IsNumber); + Assert.Equal(1, doubleValue.AsInteger); // should be rounded + Assert.Equal(.75, doubleValue.AsDouble); int innerIntValue = 100; Value intValue = new Value(innerIntValue); - Assert.True(intValue.IsNumber()); - Assert.Equal(innerIntValue, intValue.AsInteger()); - Assert.Equal(innerIntValue, intValue.AsDouble()); + Assert.True(intValue.IsNumber); + Assert.Equal(innerIntValue, intValue.AsInteger); + Assert.Equal(innerIntValue, intValue.AsDouble); } [Fact] @@ -95,8 +95,8 @@ public void String_Arg_Should_Contain_String() { string innerValue = "hi!"; Value value = new Value(innerValue); - Assert.True(value.IsString()); - Assert.Equal(innerValue, value.AsString()); + Assert.True(value.IsString); + Assert.Equal(innerValue, value.AsString); } [Fact] @@ -104,8 +104,8 @@ public void DateTime_Arg_Should_Contain_DateTime() { DateTime innerValue = new DateTime(); Value value = new Value(innerValue); - Assert.True(value.IsDateTime()); - Assert.Equal(innerValue, value.AsDateTime()); + Assert.True(value.IsDateTime); + Assert.Equal(innerValue, value.AsDateTime); } [Fact] @@ -115,8 +115,8 @@ public void Structure_Arg_Should_Contain_Structure() string INNER_VALUE = "val"; Structure innerValue = new Structure().Add(INNER_KEY, INNER_VALUE); Value value = new Value(innerValue); - Assert.True(value.IsStructure()); - Assert.Equal(INNER_VALUE, value.AsStructure().GetValue(INNER_KEY).AsString()); + Assert.True(value.IsStructure); + Assert.Equal(INNER_VALUE, value.AsStructure.GetValue(INNER_KEY).AsString); } [Fact] @@ -128,8 +128,8 @@ public void LIst_Arg_Should_Contain_LIst() new Value(ITEM_VALUE) }; Value value = new Value(innerValue); - Assert.True(value.IsList()); - Assert.Equal(ITEM_VALUE, value.AsList()[0].AsString()); + Assert.True(value.IsList); + Assert.Equal(ITEM_VALUE, value.AsList[0].AsString); } } }