TypeDescriptorTests.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Collections;
  2. using FluentAssertions;
  3. using Jint.Runtime.Interop;
  4. namespace Jint.Tests.Runtime;
  5. public class TypeDescriptorTests
  6. {
  7. [Fact]
  8. public void AnalyzesBclCollectionTypesCorrectly()
  9. {
  10. AssertInformation(typeof(ICollection), isArrayLike: true, iterable: true, isIntegerIndexedArray: false, shouldHaveLength: true);
  11. AssertInformation(typeof(IList), isArrayLike: true, iterable: true, isIntegerIndexedArray: true, shouldHaveLength: true);
  12. AssertInformation(typeof(ArrayList), isArrayLike: true, iterable: true, isIntegerIndexedArray: true, shouldHaveLength: true);
  13. AssertInformation(typeof(string[]), isArrayLike: true, iterable: true, isIntegerIndexedArray: true, shouldHaveLength: true);
  14. AssertInformation(typeof(List<string>), isArrayLike: true, iterable: true, isIntegerIndexedArray: true, shouldHaveLength: true);
  15. AssertInformation(typeof(IList<string>), isArrayLike: true, iterable: true, isIntegerIndexedArray: true, shouldHaveLength: true);
  16. AssertInformation(typeof(Dictionary<string, object>), isArrayLike: false, iterable: true, isIntegerIndexedArray: false, shouldHaveLength: false);
  17. AssertInformation(typeof(IDictionary<string, object>), isArrayLike: false, iterable: true, isIntegerIndexedArray: false, shouldHaveLength: false);
  18. AssertInformation(typeof(Dictionary<decimal, object>), isArrayLike: false, iterable: true, isIntegerIndexedArray: false, shouldHaveLength: false);
  19. AssertInformation(typeof(IDictionary<decimal, object>), isArrayLike: false, iterable: true, isIntegerIndexedArray: false, shouldHaveLength: false);
  20. AssertInformation(typeof(ISet<string>), isArrayLike: true, iterable: true, isIntegerIndexedArray: false, shouldHaveLength: true);
  21. }
  22. private static void AssertInformation(Type type, bool isArrayLike, bool iterable, bool isIntegerIndexedArray, bool shouldHaveLength)
  23. {
  24. var descriptor = TypeDescriptor.Get(type);
  25. descriptor.IsArrayLike.Should().Be(isArrayLike);
  26. descriptor.Iterable.Should().Be(iterable);
  27. descriptor.IsIntegerIndexed.Should().Be(isIntegerIndexedArray);
  28. if (shouldHaveLength)
  29. {
  30. descriptor.LengthProperty.Should().NotBeNull();
  31. }
  32. }
  33. }