From 4ba7f28a242b28c8acd2ec9df70cbba4fe6e5d43 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Tue, 12 Feb 2019 14:34:51 +0100 Subject: [PATCH] SEBWIN-296: Fixed unit tests for repeatable operation sequence. --- .../RepeatableOperationSequenceTests.cs | 256 +++++++++--------- 1 file changed, 129 insertions(+), 127 deletions(-) diff --git a/SafeExamBrowser.Core.UnitTests/OperationModel/RepeatableOperationSequenceTests.cs b/SafeExamBrowser.Core.UnitTests/OperationModel/RepeatableOperationSequenceTests.cs index 97127760..596d31ec 100644 --- a/SafeExamBrowser.Core.UnitTests/OperationModel/RepeatableOperationSequenceTests.cs +++ b/SafeExamBrowser.Core.UnitTests/OperationModel/RepeatableOperationSequenceTests.cs @@ -6,172 +6,174 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; +using System.Collections.Generic; 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 { [TestClass] public class RepeatableOperationSequenceTests { + private Mock logger; + [TestInitialize] public void Initialize() { - + logger = new Mock(); } [TestMethod] - public void TODO() + public void MustCorrectlyAbortRepeat() { - Assert.Fail(); + var operationA = new Mock(); + var operationB = new Mock(); + var operationC = new Mock(); + var operations = new Queue(); + + 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] - //public void MustCorrectlyAbortRepeat() - //{ - // var operationA = new Mock(); - // var operationB = new Mock(); - // var operationC = new Mock(); - // var operations = new Queue(); + [TestMethod] + public void MustRepeatOperations() + { + var operationA = new Mock(); + var operationB = new Mock(); + var operationC = new Mock(); + var operations = new Queue(); - // operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); - // operationB.Setup(o => o.Repeat()).Returns(OperationResult.Aborted); + operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); + operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success); + operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success); - // operations.Enqueue(operationA.Object); - // operations.Enqueue(operationB.Object); - // operations.Enqueue(operationC.Object); + operations.Enqueue(operationA.Object); + operations.Enqueue(operationB.Object); + operations.Enqueue(operationC.Object); - // var sut = new OperationSequence(loggerMock.Object, operations); - // var result = sut.TryRepeat(); + 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); + operationA.Verify(o => o.Perform(), Times.Never); + operationA.Verify(o => o.Repeat(), Times.Once); + operationA.Verify(o => o.Revert(), Times.Never); + operationB.Verify(o => o.Perform(), Times.Never); + 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.Aborted, result); - //} + Assert.AreEqual(OperationResult.Success, result); + } - //[TestMethod] - //public void MustRepeatOperations() - //{ - // var operationA = new Mock(); - // var operationB = new Mock(); - // var operationC = new Mock(); - // var operations = new Queue(); + [TestMethod] + public void MustRepeatOperationsInSequence() + { + int current = 0, a = 0, b = 0, c = 0; + var operationA = new Mock(); + var operationB = new Mock(); + var operationC = new Mock(); + var operations = new Queue(); - // operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); - // operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success); - // operationC.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).Callback(() => b = ++current); + operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => c = ++current); - // operations.Enqueue(operationA.Object); - // operations.Enqueue(operationB.Object); - // operations.Enqueue(operationC.Object); + operations.Enqueue(operationA.Object); + operations.Enqueue(operationB.Object); + operations.Enqueue(operationC.Object); - // var sut = new OperationSequence(loggerMock.Object, operations); - // var result = sut.TryRepeat(); + var sut = new RepeatableOperationSequence(logger.Object, operations); + var result = sut.TryRepeat(); - // operationA.Verify(o => o.Perform(), Times.Never); - // operationA.Verify(o => o.Repeat(), Times.Once); - // operationA.Verify(o => o.Revert(), Times.Never); - // operationB.Verify(o => o.Perform(), Times.Never); - // 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); + Assert.IsTrue(a == 1); + Assert.IsTrue(b == 2); + Assert.IsTrue(c == 3); + } - // Assert.AreEqual(OperationResult.Success, result); - //} + [TestMethod] + public void MustNotRevertOperationsInCaseOfError() + { + var operationA = new Mock(); + var operationB = new Mock(); + var operationC = new Mock(); + var operationD = new Mock(); + var operations = new Queue(); - //[TestMethod] - //public void MustRepeatOperationsInSequence() - //{ - // int current = 0, a = 0, b = 0, c = 0; - // var operationA = new Mock(); - // var operationB = new Mock(); - // var operationC = new Mock(); - // var operations = new Queue(); + operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); + operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success); + operationC.Setup(o => o.Repeat()).Throws(); - // operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => a = ++current); - // operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => b = ++current); - // operationC.Setup(o => o.Repeat()).Returns(OperationResult.Success).Callback(() => c = ++current); + operations.Enqueue(operationA.Object); + operations.Enqueue(operationB.Object); + operations.Enqueue(operationC.Object); + operations.Enqueue(operationD.Object); - // operations.Enqueue(operationA.Object); - // operations.Enqueue(operationB.Object); - // operations.Enqueue(operationC.Object); + var sut = new RepeatableOperationSequence(logger.Object, operations); + var result = sut.TryRepeat(); - // var sut = new OperationSequence(loggerMock.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.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.IsTrue(a == 1); - // Assert.IsTrue(b == 2); - // Assert.IsTrue(c == 3); - //} + Assert.AreEqual(OperationResult.Failed, result); + } - //[TestMethod] - //public void MustNotRevertOperationsInCaseOfError() - //{ - // var operationA = new Mock(); - // var operationB = new Mock(); - // var operationC = new Mock(); - // var operationD = new Mock(); - // var operations = new Queue(); + [TestMethod] + public void MustSucceedRepeatingWithEmptyQueue() + { + var sut = new RepeatableOperationSequence(logger.Object, new Queue()); + var result = sut.TryRepeat(); - // operationA.Setup(o => o.Repeat()).Returns(OperationResult.Success); - // operationB.Setup(o => o.Repeat()).Returns(OperationResult.Success); - // operationC.Setup(o => o.Repeat()).Throws(); + Assert.AreEqual(OperationResult.Success, result); + } - // operations.Enqueue(operationA.Object); - // operations.Enqueue(operationB.Object); - // operations.Enqueue(operationC.Object); - // operations.Enqueue(operationD.Object); + [TestMethod] + public void MustSucceedRepeatingWithoutCallingPerform() + { + var sut = new RepeatableOperationSequence(logger.Object, new Queue()); + var result = sut.TryRepeat(); - // var sut = new OperationSequence(loggerMock.Object, operations); - // var result = sut.TryRepeat(); + Assert.AreEqual(OperationResult.Success, result); + } - // 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.Once); - // operationC.Verify(o => o.Revert(), Times.Never); - // operationD.Verify(o => o.Repeat(), Times.Never); - // operationD.Verify(o => o.Revert(), Times.Never); + [TestMethod] + public void MustNotFailInCaseOfUnexpectedErrorWhenRepeating() + { + var sut = new RepeatableOperationSequence(logger.Object, new Queue()); - // Assert.AreEqual(OperationResult.Failed, result); - //} + sut.ProgressChanged += (args) => throw new Exception(); - //[TestMethod] - //public void MustSucceedRepeatingWithEmptyQueue() - //{ - // var sut = new OperationSequence(loggerMock.Object, new Queue()); - // var result = sut.TryRepeat(); + var result = sut.TryRepeat(); - // Assert.AreEqual(OperationResult.Success, result); - //} - - //[TestMethod] - //public void MustSucceedRepeatingWithoutCallingPerform() - //{ - // var sut = new OperationSequence(loggerMock.Object, new Queue()); - // var result = sut.TryRepeat(); - - // Assert.AreEqual(OperationResult.Success, result); - //} - - //[TestMethod] - //public void MustNotFailInCaseOfUnexpectedErrorWhenRepeating() - //{ - // var sut = new OperationSequence(loggerMock.Object, new Queue()); - - // sut.ProgressChanged += (args) => throw new Exception(); - - // var result = sut.TryRepeat(); - - // Assert.AreEqual(OperationResult.Failed, result); - //} + Assert.AreEqual(OperationResult.Failed, result); + } } }