Implement DocumentWriter, control elements, and tests.

This commit is contained in:
Daniel Kraus
2015-07-25 14:33:48 +02:00
parent 659713abe3
commit 9df937138d
22 changed files with 722 additions and 151 deletions

View File

@ -0,0 +1,99 @@
/* DocumentWriterClass.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using zaaReloaded2.Formatter;
namespace Tests.Formatter
{
[TestFixture]
class DocumentWriterTest
{
DocumentWriter _docWriter;
[SetUp]
public void SetUp()
{
_docWriter = new DocumentWriter();
}
[TearDown]
public void TearDown()
{
}
[Test]
public void AddText()
{
string s = "hello world";
_docWriter.Write(s);
Assert.AreEqual(s, _docWriter.ToString());
}
[Test]
public void AddLine()
{
string s = "hello world";
_docWriter.WriteLine(s);
Assert.AreEqual(s + Environment.NewLine, _docWriter.ToString());
}
[Test]
public void PrependText()
{
string first = "first";
string second = "second";
_docWriter.Write(first);
_docWriter.Prepend(second);
Assert.AreEqual(second + first, _docWriter.ToString());
}
[Test]
public void FlushingThrowsWithoutTarget()
{
_docWriter.Write("asdf");
Assert.Throws<InvalidOperationException>(_docWriter.Flush);
}
[Test]
public void FlushBuffer()
{
DocumentWriter parent = new DocumentWriter();
_docWriter.Parent = parent;
string s = "hello world";
_docWriter.Write(s);
_docWriter.Flush();
Assert.AreEqual(s, parent.ToString());
}
[Test]
public void BufferIsEmptyAfterFlush()
{
DocumentWriter parent = new DocumentWriter();
_docWriter.Parent = parent;
string s = "hello world";
_docWriter.Write(s);
_docWriter.Flush();
Assert.IsFalse(_docWriter.HasBufferedText);
Assert.AreEqual(String.Empty, _docWriter.ToString());
}
}
}

View File

@ -0,0 +1,7 @@

04.07.2015 12:31:00:
Klinische Chemie: Na 144 mM, K 4,3 mM
06.07.2015 10:28:00:
Klinische Chemie: Na 138 mM, K 4,6 mM

View File

@ -0,0 +1,7 @@

04.07.2015:
Klinische Chemie: Na 144 mM, K 4,3 mM
06.07.2015:
Klinische Chemie: Na 138 mM, K 4,6 mM

View File

@ -0,0 +1,4 @@

04.07.2015:
Klinische Chemie: Na 144 mM, K 4,3 mM

View File

@ -0,0 +1,4 @@

06.07.2015:
Klinische Chemie: Na 138 mM, K 4,6 mM

View File

@ -25,23 +25,95 @@ using zaaReloaded2.LabModel;
using zaaReloaded2.Importer.ZaaImporter;
using zaaReloaded2.Controller.Elements;
using Microsoft.Office.Interop.Word;
using System.IO;
namespace Tests.Formatter
{
[TestFixture]
class FormatterTest
{
Document _document;
f.Formatter _formatter;
[SetUp]
public void SetUp()
{
_document = new Document();
ZaaImporter importer = TestHelpers.ZaaImporterFromResource();
_formatter = new f.Formatter(_document);
_formatter.Laboratory = importer.Laboratory;
}
[TearDown]
public void TearDown()
{
((_Document)_document).Close(WdSaveOptions.wdDoNotSaveChanges);
}
[Test]
public void FormatLaboratory()
{
Document document = new Document();
ZaaImporter importer = TestHelpers.ZaaImporterFromResource();
f.Formatter formatter = new f.Formatter(document);
formatter.Laboratory = importer.Laboratory;
formatter.Settings.Elements.Add(new Items("Klinische Chemie: Na, K, Cl"));
formatter.Run();
Assert.AreEqual("Klinische Chemie: Na 144 mM, K 4,3 mM\r", document.Range().Text);
((_Document)document).Close(WdSaveOptions.wdDoNotSaveChanges);
_formatter.Settings.Elements.Add(new Items("Klinische Chemie: Na, K, Cl"));
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-all.txt"),
_document.Range().Text);
}
[Test]
public void SelectEachDay()
{
_formatter.Settings.Elements.Add(
new SelectEachDay(
new Items("Klinische Chemie: Na, K, Cl"))
);
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-eachday.txt"),
_document.Range().Text);
}
[Test]
public void SelectFirstDay()
{
_formatter.Settings.Elements.Add(
new SelectFirstDay(
new Items("Klinische Chemie: Na, K, Cl"))
);
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-firstday.txt"),
_document.Range().Text);
}
[Test]
public void SelectLastDay()
{
_formatter.Settings.Elements.Add(
new SelectLastDay(
new Items("Klinische Chemie: Na, K, Cl"))
);
_formatter.Run();
Assert.AreEqual(
GetResourceText("Tests.Formatter.FormatterTest-lastday.txt"),
_document.Range().Text);
}
string GetResourceText(string resource)
{
try
{
Stream s = this.GetType().Assembly
.GetManifestResourceStream(resource);
StreamReader sr = new StreamReader(s);
// Need to replace \r\n with \r because this is the newline character
// that Word uses.
return sr.ReadToEnd().Replace("\r\n", "\r");
}
catch (Exception e)
{
throw new IOException("Unable to read " + resource, e);
}
}
}
}