|
| 1 | +using System.Text.Json; |
| 2 | +using S1APICoverageAnalyzer.Analysis; |
| 3 | +using S1APICoverageAnalyzer.Models; |
| 4 | +using S1APICoverageAnalyzer.Output; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace S1API.Tests.Coverage; |
| 8 | + |
| 9 | +public sealed class CoverageAnalyzerTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void ApiAnalyzer_RecordsExplicitMappingsAndUnwrapsElementTypes() |
| 13 | + { |
| 14 | + var apiAssembly = typeof(global::S1API.Temperature.TemperatureUtility).Assembly; |
| 15 | + var analyzer = new ApiAssemblyAnalyzer(apiAssembly, apiAssembly.Location); |
| 16 | + |
| 17 | + analyzer.Analyze(); |
| 18 | + |
| 19 | + IReadOnlyDictionary<string, string> explicitMappings = |
| 20 | + analyzer.GetExplicitCoverageMappings(); |
| 21 | + Assert.Equal( |
| 22 | + "S1API.Temperature.TemperatureEmitterInfo", |
| 23 | + explicitMappings["ScheduleOne.Temperature.TemperatureEmitterInfo"]); |
| 24 | + Assert.Equal( |
| 25 | + "S1API.Temperature.TemperatureUtility", |
| 26 | + explicitMappings["ScheduleOne.Temperature.TemperatureUtility"]); |
| 27 | + |
| 28 | + Assert.Contains( |
| 29 | + "ScheduleOne.Temperature.TemperatureEmitterInfo", |
| 30 | + analyzer.GetWrappedGameTypes()); |
| 31 | + Assert.DoesNotContain( |
| 32 | + "ScheduleOne.Temperature.TemperatureEmitterInfo[]", |
| 33 | + analyzer.GetWrappedGameTypes()); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void Calculate_ReportsProvenanceAndMatchStrategyForEveryCoveredType() |
| 38 | + { |
| 39 | + var gameTypes = new List<GameType> |
| 40 | + { |
| 41 | + GameType("ScheduleOne.Temperature.TemperatureUtility"), |
| 42 | + GameType("ScheduleOne.Items.ItemDefinition"), |
| 43 | + GameType("ScheduleOne.Casino.BlackjackGameController+EStage"), |
| 44 | + GameType("ScheduleOne.Dialogue.DialogueController.Node"), |
| 45 | + GameType("ScheduleOne.Vehicles.Modification.EVehicleColor") |
| 46 | + }; |
| 47 | + var apiTypes = new List<ApiTypeInfo> |
| 48 | + { |
| 49 | + ApiType( |
| 50 | + "S1API.Casino.BlackjackGame", |
| 51 | + "BlackjackGame", |
| 52 | + "ScheduleOne.Casino.BlackjackGameController"), |
| 53 | + ApiType( |
| 54 | + "S1API.Dialogue.DialogueNode", |
| 55 | + "DialogueNode", |
| 56 | + "ScheduleOne.Dialogue.DialogueController+Node"), |
| 57 | + ApiType( |
| 58 | + "S1API.Items.ItemDefinition", |
| 59 | + "ItemDefinition", |
| 60 | + "ScheduleOne.Items.ItemDefinition"), |
| 61 | + ApiType( |
| 62 | + "S1API.Temperature.TemperatureUtility", |
| 63 | + "TemperatureUtility", |
| 64 | + "ScheduleOne.Temperature.TemperatureUtility"), |
| 65 | + ApiType( |
| 66 | + "S1API.Vehicles.VehicleColor", |
| 67 | + "VehicleColor", |
| 68 | + "ScheduleOne.Vehicles.Modification.VehicleColors") |
| 69 | + }; |
| 70 | + var explicitMappings = new Dictionary<string, string>(StringComparer.Ordinal) |
| 71 | + { |
| 72 | + ["ScheduleOne.Temperature.TemperatureUtility"] = |
| 73 | + "S1API.Temperature.TemperatureUtility" |
| 74 | + }; |
| 75 | + |
| 76 | + CoverageResult result = Calculate(gameTypes, apiTypes, explicitMappings); |
| 77 | + |
| 78 | + Assert.Collection( |
| 79 | + result.CoveredTypes.OrderBy(type => type.FullName, StringComparer.Ordinal), |
| 80 | + type => AssertMatch(type, "S1API.Casino.BlackjackGame", CoverageMatchStrategy.Nested), |
| 81 | + type => AssertMatch(type, "S1API.Dialogue.DialogueNode", CoverageMatchStrategy.Normalized), |
| 82 | + type => AssertMatch(type, "S1API.Items.ItemDefinition", CoverageMatchStrategy.Exact), |
| 83 | + type => AssertMatch(type, "S1API.Temperature.TemperatureUtility", CoverageMatchStrategy.Explicit), |
| 84 | + type => AssertMatch(type, "S1API.Vehicles.VehicleColor", CoverageMatchStrategy.Fuzzy)); |
| 85 | + Assert.Empty(result.UncoveredTypes); |
| 86 | + |
| 87 | + using JsonDocument report = JsonDocument.Parse(ReportGenerator.GenerateJsonReport(result)); |
| 88 | + foreach (JsonElement coveredType in report.RootElement.GetProperty("coveredTypes").EnumerateArray()) |
| 89 | + { |
| 90 | + Assert.False(string.IsNullOrWhiteSpace(coveredType.GetProperty("coveredBy").GetString())); |
| 91 | + Assert.False(string.IsNullOrWhiteSpace(coveredType.GetProperty("matchStrategy").GetString())); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void Calculate_DoesNotFuzzyMatchSimilarUnrelatedType() |
| 97 | + { |
| 98 | + GameType unrelatedGameType = |
| 99 | + GameType("ScheduleOne.Vehicles.VehicleSeatSnapshot"); |
| 100 | + ApiTypeInfo similarlyNamedApiType = ApiType( |
| 101 | + "S1API.Items.VehicleSeat", |
| 102 | + "VehicleSeat", |
| 103 | + "ScheduleOne.ItemFramework.ItemSlot"); |
| 104 | + |
| 105 | + CoverageResult result = Calculate( |
| 106 | + new List<GameType> { unrelatedGameType }, |
| 107 | + new List<ApiTypeInfo> { similarlyNamedApiType }, |
| 108 | + new Dictionary<string, string>()); |
| 109 | + |
| 110 | + Assert.Empty(result.CoveredTypes); |
| 111 | + Assert.Same(unrelatedGameType, Assert.Single(result.UncoveredTypes)); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void Calculate_UsesDeterministicApiTypeForEquivalentMatches() |
| 116 | + { |
| 117 | + GameType gameType = GameType("ScheduleOne.Items.ItemDefinition"); |
| 118 | + var apiTypes = new List<ApiTypeInfo> |
| 119 | + { |
| 120 | + ApiType( |
| 121 | + "S1API.Zeta.ItemDefinition", |
| 122 | + "ItemDefinition", |
| 123 | + gameType.FullName), |
| 124 | + ApiType( |
| 125 | + "S1API.Alpha.ItemDefinition", |
| 126 | + "ItemDefinition", |
| 127 | + gameType.FullName) |
| 128 | + }; |
| 129 | + |
| 130 | + CoverageResult result = Calculate( |
| 131 | + new List<GameType> { gameType }, |
| 132 | + apiTypes, |
| 133 | + new Dictionary<string, string>()); |
| 134 | + |
| 135 | + AssertMatch( |
| 136 | + Assert.Single(result.CoveredTypes), |
| 137 | + "S1API.Alpha.ItemDefinition", |
| 138 | + CoverageMatchStrategy.Exact); |
| 139 | + } |
| 140 | + |
| 141 | + private static CoverageResult Calculate( |
| 142 | + List<GameType> gameTypes, |
| 143 | + List<ApiTypeInfo> apiTypes, |
| 144 | + IReadOnlyDictionary<string, string> explicitMappings) |
| 145 | + { |
| 146 | + var calculator = new CoverageCalculator( |
| 147 | + gameTypes, |
| 148 | + new Dictionary<string, HashSet<string>>(StringComparer.Ordinal), |
| 149 | + apiTypes, |
| 150 | + explicitMappings, |
| 151 | + excludedTypeCount: 0); |
| 152 | + return calculator.Calculate(); |
| 153 | + } |
| 154 | + |
| 155 | + private static GameType GameType(string fullName) |
| 156 | + { |
| 157 | + int separatorIndex = fullName.LastIndexOfAny(['.', '+']); |
| 158 | + return new GameType |
| 159 | + { |
| 160 | + FullName = fullName, |
| 161 | + Namespace = separatorIndex < 0 ? string.Empty : fullName[..separatorIndex], |
| 162 | + Name = separatorIndex < 0 ? fullName : fullName[(separatorIndex + 1)..], |
| 163 | + Kind = GameTypeKind.Class |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + private static ApiTypeInfo ApiType( |
| 168 | + string fullName, |
| 169 | + string name, |
| 170 | + params string[] wrappedGameTypes) => |
| 171 | + new() |
| 172 | + { |
| 173 | + FullName = fullName, |
| 174 | + Name = name, |
| 175 | + WrappedGameTypes = wrappedGameTypes.ToList() |
| 176 | + }; |
| 177 | + |
| 178 | + private static void AssertMatch( |
| 179 | + GameType gameType, |
| 180 | + string expectedApiType, |
| 181 | + CoverageMatchStrategy expectedStrategy) |
| 182 | + { |
| 183 | + Assert.Equal(expectedApiType, gameType.CoveredByApiType); |
| 184 | + Assert.Equal(expectedStrategy, gameType.MatchStrategy); |
| 185 | + } |
| 186 | +} |
0 commit comments