A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Xml;
6using UnityEditor.TestTools.TestRunner.Api;
7using UnityEditor.TestTools.TestRunner.GUI;
8
9namespace UnityEditor.TestTools.TestRunner.UnityTestProtocol
10{
11 internal class TestRunnerApiMapper : ITestRunnerApiMapper
12 {
13 internal IGuiHelper guiHelper = new GuiHelper(new MonoCecilHelper(), new AssetsDatabaseHelper());
14 private readonly string _projectRepoPath;
15
16 public TestRunnerApiMapper(string projectRepoPath)
17 {
18 _projectRepoPath = projectRepoPath;
19 }
20
21 public TestPlanMessage MapTestToTestPlanMessage(ITestAdaptor testsToRun)
22 {
23 var testsNames = testsToRun != null ? FlattenTestNames(testsToRun) : new List<string>();
24
25 var msg = new TestPlanMessage
26 {
27 tests = testsNames
28 };
29
30 return msg;
31 }
32
33 public TestStartedMessage MapTestToTestStartedMessage(ITestAdaptor test)
34 {
35 return new TestStartedMessage
36 {
37 name = test.FullName
38 };
39 }
40
41 public TestFinishedMessage TestResultToTestFinishedMessage(ITestResultAdaptor result)
42 {
43 string filePathString = default;
44 int lineNumber = default;
45 if (result.Test.Method != null && result.Test.TypeInfo != null)
46 {
47 var method = result.Test.Method.MethodInfo;
48 var type = result.Test.TypeInfo.Type;
49 var fileOpenInfo = guiHelper.GetFileOpenInfo(type, method);
50 filePathString = !string.IsNullOrEmpty(_projectRepoPath) ? Path.Combine(_projectRepoPath, fileOpenInfo.FilePath) : fileOpenInfo.FilePath;
51 lineNumber = fileOpenInfo.LineNumber;
52 }
53
54 var iteration = 0;
55 if(result is TestResultAdaptor)
56 {
57 var adaptor = ((TestResultAdaptor)result);
58 iteration = adaptor.RepeatIteration == 0 ? adaptor.RetryIteration : adaptor.RepeatIteration;
59 }
60 return new TestFinishedMessage
61 {
62 name = result.Test.FullName,
63 duration = Convert.ToUInt64(result.Duration * 1000),
64 durationMicroseconds = Convert.ToUInt64(result.Duration * 1000000),
65 message = result.Message,
66 state = GetTestStateFromResult(result),
67 stackTrace = result.StackTrace,
68 fileName = filePathString,
69 lineNumber = lineNumber,
70 iteration = iteration
71 };
72 }
73
74 public string GetRunStateFromResultNunitXml(ITestResultAdaptor result)
75 {
76 var doc = new XmlDocument();
77 doc.LoadXml(result.ToXml().OuterXml);
78 return doc.FirstChild.Attributes["runstate"].Value;
79 }
80
81 public TestState GetTestStateFromResult(ITestResultAdaptor result)
82 {
83 var state = TestState.Failure;
84
85 if (result.TestStatus == TestStatus.Passed)
86 {
87 state = TestState.Success;
88 }
89 else if (result.TestStatus == TestStatus.Skipped)
90 {
91 state = TestState.Skipped;
92
93 if (result.ResultState.ToLowerInvariant().EndsWith("ignored"))
94 {
95 state = TestState.Ignored;
96 }
97 }
98 else
99 {
100 if (result.ResultState.ToLowerInvariant().Equals("inconclusive"))
101 {
102 state = TestState.Inconclusive;
103 }
104
105 if (result.ResultState.ToLowerInvariant().EndsWith("cancelled") ||
106 result.ResultState.ToLowerInvariant().EndsWith("error"))
107 {
108 state = TestState.Error;
109 }
110 }
111
112 return state;
113 }
114
115 public List<string> FlattenTestNames(ITestAdaptor test)
116 {
117 var results = new List<string>();
118
119 if (!test.IsSuite)
120 results.Add(test.FullName);
121
122 if (test.Children != null && test.Children.Any())
123 foreach (var child in test.Children)
124 results.AddRange(FlattenTestNames(child));
125
126 return results;
127 }
128 }
129}