コード例 #1
0
        protected void CheckEvents(string name, int expectedTypes, string[] expected, string[] notExpected)
        {
            HashSet <string> eventsToFind    = new HashSet <string> (expected);
            HashSet <string> eventsNotToFind = new HashSet <string> (notExpected);

            HashSet <string> methodsToFind = new HashSet <string> ();

            for (int i = 0; i < expected.Length; i++)
            {
                methodsToFind.Add("add_" + expected [i]);
                methodsToFind.Add("remove_" + expected [i]);
            }

            HashSet <string> methodsNotToFind = new HashSet <string> ();

            for (int i = 0; i < notExpected.Length; i++)
            {
                methodsNotToFind.Add("add_" + notExpected [i]);
                methodsNotToFind.Add("remove_" + notExpected [i]);
            }

            bool foundDelType = false;

            AssemblyHelper.CheckAssembly(name, expectedTypes,
                                         delegate(TypeDefinition typeDef) {
                if (typeDef.BaseType.FullName == "System.MulticastDelegate")
                {
                    foundDelType = true;
                    return(false);
                }
                else
                {
                    return(true);
                }
            },
                                         delegate(TypeDefinition typeDef) {
                // make sure we have enough methods...
                // 2 methods / event + a method to fire them
                Assert.AreEqual(methodsToFind.Count + methodsNotToFind.Count + 2, typeDef.Methods.Count,
                                "Some of the methods for the type are missing.");

                foreach (MethodDefinition method in typeDef.Methods)
                {
                    Assert.IsFalse(methodsNotToFind.Contains(method.Name), String.Format(
                                       "Did not expect to find method '{0}'.", method.Name));

                    methodsToFind.Remove(method.Name);
                }

                Assert.AreEqual(expected.Length, typeDef.Events.Count,
                                expected.Length == 1 ? "Type should have 1 event (others dropped by default)." :
                                String.Format("Type should have {0} events (others dropped by default).", expected.Length));

                foreach (EventDefinition evt in typeDef.Events)
                {
                    Assert.IsFalse(eventsNotToFind.Contains(evt.Name), String.Format(
                                       "Did not expect to find event '{0}'.", evt.Name));

                    eventsToFind.Remove(evt.Name);
                }

                Assert.IsFalse(methodsToFind.Count > 0, "Failed to find all expected methods.");
                Assert.IsFalse(eventsToFind.Count > 0, "Failed to find all expected events.");
            });

            Assert.IsTrue(foundDelType, "Should have found the delegate type.");
        }