Skip to content

Commit

Permalink
refactor: use primary constructor and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Nov 24, 2024
1 parent 02dffe1 commit 4d0b8fc
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 64 deletions.
9 changes: 2 additions & 7 deletions src/ReferenceResolver/NuGetReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace ReferenceResolver;

public sealed class NuGetReferenceResolver : IReferenceResolver
public sealed class NuGetReferenceResolver(INuGetHelper nugetHelper) : IReferenceResolver
{
private readonly INuGetHelper _nugetHelper;

public NuGetReferenceResolver(INuGetHelper nugetHelper)
{
_nugetHelper = nugetHelper;
}
private readonly INuGetHelper _nugetHelper = nugetHelper;

public ReferenceType ReferenceType => ReferenceType.NuGetPackage;

Expand Down
9 changes: 2 additions & 7 deletions src/ReferenceResolver/ReferenceResolverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@

namespace ReferenceResolver;

public sealed class ReferenceResolverFactory : IReferenceResolverFactory
public sealed class ReferenceResolverFactory(IServiceProvider? serviceProvider) : IReferenceResolverFactory
{
private static readonly char[] ReferenceSchemaSeparator = [':'];

private readonly IServiceProvider _serviceProvider;

public ReferenceResolverFactory(IServiceProvider? serviceProvider)
{
_serviceProvider = serviceProvider ?? DependencyResolver.Current;
}
private readonly IServiceProvider _serviceProvider = serviceProvider ?? DependencyResolver.Current;

public static IReference ParseReference(string referenceWithSchema)
{
Expand Down
9 changes: 2 additions & 7 deletions src/ReferenceResolver/ReferenceSchemaAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
namespace ReferenceResolver;

[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
internal sealed class ReferenceSchemaAttribute : Attribute
internal sealed class ReferenceSchemaAttribute(string schema) : Attribute
{
public string Schema { get; }

public ReferenceSchemaAttribute(string schema)
{
Schema = schema;
}
public string Schema { get; } = schema;
}
6 changes: 2 additions & 4 deletions src/dotnet-exec/Contracts/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public sealed class AppConfiguration
public Dictionary<string, string> Aliases { get; set; } = new()
{
{ "new-guid", "System.Guid.NewGuid()" },
#if NET8_0_OR_GREATER
{ "now", "System.TimeProvider.System.GetLocalNow()" },
{ "date", "System.TimeProvider.System.GetLocalNow()" },
#endif
{ "now", "System.DateTimeOffset.Now" },
{ "date", "System.DateTimeOffset.Now" },
};
}
2 changes: 1 addition & 1 deletion src/dotnet-exec/Services/Repl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task RunAsync(ExecOptions options)
Console.WriteLine("""
REPL started, Enter #q or #exit to exit
#cls or #clear to clear screen
#r to reference dll or package, for example: "#r nuget:CsvHelper", "#r nuget: CsvHelper, 30.0.0"
#r to reference dll or package, for example: "#r nuget:CsvHelper", "#r nuget: CsvHelper, 30.0.0", "#r /a/b/c.dll"
""");
ScriptState? state = null;
while (true)
Expand Down
9 changes: 2 additions & 7 deletions tests/IntegrationTest/ConfigProfileManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@

namespace IntegrationTest;

public class ConfigProfileManagerTests
public class ConfigProfileManagerTests(IConfigProfileManager configProfileManager)
{
private readonly IConfigProfileManager _configProfileManager;

public ConfigProfileManagerTests(IConfigProfileManager configProfileManager)
{
_configProfileManager = configProfileManager;
}
private readonly IConfigProfileManager _configProfileManager = configProfileManager;

[Fact]
public async Task ProfileOperation()
Expand Down
28 changes: 10 additions & 18 deletions tests/IntegrationTest/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,17 @@

namespace IntegrationTest;

public class IntegrationTests
{
private readonly CommandHandler _handler;
private readonly ICompilerFactory _compilerFactory;
private readonly IExecutorFactory _executorFactory;
private readonly ITestOutputHelper _outputHelper;

public IntegrationTests(
CommandHandler handler,
ICompilerFactory compilerFactory,
IExecutorFactory executorFactory,
ITestOutputHelper outputHelper
public class IntegrationTests(
CommandHandler handler,
ICompilerFactory compilerFactory,
IExecutorFactory executorFactory,
ITestOutputHelper outputHelper
)
{
_handler = handler;
_compilerFactory = compilerFactory;
_executorFactory = executorFactory;
_outputHelper = outputHelper;
}
{
private readonly CommandHandler _handler = handler;
private readonly ICompilerFactory _compilerFactory = compilerFactory;
private readonly IExecutorFactory _executorFactory = executorFactory;
private readonly ITestOutputHelper _outputHelper = outputHelper;

[Theory]
[InlineData("ConfigurationManagerSample")]
Expand Down
4 changes: 2 additions & 2 deletions tests/IntegrationTest/NuGetHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task GetLatestVersion()
var packageId = "WeihanLi.Common";
var result = await NuGetHelper.GetLatestPackageVersion(packageId);
Assert.NotNull(result);
Assert.True(result >= new NuGetVersion("1.0.61"));
Assert.True(result >= new NuGetVersion("1.0.72"));
}

[Fact]
Expand All @@ -77,7 +77,7 @@ public async Task GetVersions()
var result = await NuGetHelper.GetPackageVersions(packageId).ToArrayAsync();
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Contains(result, v => v.Version is { Major: 1, Minor: 0, Patch: 61 });
Assert.Contains(result, v => v.Version is { Major: 1, Minor: 0, Patch: 72 });
}

[Fact]
Expand Down
7 changes: 3 additions & 4 deletions tests/IntegrationTest/NuGetReferenceResolverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ namespace IntegrationTest;

public class NuGetReferenceResolverTest
{
private readonly NuGetReferenceResolver _resolver =
new(new NuGetHelper(NullLoggerFactory.Instance));
private readonly NuGetReferenceResolver _resolver = new(new NuGetHelper(NullLoggerFactory.Instance));

[Fact]
public async Task Resolve()
{
var references = await _resolver.Resolve("WeihanLi.Common,1.0.69", ExecOptions.DefaultTargetFramework);
var references = await _resolver.Resolve("WeihanLi.Common,1.0.72", ExecOptions.DefaultTargetFramework);
Assert.NotNull(references);
Assert.NotEmpty(references);
}

[Fact]
public async Task ResolveReference()
{
var reference = new NuGetReference("WeihanLi.Common", "1.0.69");
var reference = new NuGetReference("WeihanLi.Common", "1.0.72");
Assert.Equal($"nuget: {reference.Reference}", reference.ReferenceWithSchema());

var references = await _resolver.Resolve(reference.Reference, ExecOptions.DefaultTargetFramework);
Expand Down
9 changes: 2 additions & 7 deletions tests/UnitTest/CodeExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@

namespace UnitTest;

public sealed class CodeExecutorTest
public sealed class CodeExecutorTest(ITestOutputHelper outputHelper)
{
private readonly ITestOutputHelper _outputHelper;
private readonly ITestOutputHelper _outputHelper = outputHelper;
private readonly SimpleCodeCompiler _compiler = SimpleCodeCompilerTest.GetSimpleCodeCompiler();

public CodeExecutorTest(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}

[Theory]
[InlineData("Console.WriteLine(123);")]
[InlineData("using WeihanLi.Extensions; Console.WriteLine(args.StringJoin(\", \"));")]
Expand Down

0 comments on commit 4d0b8fc

Please sign in to comment.