From 1461be589a5d39d378fd177648c61a780271c742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Manuel=20Nieto=20S=C3=A1nchez?= Date: Wed, 23 Sep 2015 23:43:24 +0200 Subject: [PATCH] Fix problem with dependency resolver --- Source/Glass/DependencySorter.cs | 17 ++++- Source/OmniXaml/DependencySortingVisitor.cs | 6 +- .../Glass.Tests/DependencyResolverTest.cs | 62 +++++++++++++++++-- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/Source/Glass/DependencySorter.cs b/Source/Glass/DependencySorter.cs index 3adad94..547dfcd 100644 --- a/Source/Glass/DependencySorter.cs +++ b/Source/Glass/DependencySorter.cs @@ -7,7 +7,22 @@ public static class DependencySorter { - public static ICollection Sort(this T node) where T : IDependency + public static ICollection SortDependencies(this IEnumerable nodes) where T : IDependency + { + var set = new HashSet(); + + foreach (var node in nodes) + { + foreach (var dependency in node.Resolve()) + { + set.Add(dependency); + } + } + + return set.ToList(); + } + + public static ICollection Resolve(this T node) where T : IDependency { var resolved = new Collection(); ResolveDependenciesRecursive(node, resolved, new List()); diff --git a/Source/OmniXaml/DependencySortingVisitor.cs b/Source/OmniXaml/DependencySortingVisitor.cs index 80e8045..a3ead22 100644 --- a/Source/OmniXaml/DependencySortingVisitor.cs +++ b/Source/OmniXaml/DependencySortingVisitor.cs @@ -34,10 +34,8 @@ namespace OmniXaml return new List(); } - var members = list.Select(node => (MutableXamlMember) node.Leading.Member); - var xamlNode = members.First(); - var sortedList = xamlNode.Sort(); - + var members = list.Select(node => (MutableXamlMember) node.Leading.Member).ToList(); + var sortedList = members.SortDependencies(); var finalDeps = sortedList.Where(sr => members.Contains(sr)); var sortedNodes = SortNodesAccordingTo(list, finalDeps); diff --git a/Source/Tests/Glass.Tests/DependencyResolverTest.cs b/Source/Tests/Glass.Tests/DependencyResolverTest.cs index f8cf85e..6ce087b 100644 --- a/Source/Tests/Glass.Tests/DependencyResolverTest.cs +++ b/Source/Tests/Glass.Tests/DependencyResolverTest.cs @@ -1,10 +1,8 @@ namespace Glass.Tests { using System; - using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; - using System.Diagnostics; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -26,7 +24,63 @@ var expected = new List { d, e, c, b, a }; - var actual = DependencySorter.Sort(a).ToList(); + var actual = DependencySorter.SortDependencies(new Collection { a, b, c, d, e }).ToList(); + CollectionAssert.AreEqual(expected, actual); + } + + [TestMethod] + public void SimpleTest() + { + var prop = new Node("Property"); + var val = new Node("Value"); + + val.Dependencies = new Collection { prop }; + + var expected = new List { prop, val }; + + var actual = DependencySorter.SortDependencies(new Collection { prop, val }).ToList(); + CollectionAssert.AreEqual(expected, actual); + } + + [TestMethod] + public void AlreadySorted() + { + var a = new Node("A"); + var b = new Node("B"); + var c = new Node("C"); + var d = new Node("D"); + var e = new Node("E"); + + a.Dependencies = new Collection { }; + b.Dependencies = new Collection { a }; + c.Dependencies = new Collection { b }; + d.Dependencies = new Collection { c }; + e.Dependencies = new Collection { d }; + + var expected = new List { a, b, c, d, e }; + + var actual = DependencySorter.SortDependencies(new Collection { a, b, c, d, e }).ToList(); + CollectionAssert.AreEqual(expected, actual); + } + + [TestMethod] + public void PartiallySorted() + { + var a = new Node("A"); + var b = new Node("B"); + var c = new Node("C"); + var d = new Node("D"); + var e = new Node("E"); + + a.Dependencies = new Collection { }; + b.Dependencies = new Collection { a }; + c.Dependencies = new Collection { b }; + d.Dependencies = new Collection { e }; + e.Dependencies = new Collection { }; + + var expected = new List { a, b, c, e, d }; + + var actual = DependencySorter.SortDependencies(new Collection { a, b, c, d, e }).ToList(); CollectionAssert.AreEqual(expected, actual); } @@ -45,7 +99,7 @@ c.Dependencies = new Collection { d, e }; e.Dependencies = new Collection { a }; - DependencySorter.Sort(a); + DependencySorter.SortDependencies(new Collection { a, b, c, d, e }); } } }