|
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | +using Mono.Cecil; |
| 8 | + |
| 9 | +namespace nanoFramework.Tools.MetadataProcessor.Tests.Core.Tables |
| 10 | +{ |
| 11 | + [TestClass] |
| 12 | + public class nanoMethodDefinitionTableTests |
| 13 | + { |
| 14 | + private TypeDefinition _testDelegatesClassTypeDefinition; |
| 15 | + |
| 16 | + [TestInitialize] |
| 17 | + public void Setup() |
| 18 | + { |
| 19 | + var nanoTablesContext = TestObjectHelper.GetTestNFAppNanoTablesContext(); |
| 20 | + _testDelegatesClassTypeDefinition = TestObjectHelper.GetTestNFAppTestingDelegatesTypeDefinition(nanoTablesContext.AssemblyDefinition); |
| 21 | + } |
| 22 | + |
| 23 | + [TestMethod] |
| 24 | + public void GetFlags_PrivateMethod_ReturnsPrivateFlag() |
| 25 | + { |
| 26 | + var nanoTablesContext = TestObjectHelper.GetTestNFAppNanoTablesContext(); |
| 27 | + var testDelegatesClassTypeDefinition = TestObjectHelper.GetTestNFAppTestingDelegatesTypeDefinition(nanoTablesContext.AssemblyDefinition); |
| 28 | + |
| 29 | + MethodDefinition delegateMethod = TestObjectHelper.GetTestNFAppOneClassOverAllMethodDefinition(testDelegatesClassTypeDefinition, "SimpleDelegate"); |
| 30 | + |
| 31 | + uint flags = nanoMethodDefinitionTable.GetFlags(delegateMethod); |
| 32 | + |
| 33 | + const uint expectedFlag = 0x00020000; // MD_DelegateInvoke |
| 34 | + Assert.AreEqual(expectedFlag, flags); |
| 35 | + |
| 36 | + // Assert |
| 37 | + //const uint expectedFlag = 0x00000001; // MD_Scope_Private |
| 38 | + //Assert.AreEqual(expectedFlag, flags); |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void GetFlags_PublicStaticMethod_ReturnsPublicAndStaticFlags() |
| 43 | + { |
| 44 | + // Arrange |
| 45 | + var method = new MethodDefinition("TestMethod", MethodAttributes.Public | MethodAttributes.Static, null); |
| 46 | + |
| 47 | + // Act |
| 48 | + uint flags = nanoMethodDefinitionTable.GetFlags(method); |
| 49 | + |
| 50 | + // Assert |
| 51 | + const uint expectedFlags = 0x00000006 | 0x00000010; // MD_Scope_Public | MD_Static |
| 52 | + Assert.AreEqual(expectedFlags, flags); |
| 53 | + } |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void GetFlags_AbstractVirtualMethod_ReturnsAbstractAndVirtualFlags() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var method = new MethodDefinition("TestMethod", MethodAttributes.Abstract | MethodAttributes.Virtual, null); |
| 60 | + |
| 61 | + // Act |
| 62 | + uint flags = nanoMethodDefinitionTable.GetFlags(method); |
| 63 | + |
| 64 | + // Assert |
| 65 | + const uint expectedFlags = 0x00000200 | 0x00000040; // MD_Abstract | MD_Virtual |
| 66 | + Assert.AreEqual(expectedFlags, flags); |
| 67 | + } |
| 68 | + |
| 69 | + #region delegate method flags |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void TestDelegateInvokeMethodReturnsDelegateInvokeFlag() |
| 73 | + { |
| 74 | + var methodDefinition = TestObjectHelper.GetMethodDefinition(_testDelegatesClassTypeDefinition, "SimpleDelegate", "Invoke"); |
| 75 | + |
| 76 | + uint flags = nanoMethodDefinitionTable.GetFlags(methodDefinition); |
| 77 | + |
| 78 | + // Assert |
| 79 | + const uint expectedFlag = 0x00020000; // MD_DelegateInvoke |
| 80 | + Assert.AreEqual(expectedFlag, flags & expectedFlag); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void TestDelegateConstructorMethodReturnsDelegateConstructorFlag() |
| 85 | + { |
| 86 | + var methodDefinition = TestObjectHelper.GetMethodDefinition(_testDelegatesClassTypeDefinition, "SimpleDelegate", ".ctor"); |
| 87 | + |
| 88 | + uint flags = nanoMethodDefinitionTable.GetFlags(methodDefinition); |
| 89 | + |
| 90 | + // Assert |
| 91 | + const uint expectedFlag = 0x00010000; // MD_DelegateConstructor |
| 92 | + Assert.IsTrue((flags & expectedFlag) == expectedFlag, "Expected flag not set for Delegate constructor."); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void TestDelegateBeginInvokeMethodReturnsDelegateBeginInvokeFlag() |
| 97 | + { |
| 98 | + // Arrange |
| 99 | + var methodDefinition = TestObjectHelper.GetMethodDefinition(_testDelegatesClassTypeDefinition, "SimpleDelegate", "BeginInvoke"); |
| 100 | + |
| 101 | + // Act |
| 102 | + uint flags = nanoMethodDefinitionTable.GetFlags(methodDefinition); |
| 103 | + |
| 104 | + // Assert |
| 105 | + const uint expectedFlag = 0x00040000; // MD_DelegateBeginInvoke |
| 106 | + Assert.IsTrue((flags & expectedFlag) == expectedFlag, "Expected flag not set for BeginInvoke method."); |
| 107 | + } |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void TestDelegateEndInvokeMethodReturnsDelegateEndInvokeFlag() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + var methodDefinition = TestObjectHelper.GetMethodDefinition(_testDelegatesClassTypeDefinition, "SimpleDelegate", "EndInvoke"); |
| 114 | + |
| 115 | + // Act |
| 116 | + uint flags = nanoMethodDefinitionTable.GetFlags(methodDefinition); |
| 117 | + |
| 118 | + // Assert |
| 119 | + const uint expectedFlag = 0x00080000; // MD_DelegateEndInvoke |
| 120 | + Assert.IsTrue((flags & expectedFlag) == expectedFlag, "Expected flag not set for EndInvoke method."); |
| 121 | + } |
| 122 | + |
| 123 | + #endregion |
| 124 | + } |
| 125 | +} |
0 commit comments