Skip to content

Commit

Permalink
style: discard expression value (#70)
Browse files Browse the repository at this point in the history
* use expression-body method
* discard expression value
  • Loading branch information
nogic1008 authored Feb 10, 2022
1 parent 19f9551 commit ae797fd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
8 changes: 3 additions & 5 deletions sandbox/Benchmark/BenchmarkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ namespace Benchmark;
public class BenchmarkConfig : ManualConfig
{
public BenchmarkConfig()
{
AddJob(Job.Default.WithRuntime(CoreRuntime.Core31))
=> AddJob(Job.Default.WithRuntime(CoreRuntime.Core31))
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core60))
.WithOptions(ConfigOptions.DisableOptimizationsValidator);
AddJob(Job.Default.WithRuntime(CoreRuntime.Core60))
.WithOptions(ConfigOptions.DisableOptimizationsValidator);
}
}
22 changes: 10 additions & 12 deletions src/Nogic.WritableOptions/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ public static void ConfigureWritable<TOptions>(
this IServiceCollection services,
IConfigurationSection section,
string file = "appsettings.json") where TOptions : class, new()
{
services.Configure<TOptions>(section);
services.AddTransient<IWritableOptions<TOptions>>(provider =>
{
var environment = provider.GetService<IHostEnvironment>();
string jsonFilePath = environment?.ContentRootFileProvider.GetFileInfo(file).PhysicalPath
?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
=> services.Configure<TOptions>(section)
.AddTransient<IWritableOptions<TOptions>>(provider =>
{
var environment = provider.GetService<IHostEnvironment>();
string jsonFilePath = environment?.ContentRootFileProvider.GetFileInfo(file).PhysicalPath
?? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);

var configuration = provider.GetService<IConfigurationRoot>();
var options = provider.GetRequiredService<IOptionsMonitor<TOptions>>();
return new JsonWritableOptions<TOptions>(jsonFilePath, section.Key, options, configuration);
});
}
var configuration = provider.GetService<IConfigurationRoot>();
var options = provider.GetRequiredService<IOptionsMonitor<TOptions>>();
return new JsonWritableOptions<TOptions>(jsonFilePath, section.Key, options, configuration);
});
}
18 changes: 9 additions & 9 deletions test/Nogic.WritableOptions.Tests/JsonWritableOptions.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public void Value_Returns_T()
// Arrange
var sampleOption = GenerateOption();
var optionsMock = new Mock<IOptionsMonitor<SampleOption>>();
optionsMock.SetupGet(m => m.CurrentValue).Returns(sampleOption);
_ = optionsMock.SetupGet(m => m.CurrentValue).Returns(sampleOption);

var sut = new JsonWritableOptions<SampleOption>(null!, null!, optionsMock.Object, null);

// Act - Assert
sut.Value.Should().Be(sampleOption);
_ = sut.Value.Should().Be(sampleOption);
optionsMock.VerifyGet(m => m.CurrentValue, Times.Once());
}

Expand All @@ -48,13 +48,13 @@ public void Get_Returns_T()
// Arrange
var sampleOption = GenerateOption();
var optionsMock = new Mock<IOptionsMonitor<SampleOption>>();
optionsMock.Setup(m => m.Get(It.IsAny<string>())).Returns(sampleOption);
_ = optionsMock.Setup(m => m.Get(It.IsAny<string>())).Returns(sampleOption);

var sut = new JsonWritableOptions<SampleOption>(null!, null!, optionsMock.Object, null);

// Act - Assert
sut.Get("Foo").Should().Be(sampleOption);
sut.Get("Bar").Should().Be(sampleOption);
_ = sut.Get("Foo").Should().Be(sampleOption);
_ = sut.Get("Bar").Should().Be(sampleOption);

optionsMock.Verify(m => m.Get(It.IsAny<string>()), Times.Exactly(2));
optionsMock.Verify(m => m.Get("Foo"), Times.Once());
Expand Down Expand Up @@ -89,7 +89,7 @@ public void Update_Writes_Json(string fileText)

// Assert
string newLine = Environment.NewLine;
tempFile.ReadAllText().Should().Be("{" + newLine
_ = tempFile.ReadAllText().Should().Be("{" + newLine
+ " \"" + nameof(SampleOption) + "\": {" + newLine
+ " \"LastLaunchedAt\": \"2020-12-01T00:00:00\"," + newLine
+ " \"Interval\": 5000," + newLine
Expand Down Expand Up @@ -128,7 +128,7 @@ public void Update_Writes_Json_WithBOM(string fileText)

// Assert
string newLine = Environment.NewLine;
tempFile.ReadAllText(Encoding.UTF8).Should().Be("{" + newLine
_ = tempFile.ReadAllText(Encoding.UTF8).Should().Be("{" + newLine
+ " \"" + nameof(SampleOption) + "\": {" + newLine
+ " \"LastLaunchedAt\": \"2020-12-01T00:00:00\"," + newLine
+ " \"Interval\": 5000," + newLine
Expand Down Expand Up @@ -165,7 +165,7 @@ public void Update_DoesNot_Changes_Other_Section(string fileText)

// Assert
string newLine = Environment.NewLine;
tempFile.ReadAllText().Should().Contain("\"fooOption\": {},");
_ = tempFile.ReadAllText().Should().Contain("\"fooOption\": {},");
}

/// <summary>
Expand All @@ -192,7 +192,7 @@ public void Update_Calls_Reload()

// Assert
string newLine = Environment.NewLine;
tempFile.ReadAllText().Should().Be("{" + newLine
_ = tempFile.ReadAllText().Should().Be("{" + newLine
+ " \"" + nameof(SampleOption) + "\": {" + newLine
+ " \"LastLaunchedAt\": \"2020-12-01T00:00:00\"," + newLine
+ " \"Interval\": 5000," + newLine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void ConfigureWritable_Calls_AddTransient()
var option = provider.GetService<IWritableOptions<SampleOption>>();
var otherOption = provider.GetService<IWritableOptions<SampleOption>>();

option.Should().NotBeNull()
_ = option.Should().NotBeNull()
.And.BeOfType<JsonWritableOptions<SampleOption>>()
.And.NotBe(otherOption);
}
Expand Down

0 comments on commit ae797fd

Please sign in to comment.