< Summary

Class:GDX.Developer.Reports.NUnitReport
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/Reports/NUnitReport.cs
Covered lines:149
Uncovered lines:49
Coverable lines:198
Total lines:297
Line coverage:75.2% (149 of 198)
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%6.736072.73%
AddSkippedTest(...)0%2.082072.73%
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)

./Packages/com.dotbunny.gdx/GDX/Developer/Reports/NUnitReport.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 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
 27        public TestCase AddDurationResult(string name, float seconds, bool passed = true, string failMessage = null,
 28            TestSuite testSuite = null)
 729        {
 730            TestCase testCase = new TestCase
 31            {
 32                Name = name,
 33                Duration = seconds,
 34                Result = passed ? PassedString : FailedString,
 35                Message = passed ? null : failMessage
 36            };
 737            if (testSuite != null)
 138            {
 139                testSuite.TestCases.Add(testCase);
 140            }
 41            else
 642            {
 643                m_Results.TestSuite.TestCases.Add(testCase);
 644            }
 45
 746            return testCase;
 747        }
 48
 49        public TestCase AddSkippedTest(string name, string skipMessage = null, TestSuite testSuite = null)
 550        {
 551            TestCase testCase = new TestCase { Name = name, Result = SkippedString, Message = skipMessage };
 552            if (testSuite != null)
 253            {
 254                testSuite.TestCases.Add(testCase);
 255            }
 56            else
 357            {
 358                m_Results.TestSuite.TestCases.Add(testCase);
 359            }
 60
 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            }
 80
 081            return m_Results.Result;
 182        }
 83
 84        public TestSuite GetRootSuite()
 985        {
 986            return m_Results.TestSuite;
 987        }
 88
 89        public void Process()
 290        {
 291            m_Results.Process();
 292        }
 93
 94        /// <summary>
 95        ///     Builds and returns the NUnit formatted report.
 96        /// </summary>
 97        /// <remarks>Can result in a UTF-16 based XML document.</remarks>
 98        public override string ToString()
 299        {
 100            // We need to make sure it is a UTF-8 xml serializer as most parsers wont know what to do with UTF-16
 2101            TextGenerator generator = new TextGenerator();
 2102            m_Results.Process();
 2103            AddToGenerator(generator, m_Results);
 2104            return generator.ToString();
 2105        }
 106
 107        static void AddToGenerator(TextGenerator generator, Property property)
 0108        {
 0109            generator.ApplyIndent();
 0110            generator.Append("<property");
 0111            AddToGeneratorAttribute(generator, "name", property.Name);
 0112            AddToGeneratorAttribute(generator, "value", property.Value);
 0113            generator.Append(" />");
 0114            generator.NextLine();
 0115        }
 116
 117        static void AddToGenerator(TextGenerator generator, Properties properties)
 0118        {
 0119            generator.AppendLine("<properties>");
 0120            generator.PushIndent();
 0121            int count = properties.Property.Count;
 0122            for (int i = 0; i < count; i++)
 0123            {
 0124                AddToGenerator(generator, properties.Property[i]);
 0125            }
 126
 0127            generator.PopIndent();
 0128            generator.AppendLine("</properties>");
 0129        }
 130
 131        static void AddToGenerator(TextGenerator generator, TestCase testCase)
 4132        {
 4133            generator.ApplyIndent();
 4134            generator.Append("<test-case");
 135            // ReSharper disable StringLiteralTypo
 4136            AddToGeneratorAttribute(generator, "id", testCase.Id);
 4137            AddToGeneratorAttribute(generator, "name", testCase.Name);
 4138            AddToGeneratorAttribute(generator, "fullname", testCase.FullName);
 4139            AddToGeneratorAttribute(generator, "methodname", testCase.MethodName);
 4140            AddToGeneratorAttribute(generator, "classname", testCase.ClassName);
 4141            AddToGeneratorAttribute(generator, "runstate", testCase.RunState);
 4142            AddToGeneratorAttribute(generator, "seed", testCase.Seed);
 4143            AddToGeneratorAttribute(generator, "result", testCase.Result);
 4144            AddToGeneratorAttribute(generator, "start-time", testCase.StartTime);
 4145            AddToGeneratorAttribute(generator, "end-time", testCase.EndTime);
 4146            AddToGeneratorAttribute(generator, "duration", testCase.Duration);
 4147            AddToGeneratorAttribute(generator, "asserts", testCase.Asserts);
 4148            AddToGeneratorAttribute(generator, "output", testCase.Output);
 149            // ReSharper restore StringLiteralTypo
 150
 4151            if ((testCase.Properties != null && testCase.Properties.Property.Count > 0) ||
 152                testCase.Output != null)
 0153            {
 0154                generator.Append(">");
 0155                generator.NextLine();
 0156                generator.PushIndent();
 157
 0158                if (testCase.Output != null)
 0159                {
 0160                    generator.AppendLine($"<output><![CDATA[{testCase.Output}]]></output>");
 0161                }
 162
 0163                if (testCase.Message != null)
 0164                {
 0165                    generator.AppendLine($"<message><![CDATA[{testCase.Output}]]></message>");
 0166                }
 167
 0168                if (testCase.StackTrace != null)
 0169                {
 0170                    generator.AppendLine($"<stack-trace><![CDATA[{testCase.Output}]]></stack-trace>");
 0171                }
 172
 0173                if (testCase.Properties != null && testCase.Properties.Property.Count > 0)
 0174                {
 0175                    AddToGenerator(generator, testCase.Properties);
 0176                }
 177
 0178                generator.PopIndent();
 0179                generator.AppendLine("</test-case>");
 0180            }
 181            else
 4182            {
 4183                generator.Append(" />");
 4184                generator.NextLine();
 4185            }
 4186        }
 187
 188        static void AddToGenerator(TextGenerator generator, TestSuite testSuite)
 2189        {
 2190            generator.ApplyIndent();
 2191            generator.Append("<test-suite");
 192            // ReSharper disable StringLiteralTypo
 2193            AddToGeneratorAttribute(generator, "type", testSuite.Type);
 2194            AddToGeneratorAttribute(generator, "id", testSuite.Id);
 2195            AddToGeneratorAttribute(generator, "name", testSuite.Name);
 2196            AddToGeneratorAttribute(generator, "fullname", testSuite.FullName);
 2197            AddToGeneratorAttribute(generator, "classname", testSuite.ClassName);
 2198            AddToGeneratorAttribute(generator, "runstate", testSuite.RunState);
 199
 2200            AddToGeneratorAttribute(generator, "testcasecount", testSuite.TestCaseCount);
 2201            AddToGeneratorAttribute(generator, "result", testSuite.Result);
 2202            AddToGeneratorAttribute(generator, "label", testSuite.Label);
 2203            AddToGeneratorAttribute(generator, "start-time", testSuite.StartTime);
 2204            AddToGeneratorAttribute(generator, "end-time", testSuite.EndTime);
 2205            AddToGeneratorAttribute(generator, "duration", testSuite.Duration);
 206
 2207            AddToGeneratorAttribute(generator, "total", testSuite.Total);
 2208            AddToGeneratorAttribute(generator, "passed", testSuite.Passed);
 2209            AddToGeneratorAttribute(generator, "failed", testSuite.Failed);
 2210            AddToGeneratorAttribute(generator, "inconclusive", testSuite.Inconclusive);
 2211            AddToGeneratorAttribute(generator, "skipped", testSuite.Skipped);
 2212            AddToGeneratorAttribute(generator, "asserts", testSuite.Asserts);
 213            // ReSharper restore StringLiteralTypo
 214
 2215            generator.Append(">");
 2216            generator.NextLine();
 2217            generator.PushIndent();
 2218            if (testSuite.Properties != null)
 0219            {
 0220                AddToGenerator(generator, testSuite.Properties);
 0221            }
 222
 14223            foreach (TestCase t in testSuite.TestCases)
 4224            {
 4225                AddToGenerator(generator, t);
 4226            }
 227
 6228            foreach (TestSuite s in testSuite.TestSuites)
 0229            {
 0230                AddToGenerator(generator, s);
 0231            }
 232
 2233            generator.PopIndent();
 2234            generator.AppendLine("</test-suite>");
 2235        }
 236
 237        static void AddToGenerator(TextGenerator generator, TestRun testRun)
 2238        {
 2239            generator.ApplyIndent();
 2240            generator.Append("<test-run");
 2241            AddToGeneratorAttribute(generator, "id", testRun.Id);
 242            // ReSharper disable once StringLiteralTypo
 2243            AddToGeneratorAttribute(generator, "testcasecount", testRun.TestCaseCount);
 2244            AddToGeneratorAttribute(generator, "result", testRun.Result);
 2245            AddToGeneratorAttribute(generator, "total", testRun.Total);
 2246            AddToGeneratorAttribute(generator, "passed", testRun.Passed);
 2247            AddToGeneratorAttribute(generator, "failed", testRun.Failed);
 2248            AddToGeneratorAttribute(generator, "inconclusive", testRun.Inconclusive);
 2249            AddToGeneratorAttribute(generator, "skipped", testRun.Skipped);
 2250            AddToGeneratorAttribute(generator, "asserts", testRun.Asserts);
 2251            AddToGeneratorAttribute(generator, "engine-version", testRun.EngineVersion);
 2252            AddToGeneratorAttribute(generator, "clr-version", testRun.CLRVersion);
 2253            AddToGeneratorAttribute(generator, "start-time", testRun.StartTime);
 2254            AddToGeneratorAttribute(generator, "end-time", testRun.EndTime);
 2255            AddToGeneratorAttribute(generator, "duration", testRun.Duration);
 2256            generator.Append(">");
 2257            generator.NextLine();
 2258            generator.PushIndent();
 2259            AddToGenerator(generator, testRun.TestSuite);
 2260            generator.PopIndent();
 2261            generator.AppendLine("</test-run>");
 2262        }
 263
 264        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 265        static void AddToGeneratorAttribute(TextGenerator generator, string key, string value)
 70266        {
 70267            if (value == default)
 52268            {
 52269                return;
 270            }
 271
 18272            generator.Append($" {key}=\"{value}\"");
 70273        }
 274
 275        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 276        static void AddToGeneratorAttribute(TextGenerator generator, string key, int value)
 38277        {
 38278            if (value == default)
 22279            {
 22280                return;
 281            }
 282
 16283            generator.Append($" {key}=\"{value}\"");
 38284        }
 285
 286        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 287        static void AddToGeneratorAttribute(TextGenerator generator, string key, float value)
 8288        {
 8289            if (value == 0f)
 5290            {
 5291                return;
 292            }
 293
 3294            generator.Append($" {key}=\"{value}\"");
 8295        }
 296    }
 297}

Coverage by test methods














Methods/Properties

NUnitReport(System.String, System.String, System.String)
AddDurationResult(System.String, System.Single, System.Boolean, System.String, GDX.Developer.Reports.NUnit.TestSuite)
AddSkippedTest(System.String, System.String, GDX.Developer.Reports.NUnit.TestSuite)
SetForceFail()
GetResultCount()
GetResult()
GetRootSuite()
Process()
ToString()
AddToGenerator(GDX.Developer.TextGenerator, GDX.Developer.Reports.NUnit.Property)
AddToGenerator(GDX.Developer.TextGenerator, GDX.Developer.Reports.NUnit.Properties)
AddToGenerator(GDX.Developer.TextGenerator, GDX.Developer.Reports.NUnit.TestCase)
AddToGenerator(GDX.Developer.TextGenerator, GDX.Developer.Reports.NUnit.TestSuite)
AddToGenerator(GDX.Developer.TextGenerator, GDX.Developer.Reports.NUnit.TestRun)
AddToGeneratorAttribute(GDX.Developer.TextGenerator, System.String, System.String)
AddToGeneratorAttribute(GDX.Developer.TextGenerator, System.String, System.Int32)
AddToGeneratorAttribute(GDX.Developer.TextGenerator, System.String, System.Single)