Last active
July 14, 2023 16:25
-
-
Save dev-jonghoonpark/50148202c34029d912874b5c6b541ed6 to your computer and use it in GitHub Desktop.
단위 테스트 - 6장 단위 테스트 스타일
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
public class AuditManager | |
{ | |
private readonly int _maxEntriesPerFile; | |
private readonly string _directoryName; | |
public AuditManager(int maxEntriesPerFile, string directoryName) | |
{ | |
_maxEntriesPerFile = maxEntriesPerFile; | |
_directoryName = directoryName; | |
} | |
public void AddRecord(string visitoirName, DataTime timeOfVisit) | |
{ | |
string[] filePaths = Directory.GetFiles(_directoryName); | |
(int index, string path)[] sorted = SortByIndex(filePaths); | |
string newRecord = visitorNAme + ‘;’ + timeOfVisit; | |
if (sorted.Length == 0) | |
{ | |
string newFile = Path.Combine(_directoryName, “audit_1.txt”) | |
File.WriteAllText(newFile, newRecord); | |
return; | |
} | |
(int currentFileIndex, string currnetFilePath) = sorted.Last(); | |
List<string> lines = File.ReadAllLines(currentFilePath).ToList(); | |
if (lines.Count < _maxEntriesPerFile) | |
{ | |
lines.Add(newRecord); | |
string newContent = string.Join("\r\n", lines); | |
File.WriteAllTExt(currentFilePAth, newContent); | |
} | |
else | |
{ | |
int newIndex = currentFileIndex + 1; | |
string newName = $"audit_{newIndex}.txt"; | |
string newFile = Path.Combine(_directoryName, newName); | |
File.WriteAllText(newFile, newRecord); | |
} | |
} | |
} |
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
public class AuditManager | |
{ | |
private readonly int _maxEntriesPerFile; | |
private readonly string _directoryName; | |
private readonly IFileSystem _fileSystem; | |
public AuditManager( | |
int maxEntriesPerFile, | |
string directoryName, | |
IfFileSystem fileSystem) | |
{ | |
_maxEntriesPerFile = maxEntriesPerFile; | |
_directoryName = directoryName; | |
_fileSystem = fileSystem; | |
} | |
} |
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
public void AddRecord(string visitoirName, DataTime timeOfVisit) | |
{ | |
string[] filePaths = _fileSystem.GetFiles(_directoryName); | |
(int index, string path)[] sorted = SortByIndex(filePaths); | |
string newRecord = visitorNAme + ‘;’ + timeOfVisit; | |
if (sorted.Length == 0) | |
{ | |
string newFile = Path.Combine(_directoryName, “audit_1.txt”) | |
_fileSystem.WriteAllText(newFile, newRecord); | |
return; | |
} | |
(int currentFileIndex, string currnetFilePath) = sorted.Last(); | |
List<string> lines = _fileSystem.ReadAllLines(currentFilePath).ToList(); | |
if (lines.Count < _maxEntriesPerFile) | |
{ | |
lines.Add(newRecord); | |
string newContent = string.Join("\r\n", lines); | |
_fileSystem.WriteAllTExt(currentFilePAth, newContent); | |
} | |
else | |
{ | |
int newIndex = currentFileIndex + 1; | |
string newName = $"audit_{newIndex}.txt"; | |
string newFile = Path.Combine(_directoryName, newName); | |
_fileSystem.WriteAllText(newFile, newRecord); | |
} | |
} |
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
[Fact] | |
public void A_new_file_is_created_when_the_current_file_overflows() | |
{ | |
var fileSystemMock = new Mock<IFileSystem>(); | |
fileSystemMock | |
.Setup(x => x.GetFiles("audits")) | |
.Returns(new String[] | |
{ | |
@"audits\audit_1.txt", | |
@"audits\audit_2.txt" | |
}); | |
fileSystemMock | |
.Setup(x => x.ReadAllLines(@"audits\audit_2.txt")) | |
.Returns(new List<string> | |
{ | |
"Peter; 2019-04-06T16:30:00", | |
"Jane; 2019-04-06T16:40:00", | |
"Jack; 2019-04-06T17:00:00", | |
}); | |
var sut = new AuditManager(3, "audits", fileSystemMock.Object); | |
sut.AddRecord("Alice", DateTime.Parse("2019-04-16T18:00:00")); | |
fileSystemMock.Verify(x => x.WriteAllText( | |
@"audits\audit_3.txt", | |
"Alice;2019-04-06T18:00:00")); | |
} |
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
public class FileContent | |
{ | |
public readonly string FileName; | |
publllic readonly string[] Lines; | |
publlic FileContent(string fileName, string[] lines) | |
{ | |
FileName = fileName; | |
Lines = lines; | |
} | |
} |
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
public class FileContent | |
{ | |
public readonly string FileName; | |
publllic readonly string NewContent; | |
publlic FileContent(string fileName, string newContent) | |
{ | |
FileName = fileName; | |
NewContent = newContent; | |
} | |
} |
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
public class Persister | |
{ | |
public FileContent[] ReadDirectory(string direcctoryName) | |
{ | |
return Directory | |
.GetFiles(directoryName) | |
.Select(x => new FileContent( | |
Path.GetFileName(x)), | |
File.ReadAllLines()) | |
.ToArray(); | |
} | |
public void ApplyUpdate(string directoryName, FileUpdate updadte) | |
{ | |
string filePath = Path.Combine(directoryName, update.FileName); | |
File.WriteAllText(filePath, update.NewContent); | |
} | |
} |
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
public class AuditManager | |
{ | |
private readonly int _maxEntriesPerFile; | |
public AuditManager(int maxEntriesPerFile) | |
{ | |
_maxEntriesPerFile = maxEntriesPerFile; | |
} | |
public FileUpdate AddRecord( | |
FileContent[] files, | |
string visitorName, | |
DateTime tiemOfVisit) | |
{ | |
(int index, FileContent file)[] sorted = SortByIndex(files); | |
string newRecord = visitorNAme + ‘;’ + timeOfVisit; | |
if (sorted.Length == 0) | |
{ | |
return new FileUpdate( | |
"audit_1.txt", newRecord); | |
} | |
(int currentFileIndex, FileContent currentFile) = sorted.Last(); | |
List<string> lines = currentFile.Lines.ToList(); | |
if (lines.Count < _maxEntriesPerFile) | |
{ | |
lines.Add(newRecord); | |
string newContent = string.Join("\r\n", lines); | |
return new FileUpdate( | |
currentFile.FileName, newContent); | |
} | |
else | |
{ | |
int newIndex = currentFileIndex + 1; | |
string newName = $"audit_{newIndex}.txt"; | |
return new FileUpdate( | |
newName, newRecord); | |
} | |
} | |
} |
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
public class ApplicationService | |
{ | |
private readonly string _directoryName; | |
private readonly AuditManager _auditManager; | |
private readonly Persister _persister; | |
public ApplicationService( | |
string directoryName, int maxEntriesPerFile) | |
{ | |
_directoryName = directoryName; | |
_auditManager = new AuditManager(maxEntiresPerFile); | |
_persister = new Persister(); | |
} | |
public void AddRecord(string visitorName, DateTime timeOfVisit) | |
{ | |
FileContent[] files = _persister.ReadDirectory(_directoryName); | |
FileUpdate update = _auditManager.AddRecord( | |
files, visitorName, timeOfVisit); | |
_persister.ApplyUpdate(_directoryNAme, update); | |
} | |
} |
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
[Fact] | |
public void A_new_file_is_created_when_the_current_file_overflows() | |
{ | |
var sut = new AuditManager(3); | |
var files = new FileContent[] | |
{ | |
new FileContent("audit_1".txt, new String[0]), | |
new FileContent("audit_2".txt, new String[] | |
{ | |
"Peter; 2019-04-06T16:30:00", | |
"Jane; 2019-04-06T16:40:00", | |
"Jack; 2019-04-06T17:00:00" | |
}) | |
}; | |
FileUpdate update = sut.AddRecord( | |
files, "Alice", DateTime.parse("2019-04-06T18:00:00")); | |
Assert.Equal("audit_3.txt", update.FileName); | |
Assert.Equal("Alice;2019-04-06T18:00:00", update.NewContent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment