Skip to content

Commit 5ca87fc

Browse files
committed
Allow the user to specify XmlWriterSettings
1 parent 7b2b7fa commit 5ca87fc

9 files changed

Lines changed: 336 additions & 10 deletions

readme.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,24 @@ public async Task WindowUsage()
3737
var window = new MyWindow();
3838
await Verify(window);
3939
}
40+
41+
[Test]
42+
public Task WindowWithSettings()
43+
{
44+
var window = new MyWindow();
45+
return Verify(window)
46+
.VerifyXamlSerializationSettings(new System.Xml.XmlWriterSettings
47+
{
48+
Indent = true,
49+
OmitXmlDeclaration = true,
50+
NewLineHandling = System.Xml.NewLineHandling.Replace,
51+
NewLineChars = "\n",
52+
IndentChars = " ",
53+
NewLineOnAttributes = true,
54+
});
55+
}
4056
```
41-
<sup><a href='/src/Tests/TheTests.cs#L7-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-Window' title='Start of snippet'>anchor</a></sup>
57+
<sup><a href='/src/Tests/TheTests.cs#L7-L33' title='Snippet source file'>snippet source</a> | <a href='#snippet-Window' title='Start of snippet'>anchor</a></sup>
4258
<!-- endSnippet -->
4359

4460
With the state of the element being rendered as a verified files:

src/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ dotnet_style_coalesce_expression = false:error
272272
dotnet_style_null_propagation = true:error
273273
dotnet_style_explicit_tuple_names = true:error
274274

275+
# Use collection expression syntax
276+
resharper_use_collection_expression_highlighting = error
277+
275278
# Prefer "var" everywhere
276279
csharp_style_var_for_built_in_types = true:error
277280
csharp_style_var_when_type_is_apparent = true:error
8.27 KB
Loading
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<MyWindow
2+
Title="MyWindow"
3+
Width="525"
4+
Height="350"
5+
xmlns="clr-namespace:Tests;assembly=Tests"
6+
xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
7+
<av:DockPanel
8+
Name="MyPanel">
9+
<av:Menu
10+
Height="26"
11+
av:DockPanel.Dock="Top">
12+
<av:MenuItem
13+
Header="File">
14+
<av:MenuItem
15+
Header="Exit" />
16+
</av:MenuItem>
17+
<av:MenuItem
18+
Header="View">
19+
<av:MenuItem
20+
IsCheckable="True"
21+
IsChecked="True"
22+
Header="Standard"
23+
Name="StandardMenu" />
24+
</av:MenuItem>
25+
<av:MenuItem
26+
Header="Help">
27+
<av:MenuItem
28+
Header="About" />
29+
</av:MenuItem>
30+
</av:Menu>
31+
<av:Grid
32+
ShowGridLines="False"
33+
Name="MyGrid">
34+
<av:Grid.ColumnDefinitions>
35+
<av:ColumnDefinition />
36+
<av:ColumnDefinition />
37+
<av:ColumnDefinition />
38+
<av:ColumnDefinition />
39+
<av:ColumnDefinition />
40+
<av:ColumnDefinition />
41+
<av:ColumnDefinition />
42+
<av:ColumnDefinition />
43+
<av:ColumnDefinition />
44+
</av:Grid.ColumnDefinitions>
45+
<av:Grid.RowDefinitions>
46+
<av:RowDefinition />
47+
<av:RowDefinition />
48+
<av:RowDefinition />
49+
<av:RowDefinition />
50+
<av:RowDefinition />
51+
<av:RowDefinition />
52+
</av:Grid.RowDefinitions>
53+
<av:Button
54+
Name="B7"
55+
av:Grid.Column="4"
56+
av:Grid.Row="2">7</av:Button>
57+
<av:Button
58+
Name="B8"
59+
av:Grid.Column="5"
60+
av:Grid.Row="2">8</av:Button>
61+
<av:Button
62+
Name="B9"
63+
av:Grid.Column="6"
64+
av:Grid.Row="2">9</av:Button>
65+
<av:Button
66+
Name="B4"
67+
av:Grid.Column="4"
68+
av:Grid.Row="3">4</av:Button>
69+
<av:Button
70+
Name="B5"
71+
av:Grid.Column="5"
72+
av:Grid.Row="3">5</av:Button>
73+
<av:Button
74+
Name="B6"
75+
av:Grid.Column="6"
76+
av:Grid.Row="3">6</av:Button>
77+
<av:Button
78+
Name="B1"
79+
av:Grid.Column="4"
80+
av:Grid.Row="4">1</av:Button>
81+
<av:Button
82+
Name="B2"
83+
av:Grid.Column="5"
84+
av:Grid.Row="4">2</av:Button>
85+
<av:Button
86+
Name="B3"
87+
av:Grid.Column="6"
88+
av:Grid.Row="4">3</av:Button>
89+
<av:Button
90+
Name="B0"
91+
av:Grid.Column="4"
92+
av:Grid.Row="5">0</av:Button>
93+
<av:Button
94+
Name="BPeriod"
95+
av:Grid.Column="5"
96+
av:Grid.Row="5">.</av:Button>
97+
<av:Button
98+
Background="#FFA9A9A9"
99+
Name="BPM"
100+
av:Grid.Column="6"
101+
av:Grid.Row="5">+/-</av:Button>
102+
<av:Button
103+
Background="#FFA9A9A9"
104+
Name="BDevide"
105+
av:Grid.Column="7"
106+
av:Grid.Row="2">/</av:Button>
107+
<av:Button
108+
Background="#FFA9A9A9"
109+
Name="BMultiply"
110+
av:Grid.Column="7"
111+
av:Grid.Row="3">*</av:Button>
112+
<av:Button
113+
Background="#FFA9A9A9"
114+
Name="BMinus"
115+
av:Grid.Column="7"
116+
av:Grid.Row="4">-</av:Button>
117+
<av:Button
118+
Background="#FFA9A9A9"
119+
Name="BPlus"
120+
av:Grid.Column="7"
121+
av:Grid.Row="5">+</av:Button>
122+
<av:Button
123+
Background="#FFA9A9A9"
124+
Name="BSqrt"
125+
ToolTip="Usage: 'A Sqrt'"
126+
av:Grid.Column="8"
127+
av:Grid.Row="2">Sqrt</av:Button>
128+
<av:Button
129+
Background="#FFA9A9A9"
130+
Name="BPercent"
131+
ToolTip="Usage: 'A % B ='"
132+
av:Grid.Column="8"
133+
av:Grid.Row="3">%</av:Button>
134+
<av:Button
135+
Background="#FFA9A9A9"
136+
Name="BOneOver"
137+
ToolTip="Usage: 'A 1/X'"
138+
av:Grid.Column="8"
139+
av:Grid.Row="4">1/X</av:Button>
140+
<av:Button
141+
Background="#FFA9A9A9"
142+
Name="BEqual"
143+
av:Grid.Column="8"
144+
av:Grid.Row="5">=</av:Button>
145+
<av:Button
146+
Background="#FFA9A9A9"
147+
Name="BC"
148+
ToolTip="Clear All"
149+
av:Grid.Column="8"
150+
av:Grid.Row="1">C</av:Button>
151+
<av:Button
152+
Background="#FFA9A9A9"
153+
Name="BCE"
154+
ToolTip="Clear Current Entry"
155+
av:Grid.Column="7"
156+
av:Grid.Row="1">CE</av:Button>
157+
<av:Button
158+
Background="#FFA9A9A9"
159+
Name="BMemClear"
160+
ToolTip="Clear Memory"
161+
av:Grid.Column="3"
162+
av:Grid.Row="2">MC</av:Button>
163+
<av:Button
164+
Background="#FFA9A9A9"
165+
Name="BMemRecall"
166+
ToolTip="Recall Memory"
167+
av:Grid.Column="3"
168+
av:Grid.Row="3">MR</av:Button>
169+
<av:Button
170+
Background="#FFA9A9A9"
171+
Name="BMemSave"
172+
ToolTip="Store in Memory"
173+
av:Grid.Column="3"
174+
av:Grid.Row="4">MS</av:Button>
175+
<av:Button
176+
Background="#FFA9A9A9"
177+
Name="BMemPlus"
178+
ToolTip="Add To Memory"
179+
av:Grid.Column="3"
180+
av:Grid.Row="5">M+</av:Button>
181+
<av:TextBlock
182+
Text="Memory: [empty]"
183+
Name="BMemBox"
184+
Margin="10,17,10,17"
185+
av:Grid.Column="3"
186+
av:Grid.Row="1"
187+
av:Grid.ColumnSpan="2" />
188+
<av:TextBox
189+
Name="DisplayBox"
190+
Height="30"
191+
Margin="5,5,5,5"
192+
av:Grid.ColumnSpan="9"
193+
xml:space="preserve"></av:TextBox>
194+
<av:TextBox
195+
Name="PaperBox"
196+
Margin="5,5,5,5"
197+
av:Grid.Row="1"
198+
av:Grid.ColumnSpan="3"
199+
av:Grid.RowSpan="5"
200+
xml:space="preserve"></av:TextBox>
201+
</av:Grid>
202+
</av:DockPanel>
203+
</MyWindow>

src/Tests/TheTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ public async Task WindowUsage()
1313
await Verify(window);
1414
}
1515

16+
17+
[Test]
18+
public Task WindowWithSettings()
19+
{
20+
var window = new MyWindow();
21+
return Verify(window)
22+
.VerifyXamlSerializationSettings(new System.Xml.XmlWriterSettings
23+
{
24+
Indent = true,
25+
OmitXmlDeclaration = true,
26+
NewLineHandling = System.Xml.NewLineHandling.Replace,
27+
NewLineChars = "\n",
28+
IndentChars = " ",
29+
NewLineOnAttributes = true,
30+
});
31+
}
32+
1633
#endregion
1734

1835
#region Page

src/Verify.Xaml/VerifyXaml.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
using System.Windows;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Net.NetworkInformation;
3+
using System.Windows;
4+
using System.Xml;
25

36
namespace VerifyTests;
47

58
public static class VerifyXaml
69
{
10+
private static WpfUtils defaultUtils = new();
11+
712
public static bool Initialized { get; private set; }
813

914
public static void Initialize()
@@ -14,31 +19,45 @@ public static void Initialize()
1419
}
1520

1621
Initialized = true;
17-
1822
InnerVerifier.ThrowIfVerifyHasBeenRun();
1923
VerifierSettings.RegisterFileConverter<Window>(WindowToImage);
2024
VerifierSettings.RegisterFileConverter<FrameworkElement>(ElementToImage);
2125
}
2226

27+
public static void Initialize(XmlWriterSettings xmlWriterSettings)
28+
{
29+
var utils = new WpfUtilsWithWriterSettings(xmlWriterSettings);
30+
Initialize();
31+
defaultUtils = utils;
32+
}
33+
34+
2335
static ConversionResult ElementToImage(FrameworkElement element, IReadOnlyDictionary<string, object> context)
2436
{
37+
var utils = GetWpfUtils(context);
38+
2539
var pngStream = WpfUtils.ScreenCapture(element);
2640
return new(null,
2741
new List<Target>
2842
{
29-
new("xml", element.ToXamlString()),
43+
new("xml", utils.ToXamlString(element)),
3044
new("png", pngStream)
3145
});
3246
}
3347

3448
static ConversionResult WindowToImage(Window window, IReadOnlyDictionary<string, object> context)
3549
{
50+
var utils = GetWpfUtils(context);
51+
3652
var pngStream = WpfUtils.ScreenCapture(window);
3753
return new(null,
3854
new List<Target>
3955
{
40-
new("xml", window.ToXamlString()),
56+
new("xml", utils.ToXamlString(window)),
4157
new("png", pngStream)
4258
});
4359
}
44-
}
60+
61+
static WpfUtils GetWpfUtils(IReadOnlyDictionary<string, object> context)
62+
=> VerifyXamlSettings.GetWpfUtilsOrNull(context) ?? defaultUtils;
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Xml;
2+
3+
namespace VerifyTests;
4+
5+
public static class VerifyXamlSettings
6+
{
7+
public static void VerifyXamlSerializationSettings(
8+
this VerifySettings settings,
9+
XmlWriterSettings xmlWriterSettings)
10+
=>
11+
settings.Context["VerifyXaml.WpfUtils"] = new WpfUtilsWithWriterSettings(xmlWriterSettings);
12+
13+
public static SettingsTask VerifyXamlSerializationSettings(
14+
this SettingsTask settings,
15+
XmlWriterSettings xmlWriterSettings)
16+
{
17+
settings.CurrentSettings.VerifyXamlSerializationSettings(xmlWriterSettings);
18+
return settings;
19+
}
20+
21+
internal static WpfUtils? GetWpfUtilsOrNull(this IReadOnlyDictionary<string, object> context)
22+
{
23+
if (context.TryGetValue("VerifyXaml.WpfUtils", out var value))
24+
{
25+
return (WpfUtils)value;
26+
}
27+
28+
return null;
29+
}
30+
}

0 commit comments

Comments
 (0)