Skip to content

Commit

Permalink
chore: generic code improvements. + file scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastermindzh committed Oct 31, 2023
1 parent 179b5fa commit 41c73d6
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 180 deletions.
245 changes: 122 additions & 123 deletions GrowthBook.Tests/GrowthBookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object[]> RunTests()
#region data
public static IEnumerable<object[]> 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<Context>(),
testCase[2].ToObject<Experiment>(),
testCase[3],
testCase[4].ToObject<bool>(),
testCase[5].ToObject<bool>(),
};
}
yield return new object[] {
testCase[0].ToString(),
testCase[1].ToObject<Context>(),
testCase[2].ToObject<Experiment>(),
testCase[3],
testCase[4].ToObject<bool>(),
testCase[5].ToObject<bool>(),
};
}
public static IEnumerable<object[]> EvalFeatureTests()
}
public static IEnumerable<object[]> 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<Context>(),
testCase[2].ToString(),
testCase[3].ToObject<FeatureResult>(),
};
}
yield return new object[] {
testCase[0].ToString(),
testCase[1].ToObject<Context>(),
testCase[2].ToString(),
testCase[3].ToObject<FeatureResult>(),
};
}
#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>();
context.TrackingCallback = (Experiment experiment, ExperimentResult result) =>
{
JArray testCase = (JArray)getCustomCases()["run"];
int trackingCounter = 0;

Context context = testCase[0].ToObject<Context>();
context.TrackingCallback = (Experiment experiment, ExperimentResult result) =>
{
Assert.True(JToken.DeepEquals(result.Value, testCase[2]));
Assert.Equal(testCase[3].ToObject<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), result.HashUsed);
trackingCounter++;
};
Assert.True(JToken.DeepEquals(result.Value, testCase[2]));
Assert.Equal(testCase[3].ToObject<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), result.HashUsed);
trackingCounter++;
};

GrowthBook gb = new(context);
gb.Run(testCase[1].ToObject<Experiment>());
gb.Run(testCase[1].ToObject<Experiment>());
Assert.Equal(1, trackingCounter);
}

GrowthBook gb = new GrowthBook(context);
gb.Run(testCase[1].ToObject<Experiment>());
gb.Run(testCase[1].ToObject<Experiment>());
Assert.Equal(1, trackingCounter);
}
[Fact]
public void Run_ShouldCallSubscribedCallbacks()
{
JArray testCase = (JArray)getCustomCases()["run"];
GrowthBook gb = new(testCase[0].ToObject<Context>());

[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<Context>());

int subCounterOne = 0;
gb.Subscribe((Experiment experiment, ExperimentResult result) =>
{
Assert.True(JToken.DeepEquals(result.Value, testCase[2]));
Assert.Equal(testCase[3].ToObject<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), 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<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), 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<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), result.HashUsed);
subCounterThree++;
});

gb.Run(testCase[1].ToObject<Experiment>());
gb.Run(testCase[1].ToObject<Experiment>());
gb.Run(testCase[1].ToObject<Experiment>());
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<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), 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<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), 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<bool>(), result.InExperiment);
Assert.Equal(testCase[4].ToObject<bool>(), result.HashUsed);
subCounterThree++;
});

gb.Run(testCase[1].ToObject<Experiment>());
gb.Run(testCase[1].ToObject<Experiment>());
gb.Run(testCase[1].ToObject<Experiment>());
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);
}

}
61 changes: 25 additions & 36 deletions GrowthBook.Tests/NamespaceTupleConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using GrowthBook.Tests.Json;

using Newtonsoft.Json;

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<Context>(json);
}

[Fact]
public void CreateFromJson_WithFeatures_ShouldSucceed()
{
string json = JsonTestHelpers.GetTestJson("GrowthBookContext");

var gb = JsonConvert.DeserializeObject<Context>(json);
}

[Fact]
public void CreateFeaturesFromJson_WithFeatures_ShouldSucceed()
{
string json = JsonTestHelpers.GetTestJson("FeatureDictionary");

var gb = JsonConvert.DeserializeObject<Dictionary<string, Feature>>(json);
}

[Fact]
public void CreateFeaturesFromJson_OneFeatureWithNameSpace_ShouldSucceed()
{
string json = JsonTestHelpers.GetTestJson("SingleFeatureDictionary.WithNameSpace");

var gb = JsonConvert.DeserializeObject<Dictionary<string, Feature>>(json);
}
string json = JsonTestHelpers.GetTestJson("GrowthBookContext.NoFeatures");
_ = JsonConvert.DeserializeObject<Context>(json);
}

[Fact]
public void CreateFromJson_WithFeatures_ShouldSucceed()
{
string json = JsonTestHelpers.GetTestJson("GrowthBookContext");
_ = JsonConvert.DeserializeObject<Context>(json);
}

[Fact]
public void CreateFeaturesFromJson_WithFeatures_ShouldSucceed()
{
string json = JsonTestHelpers.GetTestJson("FeatureDictionary");
_ = JsonConvert.DeserializeObject<Dictionary<string, Feature>>(json);
}

[Fact]
public void CreateFeaturesFromJson_OneFeatureWithNameSpace_ShouldSucceed()
{
string json = JsonTestHelpers.GetTestJson("SingleFeatureDictionary.WithNameSpace");
_ = JsonConvert.DeserializeObject<Dictionary<string, Feature>>(json);
}
}
Loading

0 comments on commit 41c73d6

Please sign in to comment.