Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3eefff6

Browse files
committed
Factor out duplicate code
1 parent 0a75a7e commit 3eefff6

File tree

5 files changed

+82
-142
lines changed

5 files changed

+82
-142
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace GitHub.UI.UnitTests
4+
{
5+
public class AppDomainContext : IDisposable
6+
{
7+
AppDomain domain;
8+
9+
public AppDomainContext(AppDomainSetup setup)
10+
{
11+
var friendlyName = GetType().FullName;
12+
domain = AppDomain.CreateDomain(friendlyName, null, setup);
13+
}
14+
15+
public T CreateInstance<T>()
16+
{
17+
return (T)domain.CreateInstanceFromAndUnwrap(typeof(T).Assembly.CodeBase, typeof(T).FullName);
18+
}
19+
20+
public void Dispose()
21+
{
22+
AppDomain.Unload(domain);
23+
}
24+
}
25+
}

test/GitHub.UI.UnitTests/GitHub.UI.UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
<Reference Include="WindowsBase" />
4949
</ItemGroup>
5050
<ItemGroup>
51+
<Compile Include="AppDomainContext.cs" />
52+
<Compile Include="ResourceDictionaryUtilities.cs" />
5153
<Compile Include="SharedDictionaryManagerTests.cs" />
5254
<Compile Include="LoadingResourceDictionaryTests.cs" />
5355
<Compile Include="Properties\AssemblyInfo.cs" />

test/GitHub.UI.UnitTests/LoadingResourceDictionaryTests.cs

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
2-
using System.Diagnostics;
32
using System.IO;
43
using System.Windows;
54
using NUnit.Framework;
65

7-
namespace GitHub.UI.Tests
6+
namespace GitHub.UI.UnitTests
87
{
98
public class LoadingResourceDictionaryTests
109
{
@@ -45,7 +44,7 @@ class LoadingResourceDictionaryContext : MarshalByRefObject
4544
internal int CountMergedDictionaries(string url)
4645
{
4746
var target = new LoadingResourceDictionary();
48-
var packUri = ToPackUri(url);
47+
var packUri = ResourceDictionaryUtilities.ToPackUri(url);
4948

5049
target.Source = packUri;
5150

@@ -75,56 +74,13 @@ class ResourceDictionaryContext : MarshalByRefObject
7574
internal int CountMergedDictionaries(string url)
7675
{
7776
var target = new ResourceDictionary();
78-
var packUri = ToPackUri(url);
77+
var packUri = ResourceDictionaryUtilities.ToPackUri(url);
7978

8079
target.Source = packUri;
8180

8281
return target.MergedDictionaries.Count;
8382
}
8483
}
8584
}
86-
87-
static Uri ToPackUri(string url)
88-
{
89-
if (!UriParser.IsKnownScheme("pack"))
90-
{
91-
// Register the pack scheme.
92-
new Application();
93-
}
94-
95-
return new Uri(url);
96-
}
97-
98-
class AppDomainContext : IDisposable
99-
{
100-
AppDomain domain;
101-
102-
public AppDomainContext(AppDomainSetup setup)
103-
{
104-
var friendlyName = GetType().FullName;
105-
domain = AppDomain.CreateDomain(friendlyName, null, setup);
106-
}
107-
108-
public T CreateInstance<T>()
109-
{
110-
return (T)domain.CreateInstanceFromAndUnwrap(typeof(T).Assembly.CodeBase, typeof(T).FullName);
111-
}
112-
113-
public void Dispose()
114-
{
115-
AppDomain.Unload(domain);
116-
}
117-
}
118-
119-
static void DumpResourceDictionary(ResourceDictionary rd, string indent = "")
120-
{
121-
var source = rd.Source?.ToString() ?? "null";
122-
123-
Trace.WriteLine(indent + source + rd.GetType().FullName);
124-
foreach (var child in rd.MergedDictionaries)
125-
{
126-
DumpResourceDictionary(child, indent + " ");
127-
}
128-
}
12985
}
13086
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows;
4+
5+
namespace GitHub.UI.UnitTests
6+
{
7+
class ResourceDictionaryUtilities
8+
{
9+
public static Uri ToPackUri(string url)
10+
{
11+
if (!UriParser.IsKnownScheme("pack"))
12+
{
13+
// Register the pack scheme.
14+
new Application();
15+
}
16+
17+
return new Uri(url);
18+
}
19+
20+
public static string DumpResourceDictionary(ResourceDictionary rd, string indent = "")
21+
{
22+
var writer = new StringWriter();
23+
DumpResourceDictionary(writer, rd);
24+
return writer.ToString();
25+
}
26+
27+
public static void DumpResourceDictionary(TextWriter writer, ResourceDictionary rd, string indent = "")
28+
{
29+
var source = rd.Source;
30+
if (source != null)
31+
{
32+
writer.WriteLine(indent + source + " (" + rd.GetType().FullName + ") # " + rd.GetHashCode());
33+
foreach (var child in rd.MergedDictionaries)
34+
{
35+
DumpResourceDictionary(writer, child, indent + " ");
36+
}
37+
}
38+
else
39+
{
40+
// ignore our empty nodes
41+
foreach (var child in rd.MergedDictionaries)
42+
{
43+
DumpResourceDictionary(writer, child, indent);
44+
}
45+
}
46+
}
47+
}
48+
}
Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System;
2-
using System.IO;
32
using System.Windows;
43
using NUnit.Framework;
54
using GitHub.UI.Helpers;
65

7-
namespace GitHub.UI.Tests
6+
namespace GitHub.UI.UnitTests
87
{
9-
public class SharedDictionaryManagerTests
8+
public partial class SharedDictionaryManagerTests
109
{
1110
public class TheSourceProperty
1211
{
@@ -63,10 +62,10 @@ string DumpMergedDictionaries(ResourceDictionary target, params string[] urls)
6362
{
6463
foreach(var url in urls)
6564
{
66-
SetProperty(target, "Source", ToPackUri(url));
65+
SetProperty(target, "Source", ResourceDictionaryUtilities.ToPackUri(url));
6766
}
6867

69-
return DumpResourceDictionary(target);
68+
return ResourceDictionaryUtilities.DumpResourceDictionary(target);
7069
}
7170

7271
static void SetProperty(object target, string name, object value)
@@ -76,95 +75,5 @@ static void SetProperty(object target, string name, object value)
7675
}
7776
}
7877
}
79-
80-
public class TheResourceDictionarySourceProperty
81-
{
82-
[Description("This shows why LoadingResourceDictionary is necessary")]
83-
[TestCase("pack://application:,,,/GitHub.UI;component/SharedDictionary.xaml")]
84-
[TestCase("pack://application:,,,/GitHub.VisualStudio.UI;component/SharedDictionary.xaml")]
85-
public void SetInLoadFromContext(string url)
86-
{
87-
var setup = new AppDomainSetup { ApplicationBase = "NOTHING_HERE" };
88-
using (var context = new AppDomainContext(setup))
89-
{
90-
var remote = context.CreateInstance<ResourceDictionaryContext>();
91-
92-
Assert.Throws<FileNotFoundException>(() => remote.CountMergedDictionaries(url));
93-
}
94-
}
95-
96-
class ResourceDictionaryContext : MarshalByRefObject
97-
{
98-
internal int CountMergedDictionaries(string url)
99-
{
100-
var target = new ResourceDictionary();
101-
var packUri = ToPackUri(url);
102-
103-
target.Source = packUri;
104-
105-
return target.MergedDictionaries.Count;
106-
}
107-
}
108-
}
109-
110-
static Uri ToPackUri(string url)
111-
{
112-
if (!UriParser.IsKnownScheme("pack"))
113-
{
114-
// Register the pack scheme.
115-
new Application();
116-
}
117-
118-
return new Uri(url);
119-
}
120-
121-
class AppDomainContext : IDisposable
122-
{
123-
AppDomain domain;
124-
125-
public AppDomainContext(AppDomainSetup setup)
126-
{
127-
var friendlyName = GetType().FullName;
128-
domain = AppDomain.CreateDomain(friendlyName, null, setup);
129-
}
130-
131-
public T CreateInstance<T>()
132-
{
133-
return (T)domain.CreateInstanceFromAndUnwrap(typeof(T).Assembly.CodeBase, typeof(T).FullName);
134-
}
135-
136-
public void Dispose()
137-
{
138-
AppDomain.Unload(domain);
139-
}
140-
}
141-
142-
static string DumpResourceDictionary(ResourceDictionary rd, string indent = "")
143-
{
144-
var writer = new StringWriter();
145-
DumpResourceDictionary(writer, rd);
146-
return writer.ToString();
147-
}
148-
149-
static void DumpResourceDictionary(TextWriter writer, ResourceDictionary rd, string indent = "")
150-
{
151-
var source = rd.Source;
152-
if(source != null)
153-
{
154-
writer.WriteLine(indent + source + " (" + rd.GetType().FullName + ") # " + rd.GetHashCode());
155-
foreach (var child in rd.MergedDictionaries)
156-
{
157-
DumpResourceDictionary(writer, child, indent + " ");
158-
}
159-
}
160-
else
161-
{
162-
// ignore our empty nodes
163-
foreach (var child in rd.MergedDictionaries)
164-
{
165-
DumpResourceDictionary(writer, child, indent);
166-
}
167-
}
168-
}
16978
}
17079
}

0 commit comments

Comments
 (0)