< Summary

Class:GDX.Developer.Reports.NUnitReport
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Developer/Reports/NUnitReport.cs
Covered lines:143
Uncovered lines:49
Coverable lines:192
Total lines:277
Line coverage:74.4% (143 of 192)
Covered branches:0
Total branches:0
Covered methods:15
Total methods:17
Method coverage:88.2% (15 of 17)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NUnitReport(...)0%110100%
AddDurationResult(...)0%660100%
AddSkippedTest(...)0%220100%
SetForceFail()0%110100%
GetResultCount()0%110100%
GetResult()0%2.022083.33%
GetRootSuite()0%110100%
Process()0%110100%
ToString()0%110100%
AddToGenerator(...)0%2100%
AddToGenerator(...)0%6200%
AddToGenerator(...)0%19.819048.89%
AddToGenerator(...)0%4.064084.44%
AddToGenerator(...)0%110100%
AddToGeneratorAttribute(...)0%220100%
AddToGeneratorAttribute(...)0%220100%
AddToGeneratorAttribute(...)0%220100%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Developer/Reports/NUnitReport.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6using GDX.Developer.Reports.NUnit;
 7
 8namespace GDX.Developer.Reports
 9{
 10    public class NUnitReport
 11    {
 12        public const string SkippedString = "Skipped";
 13        public const string FailedString = "Failed";
 14        public const string PassedString = "Passed";
 15
 16
 1317        readonly TestRun m_Results = new TestRun();
 18        bool m_ForceFail;
 19
 1320        public NUnitReport(string name = null, string fullName = null, string className = null )
 1321        {
 1322            m_Results.TestSuite.Name = name;
 1323            m_Results.TestSuite.FullName = fullName;
 1324            m_Results.TestSuite.ClassName = className;
 1325        }
 26        public TestCase AddDurationResult(string name, float seconds, bool passed = true, string failMessage = null, Tes
 727        {
 728            TestCase testCase = new TestCase
 29            {
 30                Name = name,
 31                Duration = seconds,
 32                Result = passed ? PassedString : FailedString,
 33                Message = passed ? null : failMessage
 34            };
 735            if (testSuite != null)
 136            {
 137                testSuite.TestCases.Add(testCase);
 138            }
 39            else
 640            {
 641                m_Results.TestSuite.TestCases.Add(testCase);
 642            }
 743            return testCase;
 744        }
 45        public TestCase AddSkippedTest(string name, string skipMessage = null, TestSuite testSuite = null)
 546        {
 547            TestCase testCase = new TestCase
 48            {
 49                Name = name,
 50                Result = SkippedString,
 51                Message = skipMessage
 52            };
 553            if (testSuite != null)
 254            {
 255                testSuite.TestCases.Add(testCase);
 256            }
 57            else
 358            {
 359                m_Results.TestSuite.TestCases.Add(testCase);
 360            }
 561            return testCase;
 562        }
 63
 64        public void SetForceFail()
 165        {
 166            m_ForceFail = true;
 167        }
 68
 69        public int GetResultCount()
 270        {
 271            return m_Results.TestCaseCount;
 272        }
 73
 74        public string GetResult()
 175        {
 176            if (m_ForceFail)
 177            {
 178                return FailedString;
 79            }
 080            return m_Results.Result;
 181        }
 82
 83        public TestSuite GetRootSuite()
 984        {
 985            return m_Results.TestSuite;
 986        }
 87
 88        public void Process()
 289        {
 290            m_Results.Process();
 291        }
 92
 93        /// <summary>
 94        ///     Builds and returns the NUnit formatted report.
 95        /// </summary>
 96        /// <remarks>Can result in a UTF-16 based XML document.</remarks>
 97        public override string ToString()
 298        {
 99            // We need to make sure it is a UTF-8 xml serializer as most parsers wont know what to do with UTF-16
 2100            TextGenerator generator = new TextGenerator();
 2101            m_Results.Process();
 2102            AddToGenerator(generator, m_Results);
 2103            return generator.ToString();
 2104        }
 105
 106        static void AddToGenerator(TextGenerator generator, Property property)
 0107        {
 0108            generator.ApplyIndent();
 0109            generator.Append("<property");
 0110            AddToGeneratorAttribute(generator, "name", property.Name);
 0111            AddToGeneratorAttribute(generator, "value", property.Value);
 0112            generator.Append(" />");
 0113            generator.NextLine();
 0114        }
 115        static void AddToGenerator(TextGenerator generator, Properties properties)
 0116        {
 0117            generator.AppendLine("<properties>");
 0118            generator.PushIndent();
 0119            int count = properties.Property.Count;
 0120            for (int i = 0; i < count; i++)
 0121            {
 0122                AddToGenerator(generator, properties.Property[i]);
 0123            }
 0124            generator.PopIndent();
 0125            generator.AppendLine("</properties>");
 0126        }
 127        static void AddToGenerator(TextGenerator generator, TestCase testCase)
 4128        {
 4129            generator.ApplyIndent();
 4130            generator.Append("<test-case");
 131            // ReSharper disable StringLiteralTypo
 4132            AddToGeneratorAttribute(generator, "id", testCase.Id);
 4133            AddToGeneratorAttribute(generator, "name", testCase.Name);
 4134            AddToGeneratorAttribute(generator, "fullname", testCase.FullName);
 4135            AddToGeneratorAttribute(generator, "methodname", testCase.MethodName);
 4136            AddToGeneratorAttribute(generator, "classname", testCase.ClassName);
 4137            AddToGeneratorAttribute(generator, "runstate", testCase.RunState);
 4138            AddToGeneratorAttribute(generator, "seed", testCase.Seed);
 4139            AddToGeneratorAttribute(generator, "result", testCase.Result);
 4140            AddToGeneratorAttribute(generator, "start-time", testCase.StartTime);
 4141            AddToGeneratorAttribute(generator, "end-time", testCase.EndTime);
 4142            AddToGeneratorAttribute(generator, "duration", testCase.Duration);
 4143            AddToGeneratorAttribute(generator, "asserts", testCase.Asserts);
 4144            AddToGeneratorAttribute(generator, "output", testCase.Output);
 145            // ReSharper restore StringLiteralTypo
 146
 4147            if ((testCase.Properties != null && testCase.Properties.Property.Count > 0) ||
 148                testCase.Output != null)
 0149            {
 0150                generator.Append(">");
 0151                generator.NextLine();
 0152                generator.PushIndent();
 153
 0154                if (testCase.Output != null)
 0155                {
 0156                    generator.AppendLine($"<output><![CDATA[{testCase.Output}]]></output>");
 0157                }
 158
 0159                if (testCase.Message != null)
 0160                {
 0161                    generator.AppendLine($"<message><![CDATA[{testCase.Output}]]></message>");
 0162                }
 163
 0164                if (testCase.StackTrace != null)
 0165                {
 0166                    generator.AppendLine($"<stack-trace><![CDATA[{testCase.Output}]]></stack-trace>");
 0167                }
 168
 0169                if (testCase.Properties != null && testCase.Properties.Property.Count > 0)
 0170                {
 0171                    AddToGenerator(generator, testCase.Properties);
 0172                }
 173
 0174                generator.PopIndent();
 0175                generator.AppendLine("</test-case>");
 0176            }
 177            else
 4178            {
 4179                generator.Append(" />");
 4180                generator.NextLine();
 4181            }
 4182        }
 183        static void AddToGenerator(TextGenerator generator, TestSuite testSuite)
 2184        {
 2185            generator.ApplyIndent();
 2186            generator.Append("<test-suite");
 187            // ReSharper disable StringLiteralTypo
 2188            AddToGeneratorAttribute(generator, "type", testSuite.Type);
 2189            AddToGeneratorAttribute(generator, "id", testSuite.Id);
 2190            AddToGeneratorAttribute(generator, "name", testSuite.Name);
 2191            AddToGeneratorAttribute(generator, "fullname", testSuite.FullName);
 2192            AddToGeneratorAttribute(generator, "classname", testSuite.ClassName);
 2193            AddToGeneratorAttribute(generator, "runstate", testSuite.RunState);
 194
 2195            AddToGeneratorAttribute(generator, "testcasecount", testSuite.TestCaseCount);
 2196            AddToGeneratorAttribute(generator, "result", testSuite.Result);
 2197            AddToGeneratorAttribute(generator, "label", testSuite.Label);
 2198            AddToGeneratorAttribute(generator, "start-time", testSuite.StartTime);
 2199            AddToGeneratorAttribute(generator, "end-time", testSuite.EndTime);
 2200            AddToGeneratorAttribute(generator, "duration", testSuite.Duration);
 201
 2202            AddToGeneratorAttribute(generator, "total", testSuite.Total);
 2203            AddToGeneratorAttribute(generator, "passed", testSuite.Passed);
 2204            AddToGeneratorAttribute(generator, "failed", testSuite.Failed);
 2205            AddToGeneratorAttribute(generator, "inconclusive", testSuite.Inconclusive);
 2206            AddToGeneratorAttribute(generator, "skipped", testSuite.Skipped);
 2207            AddToGeneratorAttribute(generator, "asserts", testSuite.Asserts);
 208            // ReSharper restore StringLiteralTypo
 209
 2210            generator.Append(">");
 2211            generator.NextLine();
 2212            generator.PushIndent();
 2213            if (testSuite.Properties != null)
 0214            {
 0215                AddToGenerator(generator, testSuite.Properties);
 216
 0217            }
 218
 14219            foreach (TestCase t in testSuite.TestCases)
 4220            {
 4221                AddToGenerator(generator, t);
 4222            }
 223
 6224            foreach (TestSuite s in testSuite.TestSuites)
 0225            {
 0226                AddToGenerator(generator, s);
 0227            }
 2228            generator.PopIndent();
 2229            generator.AppendLine("</test-suite>");
 2230        }
 231        static void AddToGenerator(TextGenerator generator, TestRun testRun)
 2232        {
 2233            generator.ApplyIndent();
 2234            generator.Append("<test-run");
 2235            AddToGeneratorAttribute(generator, "id", testRun.Id);
 236            // ReSharper disable once StringLiteralTypo
 2237            AddToGeneratorAttribute(generator, "testcasecount", testRun.TestCaseCount);
 2238            AddToGeneratorAttribute(generator, "result", testRun.Result);
 2239            AddToGeneratorAttribute(generator, "total", testRun.Total);
 2240            AddToGeneratorAttribute(generator, "passed", testRun.Passed);
 2241            AddToGeneratorAttribute(generator, "failed", testRun.Failed);
 2242            AddToGeneratorAttribute(generator, "inconclusive", testRun.Inconclusive);
 2243            AddToGeneratorAttribute(generator, "skipped", testRun.Skipped);
 2244            AddToGeneratorAttribute(generator, "asserts", testRun.Asserts);
 2245            AddToGeneratorAttribute(generator, "engine-version", testRun.EngineVersion);
 2246            AddToGeneratorAttribute(generator, "clr-version", testRun.CLRVersion);
 2247            AddToGeneratorAttribute(generator, "start-time", testRun.StartTime);
 2248            AddToGeneratorAttribute(generator, "end-time", testRun.EndTime);
 2249            AddToGeneratorAttribute(generator, "duration", testRun.Duration);
 2250            generator.Append(">");
 2251            generator.NextLine();
 2252            generator.PushIndent();
 2253            AddToGenerator(generator, testRun.TestSuite);
 2254            generator.PopIndent();
 2255            generator.AppendLine("</test-run>");
 2256        }
 257
 258        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 259        static void AddToGeneratorAttribute(TextGenerator generator, string key, string value)
 70260        {
 122261            if (value == default) return;
 18262            generator.Append($" {key}=\"{value}\"");
 70263        }
 264        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 265        static void AddToGeneratorAttribute(TextGenerator generator, string key, int value)
 38266        {
 60267            if (value == default) return;
 16268            generator.Append($" {key}=\"{value}\"");
 38269        }
 270        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 271        static void AddToGeneratorAttribute(TextGenerator generator, string key, float value)
 8272        {
 13273            if (value == 0f) return;
 3274            generator.Append($" {key}=\"{value}\"");
 8275        }
 276    }
 277}