Skip to content

Commit

Permalink
Merge pull request #19 from Inforitnl/fix/moved-to-xunit
Browse files Browse the repository at this point in the history
Fix/moved to xunit
  • Loading branch information
darklordzw authored Nov 7, 2023
2 parents 4e993f1 + eaa6708 commit e008a0d
Show file tree
Hide file tree
Showing 18 changed files with 372 additions and 374 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
run: dotnet build GrowthBook/GrowthBook.csproj --configuration Release

# current tests are written for visual studio only
# - name: Test
# run: dotnet test Test/Growthook.Tests.csproj --configuration Release --no-build
- name: Test
run: dotnet test GrowthBook.Tests/GrowthBook.Tests.csproj --logger:"console;verbosity=normal"

- name: pack
run: dotnet pack --configuration Release --no-build --output dist GrowthBook/GrowthBook.csproj
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.2]

- ci: moved from MSTest to Xunit

## [0.1.1]

- Handle null namespace property.
Expand Down
51 changes: 51 additions & 0 deletions GrowthBook.Tests/GrowthBook.Tests.csproj
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>
156 changes: 156 additions & 0 deletions GrowthBook.Tests/GrowthBookTests.cs
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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using System.Reflection;

namespace Growthbook.Tests.Json;
namespace GrowthBook.Tests.Json;

public static class JsonTestHelpers
{
Expand Down
File renamed without changes.
File renamed without changes.
40 changes: 40 additions & 0 deletions GrowthBook.Tests/NamespaceTupleConverterTests.cs
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);
}
}
Loading

0 comments on commit e008a0d

Please sign in to comment.