Skip to content
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

Add some tests for DynamicClass with System.Text.Json #679

Merged
merged 10 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/DynamicClassFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,4 @@ private static string Escape(string str)
}
}
}
#endif
#endif
157 changes: 78 additions & 79 deletions test/System.Linq.Dynamic.Core.Tests/DynamicClassFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,87 @@
using FluentAssertions;
using Xunit;

namespace System.Linq.Dynamic.Core.Tests
namespace System.Linq.Dynamic.Core.Tests;

public class DynamicClassFactoryTests
{
public class DynamicClassFactoryTests
[Fact]
public void CreateGenericComparerTypeForInt()
{
// Assign
var comparer = new CustomCaseInsensitiveComparer();
var comparerGenericType = typeof(IComparer<>).MakeGenericType(typeof(int));
var a = 1;
var b = 2;

// Act
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());

// Assert
var instance = (IComparer<int>)Activator.CreateInstance(type);
int greaterThan = instance.Compare(a, b);
greaterThan.Should().Be(1);

int equal = instance.Compare(a, a);
equal.Should().Be(0);

int lessThan = instance.Compare(b, a);
lessThan.Should().Be(-1);
}

[Fact]
public void CreateGenericComparerTypeForString()
{
// Assign
var comparer = new CustomCaseInsensitiveComparer();
var comparerGenericType = typeof(IComparer<>).MakeGenericType(typeof(string));
var a = "a";
var b = "b";

// Act
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());

// Assert
var instance = (IComparer<string>)Activator.CreateInstance(type);
int greaterThan = instance.Compare(a, b);
greaterThan.Should().Be(1);

int equal = instance.Compare(a, a);
equal.Should().Be(0);

int lessThan = instance.Compare(b, a);
lessThan.Should().Be(-1);
}

[Fact]
public void CreateGenericComparerTypeForDateTime()
{
[Fact]
public void CreateGenericComparerTypeForInt()
{
// Assign
var comparer = new CustomCaseInsensitiveComparer();
var comparerGenericType = typeof(IComparer<>).MakeGenericType(typeof(int));
var a = 1;
var b = 2;

// Act
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());

// Assert
var instance = (IComparer<int>)Activator.CreateInstance(type);
int greaterThan = instance.Compare(a, b);
greaterThan.Should().Be(1);

int equal = instance.Compare(a, a);
equal.Should().Be(0);

int lessThan = instance.Compare(b, a);
lessThan.Should().Be(-1);
}

[Fact]
public void CreateGenericComparerTypeForString()
{
// Assign
var comparer = new CustomCaseInsensitiveComparer();
var comparerGenericType = typeof(IComparer<>).MakeGenericType(typeof(string));
var a = "a";
var b = "b";

// Act
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());

// Assert
var instance = (IComparer<string>)Activator.CreateInstance(type);
int greaterThan = instance.Compare(a, b);
greaterThan.Should().Be(1);

int equal = instance.Compare(a, a);
equal.Should().Be(0);

int lessThan = instance.Compare(b, a);
lessThan.Should().Be(-1);
}

[Fact]
public void CreateGenericComparerTypeForDateTime()
{
// Assign
var comparer = new CustomCaseInsensitiveComparer();
var comparerGenericType = typeof(IComparer<>).MakeGenericType(typeof(DateTime));
var a = new DateTime(2022, 1, 1);
var b = new DateTime(2023, 1, 1);

// Act
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());

// Assert
var instance = (IComparer<DateTime>)Activator.CreateInstance(type);
int greaterThan = instance.Compare(a, b);
greaterThan.Should().Be(1);

int equal = instance.Compare(a, a);
equal.Should().Be(0);

int lessThan = instance.Compare(b, a);
lessThan.Should().Be(-1);
}
// Assign
var comparer = new CustomCaseInsensitiveComparer();
var comparerGenericType = typeof(IComparer<>).MakeGenericType(typeof(DateTime));
var a = new DateTime(2022, 1, 1);
var b = new DateTime(2023, 1, 1);

// Act
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());

// Assert
var instance = (IComparer<DateTime>)Activator.CreateInstance(type);
int greaterThan = instance.Compare(a, b);
greaterThan.Should().Be(1);

int equal = instance.Compare(a, a);
equal.Should().Be(0);

int lessThan = instance.Compare(b, a);
lessThan.Should().Be(-1);
}
}

public class CustomCaseInsensitiveComparer : IComparer
public class CustomCaseInsensitiveComparer : IComparer
{
public int Compare(object x, object y)
{
public int Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare(y, x);
}
return new CaseInsensitiveComparer().Compare(y, x);
}
}
}
Loading