|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 4 | +using SchemaMagic.Core; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace SchemaMagic.Tests; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Tests to verify that all modern C# property patterns are correctly extracted |
| 11 | +/// </summary> |
| 12 | +public class PropertyExtractionTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void ExtractProperties_AutoImplementedProperty_ShouldBeFound() |
| 16 | + { |
| 17 | + // Arrange |
| 18 | + var sourceCode = @" |
| 19 | + public class TestEntity |
| 20 | + { |
| 21 | + public int Id { get; set; } |
| 22 | + public string Name { get; set; } |
| 23 | + } |
| 24 | + "; |
| 25 | + |
| 26 | + // Act |
| 27 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 28 | + |
| 29 | + // Assert |
| 30 | + Assert.Equal(2, properties.Count); |
| 31 | + Assert.Contains(properties, p => p.Name == "Id"); |
| 32 | + Assert.Contains(properties, p => p.Name == "Name"); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void ExtractProperties_RequiredAutoProperty_ShouldBeFound() |
| 37 | + { |
| 38 | + // Arrange |
| 39 | + var sourceCode = @" |
| 40 | + public class TestEntity |
| 41 | + { |
| 42 | + public required string Name { get; set; } |
| 43 | + public required int Age { get; init; } |
| 44 | + } |
| 45 | + "; |
| 46 | + |
| 47 | + // Act |
| 48 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 49 | + |
| 50 | + // Assert |
| 51 | + Assert.Equal(2, properties.Count); |
| 52 | + Assert.Contains(properties, p => p.Name == "Name"); |
| 53 | + Assert.Contains(properties, p => p.Name == "Age"); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void ExtractProperties_InitOnlyProperty_ShouldBeFound() |
| 58 | + { |
| 59 | + // Arrange |
| 60 | + var sourceCode = @" |
| 61 | + public class TestEntity |
| 62 | + { |
| 63 | + public int Id { get; init; } |
| 64 | + public string Name { get; init; } |
| 65 | + } |
| 66 | + "; |
| 67 | + |
| 68 | + // Act |
| 69 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 70 | + |
| 71 | + // Assert |
| 72 | + Assert.Equal(2, properties.Count); |
| 73 | + Assert.Contains(properties, p => p.Name == "Id"); |
| 74 | + Assert.Contains(properties, p => p.Name == "Name"); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void ExtractProperties_ExpressionBodiedProperty_ShouldBeFound() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + var sourceCode = @" |
| 82 | + public class TestEntity |
| 83 | + { |
| 84 | + public string FirstName { get; set; } |
| 85 | + public string LastName { get; set; } |
| 86 | + public string FullName => $""{FirstName} {LastName}""; |
| 87 | + } |
| 88 | + "; |
| 89 | + |
| 90 | + // Act |
| 91 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 92 | + |
| 93 | + // Assert |
| 94 | + Assert.Equal(3, properties.Count); |
| 95 | + Assert.Contains(properties, p => p.Name == "FullName"); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void ExtractProperties_NullableProperties_ShouldPreserveNullability() |
| 100 | + { |
| 101 | + // Arrange |
| 102 | + var sourceCode = @" |
| 103 | + public class TestEntity |
| 104 | + { |
| 105 | + public string Name { get; set; } |
| 106 | + public string? OptionalName { get; set; } |
| 107 | + public int Age { get; set; } |
| 108 | + public int? OptionalAge { get; set; } |
| 109 | + } |
| 110 | + "; |
| 111 | + |
| 112 | + // Act |
| 113 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 114 | + |
| 115 | + // Assert |
| 116 | + Assert.Equal(4, properties.Count); |
| 117 | + |
| 118 | + var optionalName = properties.First(p => p.Name == "OptionalName"); |
| 119 | + Assert.Contains("?", optionalName.Type); // Nullable marker preserved |
| 120 | + |
| 121 | + var optionalAge = properties.First(p => p.Name == "OptionalAge"); |
| 122 | + Assert.Contains("?", optionalAge.Type); // Nullable marker preserved |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void ExtractProperties_MixedPropertyPatterns_ShouldFindAll() |
| 127 | + { |
| 128 | + // Arrange |
| 129 | + var sourceCode = @" |
| 130 | + public class TestEntity |
| 131 | + { |
| 132 | + public int Id { get; set; } // Auto-implemented |
| 133 | + public required string Name { get; set; } // Required auto |
| 134 | + public string? Description { get; init; } // Init-only nullable |
| 135 | + public DateTime CreatedAt { get; } = DateTime.Now; // Read-only with initializer |
| 136 | + public string DisplayName => Name.ToUpper(); // Expression-bodied |
| 137 | + } |
| 138 | + "; |
| 139 | + |
| 140 | + // Act |
| 141 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 142 | + |
| 143 | + // Assert |
| 144 | + Assert.Equal(5, properties.Count); |
| 145 | + Assert.Contains(properties, p => p.Name == "Id"); |
| 146 | + Assert.Contains(properties, p => p.Name == "Name"); |
| 147 | + Assert.Contains(properties, p => p.Name == "Description"); |
| 148 | + Assert.Contains(properties, p => p.Name == "CreatedAt"); |
| 149 | + Assert.Contains(properties, p => p.Name == "DisplayName"); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public void ExtractProperties_NavigationProperties_ShouldBeFound() |
| 154 | + { |
| 155 | + // Arrange |
| 156 | + var sourceCode = @" |
| 157 | + public class TestEntity |
| 158 | + { |
| 159 | + public int Id { get; set; } |
| 160 | + public int ParentId { get; set; } |
| 161 | + public TestEntity Parent { get; set; } = null!; |
| 162 | + public ICollection<TestEntity> Children { get; set; } = new List<TestEntity>(); |
| 163 | + } |
| 164 | + "; |
| 165 | + |
| 166 | + // Act |
| 167 | + var properties = ExtractPropertiesFromSource(sourceCode); |
| 168 | + |
| 169 | + // Assert |
| 170 | + Assert.Equal(4, properties.Count); |
| 171 | + Assert.Contains(properties, p => p.Name == "ParentId"); |
| 172 | + Assert.Contains(properties, p => p.Name == "Parent"); |
| 173 | + Assert.Contains(properties, p => p.Name == "Children"); |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Helper method to extract properties from source code |
| 178 | + /// </summary> |
| 179 | + private List<Core.PropertyInfo> ExtractPropertiesFromSource(string sourceCode) |
| 180 | + { |
| 181 | + var tree = CSharpSyntaxTree.ParseText(sourceCode); |
| 182 | + var root = tree.GetCompilationUnitRoot(); |
| 183 | + |
| 184 | + var entityClass = root.DescendantNodes() |
| 185 | + .OfType<ClassDeclarationSyntax>() |
| 186 | + .First(); |
| 187 | + |
| 188 | + // Extract properties using the same logic as CoreSchemaAnalysisService |
| 189 | + var properties = new List<Core.PropertyInfo>(); |
| 190 | + |
| 191 | + var propertyDeclarations = entityClass.Members |
| 192 | + .OfType<PropertyDeclarationSyntax>() |
| 193 | + .Where(p => |
| 194 | + // Has explicit accessor list with a getter or init |
| 195 | + (p.AccessorList?.Accessors.Any(a => |
| 196 | + a.Kind() == SyntaxKind.GetAccessorDeclaration || |
| 197 | + a.Kind() == SyntaxKind.InitAccessorDeclaration) == true) || |
| 198 | + // OR is an expression-bodied property (no accessor list) |
| 199 | + p.ExpressionBody != null |
| 200 | + ); |
| 201 | + |
| 202 | + foreach (var property in propertyDeclarations) |
| 203 | + { |
| 204 | + properties.Add(new Core.PropertyInfo |
| 205 | + { |
| 206 | + Name = property.Identifier.Text, |
| 207 | + Type = property.Type.ToString().Trim(), |
| 208 | + IsKey = property.Identifier.Text.Equals("Id", StringComparison.OrdinalIgnoreCase), |
| 209 | + IsForeignKey = property.Identifier.Text.EndsWith("Id", StringComparison.OrdinalIgnoreCase) && |
| 210 | + !property.Identifier.Text.Equals("Id", StringComparison.OrdinalIgnoreCase) |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + return properties; |
| 215 | + } |
| 216 | +} |
0 commit comments