public void FormatMethodEntry_ValidLog_NullIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));

            // Act
            var outputText = formatter.FormatMethodEntry(1, 2, 33, entry, filters, true);

            // Assert
            Assert.That(outputText, Is.Null, "Expected nothing to be returned for a method entry, everything is printed in the methodExit handling.");
        }
        public void FormatLogEvent_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new LogEvent(LogLevel.Info, DateTime.Parse("2022-10-22 22:22:31.678"), "This is a log entry");

            var expectedText = "Yalf,Log,This is a log entry,,22:22:31.678,0,1,22";

            // Act
            var outputText = formatter.FormatLogEvent(22, 1, 33, entry, filters, true);

            // Assert
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(outputText, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void FormatException_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var ex = this.GenerateExceptionWithStackTrace();
            var entry = new ExceptionTrace(ex, DateTime.Parse("2022-10-22 22:22:31.678"));

            var expectedText = @"Yalf,Exception,Attempted to divide by zero.,   at Yalf.Tests.Reporting.DelimitedValuesFormatterTests.GenerateExceptionWithStackTrace() in d:\repositories\yalf\Yalf.Tests\Reporting\DelimitedValuesFormatterTests.cs:line xxx,22:22:31.678,0,1,22";
            expectedText = expectedText.Replace(@"d:\repositories\yalf", this.GetRootFolder());

            // Act
            var outputText = formatter.FormatException(22, 1, 33, entry, filters);

            // Assert
            outputText = Regex.Replace(outputText, @"DelimitedValuesFormatterTests\.cs\:line [0-9]+,", "DelimitedValuesFormatterTests.cs:line xxx,");

            Console.WriteLine(outputText);
            Console.WriteLine(System.Environment.CurrentDirectory);
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(outputText, Is.EqualTo(expectedText).IgnoreCase, "Not the expected output text, you may need to adjust the test if the formatter has been changed - it could be the line number of the mock exception has changed.");
        }
        public void DelimitedValuesFormatter_NestedMethodCalls_ReportHasCorrectFormatting()
        {
            // Arrange
            var tempFilePath = Path.ChangeExtension(Path.GetTempFileName(), ".txt");

            var startDateTime = DateTime.Now;
            var entries = new BaseEntry[]
                {
                    new ThreadData(22, null, null),
                    new MethodEntry(1, "TopLevelMethod", null, startDateTime),
                    new MethodExit(1, "TopLevelMethod", 233, true, "blackSheep"),
                    new MethodEntry(1, "FirstMethod", null, startDateTime.AddSeconds(12)),
                    new MethodEntry(2, "SecondMethod", null, startDateTime.AddSeconds(45)),
                    new LogEvent(LogLevel.Info, startDateTime.AddSeconds(47), "Information log message here"),
                    new ExceptionTrace(new ArgumentNullException("lineNo", "Test the log"), startDateTime.AddSeconds(53)),
                    new MethodEntry(3, "ThirdMethod", null, startDateTime.AddSeconds(75)),
                    new MethodExit(3, "ThirdMethod", 100, false, null),
                    new MethodEntry(3, "FourthMethod", null, startDateTime.AddSeconds(80)),
                    new MethodExit(3, "FourthMethod", 57, false, null),
                    new MethodExit(2, "SecondMethod", 178, false, null),
                    new MethodExit(1, "FirstMethod", 200, false, null),
                    new MethodEntry(1, "TopLevelMethod2", null, startDateTime.AddSeconds(99)),
                    new MethodExit(1, "TopLevelMethod2", 488, true, "whiteSheep"),
                };

            var expectedText = (new string[]
                                    {
                                        string.Format("Yalf,Method,TopLevelMethod,blackSheep,{0:HH:mm:ss.fff},233,0,22", startDateTime),
                                        string.Format("Yalf,Method,FirstMethod,,{0:HH:mm:ss.fff},200,0,22", startDateTime.AddSeconds(12)),
                                        string.Format("Yalf,Method,SecondMethod,,{0:HH:mm:ss.fff},178,1,22", startDateTime.AddSeconds(45)),
                                        string.Format("Yalf,Log,Information log message here,,{0:HH:mm:ss.fff},0,2,22", startDateTime.AddSeconds(47)),
                                        string.Format("Yalf,Exception,Test the log Parameter name: lineNo,,{0:HH:mm:ss.fff},0,2,22", startDateTime.AddSeconds(53)),
                                        string.Format("Yalf,Method,ThirdMethod,,{0:HH:mm:ss.fff},100,2,22", startDateTime.AddSeconds(75)),
                                        string.Format("Yalf,Method,FourthMethod,,{0:HH:mm:ss.fff},57,2,22", startDateTime.AddSeconds(80)),
                                        string.Format("Yalf,Method,TopLevelMethod2,whiteSheep,{0:HH:mm:ss.fff},488,0,22", startDateTime.AddSeconds(99))
                                    }
                   ).ToList();

            var formatter = new DelimitedValuesFormatter();
            var filters = this.GetDefaultFilters();
            var indentLevel = 0;

            // Act
            var outputter = new CsvFileOutputHandler(filters, formatter, tempFilePath);
            outputter.Initialise();

            foreach (var entry in entries)
            {
                if (entry is ThreadData)
                {
                    outputter.HandleThread((ThreadData)entry);
                }
                else if (entry is MethodEntry)
                {
                    outputter.HandleMethodEntry((entry as MethodEntry), indentLevel, true);
                    ++indentLevel;
                }
                else if (entry is MethodExit)
                {
                    --indentLevel;
                    outputter.HandleMethodExit((entry as MethodExit), indentLevel, true);
                }
                else if (entry is LogEvent)
                {
                    outputter.HandleLogEvent((entry as LogEvent), indentLevel, true);
                }
                else if (entry is ExceptionTrace)
                {
                    outputter.HandleException((entry as ExceptionTrace), indentLevel);
                }
            }

            outputter.Complete();
            var reportText = File.ReadAllText(tempFilePath);

            // Assert
            Console.WriteLine("Output data to '{0}'", tempFilePath);
            Console.WriteLine(reportText);

            List<String> output = reportText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            Assert.That(output.Count, Is.EqualTo(expectedText.Count), "Expected {0} output lines, but have {1}", expectedText.Count, output.Count);
            Assert.That(reportText, Is.Not.Empty, "Expected report text to be returned.");
            for (int index = 0; index < expectedText.Count; index++)
            {
                Console.WriteLine("Checking actual\r\n\"{0}\" with expected\r\n\"{1}\"", output[index], expectedText[index]);
                Assert.That(output[index], Is.EqualTo(expectedText[index]), "Not the expected text for line {0}", index + 1);
            }
        }
        public void FormatMethodExit_MixedNestting_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = LogFiltersBuilder.Create().Build();
            var formatter = new DelimitedValuesFormatter();
            var startDateTime = DateTime.Now;
            int threadId = 22;
            int lineNo = 0;
            int indentLevel = 0;

            var expectedText = (new string[]
                                    {
                                        string.Format("Yalf,Method,TopLevelMethod,blackSheep,{0:HH:mm:ss.fff},233,1,22", startDateTime),
                                        string.Format("Yalf,Method,FirstMethod,,{0:HH:mm:ss.fff},200,1,22", startDateTime.AddSeconds(12)),
                                        string.Format("Yalf,Method,SecondMethod,,{0:HH:mm:ss.fff},178,2,22", startDateTime.AddSeconds(45)),
                                        string.Format("Yalf,Log,Information log message here,,{0:HH:mm:ss.fff},0,2,22", startDateTime.AddSeconds(47)),
                                        string.Format("Yalf,Exception,Test the log Parameter name: lineNo,,{0:HH:mm:ss.fff},0,2,22", startDateTime.AddSeconds(53)),
                                        string.Format("Yalf,Method,ThirdMethod,,{0:HH:mm:ss.fff},100,3,22", startDateTime.AddSeconds(75)),
                                        string.Format("Yalf,Method,TopLevelMethod2,whiteSheep,{0:HH:mm:ss.fff},488,1,22", startDateTime.AddSeconds(99))
                                    }
                               ).ToList();

            var entries = new BaseEntry[]
                {
                    new MethodEntry(1, "TopLevelMethod", null, startDateTime),
                    new MethodExit(1, "TopLevelMethod", 233, true, "blackSheep"),
                    new MethodEntry(1, "FirstMethod", null, startDateTime.AddSeconds(12)),
                    new MethodEntry(2, "SecondMethod", null, startDateTime.AddSeconds(45)),
                    new LogEvent(LogLevel.Info, startDateTime.AddSeconds(47), "Information log message here"),
                    new ExceptionTrace(new ArgumentNullException("lineNo", "Test the log"), startDateTime.AddSeconds(53)),
                    new MethodEntry(3, "ThirdMethod", null, startDateTime.AddSeconds(75)),
                    new MethodExit(3, "ThirdMethod", 100, false, null),
                    new MethodExit(2, "SecondMethod", 178, false, null),
                    new MethodExit(1, "FirstMethod", 200, false, null),
                    new MethodEntry(1, "TopLevelMethod2", null, startDateTime.AddSeconds(99)),
                    new MethodExit(1, "TopLevelMethod2", 488, true, "whiteSheep"),
                };

            var actualText = new List<String>();

            // Act
            foreach (var entry in entries)
            {
                if (entry is MethodEntry)
                    formatter.FormatMethodEntry(threadId, indentLevel++, ++lineNo, (entry as MethodEntry), filters, true);
                else if (entry is MethodExit)
                {
                    var result = formatter.FormatMethodExitForSingleLineOutput(threadId, indentLevel--, lineNo++, (entry as MethodExit), filters, true);
                    if (result != null)
                        actualText.AddRange(result.Select(oo => oo.FormattedLine).ToList());
                }
                else if (entry is LogEvent)
                {
                    var result = formatter.FormatLogEvent(threadId, indentLevel, lineNo++, (entry as LogEvent), filters, true);
                    if (result != null)
                        actualText.Add(result);
                }
                else if (entry is ExceptionTrace)
                {
                    var result = formatter.FormatException(threadId, indentLevel, lineNo++, (entry as ExceptionTrace), filters);
                    if (result != null)
                        actualText.Add(result);
                }
            }

            // Assert
            Console.WriteLine(String.Join(Environment.NewLine, actualText.ToArray()));

            Assert.That(actualText.Count, Is.EqualTo(expectedText.Count), "Expected {0} lines to be returned overall, but have {1}.", expectedText.Count, actualText.Count);
            for (int logLine = 0; logLine < actualText.Count - 1; logLine++)
            {
                Assert.That(actualText[logLine], Is.EqualTo(expectedText[logLine]), "Text does not match on line {0}", logLine);
            }

            //var misMatchedResults = actualText.Where((at, i) => (at != expectedText[i]));
            //Assert.That(misMatchedResults.Count(), Is.EqualTo(0), "{0} of the lines do not match.\nExpected:\n{1}\n\nActual:\n{2}"
            //                                                    , misMatchedResults.Count()
            //                                                    , String.Join(Environment.NewLine, expectedText.ToArray())
            //                                                    , String.Join(Environment.NewLine, actualText.ToArray())
            //            );
        }
        public void FormatThread_ValidLog_NullIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();

            var entry1 = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            var entry2 = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var entry = new ThreadData(22, String.Empty, new BaseEntry[] { entry1, entry2 });

            // Act
            var outputText = formatter.FormatThread(entry, filters);

            // Assert
            Assert.That(outputText, Is.Null, "Did not expect any formatted text back for a delimited values formatter, threadid is in each row.");
        }
        public void FormatMethodExit_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = "Yalf,Method,Yalf.TestMethod,returnVal,22:22:31.678,345,2,1";

            var relatedEntry = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            formatter.FormatMethodEntry(1, 2, 33, relatedEntry, filters, true);

            // Act
            var orderedOutput = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);

            // Assert
            Assert.That(orderedOutput.Count, Is.Not.EqualTo(0), "Expected one string to be returned");
            Assert.That(orderedOutput[0].FormattedLine, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void FormatMethodExit_SuppliedEntryMethodNameDoesNotMatch_ExceptionIsThrown()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");

            // Act, Assert
            var outputText = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);
        }
        public void FormatMethodExit_NoEntryMethodSupplied_ExceptionIsThrown()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");

            var relatedEntry = new MethodEntry(1, "Yalf.DifferentMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            formatter.FormatMethodEntry(1, 2, 33, relatedEntry, filters, true);

            // Act, Assert
            var outputText = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);
        }