Skip to content

Commit

Permalink
fix(cfg): refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asvol committed Nov 5, 2024
1 parent 6afbe0d commit 9f6126c
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 65 deletions.
1 change: 1 addition & 0 deletions src/Asv.Cfg.Test/Asv.Cfg.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DeepEqual" Version="4.2.1" />
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="$(SystemIOAbstractionsVersion)" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
4 changes: 4 additions & 0 deletions src/Asv.Cfg.Test/Asv.Cfg.Test.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=immemory/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=inmemory/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=json/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Asv.Cfg.Test;



public abstract class ConfigurationTestBase<T>
public abstract class ConfigurationBaseTest<T>
where T:IConfiguration
{
protected abstract IDisposable CreateForTest(out T configuration);
Expand Down Expand Up @@ -65,7 +65,7 @@ public void Check_Available_Parts_Value()
Thread.Sleep(50);

var expectedResult = new string[] { "test1", "test2", "test3", "test4" };
var actualResult = cfg.AvailableParts.OrderBy(_=>_).ToArray(); // items can be reordered in any way, so we need to sort them
var actualResult = cfg.AvailableParts.OrderBy(x=>x).ToArray(); // items can be reordered in any way, so we need to sort them

Assert.Equal(expectedResult, actualResult);
}
Expand Down
20 changes: 20 additions & 0 deletions src/Asv.Cfg.Test/InMemory/InMemoryConfigurationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Reactive.Disposables;
using JetBrains.Annotations;
using Xunit.Abstractions;

namespace Asv.Cfg.Test
{
[TestSubject(typeof(InMemoryConfiguration))]
public class InMemoryConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<InMemoryConfiguration>
{
private readonly ITestOutputHelper _log = log;

protected override IDisposable CreateForTest(out InMemoryConfiguration configuration)
{
configuration = new InMemoryConfiguration(new TestLogger(_log,TimeProvider.System, "IM_MEMORY"));
var cfg = configuration;
return Disposable.Create(() => {cfg.Dispose(); });
}
}
}
23 changes: 0 additions & 23 deletions src/Asv.Cfg.Test/InMemoryTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Reactive.Disposables;
using System.Threading;
using Asv.Cfg.Json;
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -56,16 +56,11 @@ public class TestMultiThreadClassFour
}
#endregion

public class JsonConfigurationTests:ConfigurationTestBase<JsonConfiguration>

[TestSubject(typeof(JsonConfiguration))]
public class JsonConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<JsonConfiguration>
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly IFileSystem _fileSystem;

public JsonConfigurationTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_fileSystem = new MockFileSystem();
}
private readonly IFileSystem _fileSystem = new MockFileSystem();

protected override IDisposable CreateForTest(out JsonConfiguration configuration)
{
Expand All @@ -80,8 +75,8 @@ protected override IDisposable CreateForTest(out JsonConfiguration configuration
_fileSystem.Directory.Delete(workingDir);
}

_testOutputHelper.WriteLine($"Working directory: {workingDir}");
configuration = new JsonConfiguration(workingDir, fileSystem: _fileSystem);
log.WriteLine($"Working directory: {workingDir}");
configuration = new JsonConfiguration(workingDir, logger:new TestLogger(log,TimeProvider.System, "JSON"), fileSystem: _fileSystem);
var cfg = configuration;
return Disposable.Create(() =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Reactive.Disposables;
using Asv.Cfg.Json;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Xunit;
using Xunit.Abstractions;

namespace Asv.Cfg.Test
{
public class JsonOneFileConfigurationTests : ConfigurationTestBase<JsonOneFileConfiguration>
[TestSubject(typeof(JsonOneFileConfiguration))]
public class JsonOneFileConfigurationTest(ITestOutputHelper log) : ConfigurationBaseTest<JsonOneFileConfiguration>
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly IFileSystem _fileSystem;

public JsonOneFileConfigurationTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_fileSystem = new MockFileSystem();
}
private readonly IFileSystem _fileSystem = new MockFileSystem();

protected override IDisposable CreateForTest(out JsonOneFileConfiguration configuration)
{
Expand All @@ -30,6 +24,7 @@ protected override IDisposable CreateForTest(out JsonOneFileConfiguration config
filePath,
true,
null,
logger:new TestLogger(log,TimeProvider.System, "JSON_ONE_FILE"),
fileSystem: _fileSystem
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Reactive.Disposables;
using Asv.Cfg.Json;
using JetBrains.Annotations;
using Xunit;
using Xunit.Abstractions;

namespace Asv.Cfg.Test
{
public class ZipJsonConfigurationTests: ConfigurationTestBase<ZipJsonConfiguration>
[TestSubject(typeof(ZipJsonConfiguration))]
public class ZipJsonConfigurationTest(ITestOutputHelper log)
: ConfigurationBaseTest<ZipJsonConfiguration>
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly IFileSystem _fileSystem;

public ZipJsonConfigurationTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_fileSystem = new MockFileSystem();
}
private readonly IFileSystem _fileSystem = new MockFileSystem();

[Fact]
public void Configuration_Should_Throw_Argument_Exception_If_Null()
Expand All @@ -38,7 +33,7 @@ protected override IDisposable CreateForTest(out ZipJsonConfiguration configurat
_fileSystem.Directory.CreateDirectory(dir ?? throw new InvalidOperationException());
}
var file = _fileSystem.File.Open(filePath, FileMode.OpenOrCreate);
configuration = new ZipJsonConfiguration(file, true, null, fileSystem: _fileSystem);
configuration = new ZipJsonConfiguration(file, true, new TestLogger(log,TimeProvider.System, "ZIP_JSON"), fileSystem: _fileSystem);
var cfg = configuration;
return Disposable.Create(() =>
{
Expand Down
19 changes: 19 additions & 0 deletions src/Asv.Cfg.Test/Json/ZipJsonVersionedFileTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using Asv.Cfg;
using JetBrains.Annotations;
using Xunit;

namespace Asv.Cfg.Test;

[TestSubject(typeof(ZipJsonVersionedFile))]
public class ZipJsonVersionedFileTest:ConfigurationBaseTest<ZipJsonVersionedFile>
{


protected override IDisposable CreateForTest(out ZipJsonVersionedFile configuration)
{
throw new NotImplementedException();
}
}
61 changes: 61 additions & 0 deletions src/Asv.Cfg.Test/LoggerWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Reactive.Disposables;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace Asv.Cfg.Test;

public class TestLoggerFactory(ITestOutputHelper testOutputHelper, TimeProvider time, string prefix) : ILoggerFactory
{
public TimeProvider Time { get; } = time;
private readonly string _prefix = prefix;

public void Dispose()
{

}

public ILogger CreateLogger(string categoryName)
{
return new TestLogger(testOutputHelper,Time, $"{prefix}.{categoryName}");
}

public void AddProvider(ILoggerProvider provider)
{

}
}

public class TestLogger(ITestOutputHelper testOutputHelper, TimeProvider time, string? categoryName) : ILogger
{
public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

public IDisposable BeginScope<TState>(TState state) where TState : notnull => Disposable.Empty;

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
try
{
testOutputHelper.WriteLine($"{time.GetLocalNow().DateTime:HH:mm:ss.fff,15} |={ConvertToStr(logLevel)}=| {categoryName,-8} | {formatter(state, exception)}");
}
catch
{
// This can happen when the test is not active
}
}

private string ConvertToStr(LogLevel logLevel)
{
return logLevel switch
{
LogLevel.Trace => "TRC",
LogLevel.Debug => "DBG",
LogLevel.Information => "INF",
LogLevel.Warning => "WRN",
LogLevel.Error => "ERR",
LogLevel.Critical => "CRT",
LogLevel.None => "NON",
_ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null)
};
}
}
3 changes: 2 additions & 1 deletion src/Asv.Cfg/Asv.Cfg.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=inmemory/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=inmemory/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=json/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion src/Asv.Cfg/Json/JsonConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Newtonsoft.Json;
using ZLogger;

namespace Asv.Cfg.Json
namespace Asv.Cfg
{
public class JsonConfiguration:IConfiguration
{
Expand Down
2 changes: 1 addition & 1 deletion src/Asv.Cfg/Json/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace Asv.Cfg.Json;
namespace Asv.Cfg;

public static class JsonHelper
{
Expand Down
4 changes: 2 additions & 2 deletions src/Asv.Cfg/Json/JsonOneFileConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
using Asv.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZLogger;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using ZLogger;

namespace Asv.Cfg.Json
namespace Asv.Cfg
{

public class JsonOneFileConfiguration : DisposableOnce, IConfiguration
Expand Down
2 changes: 1 addition & 1 deletion src/Asv.Cfg/Json/ZipJsonConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Newtonsoft.Json;
using ZLogger;

namespace Asv.Cfg.Json
namespace Asv.Cfg
{
public class ZipJsonConfiguration:DisposableOnce, IConfiguration
{
Expand Down
2 changes: 1 addition & 1 deletion src/Asv.Cfg/Json/ZipJsonVersionedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Asv.Common;
using Newtonsoft.Json;

namespace Asv.Cfg.Json
namespace Asv.Cfg
{
[method: JsonConstructor]
public readonly struct ZipJsonFileInfo(string fileVersion, string fileType) : IEquatable<ZipJsonFileInfo>
Expand Down
1 change: 1 addition & 0 deletions src/Asv.Common.Test/Asv.Common.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="8.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
Loading

0 comments on commit 9f6126c

Please sign in to comment.