-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented ConsoleWriter and unit tests for it #17
- Loading branch information
1 parent
d0ac729
commit bb16055
Showing
4 changed files
with
123 additions
and
17 deletions.
There are no files selected for viewing
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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace USACE.HEC.Results; | ||
public class ConsoleWriter : IResultsWriter | ||
{ | ||
private bool hasHeaderWritten = false; | ||
private List<string> headers = new List<string>(); | ||
|
||
// check to make sure the result being written matches the headers already written | ||
private void CheckIfSameHeaders(Result res) | ||
{ | ||
// different number of headers | ||
if (res.GetResultItems().Length != headers.Count) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
for (int i = 0; i < headers.Count; i++) | ||
{ | ||
// headers do not match | ||
if (res.GetResultItems()[i].ResultName != headers[i]) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} | ||
public void Write(Result res) | ||
{ | ||
if (!hasHeaderWritten) | ||
{ | ||
// write the headers to the top of the file | ||
for (int i = 0; i < res.GetResultItems().Length; i++) | ||
{ | ||
Console.Write(res.GetResultItems()[i].ResultName); | ||
if (i < res.GetResultItems().Length - 1) | ||
{ | ||
Console.Write(','); | ||
} | ||
headers.Add(res.GetResultItems()[i].ResultName); | ||
} | ||
Console.WriteLine(); | ||
hasHeaderWritten = true; | ||
} | ||
CheckIfSameHeaders(res); | ||
foreach (string header in headers) | ||
{ | ||
object val = res.Fetch(header).Result; | ||
Console.Write(val); | ||
if (header != headers.Last()) | ||
{ | ||
Console.Write(','); | ||
} | ||
} | ||
Console.WriteLine(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Console.WriteLine("END OF 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
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,53 @@ | ||
using System.Diagnostics; | ||
using System.Security.Cryptography; | ||
using USACE.HEC.Hazards; | ||
using USACE.HEC.Results; | ||
using Xunit.Abstractions; | ||
namespace ConsequencesTest; | ||
public class ConsoleWrite | ||
{ | ||
static ResultItem r1 = new ResultItem { ResultName = "Depth", Result = 1.03f }; | ||
static ResultItem r2 = new ResultItem { ResultName = "Velocity", Result = 2.02f }; | ||
static ResultItem r3 = new ResultItem { ResultName = "ArrivalTime2ft", Result = new DateTime() }; | ||
|
||
static ResultItem[] resultItems = { r1, r2, r3 }; | ||
Result res = new Result(resultItems); | ||
[Fact] | ||
public void TestHeaders() | ||
{ | ||
var stringWriter = new StringWriter(); | ||
string headers = "Depth,Velocity,ArrivalTime2ft\r\n"; | ||
string row1 = "1.03,2.02,1/1/0001 12:00:00 AM\r\n"; | ||
string eof = "END OF FILE\r\n"; | ||
|
||
Console.SetOut(stringWriter); | ||
using (IResultsWriter cw = new ConsoleWriter()) | ||
{ | ||
// check empty console at first | ||
Assert.Equal("", stringWriter.ToString()); | ||
cw.Write(res); | ||
// check for header and row1 | ||
Assert.Equal(headers + row1, stringWriter.ToString()); | ||
cw.Write(res); | ||
// check for header and then two row1s, and header only written once | ||
Assert.Equal(headers + row1 + row1, stringWriter.ToString()); | ||
} | ||
Console.SetOut(Console.Out); | ||
// check for end of file | ||
Assert.Equal(headers + row1 + row1 + eof, stringWriter.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void TestInvalidResult() | ||
{ | ||
// Result with headers that do not match res | ||
ResultItem[] bad = { r1, r1, r3 }; | ||
Result invalidResult = new Result(bad); | ||
|
||
IResultsWriter cw = new ConsoleWriter(); | ||
cw.Write(res); | ||
|
||
// throw exception when adding a row with differing headers | ||
Assert.Throws<InvalidOperationException>(() => cw.Write(invalidResult)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.