SerializableConfigurationPropertyTests.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #nullable enable
  2. using System.Reflection;
  3. using System.Text.Json.Serialization;
  4. using System.Text.Json.Serialization.Metadata;
  5. namespace Terminal.Gui.ConfigurationTests;
  6. public class SerializableConfigurationPropertyTests
  7. {
  8. [Fact]
  9. public void Test_SerializableConfigurationProperty_Types_Added_To_JsonSerializerContext ()
  10. {
  11. // The assembly containing the types to inspect
  12. var assembly = Assembly.GetAssembly (typeof (SourceGenerationContext));
  13. // Get all types from the assembly
  14. var types = assembly!.GetTypes ();
  15. // Find all properties with the SerializableConfigurationProperty attribute
  16. var properties = new List<PropertyInfo> ();
  17. foreach (var type in types)
  18. {
  19. properties.AddRange (type.GetProperties ().Where (p =>
  20. p.GetCustomAttributes (typeof (SerializableConfigurationProperty), false).Any ()));
  21. }
  22. // Get the types of the properties
  23. var propertyTypes = properties.Select (p => p.PropertyType).Distinct ();
  24. // Get the types registered in the JsonSerializerContext derived class
  25. var contextType = typeof (SourceGenerationContext);
  26. var contextTypes = GetRegisteredTypes (contextType);
  27. // Ensure all property types are included in the JsonSerializerContext derived class
  28. IEnumerable<Type> collection = contextTypes as Type [] ?? contextTypes.ToArray ();
  29. foreach (var type in propertyTypes)
  30. {
  31. Assert.Contains (type, collection);
  32. }
  33. // Ensure no property has the generic JsonStringEnumConverter<>
  34. foreach (var property in properties)
  35. {
  36. var jsonConverterAttributes = property.GetCustomAttributes (typeof (JsonConverterAttribute), false)
  37. .Cast<JsonConverterAttribute> ();
  38. foreach (var attribute in jsonConverterAttributes)
  39. {
  40. Assert.False (attribute.ConverterType!.IsGenericType &&
  41. attribute.ConverterType.GetGenericTypeDefinition () == typeof (JsonStringEnumConverter<>));
  42. }
  43. }
  44. // Find all classes with the JsonConverter attribute of type ScopeJsonConverter<>
  45. var classesWithScopeJsonConverter = types.Where (t =>
  46. t.GetCustomAttributes (typeof (JsonConverterAttribute), false)
  47. .Any (attr => ((JsonConverterAttribute)attr).ConverterType!.IsGenericType &&
  48. ((JsonConverterAttribute)attr).ConverterType!.GetGenericTypeDefinition () == typeof (ScopeJsonConverter<>)));
  49. // Ensure all these classes are included in the JsonSerializerContext derived class
  50. foreach (var type in classesWithScopeJsonConverter)
  51. {
  52. Assert.Contains (type, collection);
  53. }
  54. }
  55. private IEnumerable<Type> GetRegisteredTypes (Type contextType)
  56. {
  57. // Use reflection to find which types are registered in the JsonSerializerContext
  58. var registeredTypes = new List<Type> ();
  59. var properties = contextType.GetProperties (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
  60. foreach (var property in properties)
  61. {
  62. if (property.PropertyType.IsGenericType &&
  63. property.PropertyType.GetGenericTypeDefinition () == typeof (JsonTypeInfo<>))
  64. {
  65. registeredTypes.Add (property.PropertyType.GetGenericArguments () [0]);
  66. }
  67. }
  68. return registeredTypes.Distinct ();
  69. }
  70. }