|
| 1 | +using ModelContextProtocol.Server; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +namespace ModelContextProtocol.Tests.Server; |
| 5 | + |
| 6 | +public class DelegatingMcpServerPrimitiveTests |
| 7 | +{ |
| 8 | + [Theory] |
| 9 | + [InlineData(typeof(DelegatingMcpServerTool), typeof(McpServerTool))] |
| 10 | + [InlineData(typeof(DelegatingMcpServerPrompt), typeof(McpServerPrompt))] |
| 11 | + [InlineData(typeof(DelegatingMcpServerResource), typeof(McpServerResource))] |
| 12 | + public void DelegatingType_OverridesAllVirtualAndAbstractMembers(Type delegatingType, Type baseType) |
| 13 | + { |
| 14 | + // Get all public instance methods on the base type that are virtual/abstract |
| 15 | + // (this includes property getters/setters) and declared on the base type itself, |
| 16 | + // excluding those inherited from System.Object. |
| 17 | + MethodInfo[] baseMethods = baseType.GetMethods(BindingFlags.Public | BindingFlags.Instance) |
| 18 | + .Where(m => (m.IsVirtual || m.IsAbstract) && m.DeclaringType == baseType) |
| 19 | + .ToArray(); |
| 20 | + |
| 21 | + Assert.NotEmpty(baseMethods); |
| 22 | + |
| 23 | + foreach (MethodInfo baseMethod in baseMethods) |
| 24 | + { |
| 25 | + // Find the corresponding method on the delegating type |
| 26 | + MethodInfo? overriddenMethod = delegatingType.GetMethod( |
| 27 | + baseMethod.Name, |
| 28 | + BindingFlags.Public | BindingFlags.Instance, |
| 29 | + baseMethod.GetParameters().Select(p => p.ParameterType).ToArray()); |
| 30 | + |
| 31 | + Assert.True( |
| 32 | + overriddenMethod is not null && overriddenMethod.DeclaringType == delegatingType, |
| 33 | + $"{delegatingType.Name} does not override {baseMethod.Name} from {baseType.Name}."); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
0 commit comments