From 41c73d6f6a1ea619ab4420405bd5b50c8d54bd64 Mon Sep 17 00:00:00 2001 From: Mastermindzh Date: Tue, 31 Oct 2023 09:38:04 +0100 Subject: [PATCH] chore: generic code improvements. + file scoped namespaces --- GrowthBook.Tests/GrowthBookTests.cs | 245 +++++++++--------- .../NamespaceTupleConverterTests.cs | 61 ++--- GrowthBook.Tests/UtilitiesTests.cs | 36 ++- 3 files changed, 162 insertions(+), 180 deletions(-) diff --git a/GrowthBook.Tests/GrowthBookTests.cs b/GrowthBook.Tests/GrowthBookTests.cs index a21d4c5..1ba7220 100644 --- a/GrowthBook.Tests/GrowthBookTests.cs +++ b/GrowthBook.Tests/GrowthBookTests.cs @@ -9,149 +9,148 @@ using Xunit; -namespace GrowthBook.Tests +namespace GrowthBook.Tests; + +public class GrowthBookTests { - public class GrowthBookTests - { - #region helpers - public static JObject getStandardCases() - { - return JObject.Parse(JsonTestHelpers.GetTestJson("standard-cases")); - } + #region helpers + public static JObject getStandardCases() + { + return JObject.Parse(JsonTestHelpers.GetTestJson("standard-cases")); + } - public static JObject getCustomCases() - { - return JObject.Parse(JsonTestHelpers.GetTestJson("custom-cases")); - } + public static JObject getCustomCases() + { + return JObject.Parse(JsonTestHelpers.GetTestJson("custom-cases")); + } - public static string GetTestNames(MethodInfo methodInfo, object[] values) - { - return $"{methodInfo.Name} - {values[0]}"; - } - #endregion + public static string GetTestNames(MethodInfo methodInfo, object[] values) + { + return $"{methodInfo.Name} - {values[0]}"; + } + #endregion - #region data - public static IEnumerable RunTests() + #region data + public static IEnumerable RunTests() + { + foreach (JArray testCase in (JArray)getStandardCases()["run"]) { - foreach (JArray testCase in (JArray)getStandardCases()["run"]) - { - yield return new object[] { - testCase[0].ToString(), - testCase[1].ToObject(), - testCase[2].ToObject(), - testCase[3], - testCase[4].ToObject(), - testCase[5].ToObject(), - }; - } + yield return new object[] { + testCase[0].ToString(), + testCase[1].ToObject(), + testCase[2].ToObject(), + testCase[3], + testCase[4].ToObject(), + testCase[5].ToObject(), + }; } - public static IEnumerable EvalFeatureTests() + } + public static IEnumerable EvalFeatureTests() + { + foreach (JArray testCase in (JArray)getStandardCases()["feature"]) { - foreach (JArray testCase in (JArray)getStandardCases()["feature"]) - { - yield return new object[] { - testCase[0].ToString(), - testCase[1].ToObject(), - testCase[2].ToString(), - testCase[3].ToObject(), - }; - } + yield return new object[] { + testCase[0].ToString(), + testCase[1].ToObject(), + testCase[2].ToString(), + testCase[3].ToObject(), + }; } - #endregion + } + #endregion - [Fact] - public void Run_ShouldCallTrackingCallbackOnce() + [Fact] + public void Run_ShouldCallTrackingCallbackOnce() + { + JArray testCase = (JArray)getCustomCases()["run"]; + int trackingCounter = 0; + + Context context = testCase[0].ToObject(); + context.TrackingCallback = (Experiment experiment, ExperimentResult result) => { - JArray testCase = (JArray)getCustomCases()["run"]; - int trackingCounter = 0; - - Context context = testCase[0].ToObject(); - context.TrackingCallback = (Experiment experiment, ExperimentResult result) => - { - Assert.True(JToken.DeepEquals(result.Value, testCase[2])); - Assert.Equal(testCase[3].ToObject(), result.InExperiment); - Assert.Equal(testCase[4].ToObject(), result.HashUsed); - trackingCounter++; - }; + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); + trackingCounter++; + }; + + GrowthBook gb = new(context); + gb.Run(testCase[1].ToObject()); + gb.Run(testCase[1].ToObject()); + Assert.Equal(1, trackingCounter); + } - GrowthBook gb = new GrowthBook(context); - gb.Run(testCase[1].ToObject()); - gb.Run(testCase[1].ToObject()); - Assert.Equal(1, trackingCounter); - } + [Fact] + public void Run_ShouldCallSubscribedCallbacks() + { + JArray testCase = (JArray)getCustomCases()["run"]; + GrowthBook gb = new(testCase[0].ToObject()); - [Fact] - public void Run_ShouldCallSubscribedCallbacks() + int subCounterOne = 0; + gb.Subscribe((Experiment experiment, ExperimentResult result) => { - JArray testCase = (JArray)getCustomCases()["run"]; - GrowthBook gb = new GrowthBook(testCase[0].ToObject()); - - int subCounterOne = 0; - gb.Subscribe((Experiment experiment, ExperimentResult result) => - { - Assert.True(JToken.DeepEquals(result.Value, testCase[2])); - Assert.Equal(testCase[3].ToObject(), result.InExperiment); - Assert.Equal(testCase[4].ToObject(), result.HashUsed); - subCounterOne++; - }); - - int subCounterTwo = 0; - Action unsubscribe = gb.Subscribe((Experiment experiment, ExperimentResult result) => - { - Assert.True(JToken.DeepEquals(result.Value, testCase[2])); - Assert.Equal(testCase[3].ToObject(), result.InExperiment); - Assert.Equal(testCase[4].ToObject(), result.HashUsed); - subCounterTwo++; - }); - unsubscribe(); - - int subCounterThree = 0; - gb.Subscribe((Experiment experiment, ExperimentResult result) => - { - Assert.True(JToken.DeepEquals(result.Value, testCase[2])); - Assert.Equal(testCase[3].ToObject(), result.InExperiment); - Assert.Equal(testCase[4].ToObject(), result.HashUsed); - subCounterThree++; - }); - - gb.Run(testCase[1].ToObject()); - gb.Run(testCase[1].ToObject()); - gb.Run(testCase[1].ToObject()); - Assert.Equal(1, subCounterOne); - Assert.Equal(0, subCounterTwo); - Assert.Equal(1, subCounterThree); - } + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); + subCounterOne++; + }); + + int subCounterTwo = 0; + Action unsubscribe = gb.Subscribe((Experiment experiment, ExperimentResult result) => + { + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); + subCounterTwo++; + }); + unsubscribe(); + + int subCounterThree = 0; + gb.Subscribe((Experiment experiment, ExperimentResult result) => + { + Assert.True(JToken.DeepEquals(result.Value, testCase[2])); + Assert.Equal(testCase[3].ToObject(), result.InExperiment); + Assert.Equal(testCase[4].ToObject(), result.HashUsed); + subCounterThree++; + }); + + gb.Run(testCase[1].ToObject()); + gb.Run(testCase[1].ToObject()); + gb.Run(testCase[1].ToObject()); + Assert.Equal(1, subCounterOne); + Assert.Equal(0, subCounterTwo); + Assert.Equal(1, subCounterThree); + } - [Theory] - [MemberData(nameof(RunTests))] - public void Run(string testname, Context context, Experiment experiment, JToken expectedValue, bool inExperiment, bool hashUsed) + [Theory] + [MemberData(nameof(RunTests))] + public void Run(string testName, Context context, Experiment experiment, JToken expectedValue, bool inExperiment, bool hashUsed) + { + if (testName is null) { - if (testname is null) - { - throw new ArgumentNullException(nameof(testname)); - } - - GrowthBook gb = new GrowthBook(context); - ExperimentResult actual = gb.Run(experiment); - Assert.Equal(inExperiment, actual.InExperiment); - Assert.Equal(hashUsed, actual.HashUsed); - Assert.True(JToken.DeepEquals(actual.Value, expectedValue)); + throw new ArgumentNullException(nameof(testName)); } - [Theory] - [MemberData(nameof(EvalFeatureTests))] - public void EvalFeature(string testname, Context context, string key, FeatureResult expected) + GrowthBook gb = new(context); + ExperimentResult actual = gb.Run(experiment); + Assert.Equal(inExperiment, actual.InExperiment); + Assert.Equal(hashUsed, actual.HashUsed); + Assert.True(JToken.DeepEquals(actual.Value, expectedValue)); + } + + [Theory] + [MemberData(nameof(EvalFeatureTests))] + public void EvalFeature(string testname, Context context, string key, FeatureResult expected) + { + if (testname is null) { - if (testname is null) - { - throw new ArgumentNullException(nameof(testname)); - } - - GrowthBook gb = new GrowthBook(context); - FeatureResult actual = gb.EvalFeature(key); - Assert.Equal(expected, actual); + throw new ArgumentNullException(nameof(testname)); } + GrowthBook gb = new(context); + FeatureResult actual = gb.EvalFeature(key); + Assert.Equal(expected, actual); } + } diff --git a/GrowthBook.Tests/NamespaceTupleConverterTests.cs b/GrowthBook.Tests/NamespaceTupleConverterTests.cs index d2b044f..e569aab 100644 --- a/GrowthBook.Tests/NamespaceTupleConverterTests.cs +++ b/GrowthBook.Tests/NamespaceTupleConverterTests.cs @@ -1,7 +1,4 @@ -using System; using System.Collections.Generic; -using System.Linq; -using System.Reflection; using GrowthBook.Tests.Json; @@ -9,43 +6,35 @@ using Xunit; -namespace GrowthBook.Tests +namespace GrowthBook.Tests; + +public class NamespaceTupleConverterTests { - public class NamespaceTupleConverterTests + [Fact] + public void CreateFromJson_NoFeatures_ShouldSucceed() { - [Fact] - public void CreateFromJson_NoFeatures_ShouldSucceed() - { - string json = JsonTestHelpers.GetTestJson("GrowthBookContext.NoFeatures"); - - var gb = JsonConvert.DeserializeObject(json); - } - - [Fact] - public void CreateFromJson_WithFeatures_ShouldSucceed() - { - string json = JsonTestHelpers.GetTestJson("GrowthBookContext"); - - var gb = JsonConvert.DeserializeObject(json); - } - - [Fact] - public void CreateFeaturesFromJson_WithFeatures_ShouldSucceed() - { - string json = JsonTestHelpers.GetTestJson("FeatureDictionary"); - - var gb = JsonConvert.DeserializeObject>(json); - } - - [Fact] - public void CreateFeaturesFromJson_OneFeatureWithNameSpace_ShouldSucceed() - { - string json = JsonTestHelpers.GetTestJson("SingleFeatureDictionary.WithNameSpace"); - - var gb = JsonConvert.DeserializeObject>(json); - } + string json = JsonTestHelpers.GetTestJson("GrowthBookContext.NoFeatures"); + _ = JsonConvert.DeserializeObject(json); + } + [Fact] + public void CreateFromJson_WithFeatures_ShouldSucceed() + { + string json = JsonTestHelpers.GetTestJson("GrowthBookContext"); + _ = JsonConvert.DeserializeObject(json); + } + [Fact] + public void CreateFeaturesFromJson_WithFeatures_ShouldSucceed() + { + string json = JsonTestHelpers.GetTestJson("FeatureDictionary"); + _ = JsonConvert.DeserializeObject>(json); } + [Fact] + public void CreateFeaturesFromJson_OneFeatureWithNameSpace_ShouldSucceed() + { + string json = JsonTestHelpers.GetTestJson("SingleFeatureDictionary.WithNameSpace"); + _ = JsonConvert.DeserializeObject>(json); + } } diff --git a/GrowthBook.Tests/UtilitiesTests.cs b/GrowthBook.Tests/UtilitiesTests.cs index 29dacd8..726513c 100644 --- a/GrowthBook.Tests/UtilitiesTests.cs +++ b/GrowthBook.Tests/UtilitiesTests.cs @@ -13,7 +13,6 @@ namespace GrowthBook.Tests { public class UtilitiesTests { - #region helpers public static JObject getStandardCases() @@ -26,14 +25,11 @@ public static string GetTestNames(MethodInfo methodInfo, object[] values) return $"{methodInfo.Name} - {values[0]}"; } - public double RoundStandard(double input) - { - return Math.Round(input, 6); - } + public static double RoundStandard(double input) => Math.Round(input, 6); - public IList RoundArray(IList input) + public static IList RoundArray(IList input) { - List results = new List(); + List results = []; for (int i = 0; i < input.Count; i++) { results.Add(RoundStandard(input[i])); @@ -43,7 +39,7 @@ public IList RoundArray(IList input) public IList RoundBucketRanges(IList input) { - List results = new List(); + List results = []; foreach (BucketRange range in input) { results.Add(new BucketRange(RoundStandard(range.Start), RoundStandard(range.End))); @@ -54,10 +50,10 @@ public IList RoundBucketRanges(IList input) #region data public static IEnumerable GetBucketRangeTests() { - foreach (JArray testCase in (JArray)getStandardCases()["getBucketRange"]) + foreach (JArray testCase in ((JArray)getStandardCases()["getBucketRange"]).Cast()) { - List expected = new List(); - foreach (JArray jArray in testCase[2]) + List expected = []; + foreach (JArray jArray in testCase[2].Cast()) { expected.Add(new BucketRange(jArray[0].ToObject(), jArray[1].ToObject())); } @@ -73,10 +69,10 @@ public static IEnumerable GetBucketRangeTests() public static IEnumerable ChooseVariationTests() { - foreach (JArray testCase in (JArray)getStandardCases()["chooseVariation"]) + foreach (JArray testCase in ((JArray)getStandardCases()["chooseVariation"]).Cast()) { - List ranges = new List(); - foreach (JArray jArray in testCase[2]) + List ranges = []; + foreach (JArray jArray in testCase[2].Cast()) { ranges.Add(new BucketRange(jArray[0].ToObject(), jArray[1].ToObject())); } @@ -91,7 +87,7 @@ public static IEnumerable ChooseVariationTests() public static IEnumerable InNamespaceTests() { - foreach (JArray testCase in (JArray)getStandardCases()["inNamespace"]) + foreach (JArray testCase in ((JArray)getStandardCases()["inNamespace"]).Cast()) { yield return new object[] { testCase[0].ToString(), @@ -106,7 +102,7 @@ public static IEnumerable InNamespaceTests() public static IEnumerable GetQueryStringOverrideTests() { - foreach (JArray testCase in (JArray)getStandardCases()["getQueryStringOverride"]) + foreach (JArray testCase in ((JArray)getStandardCases()["getQueryStringOverride"]).Cast()) { yield return new object[] { testCase[0].ToString(), @@ -120,8 +116,7 @@ public static IEnumerable GetQueryStringOverrideTests() public static IEnumerable EvalConditionTests() { - - foreach (JArray testCase in (JArray)getStandardCases()["evalCondition"]) + foreach (JArray testCase in ((JArray)getStandardCases()["evalCondition"]).Cast()) { yield return new object[] { testCase[0].ToString(), @@ -134,7 +129,7 @@ public static IEnumerable EvalConditionTests() public static IEnumerable GetEqualWeightsTests() { - foreach (JArray testCase in (JArray)getStandardCases()["getEqualWeights"]) + foreach (JArray testCase in ((JArray)getStandardCases()["getEqualWeights"]).Cast()) { yield return new object[] { testCase[0].ToObject(), @@ -146,7 +141,7 @@ public static IEnumerable GetEqualWeightsTests() public static IEnumerable HashTests() { - foreach (JArray testCase in (JArray)getStandardCases()["hash"]) + foreach (JArray testCase in ((JArray)getStandardCases()["hash"]).Cast()) { yield return new object[] { testCase[0].ToString(), @@ -239,6 +234,5 @@ public void GetQueryStringOverride(string testName, string id, string url, int n int? actual = Utilities.GetQueryStringOverride(id, url, numVariations); Assert.Equal(expected, actual); } - } }