| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | namespace DotNetApiDiff.Models; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Represents the complete comparison result between two API surfaces |
| | | 6 | | /// </summary> |
| | | 7 | | public class ApiComparison |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// List of API members that were added |
| | | 11 | | /// </summary> |
| | 97 | 12 | | public List<ApiChange> Additions { get; set; } = new List<ApiChange>(); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// List of API members that were removed |
| | | 16 | | /// </summary> |
| | 94 | 17 | | public List<ApiChange> Removals { get; set; } = new List<ApiChange>(); |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// List of API members that were modified |
| | | 21 | | /// </summary> |
| | 88 | 22 | | public List<ApiChange> Modifications { get; set; } = new List<ApiChange>(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// List of API members that were intentionally excluded |
| | | 26 | | /// </summary> |
| | 81 | 27 | | public List<ApiChange> Excluded { get; set; } = new List<ApiChange>(); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Summary statistics of the comparison |
| | | 31 | | /// </summary> |
| | 47 | 32 | | public ComparisonSummary Summary { get; set; } = new ComparisonSummary(); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the validation error message if the comparison is invalid, empty if valid |
| | | 36 | | /// </summary> |
| | 46 | 37 | | public string InvalidMessage { get; private set; } = string.Empty; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets all changes as a single collection |
| | | 41 | | /// </summary> |
| | | 42 | | public IEnumerable<ApiChange> AllChanges |
| | | 43 | | { |
| | | 44 | | get |
| | 32 | 45 | | { |
| | 32 | 46 | | return Additions.Concat(Removals).Concat(Modifications).Concat(Excluded); |
| | 32 | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets whether any breaking changes were detected |
| | | 52 | | /// </summary> |
| | 18 | 53 | | public bool HasBreakingChanges => AllChanges.Any(c => c.IsBreakingChange); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the total number of changes (excluding excluded items) |
| | | 57 | | /// </summary> |
| | 3 | 58 | | public int TotalChanges => Additions.Count + Removals.Count + Modifications.Count; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the total number of breaking changes |
| | | 62 | | /// </summary> |
| | 23 | 63 | | public int BreakingChangesCount => AllChanges.Count(c => c.IsBreakingChange); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Validates the ApiComparison instance |
| | | 67 | | /// </summary> |
| | | 68 | | /// <returns>True if valid, false otherwise</returns> |
| | | 69 | | public bool IsValid() |
| | 12 | 70 | | { |
| | | 71 | | // Clear any previous validation message |
| | 12 | 72 | | InvalidMessage = string.Empty; |
| | | 73 | | |
| | 12 | 74 | | var errors = new List<string>(); |
| | | 75 | | |
| | | 76 | | // Validate all changes |
| | 12 | 77 | | var allChanges = AllChanges.ToList(); |
| | 25 | 78 | | var invalidChanges = allChanges.Where(c => !c.IsValid()).ToList(); |
| | 12 | 79 | | if (invalidChanges.Any()) |
| | 5 | 80 | | { |
| | 10 | 81 | | errors.Add($"Found {invalidChanges.Count} invalid change(s): {string.Join(", ", invalidChanges.Select(c => $ |
| | 5 | 82 | | } |
| | | 83 | | |
| | | 84 | | // Validate that additions only contain Added changes |
| | 18 | 85 | | var wrongAdditions = Additions.Where(c => c.Type != ChangeType.Added).ToList(); |
| | 12 | 86 | | if (wrongAdditions.Any()) |
| | 1 | 87 | | { |
| | 2 | 88 | | errors.Add($"Additions collection contains {wrongAdditions.Count} change(s) with wrong type: {string.Join(", |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | // Validate that removals only contain Removed changes |
| | 17 | 92 | | var wrongRemovals = Removals.Where(c => c.Type != ChangeType.Removed).ToList(); |
| | 12 | 93 | | if (wrongRemovals.Any()) |
| | 1 | 94 | | { |
| | 2 | 95 | | errors.Add($"Removals collection contains {wrongRemovals.Count} change(s) with wrong type: {string.Join(", " |
| | 1 | 96 | | } |
| | | 97 | | |
| | | 98 | | // Validate that modifications only contain Modified changes |
| | 13 | 99 | | var wrongModifications = Modifications.Where(c => c.Type != ChangeType.Modified).ToList(); |
| | 12 | 100 | | if (wrongModifications.Any()) |
| | 1 | 101 | | { |
| | 2 | 102 | | errors.Add($"Modifications collection contains {wrongModifications.Count} change(s) with wrong type: {string |
| | 1 | 103 | | } |
| | | 104 | | |
| | | 105 | | // Validate that excluded only contain Excluded changes |
| | 13 | 106 | | var wrongExcluded = Excluded.Where(c => c.Type != ChangeType.Excluded).ToList(); |
| | 12 | 107 | | if (wrongExcluded.Any()) |
| | 1 | 108 | | { |
| | 2 | 109 | | errors.Add($"Excluded collection contains {wrongExcluded.Count} change(s) with wrong type: {string.Join(", " |
| | 1 | 110 | | } |
| | | 111 | | |
| | 12 | 112 | | if (errors.Count > 0) |
| | 6 | 113 | | { |
| | 6 | 114 | | InvalidMessage = string.Join("; ", errors); |
| | 6 | 115 | | return false; |
| | | 116 | | } |
| | | 117 | | |
| | 6 | 118 | | return true; |
| | 12 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Updates the summary statistics based on current changes |
| | | 123 | | /// </summary> |
| | | 124 | | public void UpdateSummary() |
| | 2 | 125 | | { |
| | 2 | 126 | | Summary.AddedCount = Additions.Count; |
| | 2 | 127 | | Summary.RemovedCount = Removals.Count; |
| | 2 | 128 | | Summary.ModifiedCount = Modifications.Count; |
| | 2 | 129 | | Summary.BreakingChangesCount = BreakingChangesCount; |
| | 2 | 130 | | } |
| | | 131 | | } |