-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Inforitnl/fix/moved-to-xunit
Fix/moved to xunit
- Loading branch information
Showing
18 changed files
with
372 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="Json/custom-cases.json" /> | ||
<None Remove="Json/FeatureDictionary.json" /> | ||
<None Remove="Json/GrowthBookContext.json" /> | ||
<None Remove="Json/GrowthBookContext.NoFeatures.json" /> | ||
<None Remove="Json/SingleFeatureDictionary.WithNameSpace.json" /> | ||
<None Remove="Json/standard-cases.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Json/custom-cases.json" /> | ||
<EmbeddedResource Include="Json/FeatureDictionary.json" /> | ||
<EmbeddedResource Include="Json/GrowthBookContext.json" /> | ||
<EmbeddedResource Include="Json/GrowthBookContext.NoFeatures.json" /> | ||
<EmbeddedResource Include="Json/SingleFeatureDictionary.WithNameSpace.json" /> | ||
<EmbeddedResource Include="Json/standard-cases.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.5.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.10" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="XunitXml.TestLogger" Version="3.0.70" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="test-results/" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../GrowthBook/GrowthBook.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
using GrowthBook.Tests.Json; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
using Xunit; | ||
|
||
namespace GrowthBook.Tests; | ||
|
||
public class GrowthBookTests | ||
{ | ||
|
||
#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 string GetTestNames(MethodInfo methodInfo, object[] values) | ||
{ | ||
return $"{methodInfo.Name} - {values[0]}"; | ||
} | ||
#endregion | ||
|
||
#region data | ||
public static IEnumerable<object[]> RunTests() | ||
{ | ||
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>(), | ||
}; | ||
} | ||
} | ||
public static IEnumerable<object[]> EvalFeatureTests() | ||
{ | ||
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>(), | ||
}; | ||
} | ||
} | ||
#endregion | ||
|
||
[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) => | ||
{ | ||
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); | ||
} | ||
|
||
[Fact] | ||
public void Run_ShouldCallSubscribedCallbacks() | ||
{ | ||
JArray testCase = (JArray)getCustomCases()["run"]; | ||
GrowthBook gb = new(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); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(RunTests))] | ||
public void Run(string testName, Context context, Experiment experiment, JToken expectedValue, bool inExperiment, bool hashUsed) | ||
{ | ||
if (testName is null) | ||
{ | ||
throw new ArgumentNullException(nameof(testName)); | ||
} | ||
|
||
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) | ||
{ | ||
throw new ArgumentNullException(nameof(testname)); | ||
} | ||
|
||
GrowthBook gb = new(context); | ||
FeatureResult actual = gb.EvalFeature(key); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Collections.Generic; | ||
|
||
using GrowthBook.Tests.Json; | ||
|
||
using Newtonsoft.Json; | ||
|
||
using Xunit; | ||
|
||
namespace GrowthBook.Tests; | ||
|
||
public class NamespaceTupleConverterTests | ||
{ | ||
[Fact] | ||
public void CreateFromJson_NoFeatures_ShouldSucceed() | ||
{ | ||
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); | ||
} | ||
} |
Oops, something went wrong.