SEBWIN-219: Updated unit tests for core library.

This commit is contained in:
dbuechel 2018-03-14 15:27:11 +01:00
parent 49d9b03d7a
commit 5a830bad42
10 changed files with 256 additions and 55 deletions

View file

@ -8,11 +8,12 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Behaviour.OperationModel;
using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Behaviour.OperationModel;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel
{
[TestClass]
public class CommunicationOperationTests
@ -40,22 +41,38 @@ namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
hostMock.Setup(h => h.Stop()).Callback(() => stop = ++order);
hostMock.Setup(h => h.Start()).Callback(() => start = ++order);
sut.Repeat();
var result = sut.Repeat();
hostMock.Verify(h => h.Stop(), Times.Once);
hostMock.Verify(h => h.Start(), Times.Once);
Assert.AreEqual(stop, 1);
Assert.AreEqual(start, 2);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustOnlyRestartHostOnRepeatIfNotRunning()
{
hostMock.SetupGet(h => h.IsRunning).Returns(true);
var result = sut.Repeat();
hostMock.Verify(h => h.Start(), Times.Never);
hostMock.Verify(h => h.Stop(), Times.Never);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustStartHostOnPerform()
{
sut.Perform();
var result = sut.Perform();
hostMock.Verify(h => h.Start(), Times.Once);
hostMock.Verify(h => h.Stop(), Times.Never);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]

View file

@ -0,0 +1,142 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Behaviour.OperationModel;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour.OperationModel;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel
{
[TestClass]
public class DelayedInitializationOperationTests
{
private Mock<IOperation> operationMock;
[TestInitialize]
public void Initialize()
{
operationMock = new Mock<IOperation>();
}
[TestMethod]
public void MustInstantiateOperationOnPerform()
{
var initialized = false;
IOperation initialize()
{
initialized = true;
return operationMock.Object;
};
var sut = new DelayedInitializationOperation(initialize);
sut.Perform();
Assert.IsTrue(initialized);
}
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void MustNotInstantiateOperationOnRepeat()
{
IOperation initialize()
{
return operationMock.Object;
};
var sut = new DelayedInitializationOperation(initialize);
sut.Repeat();
}
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void MustNotInstantiateOperationOnRevert()
{
IOperation initialize()
{
return operationMock.Object;
};
var sut = new DelayedInitializationOperation(initialize);
sut.Revert();
}
[TestMethod]
public void MustReturnCorrectOperationResult()
{
IOperation initialize()
{
return operationMock.Object;
};
operationMock.Setup(o => o.Perform()).Returns(OperationResult.Success);
operationMock.Setup(o => o.Repeat()).Returns(OperationResult.Failed);
var sut = new DelayedInitializationOperation(initialize);
var perform = sut.Perform();
var repeat = sut.Repeat();
sut.Revert();
Assert.AreEqual(OperationResult.Success, perform);
Assert.AreEqual(OperationResult.Failed, repeat);
}
[TestMethod]
public void MustUpdateProgressIndicator()
{
IOperation initialize()
{
return operationMock.Object;
};
var sut = new DelayedInitializationOperation(initialize)
{
ProgressIndicator = new Mock<IProgressIndicator>().Object
};
sut.Perform();
sut.Repeat();
sut.Revert();
operationMock.VerifySet(o => o.ProgressIndicator = It.IsAny<IProgressIndicator>(), Times.Exactly(3));
}
[TestMethod]
public void MustUseSameInstanceForAllOperations()
{
var first = true;
var operation = operationMock.Object;
IOperation initialize()
{
if (first)
{
return operation;
}
return new Mock<IOperation>().Object;
};
var sut = new DelayedInitializationOperation(initialize);
sut.Perform();
sut.Repeat();
sut.Revert();
operationMock.Verify(o => o.Perform(), Times.Once);
operationMock.Verify(o => o.Repeat(), Times.Once);
operationMock.Verify(o => o.Revert(), Times.Once);
}
}
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SafeExamBrowser.Core.Behaviour.OperationModel;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel
{
[TestClass]
public class DelegateOperationTests
{
[TestMethod]
public void MustExecutePerformAction()
{
var performed = false;
void perform() => performed = true;
var sut = new DelegateOperation(perform);
sut.Perform();
Assert.IsTrue(performed);
}
[TestMethod]
public void MustExecuteRepeatAction()
{
var repeated = false;
void repeat() => repeated = true;
var sut = new DelegateOperation(() => { }, repeat);
sut.Repeat();
Assert.IsTrue(repeated);
}
[TestMethod]
public void MustExecuteRevertAction()
{
var reverted = false;
void revert() => reverted = true;
var sut = new DelegateOperation(() => { }, revert: revert);
sut.Revert();
Assert.IsTrue(reverted);
}
}
}

View file

@ -8,11 +8,12 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Behaviour.OperationModel;
using SafeExamBrowser.Contracts.I18n;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Behaviour.OperationModel;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel
{
[TestClass]
public class I18nOperationTests
@ -34,9 +35,19 @@ namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
[TestMethod]
public void MustPerformCorrectly()
{
sut.Perform();
var result = sut.Perform();
textMock.Verify(t => t.Initialize(It.IsAny<ITextResource>()), Times.Once);
Assert.AreEqual(OperationResult.Success, result);
}
[TestMethod]
public void MustRepeatCorrectly()
{
var result = sut.Repeat();
Assert.AreEqual(OperationResult.Success, result);
}
}
}

View file

@ -15,7 +15,7 @@ using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Contracts.UserInterface;
using SafeExamBrowser.Core.Behaviour.OperationModel;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
namespace SafeExamBrowser.Core.UnitTests.Behaviour.OperationModel
{
[TestClass]
public class OperationSequenceTests

View file

@ -1,22 +0,0 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class DelayedInitializationOperationTests
{
[TestMethod]
public void TODO()
{
Assert.Fail();
}
}
}

View file

@ -1,22 +0,0 @@
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SafeExamBrowser.Core.UnitTests.Behaviour.Operations
{
[TestClass]
public class DelegateOperationTests
{
[TestMethod]
public void TODO()
{
Assert.Fail();
}
}
}

View file

@ -64,6 +64,26 @@ namespace SafeExamBrowser.Core.UnitTests.Logging
Assert.IsTrue(content.Text.Equals((log[7] as ILogText).Text));
}
[TestMethod]
public void MustAddInnerExceptionsToLog()
{
var sut = new Logger();
var outerMessage = "Some message for the outer exception";
var innerMessage = "BAAAAM! Inner one here.";
var innerInnerMessage = "Yikes, a null reference...";
var exception = new Exception(outerMessage, new ArgumentException(innerMessage, new NullReferenceException(innerInnerMessage)));
sut.Error("blubb", exception);
var log = sut.GetLog();
var logText = log[1] as ILogText;
Assert.AreEqual(2, log.Count);
Assert.IsTrue(logText.Text.Contains(outerMessage));
Assert.IsTrue(logText.Text.Contains(innerMessage));
Assert.IsTrue(logText.Text.Contains(innerInnerMessage));
}
[TestMethod]
public void MustReturnCopyOfLog()
{

View file

@ -26,6 +26,7 @@ namespace SafeExamBrowser.Core.UnitTests.Logging
var logText = new LogText("Log text");
var sut = new ModuleLogger(loggerMock.Object, typeof(ModuleLoggerTests));
sut.Debug("Debug");
sut.Info("Info");
sut.Warn("Warning");
sut.Error("Error");
@ -36,6 +37,7 @@ namespace SafeExamBrowser.Core.UnitTests.Logging
sut.Unsubscribe(logObserverMock.Object);
sut.GetLog();
loggerMock.Verify(l => l.Debug($"[{nameof(ModuleLoggerTests)}] Debug"), Times.Once);
loggerMock.Verify(l => l.Info($"[{nameof(ModuleLoggerTests)}] Info"), Times.Once);
loggerMock.Verify(l => l.Warn($"[{nameof(ModuleLoggerTests)}] Warning"), Times.Once);
loggerMock.Verify(l => l.Error($"[{nameof(ModuleLoggerTests)}] Error"), Times.Once);

View file

@ -78,11 +78,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Behaviour\Operations\CommunicationOperationTests.cs" />
<Compile Include="Behaviour\Operations\DelayedInitializationOperationTests.cs" />
<Compile Include="Behaviour\Operations\I18nOperationTests.cs" />
<Compile Include="Behaviour\Operations\DelegateOperationTests.cs" />
<Compile Include="Behaviour\Operations\OperationSequenceTests.cs" />
<Compile Include="Behaviour\OperationModel\CommunicationOperationTests.cs" />
<Compile Include="Behaviour\OperationModel\DelayedInitializationOperationTests.cs" />
<Compile Include="Behaviour\OperationModel\I18nOperationTests.cs" />
<Compile Include="Behaviour\OperationModel\DelegateOperationTests.cs" />
<Compile Include="Behaviour\OperationModel\OperationSequenceTests.cs" />
<Compile Include="I18n\TextTests.cs" />
<Compile Include="I18n\XmlTextResourceTests.cs" />
<Compile Include="Logging\DefaultLogFormatterTests.cs" />