Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisDyallo committed Aug 12, 2024
1 parent 72f4eaf commit 8f056a6
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Yubico.Core/tests/Yubico/Core/Logging/LogTests.cs
Original file line number Diff line number Diff line change
@@ -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<LogTests>();
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<ILoggerFactory>();
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<ILoggerFactory>();
#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);
}
}
}

0 comments on commit 8f056a6

Please sign in to comment.