Browse Source

Check for null reference, add generic overload for convenience

David Negstad 2 weeks ago
parent
commit
3b7db49bc9

+ 1 - 1
docs/EventSourceAndCounters.md

@@ -183,7 +183,7 @@ public class MyEventSourceTests
     [Fact]
     public void EventIdsAreConsistent()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(MyEventSource));
+        EventSourceValidator.ValidateEventSourceIds<MyEventSource>();
     }
 }
 ```

+ 1 - 1
src/Hosting/Hosting/test/Internal/HostingEventSourceTests.cs

@@ -16,7 +16,7 @@ public class HostingEventSourceTests : LoggedTest
     [Fact]
     public void EventIdsAreConsistent()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(HostingEventSource));
+        EventSourceValidator.ValidateEventSourceIds<HostingEventSourceTests>();
     }
 
     [Fact]

+ 1 - 1
src/Shared/test/Shared.Tests/CertificateManagerEventSourceTests.cs

@@ -11,6 +11,6 @@ public class CertificateManagerEventSourceTests
     [Fact]
     public void EventIdsAreConsistent()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(CertificateManager.CertificateManagerEventSource));
+        EventSourceValidator.ValidateEventSourceIds<CertificateManager.CertificateManagerEventSource>();
     }
 }

+ 1 - 1
src/SignalR/common/Http.Connections/test/Internal/HttpConnectionsEventSourceTests.cs

@@ -17,7 +17,7 @@ public class HttpConnectionsEventSourceTests
     [Fact]
     public void EventIdsAreConsistent()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(HttpConnectionsEventSource));
+        EventSourceValidator.ValidateEventSourceIds<HttpConnectionsEventSource>();
     }
 
     [Fact]

+ 9 - 0
src/Testing/src/Tracing/EventSourceValidator.cs

@@ -17,6 +17,13 @@ namespace Microsoft.AspNetCore.InternalTesting.Tracing;
 /// </summary>
 public static class EventSourceValidator
 {
+    /// <summary>
+    /// Validates all <c>[Event]</c>-attributed methods on <typeparamref name="T"/>.
+    /// </summary>
+    /// <typeparam name="T">A type that derives from <see cref="EventSource"/>.</typeparam>
+    public static void ValidateEventSourceIds<T>() where T : EventSource
+        => ValidateEventSourceIds(typeof(T));
+
     /// <summary>
     /// Validates all <c>[Event]</c>-attributed methods on the given <see cref="EventSource"/>-derived type.
     /// <para>
@@ -32,6 +39,8 @@ public static class EventSourceValidator
     /// <param name="eventSourceType">A type that derives from <see cref="EventSource"/>.</param>
     public static void ValidateEventSourceIds(Type eventSourceType)
     {
+        ArgumentNullException.ThrowIfNull(eventSourceType);
+
         Assert.True(
             typeof(EventSource).IsAssignableFrom(eventSourceType),
             $"Type '{eventSourceType.FullName}' does not derive from EventSource.");

+ 6 - 6
src/Testing/test/EventSourceValidatorTests.cs

@@ -13,14 +13,14 @@ public class EventSourceValidatorTests
     [Fact]
     public void ValidateEventSourceIds_PassesForCorrectEventSource()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(CorrectEventSource));
+        EventSourceValidator.ValidateEventSourceIds<CorrectEventSource>();
     }
 
     [Fact]
     public void ValidateEventSourceIds_FailsForMismatchedWriteEventId()
     {
         var ex = Assert.ThrowsAny<Exception>(
-            () => EventSourceValidator.ValidateEventSourceIds(typeof(MismatchedIdEventSource)));
+            () => EventSourceValidator.ValidateEventSourceIds<MismatchedIdEventSource>());
 
         Assert.Contains("was assigned event ID 1 but 99 was passed to WriteEvent", ex.Message);
     }
@@ -29,7 +29,7 @@ public class EventSourceValidatorTests
     public void ValidateEventSourceIds_FailsForDuplicateEventIds()
     {
         var ex = Assert.ThrowsAny<Exception>(
-            () => EventSourceValidator.ValidateEventSourceIds(typeof(DuplicateIdEventSource)));
+            () => EventSourceValidator.ValidateEventSourceIds<DuplicateIdEventSource>());
 
         Assert.Contains("Duplicate EventId 1", ex.Message);
         Assert.Contains("EventAlpha", ex.Message);
@@ -40,7 +40,7 @@ public class EventSourceValidatorTests
     public void ValidateEventSourceIds_FailsForNonEventSourceType()
     {
         var ex = Assert.ThrowsAny<Exception>(
-            () => EventSourceValidator.ValidateEventSourceIds(typeof(string)));
+            () => EventSourceValidator.ValidateEventSourceIds<string>());
 
         Assert.Contains("does not derive from EventSource", ex.Message);
     }
@@ -48,13 +48,13 @@ public class EventSourceValidatorTests
     [Fact]
     public void ValidateEventSourceIds_PassesForEventSourceWithNoEvents()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(EmptyEventSource));
+        EventSourceValidator.ValidateEventSourceIds<EmptyEventSource>();
     }
 
     [Fact]
     public void ValidateEventSourceIds_PassesForEventSourceWithMultipleParameterTypes()
     {
-        EventSourceValidator.ValidateEventSourceIds(typeof(MultiParamEventSource));
+        EventSourceValidator.ValidateEventSourceIds<MultiParamEventSource>();
     }
 
     // -- Test-only EventSource implementations --