Skip to content

Commit 47a7122

Browse files
committed
Moved int-string conversion from SortResult to ActionAttributes
1 parent f658c21 commit 47a7122

File tree

10 files changed

+127
-74
lines changed

10 files changed

+127
-74
lines changed

DsmSuite.DsmViewer.Application.Test/Actions/Base/ActionAttributesTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,24 @@ public void WhenSetNullableIntIsCalledWitMultipleValuesThenAllValuesAreInDiction
8989
Assert.IsTrue(data.ContainsKey(key2));
9090
Assert.AreEqual(memberValue2.ToString(), data[key2]);
9191
}
92+
93+
[TestMethod]
94+
public void SetListIntTest()
95+
{
96+
ActionAttributes atts = new ActionAttributes();
97+
atts.SetListInt("_empty", new List<int> { } );
98+
atts.SetListInt("_single", new List<int> {0});
99+
atts.SetListInt("_five", new List<int> {1,2,3,4,5});
100+
atts.SetListInt("_dups", new List<int> {5,5,5,5});
101+
atts.SetListInt("_many", new List<int> {int.MaxValue,1,0,-1,int.MinValue});
102+
103+
IReadOnlyDictionary<string, string> data = atts.Data;
104+
Assert.AreEqual(5, data.Count);
105+
Assert.AreEqual(atts.Data["empty"], "");
106+
Assert.AreEqual(atts.Data["single"], "0");
107+
Assert.AreEqual(atts.Data["five"], "1,2,3,4,5");
108+
Assert.AreEqual(atts.Data["dups"], "5,5,5,5");
109+
Assert.AreEqual(atts.Data["many"], "2147483647,1,0,-1,-2147483648");
110+
}
92111
}
93112
}

DsmSuite.DsmViewer.Application.Test/Actions/Base/ActionReadOnlyAttributesTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,26 @@ public void GivenMultipleValuesInDictionaryWhenGetValueIsCalledThenAllAreReturne
8888
Assert.AreEqual(memberValue1, attributes.GetString(memberName1));
8989
Assert.AreEqual(memberValue2, attributes.GetInt(memberName2));
9090
}
91+
92+
[TestMethod]
93+
public void GetListIntTest()
94+
{
95+
Mock<IDsmModel> model = new Mock<IDsmModel>();
96+
Dictionary<string, string> data = new Dictionary<string, string>();
97+
98+
data["empty"] = "";
99+
data["single"] = "1";
100+
data["five"] = "1,2,3,4,5";
101+
data["dups"] = "5,5,5,5";
102+
data["many"] = "2147483647,1,0,-1,-2147483648";
103+
ActionReadOnlyAttributes atts = new ActionReadOnlyAttributes(model.Object, data);
104+
CollectionAssert.AreEqual(new List<int>(), atts.GetListInt("_empty"));
105+
CollectionAssert.AreEqual(new List<int>() {1}, atts.GetListInt("_single"));
106+
CollectionAssert.AreEqual(new List<int>() {1,2,3,4,5}, atts.GetListInt("_five"));
107+
CollectionAssert.AreEqual(new List<int>() {5,5,5,5}, atts.GetListInt("_dups"));
108+
CollectionAssert.AreEqual(new List<int>() {int.MaxValue,1,0,-1,int.MinValue}, atts.GetListInt("_many"));
109+
110+
111+
}
91112
}
92113
}

DsmSuite.DsmViewer.Application.Test/Actions/Element/ElementSortActionTest.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using DsmSuite.DsmViewer.Application.Sorting;
77
using DsmSuite.DsmViewer.Application.Test.Stubs;
88
using System;
9+
using System.Linq;
910

1011
namespace DsmSuite.DsmViewer.Application.Test.Actions.Element
1112
{
@@ -20,7 +21,8 @@ public class ElementSortActionTest
2021
private const int ElementId = 1;
2122
private const string UsedAlgorithm = "MockedAlgorithm";
2223
private const string SortResult = "2,0,1";
23-
private const string InverseSortResult = "1,2,0";
24+
private List<int> SortList = new List<int>() {2, 0, 1};
25+
private List<int> InverseSortList = new List<int>() {1, 2, 0};
2426

2527
[TestInitialize()]
2628
public void Setup()
@@ -48,7 +50,8 @@ public void WhenDoActionThenElementsChildrenAreSorted()
4850
action.Do();
4951
Assert.IsTrue(action.IsValid());
5052

51-
_model.Verify(x => x.ReorderChildren(_element.Object, It.Is<SortResult>(i => i.Data == SortResult)), Times.Once());
53+
_model.Verify(x => x.ReorderChildren(_element.Object,
54+
It.Is<SortResult>(i => i.GetOrder().SequenceEqual(SortList))), Times.Once());
5255
}
5356

5457
[TestMethod]
@@ -59,7 +62,8 @@ public void WhenUndoActionThenElementsChildrenAreSortIsReverted()
5962
action.Undo();
6063
Assert.IsTrue(action.IsValid());
6164

62-
_model.Verify(x => x.ReorderChildren(_element.Object, It.Is<SortResult>(i => i.Data == InverseSortResult)), Times.Once());
65+
_model.Verify(x => x.ReorderChildren(_element.Object,
66+
It.Is<SortResult>(i => i.GetOrder().SequenceEqual(InverseSortList))), Times.Once());
6367
}
6468

6569
[TestMethod]

DsmSuite.DsmViewer.Application.Test/Algorithm/AlphabeticalSortAlgorithmTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void GivenNonSortedElementLIstWhenSortedAlphabeticallyThemElementsAreInAl
3939
object[] args = { model.Object, parent.Object };
4040
AlphabeticalSortAlgorithm algorithm = new AlphabeticalSortAlgorithm(args);
4141
SortResult result = algorithm.Sort();
42-
Assert.AreEqual("2,3,0,1", result.Data);
42+
CollectionAssert.AreEqual(new List<int>() { 2, 3, 0, 1 }, result.GetOrder());
4343

4444
Assert.AreEqual(a.Object, children[result.GetIndex(0)]);
4545
Assert.AreEqual(b.Object, children[result.GetIndex(1)]);

DsmSuite.DsmViewer.Application.Test/Algorithm/SortResultTest.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using DsmSuite.DsmViewer.Application.Sorting;
4+
using System.Collections.Generic;
45

56
namespace DsmSuite.DsmViewer.Application.Test.Algorithm
67
{
@@ -26,57 +27,49 @@ public void WhenSortResultConstructedWithNonZeroSizeThenItIsValid()
2627
}
2728

2829
[TestMethod]
29-
public void WhenSortResultConstructedWithEmptyStringThenOrderItIsInvalid()
30+
public void WhenSortResultConstructedWithEmptyListThenItIsInvalid()
3031
{
31-
SortResult result = new SortResult("");
32-
Assert.IsFalse(result.IsValid);
33-
}
34-
35-
[TestMethod]
36-
public void WhenSortResultConstructedWithTextStringThenItIsInvalid()
37-
{
38-
SortResult result = new SortResult("abc");
32+
SortResult result = new SortResult(new List<int>());
3933
Assert.IsFalse(result.IsValid);
4034
}
4135

4236
[TestMethod]
4337
public void WhenSortResultConstructedWithSingleNumberStringThenItIsValid()
4438
{
45-
string input = "0";
46-
SortResult result = new SortResult(input);
39+
SortResult result = new SortResult( new List<int> {0} );
4740
Assert.IsTrue(result.IsValid);
4841
Assert.AreEqual(0, result.GetIndex(0));
4942
}
5043

5144
[TestMethod]
52-
public void WhenSortResultConstructedWithInvalidCommaSeparatedNumberStringThenItIsInvalid()
45+
public void WhenSortResultConstructedWithInvalidListThenItIsInvalid()
5346
{
54-
SortResult result = new SortResult("3,1,0");
47+
SortResult result = new SortResult( new List<int>() {3, 1, 0} );
5548
Assert.IsFalse(result.IsValid);
5649
}
5750

5851
[TestMethod]
59-
public void WhenSortResultConstructedWithCommaSeparatedNumberStringThenItIsValid()
52+
public void WhenSortResultConstructedWithListThenItIsValid()
6053
{
61-
SortResult result = new SortResult("2,1,0");
54+
SortResult result = new SortResult( new List<int>() {2, 1, 0} );
6255
Assert.IsTrue(result.IsValid);
6356
Assert.AreEqual(2, result.GetIndex(0));
6457
Assert.AreEqual(1, result.GetIndex(1));
6558
Assert.AreEqual(0, result.GetIndex(2));
6659
}
67-
60+
6861
[TestMethod]
69-
public void WhenSortResultConstructedWithCommaSeparatedNumberStringThenDataReturnsSameString()
62+
public void SortResultConstructedWithList()
7063
{
71-
string input = "3,2,1,0";
64+
List<int> input = new List<int>() {3, 2, 1, 0};
7265
SortResult result = new SortResult(input);
73-
Assert.AreEqual(input, result.Data);
66+
CollectionAssert.AreEqual(input, result.GetOrder());
7467
}
7568

7669
[TestMethod]
7770
public void WhenSwapWithValidArgumentThenTheOrderIsChanged()
7871
{
79-
SortResult result = new SortResult("2,1,0");
72+
SortResult result = new SortResult( new List<int>() {2, 1, 0} );
8073
Assert.IsTrue(result.IsValid);
8174
Assert.AreEqual(2, result.GetIndex(0));
8275
Assert.AreEqual(1, result.GetIndex(1));
@@ -94,7 +87,7 @@ public void WhenSwapWithValidArgumentThenTheOrderIsChanged()
9487
[ExpectedException(typeof(ArgumentOutOfRangeException))]
9588
public void WhenSwapWithOutOfBoundArgumentThenExceptionIsThrown()
9689
{
97-
SortResult result = new SortResult("2,1,0");
90+
SortResult result = new SortResult( new List<int>() { 2, 1, 0} );
9891
Assert.IsTrue(result.IsValid);
9992
Assert.AreEqual(2, result.GetIndex(0));
10093
Assert.AreEqual(1, result.GetIndex(1));
@@ -106,7 +99,7 @@ public void WhenSwapWithOutOfBoundArgumentThenExceptionIsThrown()
10699
[TestMethod]
107100
public void WhenInvertOrderThenTheOrderIsChanged()
108101
{
109-
SortResult result = new SortResult("2,0,1");
102+
SortResult result = new SortResult( new List<int>() { 2, 0, 1} );
110103
Assert.IsTrue(result.IsValid);
111104
Assert.AreEqual(2, result.GetIndex(0));
112105
Assert.AreEqual(0, result.GetIndex(1));
@@ -123,7 +116,7 @@ public void WhenInvertOrderThenTheOrderIsChanged()
123116
[TestMethod]
124117
public void WhenInvertOrderIsCalledTwiceThenTheOrderIsUnchanged()
125118
{
126-
SortResult result = new SortResult("2,0,1");
119+
SortResult result = new SortResult(new List<int>() { 2, 0, 1 });
127120
Assert.IsTrue(result.IsValid);
128121
Assert.AreEqual(2, result.GetIndex(0));
129122
Assert.AreEqual(0, result.GetIndex(1));
@@ -142,7 +135,7 @@ public void WhenInvertOrderIsCalledTwiceThenTheOrderIsUnchanged()
142135
[ExpectedException(typeof(ArgumentOutOfRangeException))]
143136
public void WhenGetIndexWithOutOfBoundArgumentThenExceptionIsThrown()
144137
{
145-
SortResult result = new SortResult("2,1,0");
138+
SortResult result = new SortResult(new List<int>() { 2, 1, 0 });
146139
Assert.IsTrue(result.IsValid);
147140
result.GetIndex(3);
148141
}

DsmSuite.DsmViewer.Application.Test/Stubs/StubbedSortAlgorithm.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using DsmSuite.DsmViewer.Application.Sorting;
2+
using System.Collections.Generic;
23

34
namespace DsmSuite.DsmViewer.Application.Test.Stubs
45
{
56
public class StubbedSortAlgorithm : ISortAlgorithm
67
{
7-
private const string SortResult = "2,0,1";
8+
private List<int> SortResult = new List<int>() {2,0,1};
89

910
public StubbedSortAlgorithm(object[] args) { }
1011

DsmSuite.DsmViewer.Application/Actions/Base/ActionAttributes.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using static System.Runtime.InteropServices.JavaScript.JSType;
25

36
namespace DsmSuite.DsmViewer.Application.Actions.Base
47
{
@@ -29,6 +32,21 @@ public void SetNullableInt(string memberName, int? memberValue)
2932
}
3033
}
3134

35+
public void SetListInt(string memberName, List<int> list)
36+
{
37+
StringBuilder s = new StringBuilder();
38+
39+
for (int i = 0; i < list.Count; i++)
40+
{
41+
s.Append(list[i]);
42+
if (i < list.Count - 1)
43+
s.Append(',');
44+
}
45+
46+
_data[RemoveUnderscore(memberName)] = s.ToString();
47+
}
48+
49+
3250
public IReadOnlyDictionary<string, string> Data => _data;
3351

3452
private static string RemoveUnderscore(string memberName)

DsmSuite.DsmViewer.Application/Actions/Base/ActionReadOnlyAttributes.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System.Collections;
2+
using System.Collections.Generic;
23
using DsmSuite.DsmViewer.Model.Interfaces;
4+
using static System.Runtime.InteropServices.JavaScript.JSType;
35

46
namespace DsmSuite.DsmViewer.Application.Actions.Base
57
{
@@ -38,6 +40,23 @@ public int GetInt(string memberName)
3840
return value;
3941
}
4042

43+
public List<int> GetListInt(string memberName)
44+
{
45+
List<int> list = new List<int>();
46+
string s = _data.GetValueOrDefault(RemoveUnderscore(memberName));
47+
48+
foreach (string item in s.Split(','))
49+
{
50+
int value;
51+
if (int.TryParse(item, out value))
52+
{
53+
list.Add(value);
54+
}
55+
}
56+
57+
return list;
58+
}
59+
4160
public IDsmElement GetElement(string memberName)
4261
{
4362
int id = GetInt(memberName);

DsmSuite.DsmViewer.Application/Actions/Element/ElementSortAction.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ElementSortAction : IAction
1313
private readonly IActionContext _actionContext;
1414
private readonly IDsmElement _element;
1515
private readonly string _algorithm;
16-
private string _order;
16+
private List<int> _order;
1717

1818
public const ActionType RegisteredType = ActionType.ElementSort;
1919

@@ -27,7 +27,7 @@ public ElementSortAction(IDsmModel model, IActionContext context, IReadOnlyDicti
2727

2828
_element = attributes.GetElement(nameof(_element));
2929
_algorithm = attributes.GetString(nameof(_algorithm));
30-
_order = attributes.GetString(nameof(_order));
30+
_order = attributes.GetListInt(nameof(_order));
3131
}
3232
}
3333

@@ -36,7 +36,7 @@ public ElementSortAction(IDsmModel model, IDsmElement element, string algorithm)
3636
_model = model;
3737
_element = element;
3838
_algorithm = algorithm;
39-
_order = "";
39+
_order = null;
4040
}
4141

4242
public ActionType Type => RegisteredType;
@@ -48,7 +48,7 @@ public object Do()
4848
ISortAlgorithm sortAlgorithm = SortAlgorithmFactory.CreateAlgorithm(_model, _element, _algorithm);
4949
SortResult sortResult = sortAlgorithm.Sort();
5050
_model.ReorderChildren(_element, sortResult);
51-
_order = sortResult.Data;
51+
_order = sortResult.GetOrder();
5252

5353
_model.AssignElementOrder();
5454

@@ -79,7 +79,7 @@ public IReadOnlyDictionary<string, string> Data
7979
ActionAttributes attributes = new ActionAttributes();
8080
attributes.SetInt(nameof(_element), _element.Id);
8181
attributes.SetString(nameof(_algorithm), _algorithm);
82-
attributes.SetString(nameof(_order), _order);
82+
attributes.SetListInt(nameof(_order), _order);
8383
return attributes.Data;
8484
}
8585
}

0 commit comments

Comments
 (0)