|
| 1 | +using FluentAssertions; |
| 2 | +using JsonApiDotNetCore; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace JsonApiDotNetCoreTests.UnitTests.TypeConversion; |
| 6 | + |
| 7 | +public sealed class CollectionConverterTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Finds_element_type_for_generic_list() |
| 11 | + { |
| 12 | + // Arrange |
| 13 | + Type sourceType = typeof(List<string>); |
| 14 | + |
| 15 | + // Act |
| 16 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 17 | + |
| 18 | + // Assert |
| 19 | + elementType.Should().Be<string>(); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void Finds_element_type_for_generic_enumerable() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + Type sourceType = typeof(IEnumerable<string>); |
| 27 | + |
| 28 | + // Act |
| 29 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 30 | + |
| 31 | + // Assert |
| 32 | + elementType.Should().Be<string>(); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void Finds_element_type_for_custom_generic_collection_with_multiple_type_parameters() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + Type sourceType = typeof(CustomCollection<int, string>); |
| 40 | + |
| 41 | + // Act |
| 42 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 43 | + |
| 44 | + // Assert |
| 45 | + elementType.Should().Be<string>(); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void Finds_element_type_for_custom_non_generic_collection() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + Type sourceType = typeof(CustomCollectionOfIntString); |
| 53 | + |
| 54 | + // Act |
| 55 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 56 | + |
| 57 | + // Assert |
| 58 | + elementType.Should().Be<string>(); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Finds_no_element_type_for_non_generic_type() |
| 63 | + { |
| 64 | + // Arrange |
| 65 | + Type sourceType = typeof(int); |
| 66 | + |
| 67 | + // Act |
| 68 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 69 | + |
| 70 | + // Assert |
| 71 | + elementType.Should().BeNull(); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void Finds_no_element_type_for_non_collection_generic_type() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + Type sourceType = typeof(Tuple<int, string>); |
| 79 | + |
| 80 | + // Act |
| 81 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 82 | + |
| 83 | + // Assert |
| 84 | + elementType.Should().BeNull(); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void Finds_no_element_type_for_unbound_generic_type() |
| 89 | + { |
| 90 | + // Arrange |
| 91 | + Type sourceType = typeof(List<>); |
| 92 | + |
| 93 | + // Act |
| 94 | + Type? elementType = CollectionConverter.Instance.FindCollectionElementType(sourceType); |
| 95 | + |
| 96 | + // Assert |
| 97 | + elementType.Should().BeNull(); |
| 98 | + } |
| 99 | + |
| 100 | + // ReSharper disable once UnusedTypeParameter |
| 101 | + private class CustomCollection<TOther, TElement> : List<TElement>; |
| 102 | + |
| 103 | + private sealed class CustomCollectionOfIntString : CustomCollection<int, string>; |
| 104 | +} |
0 commit comments