Change value diagram calculator
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams;
|
||||
using StructureHelperLogics.NdmCalculations.Primitives;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StructureHelperTests.UnitTests.ValueDiagramsTests
|
||||
{
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
|
||||
[TestFixture]
|
||||
public class ValueDiagramCalculatorUpdateCloningStrategyTests
|
||||
{
|
||||
private Mock<ICloningStrategy> _cloningStrategyMock;
|
||||
private Mock<IUpdateStrategy<IHasForceActions>> _forcesUpdateMock;
|
||||
private Mock<IUpdateStrategy<IHasPrimitives>> _primitivesUpdateMock;
|
||||
private ValueDiagramCalculatorUpdateCloningStrategy _strategy;
|
||||
|
||||
private Mock<IValueDiagramCalculator> _sourceMock;
|
||||
private Mock<IValueDiagramCalculator> _targetMock;
|
||||
private Mock<IValueDiagramCalculatorInputData> _inputDataMock;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_cloningStrategyMock = new Mock<ICloningStrategy>();
|
||||
_forcesUpdateMock = new Mock<IUpdateStrategy<IHasForceActions>>();
|
||||
_primitivesUpdateMock = new Mock<IUpdateStrategy<IHasPrimitives>>();
|
||||
|
||||
_strategy = new ValueDiagramCalculatorUpdateCloningStrategy(
|
||||
_cloningStrategyMock.Object,
|
||||
_forcesUpdateMock.Object,
|
||||
_primitivesUpdateMock.Object
|
||||
);
|
||||
|
||||
_sourceMock = new Mock<IValueDiagramCalculator>();
|
||||
_targetMock = new Mock<IValueDiagramCalculator>();
|
||||
_inputDataMock = new Mock<IValueDiagramCalculatorInputData>();
|
||||
|
||||
// Both source and target share the same interface object type
|
||||
_sourceMock.Setup(s => s.InputData).Returns(_inputDataMock.Object);
|
||||
_targetMock.Setup(t => t.InputData).Returns(_inputDataMock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_SourceObjectIsNull_Throws()
|
||||
{
|
||||
Assert.That(
|
||||
() => _strategy.Update(_targetMock.Object, null),
|
||||
Throws.Exception.TypeOf<StructureHelperException>()
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_TargetObjectIsNull_Throws()
|
||||
{
|
||||
Assert.That(
|
||||
() => _strategy.Update(null, _sourceMock.Object),
|
||||
Throws.Exception.TypeOf<StructureHelperException>()
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_TargetAndSourceAreSame_DoesNothing()
|
||||
{
|
||||
_strategy.Update(_targetMock.Object, _targetMock.Object);
|
||||
|
||||
_primitivesUpdateMock.Verify(p => p.Update(It.IsAny<IHasPrimitives>(), It.IsAny<IHasPrimitives>()), Times.Never);
|
||||
_forcesUpdateMock.Verify(f => f.Update(It.IsAny<IHasForceActions>(), It.IsAny<IHasForceActions>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_CallsPrimitivesAndForcesStrategies()
|
||||
{
|
||||
// Act
|
||||
_strategy.Update(_targetMock.Object, _sourceMock.Object);
|
||||
|
||||
// Assert: Both strategies are called with the same InputData object
|
||||
_primitivesUpdateMock.Verify(p => p.Update(_inputDataMock.Object, _inputDataMock.Object), Times.Once);
|
||||
_forcesUpdateMock.Verify(f => f.Update(_inputDataMock.Object, _inputDataMock.Object), Times.Once);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models;
|
||||
using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams;
|
||||
|
||||
namespace StructureHelperTests.UnitTests.ValueDiagramsTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class ValueDiagramEntityCheckLogicTests
|
||||
{
|
||||
private Mock<ICheckEntityLogic<IValueDiagram>> _diagramCheckMock;
|
||||
private Mock<IShiftTraceLogger> _loggerMock;
|
||||
private ValueDiagramEntityCheckLogic _logic;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_diagramCheckMock = new Mock<ICheckEntityLogic<IValueDiagram>>();
|
||||
_loggerMock = new Mock<IShiftTraceLogger>();
|
||||
_logic = new ValueDiagramEntityCheckLogic(_diagramCheckMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Check_EntityIsNull_ThrowsStructureHelperException()
|
||||
{
|
||||
// Arrange
|
||||
_logic.Entity = null;
|
||||
|
||||
// Act + Assert
|
||||
Assert.That(
|
||||
() => _logic.Check(),
|
||||
Throws.Exception.TypeOf<StructureHelperException>()
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Check_DiagramCheckPasses_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var entity = new Mock<IValueDiagramEntity>();
|
||||
var diagram = new Mock<IValueDiagram>().Object;
|
||||
|
||||
entity.Setup(e => e.ValueDiagram).Returns(diagram);
|
||||
entity.Setup(e => e.Name).Returns("TestDiagram");
|
||||
|
||||
_logic.Entity = entity.Object;
|
||||
|
||||
_diagramCheckMock.Setup(c => c.Check()).Returns(true);
|
||||
|
||||
// Act
|
||||
var result = _logic.Check();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.True);
|
||||
|
||||
_diagramCheckMock.VerifySet(c => c.Entity = diagram, Times.Once);
|
||||
_diagramCheckMock.Verify(c => c.Check(), Times.Once);
|
||||
|
||||
_loggerMock.Verify(l => l.AddMessage(It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Check_DiagramCheckFails_ReturnsFalse_AndLogsError()
|
||||
{
|
||||
// Arrange
|
||||
var entity = new Mock<IValueDiagramEntity>();
|
||||
var diagram = new Mock<IValueDiagram>().Object;
|
||||
|
||||
entity.Setup(e => e.ValueDiagram).Returns(diagram);
|
||||
entity.Setup(e => e.Name).Returns("MyDiagram");
|
||||
|
||||
_logic.Entity = entity.Object;
|
||||
|
||||
_diagramCheckMock.Setup(c => c.Check()).Returns(false);
|
||||
_diagramCheckMock.Setup(c => c.CheckResult).Returns("child error message");
|
||||
|
||||
// Act
|
||||
var result = _logic.Check();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
|
||||
_diagramCheckMock.Verify(c => c.Check(), Times.Once);
|
||||
|
||||
_loggerMock.Verify(
|
||||
l => l.AddMessage(It.Is<string>(s => s.Contains("Diagram: MyDiagram has error: child error message"))),
|
||||
Times.Once
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Check_EntityNameIsNull_UsesUnnamedInMessage()
|
||||
{
|
||||
// Arrange
|
||||
var entity = new Mock<IValueDiagramEntity>();
|
||||
var diagram = new Mock<IValueDiagram>().Object;
|
||||
|
||||
entity.Setup(e => e.ValueDiagram).Returns(diagram);
|
||||
entity.Setup(e => e.Name).Returns((string)null);
|
||||
|
||||
_logic.Entity = entity.Object;
|
||||
|
||||
_diagramCheckMock.Setup(c => c.Check()).Returns(false);
|
||||
_diagramCheckMock.Setup(c => c.CheckResult).Returns("xyz");
|
||||
|
||||
// Act
|
||||
var result = _logic.Check();
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False);
|
||||
|
||||
_loggerMock.Verify(
|
||||
l => l.AddMessage(It.Is<string>(s => s.Contains("<unnamed>"))),
|
||||
Times.Once
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Shapes;
|
||||
using StructureHelperLogics.NdmCalculations.Analyses.ValueDiagrams;
|
||||
|
||||
namespace StructureHelperTests.UnitTests.ValueDiagramsTests
|
||||
{
|
||||
|
||||
|
||||
[TestFixture]
|
||||
public class ValueDiagramUpdateStrategyTests
|
||||
{
|
||||
private Mock<IUpdateStrategy<IPoint2DRange>> _rangeUpdateMock;
|
||||
private ValueDiagramUpdateStrategy _strategy;
|
||||
private Mock<IValueDiagram> _targetMock;
|
||||
private Mock<IValueDiagram> _sourceMock;
|
||||
private Mock<IPoint2DRange> _targetRangeMock;
|
||||
private Mock<IPoint2DRange> _sourceRangeMock;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_rangeUpdateMock = new Mock<IUpdateStrategy<IPoint2DRange>>();
|
||||
_strategy = new ValueDiagramUpdateStrategy(_rangeUpdateMock.Object);
|
||||
|
||||
_targetMock = new Mock<IValueDiagram>();
|
||||
_sourceMock = new Mock<IValueDiagram>();
|
||||
_targetRangeMock = new Mock<IPoint2DRange>();
|
||||
_sourceRangeMock = new Mock<IPoint2DRange>();
|
||||
|
||||
_targetMock.Setup(t => t.Point2DRange).Returns(_targetRangeMock.Object);
|
||||
_sourceMock.Setup(s => s.Point2DRange).Returns(_sourceRangeMock.Object);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_TargetIsNull_Throws()
|
||||
{
|
||||
Assert.That(
|
||||
() => _strategy.Update(null, _sourceMock.Object),
|
||||
Throws.Exception.TypeOf<StructureHelperException>()
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_SourceIsNull_Throws()
|
||||
{
|
||||
Assert.That(
|
||||
() => _strategy.Update(_targetMock.Object, null),
|
||||
Throws.Exception.TypeOf<StructureHelperException>()
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_TargetEqualsSource_DoesNothing()
|
||||
{
|
||||
_strategy.Update(_targetMock.Object, _targetMock.Object);
|
||||
|
||||
_rangeUpdateMock.Verify(r => r.Update(It.IsAny<IPoint2DRange>(), It.IsAny<IPoint2DRange>()), Times.Never);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_CopiesStepNumber_AndCallsRangeUpdate()
|
||||
{
|
||||
_sourceMock.Setup(s => s.StepNumber).Returns(5);
|
||||
|
||||
_strategy.Update(_targetMock.Object, _sourceMock.Object);
|
||||
|
||||
_targetMock.VerifySet(t => t.StepNumber = 5, Times.Once);
|
||||
_rangeUpdateMock.Verify(r => r.Update(_targetRangeMock.Object, _sourceRangeMock.Object), Times.Once);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_LazyInitializesRangeUpdateStrategy_WhenNotProvided()
|
||||
{
|
||||
// Use parameterless constructor
|
||||
var strategy = new ValueDiagramUpdateStrategy();
|
||||
var targetMock = new Mock<IValueDiagram>();
|
||||
var sourceMock = new Mock<IValueDiagram>();
|
||||
var targetRangeMock = new Mock<IPoint2DRange>();
|
||||
var sourceRangeMock = new Mock<IPoint2DRange>();
|
||||
var pointMoq = new Mock<IPoint2D>();
|
||||
|
||||
targetMock.Setup(t => t.Point2DRange).Returns(targetRangeMock.Object);
|
||||
sourceMock.Setup(s => s.Point2DRange).Returns(sourceRangeMock.Object);
|
||||
sourceRangeMock.Setup(s => s.StartPoint).Returns(pointMoq.Object);
|
||||
sourceRangeMock.Setup(s => s.EndPoint).Returns(pointMoq.Object);
|
||||
targetRangeMock.Setup(s => s.StartPoint).Returns(pointMoq.Object);
|
||||
targetRangeMock.Setup(s => s.EndPoint).Returns(pointMoq.Object);
|
||||
|
||||
// This should create Point2DRangeUpdateStrategy lazily
|
||||
Assert.DoesNotThrow(() => strategy.Update(targetMock.Object, sourceMock.Object));
|
||||
|
||||
// No exception means lazy initialization worked
|
||||
// Cannot directly mock the default Point2DRangeUpdateStrategy, but at least this ensures no null reference
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Update_DoesNotCallRangeUpdate_WhenUpdateChildrenIsFalse()
|
||||
{
|
||||
_strategy.UpdateChildren = false;
|
||||
|
||||
_strategy.Update(_targetMock.Object, _sourceMock.Object);
|
||||
|
||||
_rangeUpdateMock.Verify(r => r.Update(It.IsAny<IPoint2DRange>(), It.IsAny<IPoint2DRange>()), Times.Never);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user