diff --git a/DsmSuite.Analyzer.DotNet.Lib/BinaryFile.cs b/DsmSuite.Analyzer.DotNet.Lib/BinaryFile.cs index 2752192..0f00c69 100644 --- a/DsmSuite.Analyzer.DotNet.Lib/BinaryFile.cs +++ b/DsmSuite.Analyzer.DotNet.Lib/BinaryFile.cs @@ -13,14 +13,15 @@ namespace DsmSuite.Analyzer.DotNet.Lib public class BinaryFile { private readonly IProgress _progress; - private readonly IList _typeList = new List(); - private readonly IList _includedAssemblyStrings = new List(); + private readonly List _typeList = new List(); + private readonly List _includedAssemblyStrings = new List(); public BinaryFile(string filename, IProgress progress, List includedAssemblyStrings) { FileInfo = new FileInfo(filename); _progress = progress; - _includedAssemblyStrings = includedAssemblyStrings; + if (includedAssemblyStrings != null) + _includedAssemblyStrings.AddRange(includedAssemblyStrings); } public List Types { get; } = new List(); diff --git a/DsmSuite.Analyzer.DotNet.Test/Analysis/AnalyzerTest.cs b/DsmSuite.Analyzer.DotNet.Test/Analysis/AnalyzerTest.cs index a82d49a..07fe73e 100644 --- a/DsmSuite.Analyzer.DotNet.Test/Analysis/AnalyzerTest.cs +++ b/DsmSuite.Analyzer.DotNet.Test/Analysis/AnalyzerTest.cs @@ -11,13 +11,8 @@ namespace DsmSuite.Analyzer.DotNet.Test.Analysis [TestClass] public class AnalyzerTest { - [TestMethod] - public void TestAnalyze() + private void TestAnalyze(AnalyzerSettings analyzerSettings) { - AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); - analyzerSettings.Input.AssemblyDirectory = TestData.RootDirectory; - analyzerSettings.Transformation.IgnoredNames = new List(); - IDsiModel model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); DotNet.Analysis.Analyzer analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); analyzer.Analyze(); @@ -81,30 +76,30 @@ public void TestAnalyze() IDsiElement elementGenericParameterType = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericParameterType`1"); Assert.IsNotNull(elementGenericParameterType); - IDsiElement elementGenericParameterTypeParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericParameterTypeParameter"); + IDsiElement elementGenericParameterTypeParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericParameterTypeParameter"); Assert.IsNotNull(elementGenericParameterTypeParameter); // Return types - IDsiElement elementReturnType = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.ReturnType"); + IDsiElement elementReturnType = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.ReturnType"); Assert.IsNotNull(elementReturnType); - IDsiElement elementReturnEnum = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.ReturnEnum"); + IDsiElement elementReturnEnum = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.ReturnEnum"); Assert.IsNotNull(elementReturnEnum); - IDsiElement elementGenericReturnType = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericReturnType`1"); + IDsiElement elementGenericReturnType = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericReturnType`1"); Assert.IsNotNull(elementGenericReturnType); - IDsiElement elementGenericReturnTypeParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericReturnTypeParameter"); + IDsiElement elementGenericReturnTypeParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.GenericReturnTypeParameter"); Assert.IsNotNull(elementGenericReturnTypeParameter); IDsiElement elementDelegateGenericParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.DelegateGenericParameter"); Assert.IsNotNull(elementDelegateGenericParameter); - IDsiElement elementEventsArgsGenericParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.EventsArgsGenericParameter"); + IDsiElement elementEventsArgsGenericParameter = model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.EventsArgsGenericParameter"); Assert.IsNotNull(elementEventsArgsGenericParameter); // Main relations - Assert.IsTrue(model.DoesRelationExist(elementMainClient.Id, elementMainType.Id)); + Assert.IsTrue(model.DoesRelationExist(elementMainClient.Id, elementMainType.Id)); Assert.IsTrue(model.DoesRelationExist(elementMainClient.Id, elementEventsArgsGenericParameter.Id)); Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementNestedType.Id)); @@ -116,15 +111,15 @@ public void TestAnalyze() Assert.IsTrue(model.DoesRelationExist(elementMainTypeAnonymous.Id, elementDelegateGenericParameter.Id)); Assert.IsTrue(model.DoesRelationExist(elementMainTypeAnonymous.Id, elementDelegateGenericParameter.Id)); - Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementInterfaceA.Id)); - Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementBaseType.Id)); + Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementInterfaceA.Id)); + Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementBaseType.Id)); Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementEventsArgsGenericParameter.Id)); Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementDelegateGenericParameter.Id)); // Field relations - Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementFieldType.Id)); - Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementGenericFieldType.Id)); + Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementFieldType.Id)); + Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementGenericFieldType.Id)); // Property relations Assert.IsTrue(model.DoesRelationExist(elementMainType.Id, elementPropertyType.Id)); @@ -157,5 +152,109 @@ public void TestAnalyze() Assert.IsTrue(model.DoesRelationExist(elementInterfaceA.Id, elementGenericReturnType.Id)); Assert.IsTrue(model.DoesRelationExist(elementInterfaceA.Id, elementGenericReturnTypeParameter.Id)); } + + [TestMethod] + public void TestAnalyzeDirectory() + { + AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); + analyzerSettings.Input.AssemblyDirectory = TestData.RootDirectory; + analyzerSettings.Transformation.IgnoredNames = new List(); + + TestAnalyze(analyzerSettings); + } + + [TestMethod] + public void TestAnalyzeDirectories() + { + AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); + analyzerSettings.Input.AssemblyDirectories = new List { TestData.RootDirectory }; + analyzerSettings.Transformation.IgnoredNames = new List(); + + TestAnalyze(analyzerSettings); + } + + [TestMethod] + public void TestIncludeAssemblyNames() + { + IDsiModel model; + DotNet.Analysis.Analyzer analyzer; + + AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); + analyzerSettings.Input.AssemblyDirectories = new List { TestData.RootDirectory }; + analyzerSettings.Transformation.IgnoredNames = new List(); + + analyzerSettings.Input.IncludeAssemblyNames = new List { @".*" }; + model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); + analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); + analyzer.Analyze(); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainClient")); + + analyzerSettings.Input.IncludeAssemblyNames = new List { @"Model", @"Dot[Nn]et\.Test\.(Data)?" }; + model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); + analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); + analyzer.Analyze(); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainClient")); + + analyzerSettings.Input.IncludeAssemblyNames = new List { @"Model" }; + model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); + analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); + analyzer.Analyze(); + Assert.IsNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainClient")); + } + + [TestMethod] + public void TestIncludedNames() + { + IDsiModel model; + DotNet.Analysis.Analyzer analyzer; + + AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); + analyzerSettings.Input.AssemblyDirectories = new List { TestData.RootDirectory }; + analyzerSettings.Transformation.IgnoredNames = new List(); + + analyzerSettings.Transformation.IncludedNames = new List { @"Model", @"Dot[Nn]et\.Test\.(Data)?.Main" }; + model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); + analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); + analyzer.Analyze(); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainClient")); + Assert.IsNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.BaseType")); + } + + [TestMethod] + public void TestIgnoredNames() + { + IDsiModel model; + DotNet.Analysis.Analyzer analyzer; + + AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); + analyzerSettings.Input.AssemblyDirectories = new List { TestData.RootDirectory }; + analyzerSettings.Transformation.IncludedNames = new List(); + + analyzerSettings.Transformation.IgnoredNames = new List { @"Model", @"/" }; + model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); + analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); + analyzer.Analyze(); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainClient")); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainType")); + Assert.IsNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainType/NestedType")); + } + [TestMethod] + public void TestIncludedIgnoredNames() + { + IDsiModel model; + DotNet.Analysis.Analyzer analyzer; + + AnalyzerSettings analyzerSettings = AnalyzerSettings.CreateDefault(); + analyzerSettings.Input.AssemblyDirectories = new List { TestData.RootDirectory }; + analyzerSettings.Transformation.IncludedNames = new List { @"Main" }; + + analyzerSettings.Transformation.IgnoredNames = new List { @"MainType$" }; + model = new DsiModel("Test", analyzerSettings.Transformation.IgnoredNames, Assembly.GetExecutingAssembly()); + analyzer = new DotNet.Analysis.Analyzer(model, analyzerSettings, null); + analyzer.Analyze(); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainClient")); + Assert.IsNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainType")); + Assert.IsNotNull(model.FindElementByName("DsmSuite.Analyzer.DotNet.Test.Data.MainType/NestedType")); + } } } diff --git a/DsmSuite.Analyzer.DotNet/Analysis/Analyzer.cs b/DsmSuite.Analyzer.DotNet/Analysis/Analyzer.cs index 0b50cfd..a19ae8c 100644 --- a/DsmSuite.Analyzer.DotNet/Analysis/Analyzer.cs +++ b/DsmSuite.Analyzer.DotNet/Analysis/Analyzer.cs @@ -37,8 +37,7 @@ private void FindAssemblies() { //For every configured assembly directory lookup the files in the //directory and process the content of the files - IList assemblyFolders = _analyzerSettings.Input.AssemblyDirectories; - foreach (string assemblyFolder in assemblyFolders) + foreach (string assemblyFolder in _analyzerSettings.AssemblyDirectories()) { Logger.LogUserMessage($"Assembly folder: {assemblyFolder}"); foreach (string assemblyFilename in Directory.EnumerateFiles(assemblyFolder)) @@ -82,12 +81,12 @@ private void FindRelations() private bool Accept(string name) { - /* Check wether the name meets the regex descriptions that have been configured. + /* Check whether the name meets the regex descriptions that have been configured. * If no regex descriptions have been configured then the file is accepted. */ bool accept = false; - if (_analyzerSettings.Input.IncludeAssemblyNames.Count > 0) + if (_analyzerSettings.Input.IncludeAssemblyNames?.Count > 0) { string fileNameWithoutExtension = ""; try diff --git a/DsmSuite.Analyzer.DotNet/Program.cs b/DsmSuite.Analyzer.DotNet/Program.cs index 16ebd0d..a83fa27 100644 --- a/DsmSuite.Analyzer.DotNet/Program.cs +++ b/DsmSuite.Analyzer.DotNet/Program.cs @@ -19,8 +19,7 @@ public ConsoleAction(AnalyzerSettings analyzerSettings) : base("Analyzing .Net c protected override bool CheckPrecondition() { bool result = true; - IList assemblyFolders = _analyzerSettings.Input.AssemblyDirectories; - foreach (string assemblyFolder in assemblyFolders) + foreach (string assemblyFolder in _analyzerSettings.AssemblyDirectories()) { if (!Directory.Exists(assemblyFolder)) { diff --git a/DsmSuite.Analyzer.DotNet/Settings/AnalyzerSettings.cs b/DsmSuite.Analyzer.DotNet/Settings/AnalyzerSettings.cs index 12b01f4..5a9baf8 100644 --- a/DsmSuite.Analyzer.DotNet/Settings/AnalyzerSettings.cs +++ b/DsmSuite.Analyzer.DotNet/Settings/AnalyzerSettings.cs @@ -1,12 +1,22 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Xml; using System.Xml.Serialization; using DsmSuite.Common.Util; namespace DsmSuite.Analyzer.DotNet.Settings { + /// + /// InputSettings determine which assemblies are analyzed. + /// Either a single directory can be specified + /// in AssemblyDirectory, or multiple directories in AssemblyDirectories. Setting both + /// AssemblyDirectory and AssemblyDirectories leads to undefined behaviour. + /// If IncludeAssemblyNames is non-empty, an assembly is only analyzed if its basename contains + /// a match for a regex in IncludeAssemblyNames; otherwise all assemblies are analyzed. + /// Note that regex matching is case-sensitive. + /// [Serializable] public class InputSettings { @@ -17,6 +27,16 @@ public class InputSettings public List IncludeAssemblyNames { get; set; } } + /// + /// TransformationSettings determine which symbols are included in the output model. + /// If IncludedNames is non-empty, symbols are only included if they contain a match for + /// a regex in this list. Otherwise all symbols are included. + /// If IgnoredNames is non-empty, symbols are not included if they contain a match for + /// a regex in this list. Otherwise all symbols are included. + /// IgnoredNames is evaluated after IncludedNames, so a symbol that matches both is ignored. + /// Note that regex matching is case-sensitive. + /// + /// todo Why both IgnoredNames and IncludedNames and why is one handled by DsiModel and the other by BinaryFile [Serializable] public class TransformationSettings { @@ -43,6 +63,17 @@ public class AnalyzerSettings public TransformationSettings Transformation { get; set; } public OutputSettings Output { get; set; } + /// + /// A convenience method that returns the configured assembly directory/ies. + /// + public IEnumerable AssemblyDirectories() + { + if (Input.AssemblyDirectories?.Count > 0) + return Input.AssemblyDirectories; + else + return Enumerable.Repeat(Input.AssemblyDirectory, 1); + } + public static AnalyzerSettings CreateDefault() { AnalyzerSettings analyzerSettings = new AnalyzerSettings @@ -100,6 +131,8 @@ public static AnalyzerSettings ReadFromFile(string filename) private void ResolvePaths(string settingFilePath) { Input.AssemblyDirectory = FilePath.ResolveFile(settingFilePath, Input.AssemblyDirectory); + for (int i = 0; i < (Input.AssemblyDirectories?.Count ?? 0); i++) + Input.AssemblyDirectories[i] = FilePath.ResolveFile(settingFilePath, Input.AssemblyDirectories[i]); Output.Filename = FilePath.ResolveFile(settingFilePath, Output.Filename); } } diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeNameActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeNameActionTest.cs index 5962f41..c4163a4 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeNameActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeNameActionTest.cs @@ -3,6 +3,7 @@ using Moq; using System.Collections.Generic; using DsmSuite.DsmViewer.Application.Actions.Element; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -59,8 +60,8 @@ public void WhenUndoActionThenElementNameIsRevertedDataModel() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementChangeNameAction action = new ElementChangeNameAction(args); + object[] args = { _model.Object, null, _data }; + ElementChangeNameAction action = Activator.CreateInstance(typeof(ElementChangeNameAction), args) as ElementChangeNameAction; Assert.AreEqual(3, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeParentActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeParentActionTest.cs index 1e3caca..e6cf36d 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeParentActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeParentActionTest.cs @@ -3,6 +3,7 @@ using Moq; using System.Collections.Generic; using DsmSuite.DsmViewer.Application.Actions.Element; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -24,7 +25,7 @@ public class ElementChangeParentActionTest private const int NewIndex = 5; private const string OldName = "OldName"; - private const string NewName = "NewName"; + private const string NewName = "OldName (duplicate)"; [TestInitialize()] public void Setup() @@ -48,17 +49,17 @@ public void Setup() _data = new Dictionary { ["element"] = ElementId.ToString(), - ["old"] = OldParentId.ToString(), + ["oldParent"] = OldParentId.ToString(), ["oldIndex"] = OldIndex.ToString(), ["oldName"] = OldName, - ["new"] = NewParentId.ToString(), + ["newParent"] = NewParentId.ToString(), ["newIndex"] = NewIndex.ToString(), ["newName"] = NewName, }; } [TestMethod] - public void WhenDoActionThenElementParentIsChangedDataModel() + public void WhenDoActionThenElementParentIsChangedDataModelNoNameChange() { _newParent.Setup(x => x.ContainsChildWithName(OldName)).Returns(false); @@ -68,6 +69,21 @@ public void WhenDoActionThenElementParentIsChangedDataModel() Assert.IsTrue(action.IsValid()); _model.Verify(x => x.ChangeElementParent(_element.Object, _newParent.Object, NewIndex), Times.Once()); + _model.Verify(x => x.ChangeElementName(It.IsAny(), It.IsAny()), Times.Never()); + } + + [TestMethod] + public void WhenDoActionThenElementParentIsChangedDataModelNameChange() + { + _newParent.Setup(x => x.ContainsChildWithName(OldName)).Returns(true); + + ElementChangeParentAction action = + new ElementChangeParentAction(_model.Object, _element.Object, _newParent.Object, NewIndex); + action.Do(); + Assert.IsTrue(action.IsValid()); + + _model.Verify(x => x.ChangeElementParent(_element.Object, _newParent.Object, NewIndex), Times.Once()); + _model.Verify(x => x.ChangeElementName(_element.Object, NewName), Times.Once()); } [TestMethod] @@ -81,34 +97,45 @@ public void WhenUndoActionThenElementParentIsRevertedDataModelNoNameChange() Assert.IsTrue(action.IsValid()); _model.Verify(x => x.ChangeElementParent(_element.Object, _oldParent.Object, OldIndex), Times.Once()); + _model.Verify(x => x.ChangeElementName(It.IsAny(), It.IsAny()), Times.Never()); } [TestMethod] public void WhenUndoActionThenElementParentIsRevertedDataModelNameChange() { - Assert.Inconclusive("To be implemented"); + string elementName = null; + + _newParent.Setup(x => x.ContainsChildWithName(OldName)).Returns(true); + _model.Setup(x => x.ChangeElementName(It.IsAny(), It.IsAny())) + .Callback((IDsmElement e, string name) => { elementName = name; }); + + ElementChangeParentAction action = + new ElementChangeParentAction(_model.Object, _element.Object, _newParent.Object, NewIndex); + // Do saves state in Data so we cannot test Undo() without calling Do + action.Do(); + action.Undo(); + Assert.IsTrue(action.IsValid()); + + _model.Verify(x => x.ChangeElementParent(_element.Object, _oldParent.Object, OldIndex), Times.Once()); + _model.Verify(x => x.ChangeElementName(_element.Object, OldName), Times.Once()); + Assert.AreEqual(elementName, OldName); } [TestMethod] - public void GivenLoadedActionWhenGettingDataThenActionAttributesMatchNoNameChange() + public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = {_model.Object, _data}; - ElementChangeParentAction action = new ElementChangeParentAction(args); + object[] args = {_model.Object, null, _data}; + ElementChangeParentAction action = + Activator.CreateInstance(typeof(ElementChangeParentAction), args) as ElementChangeParentAction; Assert.AreEqual(7, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); - Assert.AreEqual(OldParentId.ToString(), _data["old"]); + Assert.AreEqual(OldParentId.ToString(), _data["oldParent"]); Assert.AreEqual(OldIndex.ToString(), _data["oldIndex"]); Assert.AreEqual(OldName, _data["oldName"]); - Assert.AreEqual(NewParentId.ToString(), _data["new"]); + Assert.AreEqual(NewParentId.ToString(), _data["newParent"]); Assert.AreEqual(NewIndex.ToString(), _data["newIndex"]); Assert.AreEqual(NewName, _data["newName"]); } - - [TestMethod] - public void GivenLoadedActionWhenGettingDataThenActionAttributesMatchNameChange() - { - Assert.Inconclusive("To be implemented"); - } } } diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeTypeActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeTypeActionTest.cs index c7dac76..4137f74 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeTypeActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementChangeTypeActionTest.cs @@ -3,6 +3,7 @@ using Moq; using System.Collections.Generic; using DsmSuite.DsmViewer.Application.Actions.Element; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -59,8 +60,9 @@ public void WhenUndoActionThenElementTypeIsRevertedDataModel() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementChangeTypeAction action = new ElementChangeTypeAction(args); + object[] args = { _model.Object, null, _data }; + ElementChangeTypeAction action = + Activator.CreateInstance(typeof(ElementChangeTypeAction), args) as ElementChangeTypeAction; Assert.AreEqual(3, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementCreateActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementCreateActionTest.cs index 1e3f1d1..a2160e0 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementCreateActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementCreateActionTest.cs @@ -3,6 +3,7 @@ using Moq; using System.Collections.Generic; using DsmSuite.DsmViewer.Application.Actions.Element; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -58,8 +59,8 @@ public void WhenDoActionThenElementIsAddedToDataModel() [TestMethod] public void WhenUndoActionThenElementIsRemovedFromDataModel() { - object[] args = { _model.Object, _data }; - ElementCreateAction action = new ElementCreateAction(args); + object[] args = { _model.Object, null, _data }; + ElementCreateAction action = Activator.CreateInstance(typeof(ElementCreateAction), args) as ElementCreateAction; action.Undo(); Assert.IsTrue(action.IsValid()); @@ -69,8 +70,8 @@ public void WhenUndoActionThenElementIsRemovedFromDataModel() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementCreateAction action = new ElementCreateAction(args); + object[] args = { _model.Object, null, _data }; + ElementCreateAction action = Activator.CreateInstance(typeof(ElementCreateAction), args) as ElementCreateAction; Assert.AreEqual(5, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementDeleteActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementDeleteActionTest.cs index 07ce4ed..343c3ca 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementDeleteActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementDeleteActionTest.cs @@ -3,6 +3,7 @@ using DsmSuite.DsmViewer.Model.Interfaces; using DsmSuite.DsmViewer.Application.Actions.Element; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -53,8 +54,9 @@ public void WhenUndoActionThenElementIsRestoredInDataModel() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementDeleteAction action = new ElementDeleteAction(args); + object[] args = { _model.Object, null, _data }; + ElementDeleteAction action = + Activator.CreateInstance(typeof(ElementDeleteAction), args) as ElementDeleteAction; Assert.AreEqual(1, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveDownActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveDownActionTest.cs index 8722832..57d0bab 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveDownActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveDownActionTest.cs @@ -3,6 +3,7 @@ using Moq; using DsmSuite.DsmViewer.Application.Actions.Element; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -59,8 +60,8 @@ public void WhenUndoActionThenElementIsRestoredInDataModel() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementMoveDownAction action = new ElementMoveDownAction(args); + object[] args = { _model.Object, null, _data }; + ElementMoveDownAction action = Activator.CreateInstance(typeof(ElementMoveDownAction), args) as ElementMoveDownAction; Assert.AreEqual(1, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveUpActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveUpActionTest.cs index 2959d82..dad3861 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveUpActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementMoveUpActionTest.cs @@ -3,6 +3,7 @@ using Moq; using DsmSuite.DsmViewer.Application.Actions.Element; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -59,8 +60,8 @@ public void WhenUndoActionThenElementIsRestoredInDataModel() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementMoveUpAction action = new ElementMoveUpAction(args); + object[] args = { _model.Object, null, _data }; + ElementMoveUpAction action = Activator.CreateInstance(typeof(ElementMoveUpAction), args) as ElementMoveUpAction; Assert.AreEqual(1, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementSortActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementSortActionTest.cs index ad02231..c423e66 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementSortActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementSortActionTest.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using DsmSuite.DsmViewer.Application.Sorting; using DsmSuite.DsmViewer.Application.Test.Stubs; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Element { @@ -53,8 +54,8 @@ public void WhenDoActionThenElementsChildrenAreSorted() [TestMethod] public void WhenUndoActionThenElementsChildrenAreSortIsReverted() { - object[] args = { _model.Object, _data }; - ElementSortAction action = new ElementSortAction(args); + object[] args = { _model.Object, null, _data }; + ElementSortAction action = Activator.CreateInstance(typeof(ElementSortAction), args) as ElementSortAction; action.Undo(); Assert.IsTrue(action.IsValid()); @@ -64,8 +65,8 @@ public void WhenUndoActionThenElementsChildrenAreSortIsReverted() [TestMethod] public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { - object[] args = { _model.Object, _data }; - ElementSortAction action = new ElementSortAction(args); + object[] args = { _model.Object, null, _data }; + ElementSortAction action = Activator.CreateInstance(typeof(ElementSortAction), args) as ElementSortAction; Assert.AreEqual(3, action.Data.Count); Assert.AreEqual(ElementId.ToString(), _data["element"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeTypeActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeTypeActionTest.cs index 51cfb85..65cbabe 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeTypeActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeTypeActionTest.cs @@ -3,6 +3,7 @@ using Moq; using DsmSuite.DsmViewer.Application.Actions.Relation; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Relation { @@ -68,8 +69,9 @@ public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { _model.Setup(x => x.GetRelationById(RelationId)).Returns(_relation.Object); - object[] args = { _model.Object, _data }; - RelationChangeTypeAction action = new RelationChangeTypeAction(args); + object[] args = { _model.Object, null, _data }; + RelationChangeTypeAction action = + Activator.CreateInstance(typeof(RelationChangeTypeAction), args) as RelationChangeTypeAction; Assert.AreEqual(3, action.Data.Count); Assert.AreEqual(RelationId.ToString(), _data["relation"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeWeightActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeWeightActionTest.cs index 8052ea5..7e76d34 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeWeightActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationChangeWeightActionTest.cs @@ -3,6 +3,7 @@ using Moq; using DsmSuite.DsmViewer.Application.Actions.Relation; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Relation { @@ -68,8 +69,9 @@ public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { _model.Setup(x => x.GetRelationById(RelationId)).Returns(_relation.Object); - object[] args = { _model.Object, _data }; - RelationChangeWeightAction action = new RelationChangeWeightAction(args); + object[] args = { _model.Object, null, _data }; + RelationChangeWeightAction action = + Activator.CreateInstance(typeof(RelationChangeWeightAction), args) as RelationChangeWeightAction; Assert.AreEqual(3, action.Data.Count); Assert.AreEqual(RelationId.ToString(), _data["relation"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationCreateActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationCreateActionTest.cs index 3ab8eb6..f363a17 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationCreateActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationCreateActionTest.cs @@ -3,6 +3,7 @@ using Moq; using DsmSuite.DsmViewer.Model.Interfaces; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Relation { @@ -63,8 +64,8 @@ public void WhenUndoActionThenRelationIsRemovedFromDataModel() { _model.Setup(x => x.GetRelationById(RelationId)).Returns(_relation.Object); - object[] args = { _model.Object, _data }; - RelationCreateAction action = new RelationCreateAction(args); + object[] args = { _model.Object, null, _data }; + RelationCreateAction action = Activator.CreateInstance(typeof(RelationCreateAction), args) as RelationCreateAction; action.Undo(); _model.Verify(x => x.RemoveRelation(RelationId), Times.Once()); @@ -75,8 +76,8 @@ public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { _model.Setup(x => x.GetRelationById(RelationId)).Returns(_relation.Object); - object[] args = { _model.Object, _data }; - RelationCreateAction action = new RelationCreateAction(args); + object[] args = { _model.Object, null, _data }; + RelationCreateAction action = Activator.CreateInstance(typeof(RelationCreateAction), args) as RelationCreateAction; Assert.AreEqual(5, action.Data.Count); Assert.AreEqual(RelationId.ToString(), _data["relation"]); diff --git a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationDeleteActionTest.cs b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationDeleteActionTest.cs index 46ee252..2c3b09e 100644 --- a/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationDeleteActionTest.cs +++ b/DsmSuite.DsmViewer.Application.Test/Actions/Relation/RelationDeleteActionTest.cs @@ -3,6 +3,7 @@ using Moq; using DsmSuite.DsmViewer.Application.Actions.Relation; using System.Collections.Generic; +using System; namespace DsmSuite.DsmViewer.Application.Test.Actions.Relation { @@ -63,8 +64,8 @@ public void GivenLoadedActionWhenGettingDataThenActionAttributesMatch() { _model.Setup(x => x.GetDeletedRelationById(RelationId)).Returns(_relation.Object); - object[] args = { _model.Object, _data }; - RelationDeleteAction action = new RelationDeleteAction(args); + object[] args = { _model.Object, null, _data }; + RelationDeleteAction action = Activator.CreateInstance(typeof(RelationDeleteAction), args) as RelationDeleteAction; Assert.AreEqual(1, action.Data.Count); Assert.AreEqual(RelationId.ToString(), _data["relation"]); diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeNameAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeNameAction.cs index 8fdb874..4c70cdf 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeNameAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeNameAction.cs @@ -16,25 +16,21 @@ public class ElementChangeNameAction : IAction public const ActionType RegisteredType = ActionType.ElementChangeName; - public ElementChangeNameAction(object[] args) + public ElementChangeNameAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _element = attributes.GetElement(nameof(_element)); - _old = attributes.GetString(nameof(_old)); - _new = attributes.GetString(nameof(_new)); - } + _element = attributes.GetElement(nameof(_element)); + _old = attributes.GetString(nameof(_old)); + _new = attributes.GetString(nameof(_new)); } } + public ElementChangeNameAction(IDsmModel model, IDsmElement element, string name) { _model = model; diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeParentAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeParentAction.cs index dcb6b8c..a105bb5 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeParentAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeParentAction.cs @@ -20,26 +20,21 @@ public class ElementChangeParentAction : IAction public const ActionType RegisteredType = ActionType.ElementChangeParent; - public ElementChangeParentAction(object[] args) + public ElementChangeParentAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _element = attributes.GetElement(nameof(_element)); - _oldParent = attributes.GetElement(nameof(_oldParent)); - _oldIndex = attributes.GetInt(nameof(_oldIndex)); - _oldName = attributes.GetString(nameof(_oldName)); - _newParent = attributes.GetElement(nameof(_newParent)); - _newIndex = attributes.GetInt(nameof(_newIndex)); - _newName = attributes.GetString(nameof(_newName)); - } + _element = attributes.GetElement(nameof(_element)); + _oldParent = attributes.GetElement(nameof(_oldParent)); + _oldIndex = attributes.GetInt(nameof(_oldIndex)); + _oldName = attributes.GetString(nameof(_oldName)); + _newParent = attributes.GetElement(nameof(_newParent)); + _newIndex = attributes.GetInt(nameof(_newIndex)); + _newName = attributes.GetString(nameof(_newName)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeTypeAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeTypeAction.cs index 948e43f..42567b5 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeTypeAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementChangeTypeAction.cs @@ -16,24 +16,19 @@ public class ElementChangeTypeAction : IAction public const ActionType RegisteredType = ActionType.ElementChangeType; - public ElementChangeTypeAction(object[] args) + public ElementChangeTypeAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _element = attributes.GetElement(nameof(_element)); - _old = attributes.GetString(nameof(_old)); - _new = attributes.GetString(nameof(_new)); - } + _element = attributes.GetElement(nameof(_element)); + _old = attributes.GetString(nameof(_old)); + _new = attributes.GetString(nameof(_new)); } - } + } public ElementChangeTypeAction(IDsmModel model, IDsmElement element, string type) { diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementCreateAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementCreateAction.cs index e082dfe..5dcf341 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementCreateAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementCreateAction.cs @@ -19,29 +19,24 @@ public class ElementCreateAction : IAction public const ActionType RegisteredType = ActionType.ElementCreate; - public ElementCreateAction(object[] args) + public ElementCreateAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _element = attributes.GetElement(nameof(_element)); - _name = attributes.GetString(nameof(_name)); - _type = attributes.GetString(nameof(_type)); + _element = attributes.GetElement(nameof(_element)); + _name = attributes.GetString(nameof(_name)); + _type = attributes.GetString(nameof(_type)); - int? parentId = attributes.GetNullableInt(nameof(_parent)); - if (parentId.HasValue) - { - _parent = _model.GetElementById(parentId.Value); - } - _index = attributes.GetInt(nameof(_index)); + int? parentId = attributes.GetNullableInt(nameof(_parent)); + if (parentId.HasValue) + { + _parent = _model.GetElementById(parentId.Value); } + _index = attributes.GetInt(nameof(_index)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementDeleteAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementDeleteAction.cs index 6ca1f54..89a72ae 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementDeleteAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementDeleteAction.cs @@ -14,20 +14,15 @@ public class ElementDeleteAction : IAction public const ActionType RegisteredType = ActionType.ElementDelete; - public ElementDeleteAction(object[] args) + public ElementDeleteAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _element = attributes.GetElement(nameof(_element)); - } + _element = attributes.GetElement(nameof(_element)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveDownAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveDownAction.cs index ab43eda..8a78398 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveDownAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveDownAction.cs @@ -14,21 +14,12 @@ public class ElementMoveDownAction : IAction public const ActionType RegisteredType = ActionType.ElementMoveDown; - public ElementMoveDownAction(object[] args) + public ElementMoveDownAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) - { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - - _element = attributes.GetElement(nameof(_element)); - } - } + _model = model; + _actionContext = context; + if (_model != null && data != null) + _element = new ActionReadOnlyAttributes(_model, data).GetElement(nameof(_element)); } public ElementMoveDownAction(IDsmModel model, IDsmElement element) diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveUpAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveUpAction.cs index b05b9ae..878d27c 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveUpAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementMoveUpAction.cs @@ -14,20 +14,12 @@ public class ElementMoveUpAction : IAction public const ActionType RegisteredType = ActionType.ElementMoveUp; - public ElementMoveUpAction(object[] args) + public ElementMoveUpAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) - { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - - _element = attributes.GetElement(nameof(_element)); - } - } + _model = model; + _actionContext = context; + if (_model != null && data != null) + _element = new ActionReadOnlyAttributes(_model, data).GetElement(nameof(_element)); } public ElementMoveUpAction(IDsmModel model, IDsmElement element) diff --git a/DsmSuite.DsmViewer.Application/Actions/Element/ElementSortAction.cs b/DsmSuite.DsmViewer.Application/Actions/Element/ElementSortAction.cs index 59c2f3e..733c169 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Element/ElementSortAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Element/ElementSortAction.cs @@ -17,22 +17,17 @@ public class ElementSortAction : IAction public const ActionType RegisteredType = ActionType.ElementSort; - public ElementSortAction(object[] args) + public ElementSortAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _element = attributes.GetElement(nameof(_element)); - _algorithm = attributes.GetString(nameof(_algorithm)); - _order = attributes.GetString(nameof(_order)); - } + _element = attributes.GetElement(nameof(_element)); + _algorithm = attributes.GetString(nameof(_algorithm)); + _order = attributes.GetString(nameof(_order)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeTypeAction.cs b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeTypeAction.cs index 16478f1..f11a79e 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeTypeAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeTypeAction.cs @@ -18,24 +18,19 @@ public class RelationChangeTypeAction : IAction public const ActionType RegisteredType = ActionType.RelationChangeType; - public RelationChangeTypeAction(object[] args) + public RelationChangeTypeAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _relation = attributes.GetRelation(nameof(_relation)); - _consumer = attributes.GetRelationConsumer(nameof(_relation)); - _provider = attributes.GetRelationProvider(nameof(_relation)); - _old = attributes.GetString(nameof(_old)); - _new = attributes.GetString(nameof(_new)); - } + _relation = attributes.GetRelation(nameof(_relation)); + _consumer = attributes.GetRelationConsumer(nameof(_relation)); + _provider = attributes.GetRelationProvider(nameof(_relation)); + _old = attributes.GetString(nameof(_old)); + _new = attributes.GetString(nameof(_new)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeWeightAction.cs b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeWeightAction.cs index 573682a..e95745a 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeWeightAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationChangeWeightAction.cs @@ -18,24 +18,19 @@ public class RelationChangeWeightAction : IAction public const ActionType RegisteredType = ActionType.RelationChangeWeight; - public RelationChangeWeightAction(object[] args) + public RelationChangeWeightAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _relation = attributes.GetRelation(nameof(_relation)); - _consumer = attributes.GetRelationConsumer(nameof(_relation)); - _provider = attributes.GetRelationProvider(nameof(_relation)); - _old = attributes.GetInt(nameof(_old)); - _new = attributes.GetInt(nameof(_new)); - } + _relation = attributes.GetRelation(nameof(_relation)); + _consumer = attributes.GetRelationConsumer(nameof(_relation)); + _provider = attributes.GetRelationProvider(nameof(_relation)); + _old = attributes.GetInt(nameof(_old)); + _new = attributes.GetInt(nameof(_new)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationCreateAction.cs b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationCreateAction.cs index 5b04f56..73c51f9 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationCreateAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationCreateAction.cs @@ -18,24 +18,19 @@ public class RelationCreateAction : IAction public const ActionType RegisteredType = ActionType.RelationCreate; - public RelationCreateAction(object[] args) + public RelationCreateAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _relation = attributes.GetRelation(nameof(_relation)); - _consumer = attributes.GetRelationConsumer(nameof(_relation)); - _provider = attributes.GetRelationProvider(nameof(_relation)); - _type = attributes.GetString(nameof(_type)); - _weight = attributes.GetInt(nameof(_weight)); - } + _relation = attributes.GetRelation(nameof(_relation)); + _consumer = attributes.GetRelationConsumer(nameof(_relation)); + _provider = attributes.GetRelationProvider(nameof(_relation)); + _type = attributes.GetString(nameof(_type)); + _weight = attributes.GetInt(nameof(_weight)); } } diff --git a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationDeleteAction.cs b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationDeleteAction.cs index 597371e..a3c43a4 100644 --- a/DsmSuite.DsmViewer.Application/Actions/Relation/RelationDeleteAction.cs +++ b/DsmSuite.DsmViewer.Application/Actions/Relation/RelationDeleteAction.cs @@ -16,22 +16,17 @@ public class RelationDeleteAction : IAction public const ActionType RegisteredType = ActionType.RelationDelete; - public RelationDeleteAction(object[] args) + public RelationDeleteAction(IDsmModel model, IActionContext context, IReadOnlyDictionary data) { - if (args.Length == 3) + _model = model; + _actionContext = context; + if (_model != null && data != null) { - _model = args[0] as IDsmModel; - _actionContext = args[1] as IActionContext; - IReadOnlyDictionary data = args[2] as IReadOnlyDictionary; - - if ((_model != null) && (data != null)) - { - ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); + ActionReadOnlyAttributes attributes = new ActionReadOnlyAttributes(_model, data); - _relation = attributes.GetRelation(nameof(_relation)); - _consumer = attributes.GetRelationConsumer(nameof(_relation)); - _provider = attributes.GetRelationProvider(nameof(_relation)); - } + _relation = attributes.GetRelation(nameof(_relation)); + _consumer = attributes.GetRelationConsumer(nameof(_relation)); + _provider = attributes.GetRelationProvider(nameof(_relation)); } } diff --git a/DsmSuite.DsmViewer.Application/Interfaces/IAction.cs b/DsmSuite.DsmViewer.Application/Interfaces/IAction.cs index a05a64b..af9dc4c 100644 --- a/DsmSuite.DsmViewer.Application/Interfaces/IAction.cs +++ b/DsmSuite.DsmViewer.Application/Interfaces/IAction.cs @@ -2,17 +2,61 @@ namespace DsmSuite.DsmViewer.Application.Interfaces { + /// + /// An action the user can perform in the application. + /// Actions execute on the data in the property. This data is injected + /// into the constructor.
+ /// When the model is saved, the property is saved with the action.
+ /// When loading the model again, the action is re-created using a + /// constructor(IDsmModel m, IActionContext c, IReadOnlyDictionary<string,string> data). + ///
public interface IAction { + /// + /// The unique type for this action. + /// + /// todo only used by ActionStore for impex. Implement locally there and remove here. ActionType Type { get; } + + /// + /// String that identifies this action in menus and action lists (e.g. undo/redo). + /// This must be the same for all instances of the action. + /// string Title { get; } + + /// + /// String that describes the details of this particular action for informative purposes + /// (e.g. in undo/redo). + /// This usually returns a different text for different instances of the action, based + /// on the contents of . + /// string Description { get; } + /// + /// Perform this action using the data stored in Data.
+ /// This usually returns null, except for actions that are expected by the application to + /// return some useful object, like create actions. + ///
+ /// Some relevant object or null object Do(); + + /// + /// Undo the result of Do(). + /// void Undo(); + /// + /// Return true iff this action has all the data it needs to be done/undone. + /// bool IsValid(); + /// + /// A key->value map of the data this action needs to be performed, injected into the + /// constructor. + /// The implementing class can determine the contents and format of Data itself, but + /// ActionAttributes and ActionReadOnlyAttributes provide convenience + /// functions for this. + /// IReadOnlyDictionary Data { get; } } } diff --git a/DsmSuite.DsmViewer.Model.Test/Core/DsmElementTest.cs b/DsmSuite.DsmViewer.Model.Test/Core/DsmElementTest.cs index 4ed6a20..b3d33f2 100644 --- a/DsmSuite.DsmViewer.Model.Test/Core/DsmElementTest.cs +++ b/DsmSuite.DsmViewer.Model.Test/Core/DsmElementTest.cs @@ -195,19 +195,74 @@ public void TestBuildElementHierarchyUsingInsertChildAtIndex() [TestMethod] public void TestContainChildWithName() { - Assert.Inconclusive("To be implemented"); + string childName = "the name"; + + int parentId = 1; + string parentName = "parent"; + DsmElement parent = new DsmElement(parentId, parentName, "", null); + Assert.IsFalse(parent.ContainsChildWithName(childName)); + + DsmElement child1 = new DsmElement(10, "Child 1", "", null); + parent.InsertChildAtEnd(child1); + Assert.IsFalse(parent.ContainsChildWithName(childName)); + + DsmElement child2 = new DsmElement(11, "Child 2", "", null); + parent.InsertChildAtEnd(child2); + Assert.IsFalse(parent.ContainsChildWithName(childName)); + + DsmElement theChild = new DsmElement(2, childName, "", null); + parent.InsertChildAtEnd(theChild); + Assert.IsTrue(parent.ContainsChildWithName(childName)); + Assert.IsFalse(parent.ContainsChildWithName(childName.ToUpper())); + Assert.IsFalse(parent.ContainsChildWithName("the náme")); } [TestMethod] public void TestInsertElementIndexInRange() { - Assert.Inconclusive("To be implemented"); + int parentId = 1; + string parentName = "parent"; + DsmElement parent = new DsmElement(parentId, parentName, "", null); + Assert.AreEqual(0, parent.Children.Count); + + DsmElement child1 = new DsmElement(10, "child 1", "", null); + parent.InsertChildAtIndex(child1, 0); + Assert.AreEqual(1, parent.Children.Count); + Assert.AreEqual(parent, child1.Parent); + + DsmElement child2 = new DsmElement(11, "child 2", "", null); + parent.InsertChildAtIndex(child2, 0); + Assert.AreEqual(2, parent.Children.Count); + Assert.AreEqual(parent, child2.Parent); + + DsmElement child3 = new DsmElement(12, "child 3", "", null); + parent.InsertChildAtIndex(child3, 2); + Assert.AreEqual(3, parent.Children.Count); + Assert.AreEqual(parent, child3.Parent); } [TestMethod] public void TestInsertElementOutOfRange() { - Assert.Inconclusive("To be implemented"); + int parentId = 1; + string parentName = "parent"; + DsmElement parent = new DsmElement(parentId, parentName, "", null); + Assert.AreEqual(0, parent.Children.Count); + + DsmElement child1 = new DsmElement(10, "child 1", "", null); + parent.InsertChildAtIndex(child1, 1); + Assert.AreEqual(1, parent.Children.Count); + Assert.AreEqual(parent, child1.Parent); + + DsmElement child2 = new DsmElement(11, "child 2", "", null); + parent.InsertChildAtIndex(child2, 1000*1000); + Assert.AreEqual(2, parent.Children.Count); + Assert.AreEqual(parent, child2.Parent); + + DsmElement child3 = new DsmElement(12, "child 3", "", null); + parent.InsertChildAtIndex(child3, -1); + Assert.AreEqual(3, parent.Children.Count); + Assert.AreEqual(parent, child3.Parent); } } }