<commit_before>﻿namespace Fixie.Tests.Internal.Listeners
{
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Xml.Linq;
    using Assertions;
    using Fixie.Internal.Listeners;

    public class ReportListenerTests : MessagingTests
    {
        public void ShouldProduceValidXmlDocument()
        {
            XDocument? actual = null;
            var listener = new ReportListener(report => actual = report);

            Run(listener, out var console);

            console
                .ShouldBe(
                    "Console.Out: Fail",
                    "Console.Error: Fail",
                    "Console.Out: FailByAssertion",
                    "Console.Error: FailByAssertion",
                    "Console.Out: Pass",
                    "Console.Error: Pass");

            if (actual == null)
                throw new Exception("Expected non-null XML report.");

            CleanBrittleValues(actual.ToString(SaveOptions.DisableFormatting))
                .Lines()
                .CleanStackTraceLineNumbers()
                .ShouldBe(ExpectedReport.Lines());
        }

        static string CleanBrittleValues(string actualRawContent)
        {
            //Avoid brittle assertion introduced by system date.
            var cleaned = Regex.Replace(actualRawContent, @"run-date=""\d\d\d\d-\d\d-\d\d""", @"run-date=""YYYY-MM-DD""");

            //Avoid brittle assertion introduced by system time.
            cleaned = Regex.Replace(cleaned, @"run-time=""\d\d:\d\d:\d\d""", @"run-time=""HH:MM:SS""");

            //Avoid brittle assertion introduced by .NET version.
            cleaned = cleaned.Replace($@"environment=""{IntPtr.Size * 8}-bit .NET {Framework}""", @"environment=""00-bit .NET 1.2.3.4""");

            //Avoid brittle assertion introduced by fixie version.
            cleaned = cleaned.Replace($@"test-framework=""{Fixie.Framework.Version}""", @"test-framework=""Fixie 1.2.3.4""");

            //Avoid brittle assertion introduced by test duration.
            cleaned = Regex.Replace(cleaned, @"time=""[\d\.]+""", @"time=""1.234""");

            return cleaned;
        }

        string ExpectedReport
        {
            get
            {
                var assemblyLocation = GetType().Assembly.Location;
                var fileLocation = TestClassPath();
                return XDocument.Parse(File.ReadAllText(Path.Combine("Internal", Path.Combine("Listeners", "XUnitXmlReport.xml"))))
                                .ToString(SaveOptions.DisableFormatting)
                                .Replace("[assemblyLocation]", assemblyLocation)
                                .Replace("[fileLocation]", fileLocation)
                                .Replace("[testClass]", TestClass)
                                .Replace("[testClassForStackTrace]", TestClass.Replace("+", "."));
            }
        }

        static string Framework => Environment.Version.ToString();
    }
}<commit_msg>Improve the precision of XML report duration string expectations, to better-test the formatting of those strings.<commit_after>﻿namespace Fixie.Tests.Internal.Listeners
{
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Xml.Linq;
    using Assertions;
    using Fixie.Internal.Listeners;

    public class ReportListenerTests : MessagingTests
    {
        public void ShouldProduceValidXmlDocument()
        {
            XDocument? actual = null;
            var listener = new ReportListener(report => actual = report);

            Run(listener, out var console);

            console
                .ShouldBe(
                    "Console.Out: Fail",
                    "Console.Error: Fail",
                    "Console.Out: FailByAssertion",
                    "Console.Error: FailByAssertion",
                    "Console.Out: Pass",
                    "Console.Error: Pass");

            if (actual == null)
                throw new Exception("Expected non-null XML report.");

            CleanBrittleValues(actual.ToString(SaveOptions.DisableFormatting))
                .Lines()
                .CleanStackTraceLineNumbers()
                .ShouldBe(ExpectedReport.Lines());
        }

        static string CleanBrittleValues(string actualRawContent)
        {
            //Avoid brittle assertion introduced by system date.
            var cleaned = Regex.Replace(actualRawContent, @"run-date=""\d\d\d\d-\d\d-\d\d""", @"run-date=""YYYY-MM-DD""");

            //Avoid brittle assertion introduced by system time.
            cleaned = Regex.Replace(cleaned, @"run-time=""\d\d:\d\d:\d\d""", @"run-time=""HH:MM:SS""");

            //Avoid brittle assertion introduced by .NET version.
            cleaned = cleaned.Replace($@"environment=""{IntPtr.Size * 8}-bit .NET {Framework}""", @"environment=""00-bit .NET 1.2.3.4""");

            //Avoid brittle assertion introduced by fixie version.
            cleaned = cleaned.Replace($@"test-framework=""{Fixie.Framework.Version}""", @"test-framework=""Fixie 1.2.3.4""");

            //Avoid brittle assertion introduced by test duration.
            cleaned = Regex.Replace(cleaned, @"time=""\d+\.\d\d\d""", @"time=""1.234""");

            return cleaned;
        }

        string ExpectedReport
        {
            get
            {
                var assemblyLocation = GetType().Assembly.Location;
                var fileLocation = TestClassPath();
                return XDocument.Parse(File.ReadAllText(Path.Combine("Internal", Path.Combine("Listeners", "XUnitXmlReport.xml"))))
                                .ToString(SaveOptions.DisableFormatting)
                                .Replace("[assemblyLocation]", assemblyLocation)
                                .Replace("[fileLocation]", fileLocation)
                                .Replace("[testClass]", TestClass)
                                .Replace("[testClassForStackTrace]", TestClass.Replace("+", "."));
            }
        }

        static string Framework => Environment.Version.ToString();
    }
}
{'fraction_non_alphanumeric': 0.12721311475409836, 'fraction_numerical': 0.004918032786885246, 'mean_word_length': 1.846943537097527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2888973', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1516, 'n_words': 321}