Ejemplo n.º 1
0
        public void All_categories_are_tested()
        {
            string[] directories = Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory)
                                   .Select(Path.GetFileName)
                                   .ToArray();
            Type[]        types             = GetType().Assembly.GetTypes();
            List <string> missingCategories = new List <string>();

            foreach (string directory in directories)
            {
                string     expectedTypeName       = ExpectedTypeName(directory);
                Type       type                   = types.SingleOrDefault(t => string.Equals(t.Name, expectedTypeName, StringComparison.InvariantCultureIgnoreCase));
                MethodInfo methodInfo             = type?.GetMethod("Test");
                TestCaseSourceAttribute attribute = methodInfo?.GetCustomAttribute <TestCaseSourceAttribute>();
                if (attribute == null || !attribute.MethodParams.Contains(directory))
                {
                    missingCategories.Add(directory);
                }
            }

            foreach (string missing in missingCategories)
            {
                Console.WriteLine($"{missing} category is missing");
            }

            Assert.AreEqual(0, missingCategories.Count);
        }
Ejemplo n.º 2
0
        private static IEnumerable getTestCaseSourceValue(MethodInfo testMethod, TestCaseSourceAttribute tcs)
        {
            var sourceDeclaringType = tcs.SourceType ?? testMethod.DeclaringType;

            Debug.Assert(sourceDeclaringType != null);

            if (tcs.SourceType != null && tcs.SourceName == null)
            {
                return((IEnumerable)Activator.CreateInstance(tcs.SourceType));
            }

            var sourceMembers = sourceDeclaringType.AsNonNull().GetMember(tcs.SourceName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            if (sourceMembers.Length == 0)
            {
                throw new InvalidOperationException($"No static member with the name of {tcs.SourceName} exists in {sourceDeclaringType} or its base types.");
            }

            if (sourceMembers.Length > 1)
            {
                throw new NotSupportedException($"There are multiple members with the same source name ({tcs.SourceName}) (e.g. method overloads).");
            }

            var sourceMember = sourceMembers.Single();

            switch (sourceMember)
            {
            case FieldInfo sf:
                return((IEnumerable)sf.GetValue(null));

            case PropertyInfo sp:
                if (!sp.CanRead)
                {
                    throw new InvalidOperationException($"The source property {sp.Name} in {sp.DeclaringType.ReadableName()} must have a getter.");
                }

                return((IEnumerable)sp.GetValue(null));

            case MethodInfo sm:
                int methodParamsLength = sm.GetParameters().Length;
                if (methodParamsLength != (tcs.MethodParams?.Length ?? 0))
                {
                    throw new InvalidOperationException($"The given source method parameters count doesn't match the method. (attribute has {tcs.MethodParams?.Length ?? 0}, method has {methodParamsLength})");
                }

                return((IEnumerable)sm.Invoke(null, tcs.MethodParams));

            default:
                throw new NotSupportedException($"{sourceMember.MemberType} is not a supported member type for {nameof(TestCaseSourceAttribute)} (must be static field, property or method)");
            }
        }