Skip to content

Commit

Permalink
Implemented ConsoleWriter and unit tests for it #17
Browse files Browse the repository at this point in the history
  • Loading branch information
jackschonherr committed Aug 19, 2024
1 parent d0ac729 commit bb16055
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 17 deletions.
65 changes: 65 additions & 0 deletions Consequences/Results/ConsoleWriter.cs
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");
}
}
5 changes: 5 additions & 0 deletions Consequences/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public Result(ResultItem[] resultItems)
_resultItems = resultItems;
}

public ResultItem[] GetResultItems()
{
return _resultItems;
}

// retrieve a ResultItem from a Result by name
public ResultItem Fetch(string name)
{
Expand Down
53 changes: 53 additions & 0 deletions ConsequencesTest/ConsoleWriterTest.cs
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));
}
}
17 changes: 0 additions & 17 deletions ConsequencesTest/UnitTest1.cs

This file was deleted.

0 comments on commit bb16055

Please sign in to comment.