Пример #1
0
        public static void XUnitLogger_Constructor_Initializes_Instance()
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();

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

            // Act
            var actual = new XUnitLogger(name, outputHelper, options);

            // Assert
            actual.Filter.ShouldBeSameAs(options.Filter);
            actual.IncludeScopes.ShouldBeTrue();
            actual.Name.ShouldBe(name);

            // Act
            actual = new XUnitLogger(name, outputHelper, null);

            // Assert
            actual.Filter.ShouldNotBeNull();
            actual.Filter(null, LogLevel.None).ShouldBeTrue();
            actual.IncludeScopes.ShouldBeFalse();
            actual.Name.ShouldBe(name);
        }
Пример #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());
        }
Пример #3
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());
        }
Пример #4
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());
        }
Пример #5
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());
        }
Пример #6
0
        public static void XUnitLogger_IsEnabled_Returns_Correct_Result(LogLevel logLevel, bool expected)
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();

            bool CustomFilter(string categoryName, LogLevel level)
            {
                categoryName.ShouldBe(name);
                level.ShouldBe(logLevel);
                return(level > LogLevel.Information);
            }

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

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

            // Act
            bool actual = logger.IsEnabled(logLevel);

            // Assert
            actual.ShouldBe(expected);
        }
Пример #7
0
        public static void XUnitLogger_BeginScope_Throws_If_State_Is_Null()
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();
            var    options      = new XUnitLoggerOptions();
            var    logger       = new XUnitLogger(name, outputHelper, options);

            // Act
            Assert.Throws <ArgumentNullException>("state", () => logger.BeginScope(null as string));
        }
Пример #8
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));
        }
Пример #9
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));
        }
Пример #10
0
        public static void XUnitLogger_BeginScope_Returns_Value()
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();
            var    options      = new XUnitLoggerOptions();
            var    logger       = new XUnitLogger(name, outputHelper, options);

            // Act
            using IDisposable actual = logger.BeginScope(true);

            // Assert
            actual.ShouldNotBeNull();
        }
Пример #11
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);
        }
Пример #12
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());
        }
Пример #13
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());
        }
Пример #14
0
        public static void XUnitLogger_Validates_Parameters()
        {
            // Arrange
            string name         = "MyName";
            var    outputHelper = Mock.Of <ITestOutputHelper>();

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

            // Act and Assert
            Assert.Throws <ArgumentNullException>("name", () => new XUnitLogger(null, outputHelper, options));
            Assert.Throws <ArgumentNullException>("outputHelper", () => new XUnitLogger(name, null as ITestOutputHelper, options));
            Assert.Throws <ArgumentNullException>("accessor", () => new XUnitLogger(name, null as ITestOutputHelperAccessor, options));

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

            // Act and Assert
            Assert.Throws <ArgumentNullException>("value", () => logger.Filter = null);
        }