Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Feb 7, 2021
1 parent ebca5d7 commit 15d3091
Show file tree
Hide file tree
Showing 47 changed files with 856 additions and 295 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Thinktecture.AspNetCore.ModelBinding;
using Thinktecture.Runtime.Tests.AspNetCore.ModelBinding.TestClasses;
using Thinktecture.Runtime.Tests.TestEnums;
using Thinktecture.Runtime.Tests.TestValueTypes;
using Xunit;

namespace Thinktecture.Runtime.Tests.AspNetCore.ModelBinding.ValueTypeModelBinderProviderTests
Expand All @@ -15,29 +16,31 @@ public class GetBinder
[Fact]
public void Should_return_binder_for_int_based_enum()
{
var binder = GetModelBinder<IntBasedEnum>();
binder.Should().BeOfType<ValueTypeModelBinder<IntBasedEnum, int>>();
var binder = GetModelBinder<IntegerEnum>();
binder.Should().BeOfType<ValueTypeModelBinder<IntegerEnum, int>>();
}

[Fact]
public void Should_return_binder_for_string_based_enum()
{
var binder = GetModelBinder<StringBasedEnum>();
binder.Should().BeOfType<ValueTypeModelBinder<StringBasedEnum, string>>();
GetModelBinder<TestEnum>().Should().BeOfType<ValueTypeModelBinder<TestEnum, string>>();

GetModelBinder<ExtensibleTestEnum>().Should().BeOfType<ValueTypeModelBinder<ExtensibleTestEnum, string>>();
GetModelBinder<ExtendedTestEnum>().Should().BeOfType<ValueTypeModelBinder<ExtendedTestEnum, string>>();
GetModelBinder<DifferentAssemblyExtendedTestEnum>().Should().BeOfType<ValueTypeModelBinder<DifferentAssemblyExtendedTestEnum, string>>();
}

[Fact]
public void Should_return_binder_for_string_based_value_type()
{
var binder = GetModelBinder<StringBasedValueType>();
binder.Should().BeOfType<ValueTypeModelBinder<StringBasedValueType, string>>();
var binder = GetModelBinder<StringBasedReferenceValueType>();
binder.Should().BeOfType<ValueTypeModelBinder<StringBasedReferenceValueType, string>>();
}

[Fact]
public void Should_return_null_for_non_enums_and_non_value_types()
{
var binder = GetModelBinder<GetBinder>();
binder.Should().BeNull();
GetModelBinder<GetBinder>().Should().BeNull();
}

private static IModelBinder GetModelBinder<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
using Microsoft.Extensions.Primitives;
using Thinktecture.AspNetCore.ModelBinding;
using Thinktecture.Internal;
using Thinktecture.Runtime.Tests.AspNetCore.ModelBinding.TestClasses;
using Thinktecture.Runtime.Tests.TestEnums;
using Thinktecture.Runtime.Tests.TestValueTypes;
using Xunit;

namespace Thinktecture.Runtime.Tests.AspNetCore.ModelBinding.ValueTypeModelBinderTests
Expand All @@ -20,17 +21,17 @@ public class BindModelAsync
[Fact]
public async Task Should_bind_int_based_enum()
{
var ctx = await BindAsync<IntBasedEnum, int>(IntBasedEnum.Validate, "1");
var ctx = await BindAsync<IntegerEnum, int>(IntegerEnum.Validate, "1");

ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
ctx.Result.Model.Should().Be(IntBasedEnum.Value1);
ctx.Result.Model.Should().Be(IntegerEnum.Item1);
}

[Fact]
public async Task Should_return_null_if_value_is_empty_string()
{
var ctx = await BindAsync<IntBasedEnum, int>(IntBasedEnum.Validate, String.Empty);
var ctx = await BindAsync<IntegerEnum, int>(IntegerEnum.Validate, String.Empty);

ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
Expand All @@ -40,7 +41,7 @@ public async Task Should_return_null_if_value_is_empty_string()
[Fact]
public async Task Should_not_bind_if_value_is_null()
{
var ctx = await BindAsync<IntBasedEnum, int>(IntBasedEnum.Validate, null);
var ctx = await BindAsync<IntegerEnum, int>(IntegerEnum.Validate, null);

ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeFalse();
Expand All @@ -49,47 +50,62 @@ public async Task Should_not_bind_if_value_is_null()
[Fact]
public async Task Should_return_error_if_value_not_exists_in_int_based_enum()
{
var ctx = await BindAsync<IntBasedEnum, int>(IntBasedEnum.Validate, "42");
var ctx = await BindAsync<IntegerEnum, int>(IntegerEnum.Validate, "42");

ctx.ModelState.ErrorCount.Should().Be(1);
ctx.ModelState[ctx.ModelName].Errors.Should().BeEquivalentTo(new ModelError("The enumeration item of type 'IntBasedEnum' with identifier '42' is not valid."));
ctx.ModelState[ctx.ModelName].Errors.Should().BeEquivalentTo(new ModelError("The enumeration item of type 'IntegerEnum' with identifier '42' is not valid."));
ctx.Result.IsModelSet.Should().BeFalse();
}

[Fact]
public async Task Should_return_error_if_value_not_matching_key_type_of_an_int_based_enum()
{
var ctx = await BindAsync<IntBasedEnum, int>(IntBasedEnum.Validate, "A");
var ctx = await BindAsync<IntegerEnum, int>(IntegerEnum.Validate, "item1");

ctx.ModelState.ErrorCount.Should().Be(1);
ctx.ModelState[ctx.ModelName].Errors.Should().BeEquivalentTo(new ModelError("The value 'A' is not valid."));
ctx.ModelState[ctx.ModelName].Errors.Should().BeEquivalentTo(new ModelError("The value 'item1' is not valid."));
ctx.Result.IsModelSet.Should().BeFalse();
}

[Fact]
public async Task Should_bind_string_based_enum()
{
var ctx = await BindAsync<StringBasedEnum, string>(StringBasedEnum.Validate, "A");
var ctx = await BindAsync<TestEnum, string>(TestEnum.Validate, "item1");
ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
ctx.Result.Model.Should().Be(TestEnum.Item1);


ctx = await BindAsync<ExtensibleTestEnum, string>(ExtensibleTestEnum.Validate, "item1");
ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
ctx.Result.Model.Should().Be(ExtensibleTestEnum.Item1);

ctx = await BindAsync<ExtendedTestEnum, string>(ExtendedTestEnum.Validate, "item1");
ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
ctx.Result.Model.Should().Be(ExtendedTestEnum.Item1);

ctx = await BindAsync<DifferentAssemblyExtendedTestEnum, string>(DifferentAssemblyExtendedTestEnum.Validate, "item1");
ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
ctx.Result.Model.Should().Be(StringBasedEnum.ValueA);
ctx.Result.Model.Should().Be(DifferentAssemblyExtendedTestEnum.Item1);
}

[Fact]
public async Task Should_bind_string_based_value_type()
{
var ctx = await BindAsync<StringBasedValueType, string>(StringBasedValueType.TryCreate, "Value");
var ctx = await BindAsync<StringBasedReferenceValueType, string>(StringBasedReferenceValueType.TryCreate, "Value");

ctx.ModelState.ErrorCount.Should().Be(0);
ctx.Result.IsModelSet.Should().BeTrue();
ctx.Result.Model.Should().Be(StringBasedValueType.Create("Value"));
ctx.Result.Model.Should().Be(StringBasedReferenceValueType.Create("Value"));
}

[Fact]
public async Task Should_return_error_if_value_violates_validation_rules()
{
var ctx = await BindAsync<StringBasedValueType, string>(StringBasedValueType.TryCreate, "A");
var ctx = await BindAsync<StringBasedReferenceValueType, string>(StringBasedReferenceValueType.TryCreate, "A");

ctx.ModelState.ErrorCount.Should().Be(1);
ctx.ModelState[ctx.ModelName].Errors.Should().BeEquivalentTo(new ModelError("Property cannot be 1 character long."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
<ProjectReference Include="..\Thinktecture.Runtime.Extensions.Tests.Shared\Thinktecture.Runtime.Extensions.Tests.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Thinktecture.Runtime.Extensions.Tests\TestEnums\*.*">
<Link>%(RecursiveDir)TestEnums/%(FileName)%(Extension)</Link>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using Thinktecture.Runtime.Tests.TestEnums;
using Thinktecture.Runtime.Tests.Text.Json.Serialization.ValueTypeJsonConverterFactoryTests.TestClasses;

namespace Thinktecture.Runtime.Tests.Text.Json.Serialization.ValueTypeJsonConverterFactoryTests
{
public class JsonTestsBase
{
public static IEnumerable<object[]> DataForStringBasedEnumTest => new[]
{
new object[] { null, "null" },
new object[] { TestEnum.Item1, "\"item1\"" },
new object[] { TestEnum.Item2, "\"item2\"" }
};

public static IEnumerable<object[]> DataForExtensibleEnumTest => new[]
{
new object[] { null, "null" },
new object[] { ExtensibleTestEnum.Item1, "\"Item1\"" },
};

public static IEnumerable<object[]> DataForExtendedEnumTest => new[]
{
new object[] { null, "null" },
new object[] { ExtendedTestEnum.Item1, "\"Item1\"" },
new object[] { ExtendedTestEnum.Item2, "\"Item2\"" }
};

public static IEnumerable<object[]> DataForDifferentAssemblyExtendedTestEnumTest => new[]
{
new object[] { null, "null" },
new object[] { DifferentAssemblyExtendedTestEnum.Item1, "\"Item1\"" },
new object[] { DifferentAssemblyExtendedTestEnum.Item2, "\"Item2\"" }
};

public static IEnumerable<object[]> DataForClassWithStringBasedEnumTest => new[]
{
new object[] { null, "null" },
new object[] { new ClassWithStringBasedEnum(), "{}", true },
new object[] { new ClassWithStringBasedEnum(), "{\"Enum\":null}" },
new object[] { new ClassWithStringBasedEnum(TestEnum.Item1), "{\"Enum\":\"item1\"}" },
new object[] { new ClassWithStringBasedEnum(TestEnum.Item2), "{\"Enum\":\"item2\"}" }
};

public static IEnumerable<object[]> DataForIntBasedEnumTest => new[]
{
new object[] { null, "null" },
new object[] { IntegerEnum.Item1, "1" },
new object[] { IntegerEnum.Item2, "2" }
};

public static IEnumerable<object[]> DataForClassWithIntBasedEnumTest => new[]
{
new object[] { null, "null" },
new object[] { new ClassWithIntBasedEnum(), "{}", true },
new object[] { new ClassWithIntBasedEnum(), "{\"Enum\":null}" },
new object[] { new ClassWithIntBasedEnum(IntegerEnum.Item1), "{\"Enum\":1}" },
new object[] { new ClassWithIntBasedEnum(IntegerEnum.Item2), "{\"Enum\":2}" }
};
}
}
Loading

0 comments on commit 15d3091

Please sign in to comment.