コード例 #1
0
        public UnitTestInfo[] GetTests(TestAssemblyInfo assemblyInfo)
        {
            List <string> assmRefs = new List <string>();

            foreach (var assmName in assemblyInfo.Assembly.GetReferencedAssemblies())
            {
                assmRefs.Add(assmName.Name);
            }

            UnitTestInfo[] tests = null;

            MethodInfo entryInfo = assemblyInfo.Assembly.EntryPoint;

            if (entryInfo != null)
            {
                UnitTestInfo test = new UnitTestInfo()
                {
                    AssemblyPath  = assemblyInfo.Assembly.Location,
                    ReferenceInfo = assemblyInfo.ReferenceInfo,
                    Class         = new TestClassInfo {
                        FullName = entryInfo.DeclaringType.FullName, IsAbstract = entryInfo.DeclaringType.IsAbstract, IsGenericType = entryInfo.DeclaringType.IsGenericType, IsPublic = entryInfo.DeclaringType.IsPublic
                    },
                    Method = new TestMethodInfo {
                        Name = entryInfo.Name, IsAbstract = entryInfo.IsAbstract, IsGenericMethodDefinition = entryInfo.IsGenericMethodDefinition, IsPublic = entryInfo.IsPublic, IsStatic = entryInfo.IsStatic
                    },
                };

                tests = new UnitTestInfo[] { test };
            }

            return(tests ?? new UnitTestInfo[] { });
        }
コード例 #2
0
        private string BuildUnitTestMethodCallSnippet(UnitTestInfo uTest)
        {
            StringBuilder snippet = new StringBuilder();

            snippet.Append(uTest.Method.IsStatic ? $"{uTest.QualifiedMethodStr}(" : $"new { uTest.QualifiedTypeStr }().{ uTest.QualifiedMethodStr}(");

            for (int i = 0; i < uTest.ParameterCount; i++)
            {
                if (i > 0)
                {
                    snippet.Append(", ");
                }

                snippet.Append($"({ReplaceMangledAssembliesWithAliases(uTest.ArgumentInfo.ArgumentTypes[i])})args[{i}]");
            }

            snippet.Append(")");

            if (uTest.Method.IsTaskReturn)
            {
                snippet.Append(".GetAwaiter().GetResult()");
            }

            snippet.Append(";");

            return(snippet.ToString());
        }
コード例 #3
0
        public UnitTestInfo[] GetTests(TestAssemblyInfo assemblyInfo)
        {
            List<string> assmRefs = new List<string>();

            foreach (var assmName in assemblyInfo.Assembly.GetReferencedAssemblies())
            {
                assmRefs.Add(assmName.Name);
            }

            UnitTestInfo[] tests = null;

            MethodInfo entryInfo = assemblyInfo.Assembly.EntryPoint;

            if (entryInfo != null)
            {
                UnitTestInfo test = new UnitTestInfo()
                {
                    AssemblyPath = assemblyInfo.Assembly.Location,
                    ReferenceInfo = assemblyInfo.ReferenceInfo,
                    Class = new TestClassInfo { FullName = entryInfo.DeclaringType.FullName, IsAbstract = entryInfo.DeclaringType.IsAbstract, IsGenericType = entryInfo.DeclaringType.IsGenericType, IsPublic = entryInfo.DeclaringType.IsPublic },
                    Method = new TestMethodInfo { Name = entryInfo.Name, IsAbstract = entryInfo.IsAbstract, IsGenericMethodDefinition = entryInfo.IsGenericMethodDefinition, IsPublic = entryInfo.IsPublic, IsStatic = entryInfo.IsStatic },
                };

                tests = new UnitTestInfo[] { test };
            }

            return tests ?? new UnitTestInfo[] { };
        }
コード例 #4
0
        private string BuildDataSourceSnippet(string datasourceName, UnitTestInfo uTest)
        {
            StringBuilder snippet = new StringBuilder();

            if (datasourceName != null)
            {
                string datafieldName = $"s_{datasourceName.ToLower()}";

                snippet.Append($@"
        private static object[][] {datafieldName} = null;

        public static object[][] {datasourceName}
        {{
            get
            {{
                if({datafieldName} == null)
                {{
                    lock(s_lock)
                    {{        
                        if({datafieldName} == null)
                        {{
                            {ReplaceMangledAssembliesWithAliases(BuildDataFieldInitSnippet(datafieldName, uTest))}
                        }}
                    }}
                }}
                
                return {datafieldName};
            }}
        }}
");
            }

            return(snippet.ToString());
        }
コード例 #5
0
        public UnitTestInfo[] GetTests(TestAssemblyInfo assemblyInfo)
        {
            _assemblyInfo = assemblyInfo;

            List <string> assmRefs = new List <string>();

            foreach (var refAssm in assemblyInfo.Assembly.GetReferencedAssemblies().Select(name => name.Name).Where(name => !name.StartsWith("System")))
            {
                assmRefs.Add(refAssm);
            }

            List <UnitTestInfo> tests = new List <UnitTestInfo>();

            if (assemblyInfo != null)
            {
                foreach (var assmClass in assemblyInfo.Assembly.GetTypes())
                {
                    foreach (var method in assmClass.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
                    {
                        var attributes = method.GetCustomAttributesData();

                        var testAttr = attributes.FirstOrDefault(attr => attr.AttributeType.Name == "FactAttribute" || attr.AttributeType.Name == "TheoryAttribute");

                        if (testAttr != null)
                        {
                            UnitTestInfo test = new UnitTestInfo()
                            {
                                AssemblyPath         = assemblyInfo.Assembly.Location,
                                AssemblyLastModified = File.GetLastWriteTime(assemblyInfo.Assembly.Location),
                                ReferenceInfo        = assemblyInfo.ReferenceInfo,
                                Class = new TestClassInfo
                                {
                                    FullName       = assmClass.FullName,
                                    IsAbstract     = assmClass.IsAbstract,
                                    IsGenericType  = assmClass.IsGenericType || assmClass.IsGenericTypeDefinition,
                                    IsPublic       = assmClass.IsPublic,
                                    HasDefaultCtor = assmClass.GetConstructor(Type.EmptyTypes) != null
                                },
                                Method = new TestMethodInfo
                                {
                                    Name       = method.Name,
                                    IsAbstract = method.IsAbstract,
                                    IsGenericMethodDefinition = method.IsGenericMethodDefinition,
                                    IsPublic     = method.IsPublic,
                                    IsStatic     = method.IsStatic,
                                    IsTaskReturn = method.ReturnType.FullName.StartsWith(typeof(Task).FullName),
                                },
                                ArgumentInfo = GetTestArgumentInfo(method, attributes),
                            };

                            tests.Add(test);
                        }
                    }
                }
            }

            return(tests.ToArray());
        }
コード例 #6
0
        private static string GenerateTestReferencesSnippet(LoadTestInfo loadTest)
        {
            HashSet <string> uniqueAssemblies = new HashSet <string>();

            var packageInfo = loadTest.PackageInfo;

            StringBuilder snippet = new StringBuilder();

            foreach (var test in loadTest.UnitTests)
            {
                if (uniqueAssemblies.Add(test.AssemblyName))
                {
                    string refSnippet = $@"
    <Reference Include='{test.AssemblyName}'>
      <HintPath>$(MSBuildThisFileDirectory)\refs\{test.AssemblyName}</HintPath>
      <Aliases>{UnitTestInfo.GetAssemblyAlias(test.AssemblyName)}</Aliases>
    </Reference>";

                    snippet.Append(refSnippet);
                }

                foreach (var assmref in test.ReferenceInfo.ReferencedAssemblies)
                {
                    if (uniqueAssemblies.Add(assmref.Name) && packageInfo.dependencies[Path.GetFileNameWithoutExtension(assmref.Name)] == null)
                    {
                        string refSnippet = $@"
    <Reference Include='{assmref.Name}'>
      <HintPath>$(MSBuildThisFileDirectory)\refs\{assmref.Name}</HintPath>
      <Aliases>{UnitTestInfo.GetAssemblyAlias(assmref.Name)}</Aliases>
    </Reference>";

                        snippet.Append(refSnippet);
                    }
                }
            }

            string stressexecutionsnippet = $@"
    <Reference Include='stress.execution.dll'>
      <HintPath>{typeof(stress.execution.UnitTest).Assembly.Location}</HintPath>
    </Reference>";

            snippet.Append(stressexecutionsnippet);

            return(snippet.ToString());
        }
コード例 #7
0
        private string BuildDataFieldInitSnippet(string datafieldName, UnitTestInfo uTest)
        {
            StringBuilder snippet = new StringBuilder($"{datafieldName} = ((IEnumerable<object[]>)");

            snippet.Append(uTest.ArgumentInfo.DataSources[0]);

            snippet.Append(")");

            for (int i = 1; i < uTest.ArgumentInfo.DataSources.Length; i++)
            {
                snippet.Append(".Concat((IEnumberable<object[]>)");

                snippet.Append(uTest.ArgumentInfo.DataSources[i]);

                snippet.Append(")");
            }

            snippet.Append(".ToArray();");

            return(snippet.ToString());
        }
コード例 #8
0
        private string BuildUnitTestMethodSnippet(int index, UnitTestInfo uTest)
        {
            string testName = $"UT{index.ToString("X")}";

            string datasourceName = uTest.IsParameterized ? $"{testName}_DataSource" : null;

            _testNames.Add(testName);

            StringBuilder snippet = new StringBuilder();

            snippet.Append(BuildDataSourceSnippet(datasourceName, uTest));

            snippet.Append($@" 
        [Fact]
        public static void {testName}()
        {{
            {BuildArgLookupSnippet(datasourceName)}{BuildUnitTestMethodCallSnippet(uTest)}
        }}
");
            return(snippet.ToString());
        }
コード例 #9
0
        private string ReplaceMangledAssembliesWithAliases(string str)
        {
            var remaining = str;

            string[] split;

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

            while ((split = remaining.Split(new string[] { "```[", "]~~~" }, 3, StringSplitOptions.None)).Length == 3)
            {
                mangledAssemblies.Add(split[1]);

                remaining = split[2];
            }

            foreach (var assm in mangledAssemblies)
            {
                str = str.Replace("```[" + assm + "]~~~", UnitTestInfo.GetAssemblyAlias(assm));
            }

            return(str);
        }
コード例 #10
0
        private static string GenerateReferencesSnippet(LoadTestInfo loadTest)
        {
            HashSet <string> uniqueAssemblies = new HashSet <string>();

            StringBuilder snippet = new StringBuilder();

            foreach (var test in loadTest.UnitTests)
            {
                if (uniqueAssemblies.Add(test.AssemblyPath))
                {
                    string refSnippet = $@"
    <Reference Include='{test.AssemblyName}'>
      <HintPath>$(MSBuildThisFileDirectory)\refs\{test.AssemblyName}</HintPath>
      <Aliases>{UnitTestInfo.GetAssemblyAlias(test.AssemblyPath)}</Aliases>
      <NotForTests>true</NotForTests>
    </Reference>";

                    snippet.Append(refSnippet);
                }

                foreach (var assmref in test.ReferenceInfo.ReferencedAssemblies)
                {
                    if (uniqueAssemblies.Add(assmref.Path))
                    {
                        string refSnippet = $@"
    <Reference Include='{assmref.Name}'>
      <HintPath>$(MSBuildThisFileDirectory)\refs\{assmref.Name}</HintPath>
      <Aliases>{UnitTestInfo.GetAssemblyAlias(assmref.Path)}</Aliases>
      <NotForTests>true</NotForTests>
    </Reference>";

                        snippet.Append(refSnippet);
                    }
                }
            }

            return(snippet.ToString());
        }
コード例 #11
0
        public UnitTestInfo[] GetTests(TestAssemblyInfo assemblyInfo)
        {
            List<string> assmRefs = new List<string>();

            foreach (var refAssm in assemblyInfo.Assembly.GetReferencedAssemblies().Select(name => name.Name).Where(name => !name.StartsWith("System")))
            {
                assmRefs.Add(refAssm);
            }

            List<UnitTestInfo> tests = new List<UnitTestInfo>();

            if (assemblyInfo != null)
            {
                foreach (var assmClass in assemblyInfo.Assembly.GetTypes())
                {
                    foreach (var method in assmClass.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
                    {
                        var attributes = method.GetCustomAttributesData();

                        if (attributes.Any(attr => attr.AttributeType.Name == "FactAttribute" || attr.AttributeType.Name == "TestMethodAttribute"))
                        {
                            UnitTestInfo test = new UnitTestInfo()
                            {
                                AssemblyPath = assemblyInfo.Assembly.Location,
                                ReferenceInfo = assemblyInfo.ReferenceInfo,
                                Class = new TestClassInfo { FullName = assmClass.FullName, IsAbstract = assmClass.IsAbstract, IsGenericType = assmClass.IsGenericType || assmClass.IsGenericTypeDefinition, IsPublic = assmClass.IsPublic, HasDefaultCtor = assmClass.GetConstructor(Type.EmptyTypes) != null },
                                Method = new TestMethodInfo { Name = method.Name, IsAbstract = method.IsAbstract, IsGenericMethodDefinition = method.IsGenericMethodDefinition, IsPublic = method.IsPublic, IsStatic = method.IsStatic, IsVoidReturn = method.ReturnType.Name.ToLowerInvariant() == "void" },
                            };

                            tests.Add(test);
                        }
                    }
                }
            }

            return tests.ToArray();
        }
コード例 #12
0
        public UnitTestInfo[] GetTests(TestAssemblyInfo assemblyInfo)
        {
            _assemblyInfo = assemblyInfo;

            List<string> assmRefs = new List<string>();

            foreach (var refAssm in assemblyInfo.Assembly.GetReferencedAssemblies().Select(name => name.Name).Where(name => !name.StartsWith("System")))
            {
                assmRefs.Add(refAssm);
            }

            List<UnitTestInfo> tests = new List<UnitTestInfo>();

            if (assemblyInfo != null)
            {
                foreach (var assmClass in assemblyInfo.Assembly.GetTypes())
                {
                    foreach (var method in assmClass.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
                    {
                        var attributes = method.GetCustomAttributesData();

                        var testAttr = attributes.FirstOrDefault(attr => attr.AttributeType.Name == "FactAttribute" || attr.AttributeType.Name == "TheoryAttribute");

                        if (testAttr != null)
                        {
                            UnitTestInfo test = new UnitTestInfo()
                            {
                                AssemblyPath = assemblyInfo.Assembly.Location,
                                AssemblyLastModified = File.GetLastWriteTime(assemblyInfo.Assembly.Location),
                                ReferenceInfo = assemblyInfo.ReferenceInfo,
                                Class = new TestClassInfo
                                {
                                    FullName = assmClass.FullName,
                                    IsAbstract = assmClass.IsAbstract,
                                    IsGenericType = assmClass.IsGenericType || assmClass.IsGenericTypeDefinition,
                                    IsPublic = assmClass.IsPublic,
                                    HasDefaultCtor = assmClass.GetConstructor(Type.EmptyTypes) != null
                                },
                                Method = new TestMethodInfo
                                {
                                    Name = method.Name,
                                    IsAbstract = method.IsAbstract,
                                    IsGenericMethodDefinition = method.IsGenericMethodDefinition,
                                    IsPublic = method.IsPublic,
                                    IsStatic = method.IsStatic,
                                    IsTaskReturn = method.ReturnType.FullName.StartsWith(typeof(Task).FullName),
                                },
                                ArgumentInfo = GetTestArgumentInfo(method, attributes),
                            };

                            tests.Add(test);
                        }
                    }
                }
            }

            return tests.ToArray();
        }
コード例 #13
0
        public UnitTestInfo[] GetTests(TestAssemblyInfo assemblyInfo)
        {
            _assemblyInfo = assemblyInfo;

            List <string> assmRefs = new List <string>();

            foreach (var refAssm in assemblyInfo.Assembly.GetReferencedAssemblies().Select(name => name.Name).Where(name => !name.StartsWith("System")))
            {
                assmRefs.Add(refAssm);
            }

            List <UnitTestInfo> tests = new List <UnitTestInfo>();

            if (assemblyInfo != null)
            {
                try
                {
                    var explExcluded = assemblyInfo.Assembly.GetCustomAttributesData().FirstOrDefault(attrData => attrData.AttributeType.Name == "AssemblyTraitAttribute" &&
                                                                                                      attrData.ConstructorArguments.Count == 2 &&
                                                                                                      attrData.ConstructorArguments[0].Value as string == "StressCategory" &&
                                                                                                      attrData.ConstructorArguments[1].Value as string == "Excluded") != null;
                    foreach (var assmClass in assemblyInfo.Assembly.GetTypes())
                    {
                        foreach (var method in assmClass.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
                        {
                            var attributes = method.GetCustomAttributesData();

                            var testAttr = attributes.FirstOrDefault(attr => attr.AttributeType.Name == "FactAttribute" || attr.AttributeType.Name == "TheoryAttribute");

                            if (testAttr != null)
                            {
                                UnitTestInfo test = new UnitTestInfo()
                                {
                                    AssemblyPath         = assemblyInfo.Assembly.Location,
                                    AssemblyLastModified = File.GetLastWriteTime(assemblyInfo.Assembly.Location),
                                    ReferenceInfo        = assemblyInfo.ReferenceInfo,
                                    Class = new TestClassInfo
                                    {
                                        FullName       = assmClass.FullName,
                                        IsAbstract     = assmClass.IsAbstract,
                                        IsGenericType  = assmClass.IsGenericType || assmClass.IsGenericTypeDefinition,
                                        IsPublic       = assmClass.IsPublic,
                                        HasDefaultCtor = assmClass.GetConstructor(Type.EmptyTypes) != null
                                    },
                                    Method = new TestMethodInfo
                                    {
                                        Name       = method.Name,
                                        IsAbstract = method.IsAbstract,
                                        IsGenericMethodDefinition = method.IsGenericMethodDefinition,
                                        IsPublic           = method.IsPublic,
                                        IsStatic           = method.IsStatic,
                                        IsTaskReturn       = method.ReturnType.FullName.StartsWith(typeof(Task).FullName),
                                        ExplicitlyExcluded = explExcluded,
                                    },
                                    ArgumentInfo = GetTestArgumentInfo(method, attributes),
                                };

                                tests.Add(test);
                            }
                        }
                    }
                }
                catch (ReflectionTypeLoadException typeException)
                {
                    Console.WriteLine($"Type loading exception: {typeException.Message}\r\nLoader Exceptions:\r\n\t{string.Join("\r\n", typeException.LoaderExceptions.Select(x => x.Message))}");
                }
            }

            return(tests.ToArray());
        }
コード例 #14
0
        private string BuildUnitTestMethodContentSnippet(UnitTestInfo uTest)
        {
            string contentSnippet = uTest.Method.IsStatic ? $"{uTest.QualifiedMethodStr}();" : $"new { uTest.QualifiedTypeStr }().{ uTest.QualifiedMethodStr}();";

            return(contentSnippet);
        }