-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
171 additions
and
14 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
namespace Chickensoft.Log.Tests; | ||
|
||
using System; | ||
using System.IO; | ||
using Shouldly; | ||
|
||
public class FileWriterStreamTester : IDisposable { | ||
private bool _isDisposed; | ||
private readonly MemoryStream _memoryStream; | ||
|
||
public FileWriterStreamTester( | ||
string filename = FileWriter.DEFAULT_FILE_NAME | ||
) { | ||
_memoryStream = new MemoryStream(); | ||
|
||
FileWriter.AppendText = fileName => new StreamWriter( | ||
_memoryStream, | ||
System.Text.Encoding.UTF8, | ||
bufferSize: 1024, | ||
leaveOpen: true | ||
); | ||
} | ||
|
||
public string GetString() { | ||
_memoryStream.Position = 0; | ||
using var reader = new StreamReader(_memoryStream); | ||
var result = reader.ReadToEnd(); | ||
return result; | ||
} | ||
|
||
public void Dispose() { | ||
if (_isDisposed) { return; } | ||
|
||
GC.SuppressFinalize(this); | ||
_isDisposed = true; | ||
|
||
FileWriter.AppendText = FileWriter.AppendTextDefault; | ||
_memoryStream.Dispose(); | ||
} | ||
} | ||
|
||
public class FileWriterTest { | ||
[Fact] | ||
public void DefaultFileName() { | ||
FileWriter.DefaultFileName.ShouldBe(FileWriter.DEFAULT_FILE_NAME); | ||
|
||
var filename = "test.log"; | ||
FileWriter.DefaultFileName = filename; | ||
FileWriter.DefaultFileName.ShouldBe(filename); | ||
|
||
FileWriter.DefaultFileName = FileWriter.DEFAULT_FILE_NAME; | ||
} | ||
|
||
[Fact] | ||
public void DefaultInstance() { | ||
var writer = FileWriter.Instance(); | ||
writer.ShouldNotBeNull(); | ||
writer.ShouldBeOfType<FileWriter>(); | ||
} | ||
|
||
[Fact] | ||
public void NewInstance() { | ||
var filename = "test.log"; | ||
var writer = FileWriter.Instance(filename); | ||
writer.ShouldNotBeNull(); | ||
writer.ShouldBeOfType<FileWriter>(); | ||
} | ||
|
||
[Fact] | ||
public void ReusesInstanceAndRemoves() { | ||
var filename = "test.log"; | ||
var writer1 = FileWriter.Instance(filename); | ||
var writer2 = FileWriter.Instance(filename); | ||
writer1.ShouldBeSameAs(writer2); | ||
|
||
FileWriter.Remove(filename).ShouldBeSameAs(writer1); | ||
FileWriter.Remove(filename).ShouldBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void WriteMessage() { | ||
using var tester = new FileWriterStreamTester(); | ||
|
||
var writer = FileWriter.Instance(); | ||
var value = "test message"; | ||
|
||
writer.WriteMessage(value); | ||
|
||
tester.GetString().ShouldBe(value + Environment.NewLine); | ||
} | ||
|
||
[Fact] | ||
public void WriteWarning() { | ||
using var tester = new FileWriterStreamTester(); | ||
|
||
var writer = FileWriter.Instance(); | ||
var value = "test message"; | ||
|
||
writer.WriteWarning(value); | ||
|
||
tester.GetString().ShouldBe(value + Environment.NewLine); | ||
} | ||
|
||
[Fact] | ||
public void WriteError() { | ||
using var tester = new FileWriterStreamTester(); | ||
|
||
var writer = FileWriter.Instance(); | ||
var value = "test message"; | ||
|
||
writer.WriteError(value); | ||
|
||
tester.GetString().ShouldBe(value + Environment.NewLine); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Chickensoft.Log.Tests")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters