Esempio n. 1
0
        public void ScanAssembliesForCheckAndGenerateReport()
        {
            var report = new FullRunDescription();

            // scan all assemblies
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                // get all test fixtures
                foreach (var type in
                         assembly.GetTypes()
                         .Where(
                             type =>
                             ((type.GetInterface("IForkableFluentAssertion") != null &&
                               (type.Attributes & TypeAttributes.Abstract) == 0) ||
                              type.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0) &&
                             ((type.Attributes & TypeAttributes.Public) == TypeAttributes.Public)))
                {
                    try
                    {
                        // enumerate public methods
                        IEnumerable <MethodInfo> tests =
                            type.GetMethods(
                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static
                                | BindingFlags.DeclaredOnly)
                            .Where(
                                method =>
                                method.MemberType == MemberTypes.Method &&
                                ((method.Attributes & MethodAttributes.SpecialName) == 0));
                        var publicMethods = tests as IList <MethodInfo> ?? tests.ToList();
                        if (publicMethods.Count > 0)
                        {
                            // run all tests
                            foreach (var checkMethod in publicMethods)
                            {
                                CheckDescription desc = RunnerHelper.AnalyzeSignature(checkMethod);

                                if (desc != null)
                                {
                                    RunnerHelper.Log(string.Format("Method :{0}.{1}({2})", type.Name, checkMethod.Name, desc.CheckedType.Name));
                                    report.AddEntry(desc);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e));
                    }
                }
            }

            const string Name = "FluentChecks.xml";

            report.Save(Name);
            Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name)));
        }
Esempio n. 2
0
        public void AddEntry(CheckDescription desc)
        {
            foreach (TypeChecks typeCheckse in this.runDescription)
            {
                if (typeCheckse.CheckedType == desc.CheckedType)
                {
                    typeCheckse.AddCheck(desc);
                    return;
                }
            }

            var addedType = new TypeChecks(desc.CheckedType);
            addedType.AddCheck(desc);
            this.runDescription.Add(addedType);
        }
Esempio n. 3
0
        public void AddEntry(CheckDescription desc)
        {
            foreach (TypeChecks typeCheckse in this.runDescription)
            {
                if (typeCheckse.CheckedType == desc.CheckedType)
                {
                    typeCheckse.AddCheck(desc);
                    return;
                }
            }

            var addedType = new TypeChecks(desc.CheckedType);

            addedType.AddCheck(desc);
            this.runDescription.Add(addedType);
        }
Esempio n. 4
0
        public void AddCheck(CheckDescription description)
        {
            foreach (CheckList checkList in this.checks)
            {
                if (checkList.CheckName == description.CheckName)
                {
                    checkList.AddCheck(description);
                    return;
                }
            }

            var toAdd = new CheckList();

            toAdd.CheckName = description.CheckName;
            toAdd.AddCheck(description);
            this.checks.Add(toAdd);
        }
Esempio n. 5
0
        private static CheckDescription GetCheckAndType(FluentCheckException fluExc)
        {
            // identify failing test
            var trace = new StackTrace(fluExc);

            // get fluententrypoint stackframe
            var frameIndex = trace.FrameCount - 2;

            if (frameIndex < 0)
            {
                frameIndex = 0;
            }

            var frame = trace.GetFrame(frameIndex);

            // get method
            var method = frame.GetMethod();

            return(CheckDescription.AnalyzeSignature(method));
        }
Esempio n. 6
0
        public static CheckDescription AnalyzeSignature(MethodBase method)
        {
            var result = new CheckDescription { Check = method };

            if (method.IsStatic)
            {
#if !CORE
                // check if this is an extension method
                if (method.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0)
                {
                    var parameters = method.GetParameters();
                    var param = parameters[0];
                    var paramType = param.ParameterType;
                    if (!paramType.IsGenericType)
                    {
                        // this is not an check implementation
                        return null;
                    }

                    // if it is specific to chains
                    if (paramType.Name == "ICheckLink`1")
                    {
                        paramType = paramType.GetGenericArguments()[0];
                    }

                    if (paramType.Name == "IExtendableCheckLink`1"
                        || paramType.Name == "IExtendableCheckLink`2"
                        || paramType.Name == "ICheck`1"
                        || paramType.GetInterface("ICheck`1") != null
                        || paramType.Name == "IStructCheck`1"
                        || paramType.Name == "ICodeCheck`1")
                    {
                        result.entryPoint = paramType.Name == "IStructCheck`1" ? "ThatEnum" : "That";

                        var testedtype = paramType.GetGenericArguments()[0];
                        if (testedtype.IsGenericParameter)
                        {
                            testedtype = testedtype.BaseType;
                        }

                        result.CheckedType = testedtype;

                        // get other parameters
                        result.CheckParameters = new List<Type>(parameters.Length - 1);
                        for (var i = 1; i < parameters.Length; i++)
                        {
                            result.CheckParameters.Add(parameters[i].ParameterType);
                        }
                    }
                    else
                    {
                        // this is not an check implementation
                        return null;
                    }

                    // identify type subjected to test
                }
                else
                {
                    // unexpected case: check is a static method which is not an extension method
                    return null;
                }
            }
            else
            {
                result.entryPoint = "That";
                if (method.DeclaringType == null)
                {
                    return null;
                }

                // this is an instance method, tested type is part of type defintion
                Type scanning = method.DeclaringType.GetInterface("ICheck`1");
                if (scanning != null && scanning.IsGenericType)
                {
                    // the type implements ICheck<T>
                    result.CheckedType = scanning.IsGenericType ? scanning.GetGenericArguments()[0] : null;

                    if (result != null && result.CheckedType.IsGenericType)
                    {
                        result.CheckedType = result.CheckedType.BaseType;
                    }

                    // get other parameters
                    result.CheckParameters = new List<Type>();
                    foreach (var t in method.GetParameters())
                    {
                        result.CheckParameters.Add(t.ParameterType);
                    }
                }
                else
                {
                    // type does not implement ICheck<T>, we try to find a 'Value' property
                    var prop = method.DeclaringType.GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    if (prop != null)
                    {
                        result.CheckedType = prop.PropertyType;

                        // get other parameters
                        result.CheckParameters = new List<Type>();
                        foreach (var t in method.GetParameters())
                        {
                            result.CheckParameters.Add(t.ParameterType);
                        }
                    }
                    else
                    {
                        // not a check method
                        Debug.WriteLine(string.Format("Type {0} needs to implement a Value property (method {1})", method.DeclaringType.Name, method.Name));
                    }
                }
#endif
            }

            return result;
        }
Esempio n. 7
0
 public void AddCheck(CheckDescription description)
 {
     this.checks.Add(description);
 }
Esempio n. 8
0
        public void AddCheck(CheckDescription description)
        {
            foreach (CheckList checkList in this.checks)
            {
                if (checkList.CheckName == description.CheckName)
                {
                    checkList.AddCheck(description);
                    return;
                }
            }

            var toAdd = new CheckList();
            toAdd.CheckName = description.CheckName;
            toAdd.AddCheck(description);
            this.checks.Add(toAdd);
        }
Esempio n. 9
0
        public static CheckDescription AnalyzeSignature(MethodBase method)
        {
            var result = new CheckDescription {
                Check = method
            };

            if (method.IsStatic)
            {
                // check if this is an extension method
                if (method.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0)
                {
                    ParameterInfo[] parameters = method.GetParameters();
                    ParameterInfo   param      = parameters[0];
                    Type            paramType  = param.ParameterType;
                    if (!paramType.IsGenericType)
                    {
                        // this is not an check implementation
                        return(null);
                    }

                    if (paramType.Name != "IFluentAssertion`1" && paramType.GetInterface("IFluentAssertion`1") == null)
                    {
                        // this is not an check implementation
                        return(null);
                    }

                    // identify type subjected to test
                    var testedtype = paramType.GetGenericArguments()[0];
                    result.CheckedType = testedtype;

                    // get other parameters
                    result.CheckParameters = new List <Type>(parameters.Length - 1);
                    for (int i = 1; i < parameters.Length; i++)
                    {
                        result.CheckParameters.Add(parameters[i].ParameterType);
                    }
                }
                else
                {
                    // unexpected case: check is a static method which is not an extension method
                    return(null);
                }
            }
            else
            {
                if (method.DeclaringType == null)
                {
                    return(null);
                }

                // this is an instance method, tested type is part of type defintion
                Type scanning = method.DeclaringType.GetInterface("IFluentAssertion`1");
                if (scanning != null && scanning.IsGenericType)
                {
                    // the type implements IFluentAssertion<T>
                    result.CheckedType = scanning.IsGenericType ? scanning.GetGenericArguments()[0] : null;

                    // get other parameters
                    result.CheckParameters = new List <Type>();
                    foreach (ParameterInfo t in method.GetParameters())
                    {
                        result.CheckParameters.Add(t.ParameterType);
                    }
                }
                else
                {
                    // type does not implement IFluentAssertion<T>, we try to find a 'Value' property
                    var prop = method.DeclaringType.GetProperty("Value");
                    if (prop != null)
                    {
                        result.CheckedType = prop.PropertyType;

                        // get other parameters
                        result.CheckParameters = new List <Type>();
                        foreach (ParameterInfo t in method.GetParameters())
                        {
                            result.CheckParameters.Add(t.ParameterType);
                        }
                    }
                    else
                    {
                        // not a check method
                        Debug.WriteLine(string.Format("Type {0} needs to implement a Value property (method {1})", method.DeclaringType.Name, method.Name));
                    }
                }
            }

            return(result);
        }
Esempio n. 10
0
        public static void RunMethod(MethodInfo specificTest, object test, FullRunDescription report)
        {
            try
            {
                specificTest.Invoke(test, new object[] { });
            }
            catch (Exception e)
            {
                if (specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false).Length == 0)
                {
                    throw;
                }

                Type expectedType =
                    ((ExpectedExceptionAttribute)
                     specificTest.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false)[0]).ExpectedException;
                if (e.InnerException != null)
                {
                    if (e.InnerException is FluentAssertionException)
                    {
                        var              fluExc = e.InnerException as FluentAssertionException;
                        Type             testedtype;
                        CheckDescription desc   = GetCheckAndType(fluExc);
                        MethodBase       method = desc.Check;
                        testedtype = desc.CheckedType;
                        desc.ErrorSampleMessage = fluExc.Message;

                        // are we building a report
                        if (report != null)
                        {
                            Log(
                                string.Format(
                                    "Check.That({1} sut).{0} failure message\n****\n{2}\n****",
                                    method.Name,
                                    testedtype.Name,
                                    fluExc.Message));
                            report.AddEntry(desc);
                        }

                        return;
                    }

                    if (report != null)
                    {
                        Log(
                            string.Format(
                                "{0} did not generate a FLUENT ASSERTION:\n{1}",
                                specificTest.Name,
                                e.InnerException.Message));
                    }

                    if (e.InnerException.GetType() != expectedType && expectedType != null)
                    {
                        throw;
                    }
                }
                else
                {
                    if (report != null)
                    {
                        Log(string.Format("{0} failed to run:\n{1}", specificTest.Name, e));
                    }

                    throw;
                }
            }
        }
Esempio n. 11
0
 public void AddCheck(CheckDescription description)
 {
     this.checks.Add(description);
 }
Esempio n. 12
0
        public void ScanAssembliesForCheckAndGenerateReport()
        {
            var report = new FullRunDescription();

            // scan all assemblies
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (assembly.GetName().Name == "MonoDevelop.NUnit")
                {
                    // skip MonoDevelop.NUnit assembly because GetTypes fails
                    continue;
                }

                try
                {
                    // get all test fixtures
                    foreach (var type in
                             assembly.GetTypes()
                             .Where(
                                 type =>
                                 ((type.GetInterface("IForkableCheck") != null &&
                                   (type.Attributes & TypeAttributes.Abstract) == 0) ||
                                  type.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0) &&
                                 ((type.Attributes & TypeAttributes.Public) == TypeAttributes.Public)))
                    {
                        try
                        {
                            // enumerate public methods
                            IEnumerable <MethodInfo> tests =
                                type.GetMethods(
                                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static
                                    | BindingFlags.DeclaredOnly)
                                .Where(
                                    method =>
                                    method.MemberType == MemberTypes.Method &&
                                    ((method.Attributes & MethodAttributes.SpecialName) == 0));
                            var publicMethods = tests as IList <MethodInfo> ?? tests.ToList();
                            if (publicMethods.Count > 0)
                            {
                                // scan all methods
                                foreach (var checkMethod in publicMethods)
                                {
                                    try
                                    {
                                        if (checkMethod.Name == "ForkInstance")
                                        {
                                            // skip forkinstance
                                            continue;
                                        }

                                        var desc = CheckDescription.AnalyzeSignature(checkMethod);

                                        if (desc != null)
                                        {
                                            if (desc.CheckedType == null)
                                            {
                                                RunnerHelper.Log(string.Format("Failed to identify checked type on test {0}", checkMethod.Name));
                                            }
                                            else
                                            {
                                                RunnerHelper.Log(string.Format("Method :{0}.{1}({2})", type.Name, checkMethod.Name, desc.CheckedType.Name));
                                                report.AddEntry(desc);
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        RunnerHelper.Log(string.Format("Exception while assessing test {2} on type:{0}\n{1}", type.FullName, e, checkMethod.Name));
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            RunnerHelper.Log(string.Format("Exception while working on type:{0}\n{1}", type.FullName, e));
                        }
                    }
                }
                catch (Exception e)
                {
                    RunnerHelper.Log(string.Format("Exception while working on assembly:{0}\n{1}", assembly.FullName, e));
                    throw new ApplicationException(string.Format("Exception while working on assembly:{0}\n{1}", assembly.FullName, e));
                }
            }

            // xml save
            const string Name = "FluentChecks.xml";

            report.Save(Name);

            const string Name2 = "FluentChecks.csv";

            // csv file
            using (var writer = new StreamWriter(Name2, false))
            {
                foreach (var typeChecks in report.RunDescription)
                {
                    foreach (var checkList in typeChecks.Checks)
                    {
                        foreach (var signature in checkList.CheckSignatures)
                        {
                            var message = string.Format(
                                "{0};{1};{2}", typeChecks.CheckedType.TypeToStringProperlyFormated(true), checkList.CheckName, signature.Signature);
                            writer.WriteLine(message);
                        }
                    }
                }
            }

            Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name)));
            Debug.Write(string.Format("Report generated in {0}", Path.GetFullPath(Name2)));
        }
Esempio n. 13
0
        public static CheckDescription AnalyzeSignature(MethodBase method)
        {
            var result = new CheckDescription {
                Check = method
            };

            if (method.IsStatic)
            {
                // check if this is an extension method
                if (method.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0)
                {
                    var parameters = method.GetParameters();
                    var param      = parameters[0];
                    var paramType  = param.ParameterType;
                    if (!paramType.IsGenericType)
                    {
                        // this is not an check implementation
                        return(null);
                    }

                    // if it is specific to chains
                    if (paramType.Name == "ICheckLink`1")
                    {
                        paramType = paramType.GetGenericArguments()[0];
                    }

                    if (paramType.Name == "IExtendableCheckLink`1" ||
                        paramType.Name == "IExtendableCheckLink`2" ||
                        paramType.Name == "ICheck`1" ||
                        paramType.GetInterface("ICheck`1") != null ||
                        paramType.Name == "IStructCheck`1" ||
                        paramType.Name == "ICodeCheck`1")
                    {
                        result.entryPoint = paramType.Name == "IStructCheck`1" ? "ThatEnum" : "That";

                        var testedtype = paramType.GetGenericArguments()[0];
                        if (testedtype.IsGenericParameter)
                        {
                            testedtype = testedtype.BaseType;
                        }

                        result.CheckedType = testedtype;

                        // get other parameters
                        result.CheckParameters = new List <Type>(parameters.Length - 1);
                        for (var i = 1; i < parameters.Length; i++)
                        {
                            result.CheckParameters.Add(parameters[i].ParameterType);
                        }
                    }
                    else
                    {
                        // this is not an check implementation
                        return(null);
                    }

                    // identify type subjected to test
                }
                else
                {
                    // unexpected case: check is a static method which is not an extension method
                    return(null);
                }
            }
            else
            {
                result.entryPoint = "That";
                if (method.DeclaringType == null)
                {
                    return(null);
                }

                // this is an instance method, tested type is part of type defintion
                Type scanning = method.DeclaringType.GetInterface("ICheck`1");
                if (scanning != null && scanning.IsGenericType)
                {
                    // the type implements ICheck<T>
                    result.CheckedType = scanning.IsGenericType ? scanning.GetGenericArguments()[0] : null;

                    if (result != null && result.CheckedType.IsGenericType)
                    {
                        result.CheckedType = result.CheckedType.BaseType;
                    }

                    // get other parameters
                    result.CheckParameters = new List <Type>();
                    foreach (var t in method.GetParameters())
                    {
                        result.CheckParameters.Add(t.ParameterType);
                    }
                }
                else
                {
                    // type does not implement ICheck<T>, we try to find a 'Value' property
                    var prop = method.DeclaringType.GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    if (prop != null)
                    {
                        result.CheckedType = prop.PropertyType;

                        // get other parameters
                        result.CheckParameters = new List <Type>();
                        foreach (var t in method.GetParameters())
                        {
                            result.CheckParameters.Add(t.ParameterType);
                        }
                    }
                    else
                    {
                        // not a check method
                        Debug.WriteLine(string.Format("Type {0} needs to implement a Value property (method {1})", method.DeclaringType.Name, method.Name));
                    }
                }
            }

            return(result);
        }
Esempio n. 14
0
        public static CheckDescription AnalyzeSignature(MethodBase method)
        {
            var result = new CheckDescription { Check = method };

            if (method.IsStatic)
            {
                // check if this is an extension method
                if (method.GetCustomAttributes(typeof(ExtensionAttribute), false).Length > 0)
                {
                    ParameterInfo[] parameters = method.GetParameters();
                    ParameterInfo param = parameters[0];
                    Type paramType = param.ParameterType;
                    if (!paramType.IsGenericType)
                    {
                        // this is not an check implementation
                        return null;
                    }

                    if (paramType.Name != "IFluentAssertion`1" && paramType.GetInterface("IFluentAssertion`1") == null)
                    {
                        // this is not an check implementation
                        return null;
                    }

                    // identify type subjected to test
                    var testedtype = paramType.GetGenericArguments()[0];
                    result.CheckedType = testedtype;

                    // get other parameters
                    result.CheckParameters = new List<Type>(parameters.Length - 1);
                    for (int i = 1; i < parameters.Length; i++)
                    {
                        result.CheckParameters.Add(parameters[i].ParameterType);
                    }
                }
                else
                {
                    // unexpected case: check is a static method which is not an extension method
                    return null;
                }
            }
            else
            {
                if (method.DeclaringType == null)
                {
                    return null;
                }

                // this is an instance method, tested type is part of type defintion
                Type scanning = method.DeclaringType.GetInterface("IFluentAssertion`1");
                if (scanning != null && scanning.IsGenericType)
                {
                    // the type implements IFluentAssertion<T>
                    result.CheckedType = scanning.IsGenericType ? scanning.GetGenericArguments()[0] : null;

                    // get other parameters
                    result.CheckParameters = new List<Type>();
                    foreach (ParameterInfo t in method.GetParameters())
                    {
                        result.CheckParameters.Add(t.ParameterType);
                    }
                }
                else
                {
                    // type does not implement IFluentAssertion<T>, we try to find a 'Value' property
                    var prop = method.DeclaringType.GetProperty("Value");
                    if (prop != null)
                    {
                        result.CheckedType = prop.PropertyType;

                        // get other parameters
                        result.CheckParameters = new List<Type>();
                        foreach (ParameterInfo t in method.GetParameters())
                        {
                            result.CheckParameters.Add(t.ParameterType);
                        }
                    }
                    else
                    {
                        // not a check method
                        Debug.WriteLine(string.Format("Type {0} needs to implement a Value property (method {1})", method.DeclaringType.Name, method.Name));
                    }
                }
            }

            return result;
        }