Exemplo n.º 1
0
        public static void XUnitLogger_Log_Logs_Message_If_Message_And_No_Exception()
        {
            // Arrange
            var mock = new Mock <ITestOutputHelper>();

            string name         = "MyName";
            var    outputHelper = mock.Object;

            var options = new XUnitLoggerOptions()
            {
                Filter = FilterTrue,
            };

            var logger = new XUnitLogger(name, outputHelper, options)
            {
                Clock = StaticClock,
            };

            string expected = string.Join(
                Environment.NewLine,
                new[] { "[2018-08-19 16:12:16Z] fail: MyName[4]", "      Message|False|False" });

            // Act
            logger.Log <string>(LogLevel.Error, new EventId(4), null, null, Formatter);

            // Assert
            mock.Verify((p) => p.WriteLine(expected), Times.Once());
        }
Exemplo n.º 2
0
        public static void XUnitLogger_Log_Logs_Message_If_Scopes_Included_But_There_Are_No_Scopes()
        {
            // Arrange
            var mock = new Mock <ITestOutputHelper>();

            string name         = "MyName";
            var    outputHelper = mock.Object;

            var options = new XUnitLoggerOptions()
            {
                Filter        = FilterTrue,
                IncludeScopes = true,
            };

            var logger = new XUnitLogger(name, outputHelper, options)
            {
                Clock = StaticClock,
            };

            string expected = string.Join(
                Environment.NewLine,
                new[] { "[2018-08-19 16:12:16Z] info: MyName[0]", "      Message|False|False" });

            // Act
            logger.Log <string>(LogLevel.Information, 0, null, null, Formatter);

            // Assert
            mock.Verify((p) => p.WriteLine(expected), Times.Once());
        }
Exemplo n.º 3
0
        public static void XUnitLogger_Log_Logs_Message_If_Message_And_Exception()
        {
            // Arrange
            var mock = new Mock <ITestOutputHelper>();

            string name         = "MyName";
            var    outputHelper = mock.Object;

            var options = new XUnitLoggerOptions()
            {
                Filter = FilterTrue,
            };

            var logger = new XUnitLogger(name, outputHelper, options)
            {
                Clock = StaticClock,
            };

            var exception = new InvalidOperationException("Invalid");

            string expected = string.Join(
                Environment.NewLine,
                new[] { "[2018-08-19 16:12:16Z] warn: MyName[3]", "      Message|False|True", "System.InvalidOperationException: Invalid" });

            // Act
            logger.Log <string>(LogLevel.Warning, new EventId(3), null, exception, Formatter);

            // Assert
            mock.Verify((p) => p.WriteLine(expected), Times.Once());
        }
Exemplo n.º 4
0
        public static void XUnitLogger_Log_Logs_Messages(LogLevel logLevel, string shortLevel)
        {
            // Arrange
            var mock = new Mock <ITestOutputHelper>();

            string name         = "Your Name";
            var    outputHelper = mock.Object;

            var options = new XUnitLoggerOptions()
            {
                Filter = FilterTrue,
            };

            var logger = new XUnitLogger(name, outputHelper, options)
            {
                Clock = StaticClock,
            };

            string expected = string.Join(
                Environment.NewLine,
                new[] { $"[2018-08-19 16:12:16Z] {shortLevel}: Your Name[85]", "      Message|True|False" });

            // Act
            logger.Log(logLevel, new EventId(85), "Martin", null, Formatter);

            // Assert
            mock.Verify((p) => p.WriteLine(expected), Times.Once());
        }
Exemplo n.º 5
0
        public static void XUnitLogger_Log_Throws_If_LogLevel_Is_Invalid()
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();
            var    options      = new XUnitLoggerOptions();

            var logger = new XUnitLogger(name, outputHelper, options);

            // Act and Assert
            Assert.Throws <ArgumentOutOfRangeException>("logLevel", () => logger.Log((LogLevel)int.MaxValue, 0, "state", null, Formatter));
        }
Exemplo n.º 6
0
        public static void XUnitLogger_Log_Throws_If_Formatter_Is_Null()
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();
            var    options      = new XUnitLoggerOptions();

            var logger = new XUnitLogger(name, outputHelper, options);

            // Act and Assert
            Assert.Throws <ArgumentNullException>("formatter", () => logger.Log(LogLevel.Information, new EventId(2), true, null, null));
        }
Exemplo n.º 7
0
        public static void XUnitLogger_Log_Does_Nothing_If_No_OutputHelper()
        {
            // Arrange
            string name     = "MyName";
            var    accessor = Mock.Of <ITestOutputHelperAccessor>();

            var options = new XUnitLoggerOptions()
            {
                Filter = FilterTrue,
            };

            var logger = new XUnitLogger(name, accessor, options);

            // Act (no Assert)
            logger.Log(LogLevel.Information, new EventId(2), "state", null, Formatter);
        }
Exemplo n.º 8
0
        public static void XUnitLogger_Log_Logs_Very_Long_Messages()
        {
            // Arrange
            var mock = new Mock <ITestOutputHelper>();

            string name         = "MyName";
            var    outputHelper = mock.Object;

            var options = new XUnitLoggerOptions()
            {
                Filter = FilterTrue,
            };

            var logger = new XUnitLogger(name, outputHelper, options);

            // Act
            logger.Log(LogLevel.Information, 1, "state", null, FormatterLong);

            // Assert
            mock.Verify((p) => p.WriteLine(It.Is <string>((r) => r.Length > 1024)), Times.Once());
        }
Exemplo n.º 9
0
        public static void XUnitLogger_Log_Does_Nothing_If_Empty_Message_And_No_Exception()
        {
            // Arrange
            var mock = new Mock <ITestOutputHelper>();

            string name         = "MyName";
            var    outputHelper = mock.Object;

            var options = new XUnitLoggerOptions()
            {
                Filter = FilterTrue,
            };

            var logger = new XUnitLogger(name, outputHelper, options);

            // Act
            logger.Log(LogLevel.Information, new EventId(2), "state", null, FormatterEmpty);

            // Assert
            mock.Verify((p) => p.WriteLine(It.IsAny <string>()), Times.Never());
        }