|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using AustinHarris.JsonRpc; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace AustinHarris.JsonRpcTestN |
| 9 | +{ |
| 10 | + /// <summary>Verifies the stable diagnostics on obsolete public APIs.</summary> |
| 11 | + [TestFixture] |
| 12 | + public class ObsoletionTests |
| 13 | + { |
| 14 | + private const string UrlFormat = "https://astn.github.io/JSON-RPC.NET/obsoletions.html#{0}"; |
| 15 | + |
| 16 | + /// <summary>Checks the session pre-process alias diagnostic.</summary> |
| 17 | + [Test] |
| 18 | + public void SetBeforeProcessHandler_HasDiagnosticAndMigrationMessage() |
| 19 | + { |
| 20 | + AssertObsolete(typeof(Config), "SetBeforeProcessHandler", "JSONRPC0001", |
| 21 | + "Use SetPreProcessHandler(sessionId, handler)."); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary>Checks the legacy registration diagnostic.</summary> |
| 25 | + [Test] |
| 26 | + public void RegisterFuction_HasDiagnosticAndMigrationMessage() |
| 27 | + { |
| 28 | + AssertObsolete(typeof(Handler), "RegisterFuction", "JSONRPC0002", |
| 29 | + "Use ServiceBinder.BindMethod; unlike RegisterFuction it throws when the name is already registered instead of replacing it."); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary>Checks the legacy unregistration diagnostic.</summary> |
| 33 | + [Test] |
| 34 | + public void UnRegisterFunction_HasDiagnosticAndMigrationMessage() |
| 35 | + { |
| 36 | + AssertObsolete(typeof(Handler), "UnRegisterFunction", "JSONRPC0003", |
| 37 | + "Use ServiceBinder.UnbindMethod."); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Checks that core obsoletion IDs are reserved, unique and discoverable.</summary> |
| 41 | + [Test] |
| 42 | + public void DiagnosticIds_AreUniqueAndInTheObsoletionRange() |
| 43 | + { |
| 44 | + var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; |
| 45 | + var ids = typeof(Config).Assembly.GetTypes() |
| 46 | + .SelectMany(type => new MemberInfo[] { type }.Concat(type.GetMembers(flags))) |
| 47 | + .Select(member => member.GetCustomAttribute<ObsoleteAttribute>()) |
| 48 | + .Where(attribute => attribute != null && attribute.DiagnosticId != null) |
| 49 | + .Select(attribute => attribute.DiagnosticId) |
| 50 | + .ToArray(); |
| 51 | + |
| 52 | + Assert.GreaterOrEqual(ids.Length, 3, "The scan must find the three current obsoletions."); |
| 53 | + CollectionAssert.Contains(ids, "JSONRPC0001"); |
| 54 | + CollectionAssert.Contains(ids, "JSONRPC0002"); |
| 55 | + CollectionAssert.Contains(ids, "JSONRPC0003"); |
| 56 | + Assert.AreEqual(ids.Length, ids.Distinct(StringComparer.Ordinal).Count(), "Diagnostic IDs must be unique."); |
| 57 | + foreach (string id in ids) |
| 58 | + Assert.IsTrue(Regex.IsMatch(id, @"^JSONRPC0\d{3}$"), $"Unexpected diagnostic ID: {id}"); |
| 59 | + } |
| 60 | + |
| 61 | + private static void AssertObsolete(Type declaringType, string memberName, string diagnosticId, string message) |
| 62 | + { |
| 63 | + var method = declaringType.GetMethod(memberName); |
| 64 | + Assert.NotNull(method); |
| 65 | + var attribute = method.GetCustomAttribute<ObsoleteAttribute>(); |
| 66 | + Assert.NotNull(attribute); |
| 67 | + Assert.IsFalse(attribute.IsError); |
| 68 | +#if NET5_0_OR_GREATER |
| 69 | + Assert.AreEqual(diagnosticId, attribute.DiagnosticId); |
| 70 | + Assert.AreEqual(UrlFormat, attribute.UrlFormat); |
| 71 | +#endif |
| 72 | + Assert.AreEqual(message, attribute.Message); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments