From 8f056a6624a5d325a81aa6e98b831d5c95f2171c Mon Sep 17 00:00:00 2001 From: Dennis Dyall Date: Mon, 12 Aug 2024 17:42:31 +0200 Subject: [PATCH] Add tests --- .../tests/Yubico/Core/Logging/LogTests.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Yubico.Core/tests/Yubico/Core/Logging/LogTests.cs diff --git a/Yubico.Core/tests/Yubico/Core/Logging/LogTests.cs b/Yubico.Core/tests/Yubico/Core/Logging/LogTests.cs new file mode 100644 index 00000000..138c3ce9 --- /dev/null +++ b/Yubico.Core/tests/Yubico/Core/Logging/LogTests.cs @@ -0,0 +1,68 @@ +// Copyright 2024 Yubico AB +// +// Licensed under the Apache License, Version 2.0 (the "License"). +// You may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +using Microsoft.Extensions.Logging; +using Moq; +using Xunit; + +namespace Yubico.Core.Logging +{ + public class LogTests + { + // Ensure that the default LoggerFactory is created when no configuration is provided. + [Fact] + public void DefaultLoggerFactory_IsCreated_WhenNoConfigurationProvided() + { + // Act + ILoggerFactory loggerFactory = Log.Instance; + + // Assert + Assert.NotNull(loggerFactory); + ILogger logger = loggerFactory.CreateLogger(); + Assert.NotNull(logger); + } + + // Ensure that LoggerFactory can be replaced manually using the Instance property. + [Fact] + public void ManualLoggerFactory_SettingInstance_OverridesDefaultFactory() + { + // Arrange + var mockLoggerFactory = new Mock(); + Log.Instance = mockLoggerFactory.Object; + + // Act + ILoggerFactory actualFactory = Log.Instance; + + // Assert + Assert.Same(mockLoggerFactory.Object, actualFactory); + } + + // Ensure that LoggerFactory can be replaced manually using the Instance property. + // Remove this once we remove Log.Legacy.cs + [Fact] + public void Legacy_ManualLoggerFactory_SettingInstance_OverridesDefaultFactory() + { + // Arrange + var mockLoggerFactory = new Mock(); +#pragma warning disable CS0618 // Type or member is obsolete + Log.LoggerFactory = mockLoggerFactory.Object; +#pragma warning restore CS0618 // Type or member is obsolete + + // Act + ILoggerFactory actualFactory = Log.Instance; + + // Assert + Assert.Same(mockLoggerFactory.Object, actualFactory); + } + } +}