public void AnonymousMethodTest()
        {
            MethodDefinition method = null;
            // compiler generated code is compiler dependant, check for [g]mcs (inner type)
            TypeDefinition type = DefinitionLoader.GetTypeDefinition(typeof(AvoidUnusedParametersTest).Assembly, "AvoidUnusedParametersTest/<>c__CompilerGenerated0");

            if (type != null)
            {
                method = DefinitionLoader.GetMethodDefinition(type, "<AnonymousMethodWithUnusedParameters>c__2", null);
            }
            // otherwise try for csc (inside same class)
            if (method == null)
            {
                type = DefinitionLoader.GetTypeDefinition <AvoidUnusedParametersTest> ();
                foreach (MethodDefinition md in type.Methods)
                {
                    if (md.Name.StartsWith("<AnonymousMethodWithUnusedParameters>"))
                    {
                        method = md;
                        break;
                    }
                }
            }
            Assert.IsNotNull(method, "method not found!");
            AssertRuleDoesNotApply(method);
        }
예제 #2
0
        public void FxCop_ManuallySuppressed()
        {
            AssemblyDefinition assembly = CreateAssembly("SuppressedVersion", ModuleKind.Dll);
            TypeDefinition     type     = DefinitionLoader.GetTypeDefinition <SuppressMessageAttribute> ();

            MethodDefinition ctor = DefinitionLoader.GetMethodDefinition(type, ".ctor",
                                                                         new Type [] { typeof(string), typeof(string) });
            CustomAttribute ca = new CustomAttribute(assembly.MainModule.Import(ctor));

            ca.ConstructorArguments.Add(
                new CustomAttributeArgument(
                    assembly.MainModule.TypeSystem.String, "Microsoft.Design"));
            ca.ConstructorArguments.Add(
                new CustomAttributeArgument(
                    assembly.MainModule.TypeSystem.String, "CA1016:MarkAssembliesWithAssemblyVersion"));
            assembly.CustomAttributes.Add(ca);

            var stream = new MemoryStream();

            assembly.Write(stream);

            stream.Position = 0;

            assembly = AssemblyDefinition.ReadAssembly(stream);

            AssertRuleDoesNotApply(assembly);
        }
 public void FixtureSetUp()
 {
     assembly     = AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition("CLSCompliant", new Version()), "CLSCompliant", ModuleKind.Dll);
     comvisible   = new CustomAttribute(DefinitionLoader.GetMethodDefinition <ComVisibleAttribute> (".ctor"));
     clscompliant = new CustomAttribute(DefinitionLoader.GetMethodDefinition <CLSCompliantAttribute> (".ctor"));
     Runner.Engines.Subscribe("Gendarme.Framework.Engines.SuppressMessageEngine");
 }
예제 #4
0
        public void Bad()
        {
            MethodDefinition m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("BadTestAttribute");

            m.AddTestAttribute();
            AssertRuleFailure(m, 2);

            m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("FourBadAsserts");
            m.AddTestAttribute();
            AssertRuleFailure(m, 4);
        }
        public void EntryPoint()
        {
            MethodDefinition main = DefinitionLoader.GetMethodDefinition <InstantiatedInternalClass> ("Main");

            try {
                main.DeclaringType.Module.Assembly.EntryPoint = main;
                AssertRuleSuccess(main.DeclaringType.Resolve());
            }
            finally {
                main.DeclaringType.Module.Assembly.EntryPoint = null;
            }
        }
        public void DoesNotApply()
        {
            MethodDefinition md = DefinitionLoader.GetMethodDefinition <DoNotUseMethodImplOptionsSynchronizedTest> ("add_CompilerGeneratedSynchronized");

            if (!md.IsSynchronized)
            {
                Assert.Ignore("newer versions of CSC (e.g. 10.0) does not set the Synchronized");
            }

            AssertRuleDoesNotApply(md);
            AssertRuleDoesNotApply <DoNotUseMethodImplOptionsSynchronizedTest> ("remove_CompilerGeneratedSynchronized");
        }
예제 #7
0
        public void GenericsTest()
        {
            Type           type = typeof(BadGenericEquality <>);
            TypeDefinition td   = DefinitionLoader.GetTypeDefinition(type);

            MethodDefinition md = DefinitionLoader.GetMethodDefinition(td, "op_Equality", null);

            AssertRuleFailure(md, 2);

            md = DefinitionLoader.GetMethodDefinition(td, "op_Inequality", null);
            AssertRuleSuccess(md);
        }
        public void Events_Autogenerated()
        {
            MethodDefinition md = DefinitionLoader.GetMethodDefinition <EventCases> ("add_foo");

            if (!md.IsSynchronized)
            {
                Assert.Ignore("newer versions of CSC (e.g. 10.0) does not set the Synchronized");
            }
            // older CSC (and xMCS) compilers still generate it's own add/remove methods that set the
            // synchronize flags and we simply ignore them since they are out of the developer's control
            AssertRuleDoesNotApply(md);
            AssertRuleDoesNotApply <EventCases> ("remove_foo");
        }
예제 #9
0
        public void DoesNotApply()
        {
            MethodDefinition m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("DoesNotApplyExternal");

            m.AddTestAttribute();
            AssertRuleDoesNotApply(m);

            m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("DoesNotApplyEmpty");
            m.AddTestAttribute();
            AssertRuleDoesNotApply(m);

            AssertRuleDoesNotApply <ProvideMessageOnAssertCallsTest> ("DoesNotApplyNoAttributes");
        }
        public void DoubleCheck()
        {
            MethodDefinition md = DefinitionLoader.GetMethodDefinition <Singleton> ("get_DoubleCheck");

            // even if the rule applies only to < 2.0 it still works (for unit testsing) until 4.0
            if (md.DeclaringType.Module.Runtime < TargetRuntime.Net_4_0)
            {
                AssertRuleFailure(md);
            }
            else
            {
                Assert.Ignore("Rule applies for < 2.0 and works only for < 4.0");
            }
        }
예제 #11
0
        public void Good()
        {
            MethodDefinition m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("GoodOneBadAssert");

            m.AddTestAttribute();
            AssertRuleSuccess(m);

            m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("GoodExceptions");
            m.AddTestAttribute();
            AssertRuleSuccess(m);

            m = DefinitionLoader.GetMethodDefinition <ProvideMessageOnAssertCallsTest> ("GoodWithMessages");
            m.AddTestAttribute();
            AssertRuleSuccess(m);
        }
예제 #12
0
        public void Visible()
        {
            AssertRuleSuccess <PublicManualType> ();
            AssertRuleSuccess <ProtectedManualType> ();

            MethodDefinition md = DefinitionLoader.GetMethodDefinition <PublicDefaultType> ("add_Public");

            if (md.IsSynchronized)
            {
                // older CSC (and xMCS) compilers still generate it's own add/remove methods that set the
                // synchronize flags and we simply ignore them since they are out of the developer's control
                AssertRuleFailure <PublicDefaultType> (2);
                AssertRuleFailure <ProtectedDefaultType> (2);
            }
        }
예제 #13
0
        public void Bad()
        {
            MethodDefinition m = DefinitionLoader.GetMethodDefinition <TestMethodsMustBePublicTest> ("PrivateTestMethod");

            m.AddTestAttribute();
            AssertRuleFailure(m);

            m = DefinitionLoader.GetMethodDefinition <TestMethodsMustBePublicTest> ("InternalTestMethod");
            m.AddTestAttribute();
            AssertRuleFailure(m);

            m = DefinitionLoader.GetMethodDefinition <TestMethodsMustBePublicTest> ("ProtectedTestMethod");
            m.AddTestAttribute();
            AssertRuleFailure(m);
        }
예제 #14
0
        public void NonVisible()
        {
            MethodDefinition md = DefinitionLoader.GetMethodDefinition <PrivateDefaultType> ("add_Public");

            if (!md.IsSynchronized)
            {
                Assert.Ignore("newer versions of CSC (e.g. 10.0) does not set the Synchronized");
            }

            // older CSC (and xMCS) compilers still generate it's own add/remove methods that set the
            // synchronize flags and we simply ignore them since they are out of the developer's control
            AssertRuleFailure <PrivateDefaultType> (4);
            AssertRuleFailure <PrivateManualType> (2);

            AssertRuleFailure <InternalDefaultType> (4);
            AssertRuleFailure <InternalManualType> (2);
        }
        public void Good()
        {
            // self-testing, TestFixture present
            AssertRuleSuccess <TestFixtureClass> ();
            AssertRuleSuccess <InheritsFromTestFixtureClass> ();

            DefinitionLoader.GetMethodDefinition <TestFixtureClass> ("TestMethod").AddTestAttribute();
            DefinitionLoader.GetMethodDefinition <InheritsFromTestFixtureClass> ("AnotherTestMethod").AddTestAttribute();
            AssertRuleSuccess <TestFixtureClass> ();
            AssertRuleSuccess <InheritsFromTestFixtureClass> ();

            // no TestFixture and no Test attributes
            AssertRuleSuccess(SimpleTypes.Class);

            // deep (more than one) hierarchy / loop
            AssertRuleSuccess <UnitTestsMissingTestFixtureRule> ();
        }
        private AssemblyDefinition GenerateFakeAssembly(string version, string url, string guid)
        {
            AssemblyDefinition definition = DefinitionLoader.GetAssemblyDefinition(this.GetType());
            CustomAttribute    attribute  = new CustomAttribute(DefinitionLoader.GetMethodDefinition <ValidSince> (".ctor", new Type[] { typeof(string) }));

            AddStringArgument(attribute, definition, version);
            definition.CustomAttributes.Add(attribute);

            attribute = new CustomAttribute(DefinitionLoader.GetMethodDefinition <Reference> (".ctor", new Type[] { typeof(string) }));
            AddStringArgument(attribute, definition, url);
            definition.CustomAttributes.Add(attribute);

            attribute = new CustomAttribute(DefinitionLoader.GetMethodDefinition <Uses> (".ctor", new Type[] { typeof(string) }));
            AddStringArgument(attribute, definition, guid);
            definition.CustomAttributes.Add(attribute);

            return(definition);
        }
        public void Cases()
        {
            AssertRuleSuccess <TestCases> ("Good1");
            AssertRuleSuccess <TestCases> ("Good2");

            AssertRuleFailure <TestCases> ("Bad1", 5);

            // Bad2 can "avoid to fail" if debugging symbols are not found - that includes not being
            // able to load Mono.Cecil.Pdb.dll (on Windows / CSC) or Mono.Cecil.Mdb.dll (xMCS)
            MethodDefinition md = DefinitionLoader.GetMethodDefinition <TestCases> ("Bad2");

            if (md.DeclaringType.Module.HasSymbols)
            {
                AssertRuleFailure(md);
            }

            AssertRuleFailure <TestCases> ("Bad3");
            AssertRuleFailure <TestCases> ("Bad4");
        }
예제 #18
0
        private void AssertTest(string name, int expectedCount)
        {
            TestRunner       runner;
            MethodDefinition method;
            RuleResult       result;
            RuleResult       expected = name.Contains("OK") ? RuleResult.Success : RuleResult.Failure;

            if (expected == RuleResult.Success)
            {
                expectedCount = 0;
            }

            // Since the rule only reports errors once for each method, and these tests reuse methods,
            // we need a new test runner for each test.

            runner = new TestRunner(new DelegatesPassedToNativeCodeMustIncludeExceptionHandlingRule());
            runner.Rules.Clear();
            method = DefinitionLoader.GetMethodDefinition <DelegatesPassedToNativeCodeMustIncludeExceptionHandlingTest> (name);
            result = runner.CheckMethod(method);

            Assert.AreEqual(expected, result);
            Assert.AreEqual(expectedCount, runner.Defects.Count, "DefectCount");
        }
 /// <summary>
 /// Asserts that the rule has been executed successfully.
 /// </summary>
 /// <typeparam name="T">Type containing the method to test.</typeparam>
 /// <param name="method">Method name.</param>
 /// <param name="parameters">Parameter types.</param>
 protected void AssertRuleSuccess <T> (string method, Type [] parameters)
 {
     base.AssertRuleSuccess(DefinitionLoader.GetMethodDefinition <T> (method, parameters));
 }
 /// <summary>
 /// Asserts that the rule has been executed successfully.
 /// </summary>
 /// <typeparam name="T">Type containing the method to test.</typeparam>
 /// <param name="method">Method name.</param>
 protected void AssertRuleSuccess <T> (string method)
 {
     base.AssertRuleSuccess(DefinitionLoader.GetMethodDefinition <T> (method));
 }
 /// <summary>
 /// Asserts that the rule has failed to execute successfully.
 /// </summary>
 /// <typeparam name="T">Type containing the method to test.</typeparam>
 /// <param name="method">Method name.</param>
 protected void AssertRuleFailure <T> (string method)
 {
     base.AssertRuleFailure(DefinitionLoader.GetMethodDefinition <T> (method));
 }
 public void Bad()
 {
     DefinitionLoader.GetMethodDefinition <NoTestFixtureClass> ("TestMethod").AddTestAttribute();
     AssertRuleFailure <NoTestFixtureClass> ();
 }
 /// <summary>
 /// Asserts that the rule has failed to execute successfully.
 /// </summary>
 /// <typeparam name="T">Type containing the method to test.</typeparam>
 /// <param name="method">Method name.</param>
 /// <param name="parameters">Parameter types.</param>
 /// <param name="expectedCount">Expected message count.</param>
 protected void AssertRuleFailure <T> (string method, Type [] parameters, int expectedCount)
 {
     base.AssertRuleFailure(DefinitionLoader.GetMethodDefinition <T> (method, parameters), expectedCount);
 }
예제 #24
0
        /// <summary>
        /// Adds NUnit.Framework.TestAttribute to the specified ICustomAttributeProvicer
        /// </summary>
        /// <param name="self">ICustomAttributeProvider to which the attribute should be added</param>
        public static void AddTestAttribute(this ICustomAttributeProvider self)
        {
            MethodDefinition ctor = DefinitionLoader.GetMethodDefinition <TestAttribute> (".ctor");

            self.CustomAttributes.Add(new CustomAttribute(ctor));
        }
예제 #25
0
 public void True()
 {
     Assert.IsTrue(DefinitionLoader.GetMethodDefinition <NUnitRocksTest> ("True").IsTest());
 }
        protected void AssertRuleDoesNotApply(Type type, string method)
        {
            TypeDefinition td = DefinitionLoader.GetTypeDefinition(type);

            base.AssertRuleDoesNotApply(DefinitionLoader.GetMethodDefinition(td, method, null));
        }
 /// <summary>
 /// Asserts that the rule does not apply to the method.
 /// </summary>
 /// <typeparam name="T">Type containing the method to test.</typeparam>
 /// <param name="method">Method name.</param>
 /// <param name="parameters">Parameter types.</param>
 protected void AssertRuleDoesNotApply <T> (string method, Type [] parameters)
 {
     base.AssertRuleDoesNotApply(DefinitionLoader.GetMethodDefinition <T> (method, parameters));
 }
 /// <summary>
 /// Asserts that the rule does not apply to the method.
 /// </summary>
 /// <param name="method">Method to check.</param>
 /// <typeparam name="T">Type containing the method.</typeparam>
 protected void AssertRuleDoesNotApply <T> (string method)
 {
     base.AssertRuleDoesNotApply(DefinitionLoader.GetMethodDefinition <T> (method));
 }
        protected void AssertRuleFailure(Type type, string method, int expectedCount)
        {
            TypeDefinition td = DefinitionLoader.GetTypeDefinition(type);

            base.AssertRuleFailure(DefinitionLoader.GetMethodDefinition(td, method, null), expectedCount);
        }