SEBWIN-296: Fixed unit tests for repeatable operation sequence.

This commit is contained in:
dbuechel 2019-02-12 14:34:51 +01:00
parent 1db30e7d2c
commit 4ba7f28a24

View file

@ -6,172 +6,174 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SafeExamBrowser.Contracts.Core.OperationModel;
using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.OperationModel;
namespace SafeExamBrowser.Core.UnitTests.OperationModel namespace SafeExamBrowser.Core.UnitTests.OperationModel
{ {
[TestClass] [TestClass]
public class RepeatableOperationSequenceTests public class RepeatableOperationSequenceTests
{ {
private Mock<ILogger> logger;
[TestInitialize] [TestInitialize]
public void Initialize() public void Initialize()
{ {
logger = new Mock<ILogger>();
} }
[TestMethod] [TestMethod]
public void TODO() public void MustCorrectlyAbortRepeat()
{ {
Assert.Fail(); var operationA = new Mock<IRepeatableOperation>();
var operationB = new Mock<IRepeatableOperation>();
var operationC = new Mock<IRepeatableOperation>();
var operations = new Queue<IRepeatableOperation>();
operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
operationB.Setup(o => o.Repeat()).Returns(OperationResult.Aborted);
operations.Enqueue(operationA.Object);
operations.Enqueue(operationB.Object);
operations.Enqueue(operationC.Object);
var sut = new RepeatableOperationSequence(logger.Object, operations);
var result = sut.TryRepeat();
operationA.Verify(o => o.Repeat(), Times.Once);
operationA.Verify(o => o.Revert(), Times.Never);
operationB.Verify(o => o.Repeat(), Times.Once);
operationB.Verify(o => o.Revert(), Times.Never);
operationC.Verify(o => o.Repeat(), Times.Never);
operationC.Verify(o => o.Revert(), Times.Never);
Assert.AreEqual(OperationResult.Aborted, result);
} }
//[TestMethod] [TestMethod]
//public void MustCorrectlyAbortRepeat() public void MustRepeatOperations()
//{ {
// var operationA = new Mock<IOperation>(); var operationA = new Mock<IRepeatableOperation>();
// var operationB = new Mock<IOperation>(); var operationB = new Mock<IRepeatableOperation>();
// var operationC = new Mock<IOperation>(); var operationC = new Mock<IRepeatableOperation>();
// var operations = new Queue<IOperation>(); var operations = new Queue<IRepeatableOperation>();
// operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
// operationB.Setup(o => o.Repeat()).Returns(OperationResult.Aborted); operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success);
operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success);
// operations.Enqueue(operationA.Object); operations.Enqueue(operationA.Object);
// operations.Enqueue(operationB.Object); operations.Enqueue(operationB.Object);
// operations.Enqueue(operationC.Object); operations.Enqueue(operationC.Object);
// var sut = new OperationSequence(loggerMock.Object, operations); var sut = new RepeatableOperationSequence(logger.Object, operations);
// var result = sut.TryRepeat(); var result = sut.TryRepeat();
// operationA.Verify(o => o.Repeat(), Times.Once); operationA.Verify(o => o.Perform(), Times.Never);
// operationA.Verify(o => o.Revert(), Times.Never); operationA.Verify(o => o.Repeat(), Times.Once);
// operationB.Verify(o => o.Repeat(), Times.Once); operationA.Verify(o => o.Revert(), Times.Never);
// operationB.Verify(o => o.Revert(), Times.Never); operationB.Verify(o => o.Perform(), Times.Never);
// operationC.Verify(o => o.Repeat(), Times.Never); operationB.Verify(o => o.Repeat(), Times.Once);
// operationC.Verify(o => o.Revert(), Times.Never); operationB.Verify(o => o.Revert(), Times.Never);
operationC.Verify(o => o.Perform(), Times.Never);
operationC.Verify(o => o.Repeat(), Times.Once);
operationC.Verify(o => o.Revert(), Times.Never);
// Assert.AreEqual(OperationResult.Aborted, result); Assert.AreEqual(OperationResult.Success, result);
//} }
//[TestMethod] [TestMethod]
//public void MustRepeatOperations() public void MustRepeatOperationsInSequence()
//{ {
// var operationA = new Mock<IOperation>(); int current = 0, a = 0, b = 0, c = 0;
// var operationB = new Mock<IOperation>(); var operationA = new Mock<IRepeatableOperation>();
// var operationC = new Mock<IOperation>(); var operationB = new Mock<IRepeatableOperation>();
// var operations = new Queue<IOperation>(); var operationC = new Mock<IRepeatableOperation>();
var operations = new Queue<IRepeatableOperation>();
// operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => a = ++current);
// operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success); operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => b = ++current);
// operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success); operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => c = ++current);
// operations.Enqueue(operationA.Object); operations.Enqueue(operationA.Object);
// operations.Enqueue(operationB.Object); operations.Enqueue(operationB.Object);
// operations.Enqueue(operationC.Object); operations.Enqueue(operationC.Object);
// var sut = new OperationSequence(loggerMock.Object, operations); var sut = new RepeatableOperationSequence(logger.Object, operations);
// var result = sut.TryRepeat(); var result = sut.TryRepeat();
// operationA.Verify(o => o.Perform(), Times.Never); Assert.AreEqual(OperationResult.Success, result);
// operationA.Verify(o => o.Repeat(), Times.Once); Assert.IsTrue(a == 1);
// operationA.Verify(o => o.Revert(), Times.Never); Assert.IsTrue(b == 2);
// operationB.Verify(o => o.Perform(), Times.Never); Assert.IsTrue(c == 3);
// operationB.Verify(o => o.Repeat(), Times.Once); }
// operationB.Verify(o => o.Revert(), Times.Never);
// operationC.Verify(o => o.Perform(), Times.Never);
// operationC.Verify(o => o.Repeat(), Times.Once);
// operationC.Verify(o => o.Revert(), Times.Never);
// Assert.AreEqual(OperationResult.Success, result); [TestMethod]
//} public void MustNotRevertOperationsInCaseOfError()
{
var operationA = new Mock<IRepeatableOperation>();
var operationB = new Mock<IRepeatableOperation>();
var operationC = new Mock<IRepeatableOperation>();
var operationD = new Mock<IRepeatableOperation>();
var operations = new Queue<IRepeatableOperation>();
//[TestMethod] operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success);
//public void MustRepeatOperationsInSequence() operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success);
//{ operationC.Setup(o => o.Repeat()).Throws<Exception>();
// int current = 0, a = 0, b = 0, c = 0;
// var operationA = new Mock<IOperation>();
// var operationB = new Mock<IOperation>();
// var operationC = new Mock<IOperation>();
// var operations = new Queue<IOperation>();
// operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => a = ++current); operations.Enqueue(operationA.Object);
// operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => b = ++current); operations.Enqueue(operationB.Object);
// operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => c = ++current); operations.Enqueue(operationC.Object);
operations.Enqueue(operationD.Object);
// operations.Enqueue(operationA.Object); var sut = new RepeatableOperationSequence(logger.Object, operations);
// operations.Enqueue(operationB.Object); var result = sut.TryRepeat();
// operations.Enqueue(operationC.Object);
// var sut = new OperationSequence(loggerMock.Object, operations); operationA.Verify(o => o.Repeat(), Times.Once);
// var result = sut.TryRepeat(); operationA.Verify(o => o.Revert(), Times.Never);
operationB.Verify(o => o.Repeat(), Times.Once);
operationB.Verify(o => o.Revert(), Times.Never);
operationC.Verify(o => o.Repeat(), Times.Once);
operationC.Verify(o => o.Revert(), Times.Never);
operationD.Verify(o => o.Repeat(), Times.Never);
operationD.Verify(o => o.Revert(), Times.Never);
// Assert.AreEqual(OperationResult.Success, result); Assert.AreEqual(OperationResult.Failed, result);
// Assert.IsTrue(a == 1); }
// Assert.IsTrue(b == 2);
// Assert.IsTrue(c == 3);
//}
//[TestMethod] [TestMethod]
//public void MustNotRevertOperationsInCaseOfError() public void MustSucceedRepeatingWithEmptyQueue()
//{ {
// var operationA = new Mock<IOperation>(); var sut = new RepeatableOperationSequence(logger.Object, new Queue<IRepeatableOperation>());
// var operationB = new Mock<IOperation>(); var result = sut.TryRepeat();
// var operationC = new Mock<IOperation>();
// var operationD = new Mock<IOperation>();
// var operations = new Queue<IOperation>();
// operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); Assert.AreEqual(OperationResult.Success, result);
// operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success); }
// operationC.Setup(o => o.Repeat()).Throws<Exception>();
// operations.Enqueue(operationA.Object); [TestMethod]
// operations.Enqueue(operationB.Object); public void MustSucceedRepeatingWithoutCallingPerform()
// operations.Enqueue(operationC.Object); {
// operations.Enqueue(operationD.Object); var sut = new RepeatableOperationSequence(logger.Object, new Queue<IRepeatableOperation>());
var result = sut.TryRepeat();
// var sut = new OperationSequence(loggerMock.Object, operations); Assert.AreEqual(OperationResult.Success, result);
// var result = sut.TryRepeat(); }
// operationA.Verify(o => o.Repeat(), Times.Once); [TestMethod]
// operationA.Verify(o => o.Revert(), Times.Never); public void MustNotFailInCaseOfUnexpectedErrorWhenRepeating()
// operationB.Verify(o => o.Repeat(), Times.Once); {
// operationB.Verify(o => o.Revert(), Times.Never); var sut = new RepeatableOperationSequence(logger.Object, new Queue<IRepeatableOperation>());
// operationC.Verify(o => o.Repeat(), Times.Once);
// operationC.Verify(o => o.Revert(), Times.Never);
// operationD.Verify(o => o.Repeat(), Times.Never);
// operationD.Verify(o => o.Revert(), Times.Never);
// Assert.AreEqual(OperationResult.Failed, result); sut.ProgressChanged += (args) => throw new Exception();
//}
//[TestMethod] var result = sut.TryRepeat();
//public void MustSucceedRepeatingWithEmptyQueue()
//{
// var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
// var result = sut.TryRepeat();
// Assert.AreEqual(OperationResult.Success, result); Assert.AreEqual(OperationResult.Failed, result);
//} }
//[TestMethod]
//public void MustSucceedRepeatingWithoutCallingPerform()
//{
// var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
// var result = sut.TryRepeat();
// Assert.AreEqual(OperationResult.Success, result);
//}
//[TestMethod]
//public void MustNotFailInCaseOfUnexpectedErrorWhenRepeating()
//{
// var sut = new OperationSequence(loggerMock.Object, new Queue<IOperation>());
// sut.ProgressChanged += (args) => throw new Exception();
// var result = sut.TryRepeat();
// Assert.AreEqual(OperationResult.Failed, result);
//}
} }
} }