public void ApproveStructsWhichDontFollowStructGuidelines() { var approvalBuilder = new StringBuilder(); approvalBuilder.AppendLine(@"-------------------------------------------------- REMEMBER -------------------------------------------------- CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects. AVOID defining a struct unless the type has all of the following characteristics: * It logically represents a single value, similar to primitive types(int, double, etc.). * It has an instance size under 16 bytes. * It is immutable. * It will not have to be boxed frequently. In all other cases, you should define your types as classes. -------------------------------------------------- REMEMBER -------------------------------------------------- "); var assembly = typeof(Endpoint).Assembly; foreach (var type in assembly.GetTypes().OrderBy(t => t.FullName)) { if (!type.IsValueType || type.IsEnum || type.IsSpecialName || type.Namespace == null || !type.Namespace.StartsWith("NServiceBus") || type.FullName.Contains("__")) { continue; } var violatedRules = new List <string> { $"{type.FullName} violates the following rules:" }; InspectSizeOfStruct(type, violatedRules); InspectWhetherStructViolatesMutabilityRules(type, violatedRules); if (violatedRules.Count <= 1) { continue; } foreach (var violatedRule in violatedRules) { approvalBuilder.AppendLine(violatedRule); } approvalBuilder.AppendLine(); } TestApprover.Verify(approvalBuilder.ToString()); }
public void ApproveStructsWhichDontFollowStructGuidelines() { if (Environment.OSVersion.Platform != PlatformID.Win32NT) { Assert.Ignore("ApprovalTests only works on Windows"); } var approvalBuilder = new StringBuilder(); approvalBuilder.AppendLine(@"-------------------------------------------------- REMEMBER -------------------------------------------------- CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects. AVOID defining a struct unless the type has all of the following characteristics: * It logically represents a single value, similar to primitive types(int, double, etc.). * It has an instance size under 16 bytes. * It is immutable. * It will not have to be boxed frequently. In all other cases, you should define your types as classes. -------------------------------------------------- REMEMBER -------------------------------------------------- "); var assembly = typeof(Endpoint).Assembly; foreach (var type in assembly.GetTypes().OrderBy(t => t.FullName)) { if (!type.IsValueType || type.IsEnum || type.IsSpecialName || type.Namespace == null || !type.Namespace.StartsWith("NServiceBus") || type.FullName.Contains("__")) { continue; } var violatedRules = new List <string> { $"{type.FullName} violates the following rules:" }; InspectWhetherStructContainsPublicFields(type, violatedRules); InspectWhetherStructContainsWritableProperties(type, violatedRules); var containsRefereneceTypes = InspectWhetherStructContainsReferenceTypes(type, violatedRules); if (containsRefereneceTypes) { violatedRules.Add(" - The size cannot be determined because there are fields that are reference types."); } else { InspectSizeOfStruct(type, violatedRules); } if (violatedRules.Count <= 1) { continue; } foreach (var violatedRule in violatedRules) { approvalBuilder.AppendLine(violatedRule); } approvalBuilder.AppendLine(); } TestApprover.Verify(approvalBuilder.ToString()); }