A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEditor.TestTools.TestRunner.Api;
5using UnityEngine;
6
7namespace UnityEditor.TestTools.TestRunner.GUI
8{
9 [Serializable]
10 internal class TestRunnerResult : UITestRunnerFilter.IClearableResult
11 {
12 public string id;
13 public string uniqueId;
14 public string name;
15 public string fullName;
16 public ResultStatus resultStatus = ResultStatus.NotRun;
17 public float duration;
18 public string messages;
19 public string output;
20 public string stacktrace;
21 public bool notRunnable;
22 public bool ignoredOrSkipped;
23 public string description;
24 public bool isSuite;
25 public List<string> categories;
26 public string parentId;
27 public string parentUniqueId;
28
29 //This field is suppose to mark results from before domain reload
30 //Such result is outdated because the code might haev changed
31 //This field will get reset every time a domain reload happens
32 [NonSerialized]
33 public bool notOutdated;
34
35 protected Action<TestRunnerResult> m_OnResultUpdate;
36
37 internal TestRunnerResult(ITestAdaptor test)
38 {
39 id = test.Id;
40 uniqueId = test.UniqueName;
41
42 fullName = test.FullName;
43 name = test.Name;
44 description = test.Description;
45 isSuite = test.IsSuite;
46
47 ignoredOrSkipped = test.RunState == RunState.Ignored || test.RunState == RunState.Skipped;
48 notRunnable = test.RunState == RunState.NotRunnable;
49
50 if (ignoredOrSkipped)
51 {
52 messages = test.SkipReason;
53 }
54 if (notRunnable)
55 {
56 resultStatus = ResultStatus.Failed;
57 messages = test.SkipReason;
58 }
59 categories = test.Categories.ToList();
60 parentId = test.ParentId;
61 parentUniqueId = test.ParentUniqueName;
62 }
63
64 internal TestRunnerResult(ITestResultAdaptor testResult) : this(testResult.Test)
65 {
66 notOutdated = true;
67
68 messages = testResult.Message;
69 output = testResult.Output;
70 stacktrace = testResult.StackTrace;
71 duration = (float)testResult.Duration;
72 if (testResult.Test.IsSuite && testResult.ResultState == "Ignored")
73 {
74 resultStatus = ResultStatus.Passed;
75 }
76 else
77 {
78 resultStatus = ParseNUnitResultStatus(testResult.TestStatus);
79 }
80 }
81
82 public void CalculateParentResult(string parentId, IDictionary<string, List<TestRunnerResult>> results)
83 {
84 if (results == null) return;
85 results.TryGetValue(parentId , out var childrenResult);
86 if (childrenResult == null) return;
87 if (childrenResult.TrueForAll(x => x.resultStatus == ResultStatus.Passed)) resultStatus = ResultStatus.Passed;
88 if (childrenResult.TrueForAll(x => x.resultStatus == ResultStatus.Skipped)) resultStatus = ResultStatus.Skipped;
89 else if (childrenResult.Any(x => x.resultStatus == ResultStatus.Skipped))
90 {
91 resultStatus = ResultStatus.Passed;
92 }
93 if (childrenResult.Any(x => x.resultStatus == ResultStatus.Inconclusive)) resultStatus = ResultStatus.Inconclusive;
94 if (childrenResult.Any(x => x.resultStatus == ResultStatus.Failed)) resultStatus = ResultStatus.Failed;
95 UpdateParentResult(results);
96 }
97
98 private void UpdateParentResult(IDictionary<string, List<TestRunnerResult>> results)
99 {
100 if (string.IsNullOrEmpty(parentUniqueId)) return;
101 results.TryGetValue(parentUniqueId, out var parentResultList);
102 if (parentResultList != null && parentResultList.Count > 0)
103 {
104 parentResultList.Add(this);
105 }
106 else
107 {
108 results.Add(parentUniqueId, new List<TestRunnerResult> {this});
109 }
110 }
111
112 public void Update(TestRunnerResult result)
113 {
114 if (ReferenceEquals(result, null))
115 return;
116 resultStatus = result.resultStatus;
117 duration = result.duration;
118 messages = result.messages;
119 output = result.output;
120 stacktrace = result.stacktrace;
121 ignoredOrSkipped = result.ignoredOrSkipped;
122 notRunnable = result.notRunnable;
123 description = result.description;
124 notOutdated = result.notOutdated;
125 if (m_OnResultUpdate != null && !result.isSuite)
126 {
127 m_OnResultUpdate(this);
128 }
129 }
130
131 public void SetResultChangedCallback(Action<TestRunnerResult> resultUpdated)
132 {
133 m_OnResultUpdate = resultUpdated;
134 }
135
136 [Serializable]
137 internal enum ResultStatus
138 {
139 NotRun,
140 Passed,
141 Failed,
142 Inconclusive,
143 Skipped
144 }
145
146 private static ResultStatus ParseNUnitResultStatus(TestStatus status)
147 {
148 switch (status)
149 {
150 case TestStatus.Passed:
151 return ResultStatus.Passed;
152 case TestStatus.Failed:
153 return ResultStatus.Failed;
154 case TestStatus.Inconclusive:
155 return ResultStatus.Inconclusive;
156 case TestStatus.Skipped:
157 return ResultStatus.Skipped;
158 default:
159 return ResultStatus.NotRun;
160 }
161 }
162
163 public override string ToString()
164 {
165 return string.Format("{0} ({1})", name, fullName);
166 }
167
168 public string Id { get { return uniqueId; } }
169 public string FullName { get { return fullName; } }
170 public string ParentId { get { return parentUniqueId; } }
171 public bool IsSuite { get { return isSuite; } }
172 public List<string> Categories { get { return categories; } }
173
174 public void Clear()
175 {
176 resultStatus = ResultStatus.NotRun;
177 stacktrace = string.Empty;
178 duration = 0.0f;
179 if (m_OnResultUpdate != null)
180 m_OnResultUpdate(this);
181 }
182 }
183}