Skip to content

Commit

Permalink
Add some tests for DynamicClass with System.Text.Json (#679)
Browse files Browse the repository at this point in the history
* Add unit test for issue 593

* Add some tests for DynamicClass with System.Text.Josn

* Add / update tests

* .

* any

* .

* tests

* f
  • Loading branch information
StefH authored Mar 11, 2023
1 parent 7f45e14 commit 76ac418
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 264 deletions.
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

0 comments on commit 76ac418

Please sign in to comment.