зеркало из https://github.com/AvaloniaUI/OmniXAML.git
Fix problem with dependency resolver
This commit is contained in:
Родитель
7b13ebcde0
Коммит
1461be589a
|
@ -7,7 +7,22 @@
|
||||||
|
|
||||||
public static class DependencySorter
|
public static class DependencySorter
|
||||||
{
|
{
|
||||||
public static ICollection<T> Sort<T>(this T node) where T : IDependency<T>
|
public static ICollection<T> SortDependencies<T>(this IEnumerable<T> nodes) where T : IDependency<T>
|
||||||
|
{
|
||||||
|
var set = new HashSet<T>();
|
||||||
|
|
||||||
|
foreach (var node in nodes)
|
||||||
|
{
|
||||||
|
foreach (var dependency in node.Resolve())
|
||||||
|
{
|
||||||
|
set.Add(dependency);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return set.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ICollection<T> Resolve<T>(this T node) where T : IDependency<T>
|
||||||
{
|
{
|
||||||
var resolved = new Collection<T>();
|
var resolved = new Collection<T>();
|
||||||
ResolveDependenciesRecursive(node, resolved, new List<T>());
|
ResolveDependenciesRecursive(node, resolved, new List<T>());
|
||||||
|
|
|
@ -34,10 +34,8 @@ namespace OmniXaml
|
||||||
return new List<InstructionNode>();
|
return new List<InstructionNode>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var members = list.Select(node => (MutableXamlMember) node.Leading.Member);
|
var members = list.Select(node => (MutableXamlMember) node.Leading.Member).ToList();
|
||||||
var xamlNode = members.First();
|
var sortedList = members.SortDependencies();
|
||||||
var sortedList = xamlNode.Sort();
|
|
||||||
|
|
||||||
var finalDeps = sortedList.Where(sr => members.Contains(sr));
|
var finalDeps = sortedList.Where(sr => members.Contains(sr));
|
||||||
|
|
||||||
var sortedNodes = SortNodesAccordingTo(list, finalDeps);
|
var sortedNodes = SortNodesAccordingTo(list, finalDeps);
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
namespace Glass.Tests
|
namespace Glass.Tests
|
||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
@ -26,7 +24,63 @@
|
||||||
|
|
||||||
var expected = new List<Node> { d, e, c, b, a };
|
var expected = new List<Node> { d, e, c, b, a };
|
||||||
|
|
||||||
var actual = DependencySorter.Sort(a).ToList();
|
var actual = DependencySorter.SortDependencies(new Collection<Node> { 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<Node> { prop };
|
||||||
|
|
||||||
|
var expected = new List<Node> { prop, val };
|
||||||
|
|
||||||
|
var actual = DependencySorter.SortDependencies(new Collection<Node> { 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<Node> { };
|
||||||
|
b.Dependencies = new Collection<Node> { a };
|
||||||
|
c.Dependencies = new Collection<Node> { b };
|
||||||
|
d.Dependencies = new Collection<Node> { c };
|
||||||
|
e.Dependencies = new Collection<Node> { d };
|
||||||
|
|
||||||
|
var expected = new List<Node> { a, b, c, d, e };
|
||||||
|
|
||||||
|
var actual = DependencySorter.SortDependencies(new Collection<Node> { 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<Node> { };
|
||||||
|
b.Dependencies = new Collection<Node> { a };
|
||||||
|
c.Dependencies = new Collection<Node> { b };
|
||||||
|
d.Dependencies = new Collection<Node> { e };
|
||||||
|
e.Dependencies = new Collection<Node> { };
|
||||||
|
|
||||||
|
var expected = new List<Node> { a, b, c, e, d };
|
||||||
|
|
||||||
|
var actual = DependencySorter.SortDependencies(new Collection<Node> { a, b, c, d, e }).ToList();
|
||||||
CollectionAssert.AreEqual(expected, actual);
|
CollectionAssert.AreEqual(expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +99,7 @@
|
||||||
c.Dependencies = new Collection<Node> { d, e };
|
c.Dependencies = new Collection<Node> { d, e };
|
||||||
e.Dependencies = new Collection<Node> { a };
|
e.Dependencies = new Collection<Node> { a };
|
||||||
|
|
||||||
DependencySorter.Sort(a);
|
DependencySorter.SortDependencies(new Collection<Node> { a, b, c, d, e });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче