TypeDescriptorTests.cs 2.2 KB

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