Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestCase.cs « ILLink.RoslynAnalyzer.Tests « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f35bdbe21061a448dcb7da94f0ff66aa7c2336d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace ILLink.RoslynAnalyzer.Tests
{
	public class TestCase
	{
		public readonly MemberDeclarationSyntax MemberSyntax;

		private readonly IEnumerable<AttributeSyntax> Attributes;

		public string? Name { get; set; }

		public TestCase (MemberDeclarationSyntax memberSyntax, IEnumerable<AttributeSyntax> attributes)
		{
			MemberSyntax = memberSyntax;
			Attributes = attributes;
		}

		public void Run (params (string, string)[] MSBuildProperties)
		{
			var testSyntaxTree = MemberSyntax.SyntaxTree.GetRoot ().SyntaxTree;
			var testDependenciesSource = GetTestDependencies (testSyntaxTree)
				.Select (testDependency => CSharpSyntaxTree.ParseText (File.ReadAllText (testDependency)));

			var test = new TestChecker (
				MemberSyntax,
				TestCaseCompilation.CreateCompilation (
					testSyntaxTree,
					MSBuildProperties,
					additionalSources: testDependenciesSource).Result);

			test.ValidateAttributes (Attributes);
		}

		private static IEnumerable<string> GetTestDependencies (SyntaxTree testSyntaxTree)
		{
			TestCaseUtils.GetDirectoryPaths (out var rootSourceDir, out _);
			foreach (var attribute in testSyntaxTree.GetRoot ().DescendantNodes ().OfType<AttributeSyntax> ()) {
				if (attribute.Name.ToString () != "SetupCompileBefore")
					continue;

				var testNamespace = testSyntaxTree.GetRoot ().DescendantNodes ().OfType<NamespaceDeclarationSyntax> ().Single ().Name.ToString ();
				var testSuiteName = testNamespace.Substring (testNamespace.LastIndexOf ('.') + 1);
				var args = TestCaseUtils.GetAttributeArguments (attribute);
				foreach (var sourceFile in ((ImplicitArrayCreationExpressionSyntax) args["#1"]).DescendantNodes ().OfType<LiteralExpressionSyntax> ())
					yield return Path.Combine (rootSourceDir, testSuiteName, TestCaseUtils.GetStringFromExpression (sourceFile));
			}
		}
	}
}