|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using Promact.Caching; |
| 3 | +using System; |
| 4 | +using System.Reflection; |
| 5 | +using System.Reflection.Emit; |
| 6 | + |
| 7 | +namespace Promact.Caching.Test |
| 8 | +{ |
| 9 | + [TestClass] |
| 10 | + public class FunctionalLogicBasedUniqueKeyGenerationTests |
| 11 | + { |
| 12 | + [TestMethod] |
| 13 | + [ExpectedException(typeof(ArgumentNullException))] |
| 14 | + public void GenerateUniqueKey_NullKey_ThrowsArgumentNullException() |
| 15 | + { |
| 16 | + // Arrange |
| 17 | + var generator = new FunctionalLogicBasedUniqueKeyGeneration(); |
| 18 | + string key = null; |
| 19 | + string[] args = { "SomeType", "SomeMethod" }; |
| 20 | + |
| 21 | + // Act |
| 22 | + generator.GenerateUniqueKey(key, args); |
| 23 | + |
| 24 | + // Assert is handled by ExpectedException |
| 25 | + } |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + [ExpectedException(typeof(ArgumentNullException))] |
| 29 | + public void GenerateUniqueKey_NullArgs_ThrowsArgumentNullException() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var generator = new FunctionalLogicBasedUniqueKeyGeneration(); |
| 33 | + string key = "SomeKey"; |
| 34 | + string[] args = null; |
| 35 | + |
| 36 | + // Act |
| 37 | + generator.GenerateUniqueKey(key, args); |
| 38 | + |
| 39 | + // Assert is handled by ExpectedException |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + [ExpectedException(typeof(ArgumentException), "Type not found")] |
| 44 | + public void GenerateUniqueKey_InvalidTypeName_ThrowsArgumentException() |
| 45 | + { |
| 46 | + // Arrange |
| 47 | + var generator = new FunctionalLogicBasedUniqueKeyGeneration(); |
| 48 | + string key = "SomeKey"; |
| 49 | + string[] args = { "InvalidTypeName", "SomeMethod" }; |
| 50 | + |
| 51 | + // Act |
| 52 | + generator.GenerateUniqueKey(key, args); |
| 53 | + |
| 54 | + // Assert is handled by ExpectedException |
| 55 | + } |
| 56 | + |
| 57 | + [TestMethod] |
| 58 | + public void GenerateUniqueKey_ValidInput_ReturnsExpectedKeyFormat() |
| 59 | + { |
| 60 | + // Arrange |
| 61 | + var generator = new FunctionalLogicBasedUniqueKeyGeneration(); |
| 62 | + string key = "SomeKey"; |
| 63 | + // Use a real type and method name from your project or .NET framework |
| 64 | + string[] args = { typeof(string).FullName, "Insert" }; |
| 65 | + |
| 66 | + // Act |
| 67 | + var result = generator.GenerateUniqueKey(key, args); |
| 68 | + |
| 69 | + // Assert |
| 70 | + Assert.IsTrue(result.StartsWith($"{key}-")); |
| 71 | + } |
| 72 | + |
| 73 | + //[TestMethod] |
| 74 | + //public void ComputeMethodHash_DifferentMethodBodies_ProducesDifferentHashes() |
| 75 | + //{ |
| 76 | + // var generator = new FunctionalLogicBasedUniqueKeyGeneration(); |
| 77 | + |
| 78 | + // // Create a dynamic type with a method |
| 79 | + // var type1 = DynamicMethodExample.CreateDynamicTypeWithMethod("TestMethod", "Body1"); |
| 80 | + // var hash1 = generator.GenerateUniqueKey("SomeKey", new string[] { type1.FullName, "TestMethod" }); |
| 81 | + |
| 82 | + // // Create another dynamic type with a different method body |
| 83 | + // var type2 = DynamicMethodExample.CreateDynamicTypeWithMethod("TestMethod", "Body2"); |
| 84 | + // var hash2 = generator.GenerateUniqueKey("SomeKey", new string[] { type2.FullName, "TestMethod" }); |
| 85 | + |
| 86 | + // Assert.AreNotEqual(hash1, hash2, "Hashes should be different for different method bodies."); |
| 87 | + //} |
| 88 | + |
| 89 | + |
| 90 | + // Additional tests can be added here to cover more scenarios |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + internal class FakeType |
| 95 | + { |
| 96 | + public void FakeMethod() |
| 97 | + { |
| 98 | + int a = 10, b = 20; |
| 99 | + var c = a + b; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public class DynamicMethodExample |
| 104 | + { |
| 105 | + public static Type CreateDynamicTypeWithMethod(string methodName, string methodBody) |
| 106 | + { |
| 107 | + var assemblyName = new AssemblyName("DynamicAssembly"); |
| 108 | + var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); |
| 109 | + var moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule"); |
| 110 | + var typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public); |
| 111 | + |
| 112 | + var methodBuilder = typeBuilder.DefineMethod(methodName, |
| 113 | + MethodAttributes.Public | MethodAttributes.Static, |
| 114 | + typeof(void), |
| 115 | + Type.EmptyTypes); |
| 116 | + |
| 117 | + var ilGenerator = methodBuilder.GetILGenerator(); |
| 118 | + ilGenerator.EmitWriteLine(methodBody); |
| 119 | + ilGenerator.Emit(OpCodes.Ret); |
| 120 | + |
| 121 | + return typeBuilder.CreateType(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
0 commit comments