Пример #1
0
        private static MethodFilter FilterFromMethod(MethodInfo methodInfo)
        {
            var match         = FilterRegex.Match(methodInfo.Name);
            var filterMatch   = match.Groups["filter"];
            var selectorMatch = match.Groups["selector"];
            var propertyMatch = match.Groups["property"];

            var filterMap = new MethodFilter();

            if (filterMatch.Success && !string.IsNullOrEmpty(filterMatch.Value))
            {
                filterMap.Filter = (Filter)Enum.Parse(typeof(Filter), filterMatch.Value);
            }
            else
            {
                filterMap.Filter = Filter.FirstOrDefault;
            }

            if (selectorMatch.Success && !string.IsNullOrEmpty(selectorMatch.Value))
            {
                filterMap.Selector = (Selector)Enum.Parse(typeof(Selector), selectorMatch.Value);
            }
            else
            {
                filterMap.Selector = Selector.By;
            }

            if (propertyMatch.Success)
            {
                //TODO implement property match
            }

            return(filterMap);
        }
Пример #2
0
 private static bool CanApply(IMethodSymbol symbol, MethodFilter filter, Dictionary <string, List <MethodFilter> > rules)
 {
     if (filter.All)
     {
         return(true);
     }
     if (!CanApply(symbol, filter))
     {
         return(false);
     }
     if (filter.ReturnsVoid.HasValue && filter.ReturnsVoid.Value != symbol.ReturnsVoid)
     {
         return(false);
     }
     if (!string.IsNullOrEmpty(filter.Rule))
     {
         if (!rules.TryGetValue(filter.Rule, out var rule))
         {
             throw new InvalidOperationException($"Method rule {filter.Rule} do not exist");
         }
         if (!rule.Any(o => CanApply(symbol, o, rules)))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #3
0
            private string BuildExceptionMessage()
            {
                StringBuilder sb = new StringBuilder()
                                   .Append("No matching method found in the type ")
                                   .Append(_targetType)
                                   .Append(" for signature ")
                                   .Append(_returnType).Append(" ")
                                   .Append(_methodName).Append("(");

                if (_parameterTypes.Length > 0)
                {
                    foreach (Type parameter in _parameterTypes)
                    {
                        sb.Append(parameter).Append(", ");
                    }
                    sb.Length -= 2;
                }
                sb.Append(") with binding flags: ").Append(_bindingAttr);
                if (MethodFilter != null)
                {
                    sb.Append(" with filter ").Append(MethodFilterMessage ?? MethodFilter.ToString());
                }
                sb.Append(".");
                return(sb.ToString());
            }
Пример #4
0
 public Convention()
 {
     Classes = new ClassFilter().Where(type => !type.IsSubclassOf(typeof(Convention)));
     Cases = new MethodFilter().Where(m => !m.IsDispose());
     CaseExecution = new CaseBehaviorBuilder();
     InstanceExecution = new InstanceBehaviorBuilder();
     ClassExecution = new TypeBehaviorBuilder().CreateInstancePerCase();
 }
Пример #5
0
        public DefaultConvention()
        {
            Classes
            .NameEndsWith("Tests");

            Cases = new MethodFilter()
                    .Where(method => method.Void() || method.Async())
                    .ZeroParameters();
        }
Пример #6
0
        public DefaultConvention()
        {
            Classes
                .NameEndsWith("Tests");

            Cases = new MethodFilter()
                .Where(method => method.Void() || method.Async())
                .ZeroParameters();
        }
Пример #7
0
        public Convention()
        {
            Classes = new ClassFilter().Where(type => !type.IsSubclassOf(typeof(Convention)));
            Methods = new MethodFilter().Where(m => !m.IsDispose());
            CaseExecution = new CaseBehaviorBuilder();
            InstanceExecution = new InstanceBehaviorBuilder();
            ClassExecution = new TypeBehaviorBuilder().CreateInstancePerCase();

            methodCallParameterBuilder = method => new object[][] { };
        }
Пример #8
0
        public SelfTestConvention()
        {
            Classes
                .Where(testClass => testClass.IsNestedPrivate)
                .NameEndsWith("TestClass");

            Cases = new MethodFilter()
                .Where(method => method.Void() || method.Async())
                .ZeroParameters();
        }
Пример #9
0
        public SelfTestConvention()
        {
            Classes
            .Where(testClass => testClass.IsNestedPrivate)
            .NameEndsWith("TestClass");

            Cases = new MethodFilter()
                    .Where(method => method.Void() || method.Async())
                    .ZeroParameters();
        }
Пример #10
0
        public CustomConvention()
        {
            //In this example, the description of test classes is so inclusive that this convention
            //class itself could be mistaken for a test class. Since convention classes are
            //automatically excluded, though, ShouldNotBeCalled() will not be called.

            Classes
            .Where(type => type.IsInNamespace(GetType().Namespace));

            Cases = new MethodFilter()
                    .Where(method => method.Void())
                    .ZeroParameters();
        }
Пример #11
0
        public CustomConvention()
        {
            //In this example, the description of test classes is so inclusive that this convention
            //class itself could be mistaken for a test class. Since convention classes are
            //automatically excluded, though, ShouldNotBeCalled() will not be called.

            Classes
                .Where(type => type.IsInNamespace(GetType().Namespace));

            Cases = new MethodFilter()
                .Where(method => method.Void())
                .ZeroParameters();
        }
Пример #12
0
        public CustomConvention()
        {
            container = InitContainerForIntegrationTests();

            Classes
            .Where(type => type.IsInNamespace(GetType().Namespace))
            .NameEndsWith("Tests");

            Cases = new MethodFilter()
                    .Where(method => method.Void())
                    .ZeroParameters();

            ClassExecution
            .CreateInstancePerTestClass(UsingContainer);
        }
Пример #13
0
        public CustomConvention()
        {
            Classes
            .Where(x => factMethods.Filter(x).Any());

            Cases = new MethodFilter()
                    .HasOrInherits <FactAttribute>();

            ClassExecution
            .CreateInstancePerCase()
            .SetUpTearDown(PrepareFixtureData, DisposeFixtureData);

            InstanceExecution
            .SetUpTearDown(InjectFixtureData, fixture => { });
        }
Пример #14
0
        public CustomConvention()
        {
            container = InitContainerForIntegrationTests();

            Classes
                .Where(type => type.IsInNamespace(GetType().Namespace))
                .NameEndsWith("Tests");

            Cases = new MethodFilter()
                .Where(method => method.Void())
                .ZeroParameters();

            ClassExecution
                .CreateInstancePerTestClass(UsingContainer);
        }
Пример #15
0
        public CustomConvention()
        {
            Classes
                .Where(x => factMethods.Filter(x).Any());

            Cases = new MethodFilter()
                .HasOrInherits<FactAttribute>();

            ClassExecution
                .CreateInstancePerCase()
                .SetUpTearDown(PrepareFixtureData, DisposeFixtureData);

            InstanceExecution
                .SetUpTearDown(InjectFixtureData, fixture => { });
        }
Пример #16
0
        public CustomConvention()
        {
            Classes
            .HasOrInherits <TestFixtureAttribute>();

            Cases = new MethodFilter()
                    .HasOrInherits <TestAttribute>();

            ClassExecution
            .CreateInstancePerTestClass();

            InstanceExecution
            .SetUpTearDown <TestFixtureSetUpAttribute, TestFixtureTearDownAttribute>();

            CaseExecution
            .SetUpTearDown <SetUpAttribute, TearDownAttribute>();
        }
Пример #17
0
        private string BuildExceptionMessage()
        {
            StringBuilder sb = new StringBuilder()
                               .Append("No matching method found in the type ")
                               .Append(_targetType)
                               .Append(" for signature ")
                               .Append(_returnType).Append(" ")
                               .Append(_methodName).Append("(");

            sb.AppendArrayCommaSeparated(_parameterTypes);
            sb.Append(") with binding flags: ").Append(_bindingAttr);
            if (MethodFilter != null)
            {
                sb.Append(" with filter ").Append(MethodFilterMessage ?? MethodFilter.ToString());
            }
            sb.Append(".");
            return(sb.ToString());
        }
Пример #18
0
        public CustomConvention(RunContext runContext)
        {
            Classes
            .Where(type => type.IsInNamespace(GetType().Namespace))
            .NameEndsWith("Tests");

            Cases = new MethodFilter()
                    .Where(method => method.Void())
                    .ZeroParameters()
                    .Where(method =>
            {
                var isMarkedExplicit = method.Has <ExplicitAttribute>();

                return(!isMarkedExplicit || runContext.TargetMember == method);
            });

            ClassExecution
            .CreateInstancePerTestClass();
        }
Пример #19
0
        public CustomConvention(RunContext runContext)
        {
            Classes
                .Where(type => type.IsInNamespace(GetType().Namespace))
                .NameEndsWith("Tests");

            Cases = new MethodFilter()
                .Where(method => method.Void())
                .ZeroParameters()
                .Where(method =>
                {
                    var isMarkedExplicit = method.Has<ExplicitAttribute>();

                    return !isMarkedExplicit || runContext.TargetMember == method;
                });

            ClassExecution
                .CreateInstancePerTestClass();
        }
Пример #20
0
        public CustomConvention()
        {
            Classes
                .Where(type => type.IsInNamespace(GetType().Namespace))
                .NameEndsWith("Tests");

            Cases = new MethodFilter()
                .Where(method => method.Void())
                .Where(method => LifecycleMethods.All(x => x != method.Name))
                .ZeroParameters();

            ClassExecution
                .CreateInstancePerTestClass();

            InstanceExecution
                .SetUpTearDown("FixtureSetUp", "FixtureTearDown");

            CaseExecution
                .SetUpTearDown("SetUp", "TearDown");
        }
Пример #21
0
        static void TryInvoke(string method, Type type, object instance)
        {
            var lifecycleMethod =
                new MethodFilter()
                    .Where(x => x.HasSignature(typeof(void), method))
                    .Filter(type)
                    .SingleOrDefault();

            if (lifecycleMethod == null)
                return;

            try
            {
                lifecycleMethod.Invoke(instance, null);
            }
            catch (TargetInvocationException exception)
            {
                throw new PreservedException(exception.InnerException);
            }
        }
Пример #22
0
        public CustomConvention(RunContext runContext)
        {
            var desiredCategories = runContext.Options["include"].ToArray();
            var shouldRunAll      = !desiredCategories.Any();

            Classes
            .Where(type => type.IsInNamespace(GetType().Namespace))
            .NameEndsWith("Tests");

            Cases = new MethodFilter()
                    .Where(method => method.Void())
                    .ZeroParameters()
                    .Where(method => shouldRunAll || MethodHasAnyDesiredCategory(method, desiredCategories));

            if (!shouldRunAll)
            {
                Console.WriteLine("Categories: " + string.Join(", ", desiredCategories));
                Console.WriteLine();
            }
        }
Пример #23
0
        public CustomConvention()
        {
            Classes
                .Where(type => type.IsInNamespace(GetType().Namespace))
                .NameEndsWith("Tests");

            Cases = new MethodFilter()
                .Where(method => method.Void())
                .Where(method => LifecycleMethods.All(x => x != method.Name))
                .ZeroParameters();

            ClassExecution
                .CreateInstancePerTestClass();

            InstanceExecution
                .SetUpTearDown("FixtureSetUp", "FixtureTearDown");

            CaseExecution
                .SetUpTearDown("SetUp", "TearDown");
        }
Пример #24
0
        public CustomConvention(RunContext runContext)
        {
            var desiredCategories = runContext.Options["include"].ToArray();
            var shouldRunAll = !desiredCategories.Any();

            Classes
                .Where(type => type.IsInNamespace(GetType().Namespace))
                .NameEndsWith("Tests");

            Cases = new MethodFilter()
                .Where(method => method.Void())
                .ZeroParameters()
                .Where(method => shouldRunAll || MethodHasAnyDesiredCategory(method, desiredCategories));

            if (!shouldRunAll)
            {
                Console.WriteLine("Categories: " + string.Join(", ", desiredCategories));
                Console.WriteLine();
            }
        }
 private static bool CanApply(IMethodSymbol symbol, MethodFilter filter, Dictionary <string, List <MethodFilter> > rules)
 {
     if (filter.All)
     {
         return(true);
     }
     if (!CanApply(symbol, filter))
     {
         return(false);
     }
     if (filter.ReturnsVoid.HasValue && filter.ReturnsVoid.Value != symbol.ReturnsVoid)
     {
         return(false);
     }
     if (!string.IsNullOrEmpty(filter.Rule) && !rules[filter.Rule].Any(o => CanApply(symbol, o, rules)))
     {
         return(false);
     }
     return(true);
 }
Пример #26
0
        static void TryInvoke(string method, Type type, object instance)
        {
            var lifecycleMethod =
                new MethodFilter()
                .Where(x => x.HasSignature(typeof(void), method))
                .Filter(type)
                .SingleOrDefault();

            if (lifecycleMethod == null)
            {
                return;
            }

            try
            {
                lifecycleMethod.Invoke(instance, null);
            }
            catch (TargetInvocationException exception)
            {
                throw new PreservedException(exception.InnerException);
            }
        }
Пример #27
0
 public void AddFilter(MethodFilter <Method> filter)
 {
     this.filters.Add(filter);
 }
Пример #28
0
 public void AddFilter(MethodFilter <Method> filter)
 {
     this.underlying.AddFilter(filter);
 }