-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/moved to xunit #19
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace Growthbook.Tests.Json; | ||
namespace GrowthBook.Tests.Json; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like I wasn't the only one misspelling GrowthBook |
||
|
||
public static class JsonTestHelpers | ||
{ | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While you're touching it. You might as well be updating the package references, because this package for example is already at version
7.x.x