Example #1
0
            public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
            {
                var method = semanticModel.GetDeclaredSymbol(node);

                if (method == null)
                {
                    return;
                }
                var parentClass = node.Parent as ClassDeclarationSyntax;

                if (parentClass == null)
                {
                    return;
                }
                UnitTestLocation test    = null;
                IUnitTestMarkers markers = null;

                foreach (var attr in method.GetAttributes())
                {
                    var cname = attr.AttributeClass.GetFullName();
                    markers = unitTestMarkers.FirstOrDefault(m => m.TestMethodAttributeMarker == cname);
                    if (markers != null)
                    {
                        if (test == null)
                        {
                            TagClass(parentClass, markers);
                            test = new UnitTestLocation(node.Identifier.SpanStart);
                            test.UnitTestIdentifier = GetFullName(parentClass) + "." + method.Name;
                            foundTests.Add(test);
                        }
                        break;
                    }
                }
                if (test != null)
                {
                    foreach (var attr in method.GetAttributes())
                    {
                        if (attr.AttributeClass.GetFullName() == markers.TestCaseMethodAttributeMarker)
                        {
                            test.TestCases.Add("(" + BuildArguments(attr) + ")");
                        }
                        else
                        {
                            test.IsIgnored |= attr.AttributeClass.GetFullName() == markers.IgnoreTestMethodAttributeMarker;
                        }
                    }
                }
            }
		public override Task<IList<UnitTestLocation>> GatherUnitTests (IUnitTestMarkers[] unitTestMarkers, CancellationToken token)
		{
			var parsedDocument = DocumentContext.ParsedDocument;
			if (parsedDocument == null)
				return Task.FromResult (emptyList);
			
			var semanticModel = parsedDocument.GetAst<SemanticModel> ();
			if (semanticModel == null)
				return Task.FromResult (emptyList);

			var visitor = new NUnitVisitor (semanticModel, unitTestMarkers, token);
			try {
				visitor.Visit (semanticModel.SyntaxTree.GetRoot (token));
			} catch (OperationCanceledException) {
				throw;
			}catch (Exception ex) {
				LoggingService.LogError ("Exception while analyzing ast for unit tests.", ex);
				return Task.FromResult (emptyList);
			}
			return Task.FromResult (visitor.FoundTests);
		}
Example #3
0
            void TagClass(ClassDeclarationSyntax c, IUnitTestMarkers markers)
            {
                if (unitTestClasses.Contains(c))
                {
                    return;
                }
                unitTestClasses.Add(c);

                var type = semanticModel.GetDeclaredSymbol(c);
                var test = new UnitTestLocation(c.Identifier.SpanStart);

                test.IsFixture          = true;
                test.UnitTestIdentifier = GetFullName(c);
                foundTests.Add(test);

                if (test != null)
                {
                    foreach (var attr in type.GetAttributes())
                    {
                        test.IsIgnored |= attr.AttributeClass.GetFullName() == markers.IgnoreTestClassAttributeMarker;
                    }
                }
            }
		public abstract Task<IList<UnitTestLocation>> GatherUnitTests (IUnitTestMarkers[] unitTestMarkers, CancellationToken token);
			public NUnitVisitor (SemanticModel semanticModel, IUnitTestMarkers[] unitTestMarkers, CancellationToken token)
			{
				this.semanticModel = semanticModel;
				this.token = token;
				this.unitTestMarkers = unitTestMarkers;
			}
			void TagClass (ClassDeclarationSyntax c, IUnitTestMarkers markers)
			{
				if (unitTestClasses.Contains (c))
					return;
				unitTestClasses.Add (c);

				var type = semanticModel.GetDeclaredSymbol (c);
				var test = new UnitTestLocation (c.Identifier.SpanStart);
				test.IsFixture = true;
				test.UnitTestIdentifier = GetFullName (c);
				foundTests.Add (test);

				if (test != null) {
					foreach (var attr in type.GetAttributes ()) {
						test.IsIgnored |= attr.AttributeClass.GetFullName () == markers.IgnoreTestClassAttributeMarker;
					}
				}
			}