/
HtmlFormatterTests.Objects.cs
433 lines (359 loc) · 15.9 KB
/
HtmlFormatterTests.Objects.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using Dummy;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.DotNet.Interactive.Formatting.Tests.Utility;
using Xunit;
namespace Microsoft.DotNet.Interactive.Formatting.Tests;
public partial class HtmlFormatterTests
{
public class Objects : FormatterTestBase
{
[Fact]
public void Formatters_are_generated_on_the_fly_for_anonymous_types()
{
var output = new { a = 123 }.ToDisplayString(HtmlFormatter.MimeType);
output
.RemoveStyleElement()
.Should()
.BeEquivalentHtmlTo($$"""
<details open="open" class="dni-treeview">
{{Tags.SummaryTextBegin}}{ a = 123 }{{Tags.SummaryTextEnd}}
<div>
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>
<div class="dni-plaintext">
<pre>123</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</details>
""");
}
[Fact]
public void Null_references_are_indicated()
{
string value = null;
value.ToDisplayString(HtmlFormatter.MimeType).RemoveStyleElement()
.Should()
.Be($"{Tags.PlainTextBegin}<null>{Tags.PlainTextEnd}");
}
[Fact]
public void Formatter_puts_div_with_class_around_string()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor<string>();
var s = "hello".ToDisplayString(formatter).RemoveStyleElement();
s.Should().Be($"{Tags.PlainTextBegin}hello{Tags.PlainTextEnd}");
}
[Fact]
public void Formatter_expands_properties_of_ExpandoObjects()
{
dynamic expando = new ExpandoObject();
expando.Name = "socks";
expando.Count = 2;
var formatter = HtmlFormatter.GetPreferredFormatterFor<ExpandoObject>();
var output = ((object)expando).ToDisplayString(formatter).RemoveStyleElement();
output.Should().BeEquivalentHtmlTo($@"
<table>
<thead>
<tr><th>Count</th><th>Name</th></tr>
</thead>
<tbody><tr><td>{Tags.PlainTextBegin}2{Tags.PlainTextEnd}</td><td>socks</td></tr>
</tbody>
</table>");
}
[Fact]
public void It_formats_objects_as_tree()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(EntityId));
var writer = new StringWriter();
var instance = new EntityId("TheEntity", "123");
formatter.Format(instance, writer);
var html = writer.ToString().RemoveStyleElement();
html
.Should()
.BeEquivalentHtmlTo($"""
<details open="open" class="dni-treeview">
{Tags.SummaryTextBegin}{typeof(EntityId).FullName}{Tags.SummaryTextEnd}
<div>
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr>
<td>TypeName</td>
<td>
<div class="dni-plaintext">
<pre>TheEntity</pre>
</div>
</td>
</tr>
<tr>
<td>Id</td>
<td>
<div class="dni-plaintext">
<pre>123</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</details>
""");
}
[Fact]
public void Recursive_formatter_calls_do_not_cause_exceptions()
{
var widget = new Widget();
widget.Parts = new List<Part> { new() { Widget = widget } };
var formatter = HtmlFormatter.GetPreferredFormatterFor(widget.GetType());
FormatContext context = new(new StringWriter());
formatter.Invoking(f => f.Format(widget, context)).Should().NotThrow();
}
[Fact]
public void Formatted_objects_include_custom_styles()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(DirectoryInfo));
var writer = new StringWriter();
var instance = new DirectoryInfo(".");
formatter.Format(instance, writer);
var html = writer.ToString();
html.Should().Contain(Tags.DefaultStyles);
}
[Fact]
public void It_formats_value_tuples_as_tree()
{
var writer = new StringWriter();
var instance = (123, "hello");
var formatter = HtmlFormatter.GetPreferredFormatterFor(instance.GetType());
formatter.Format(instance, writer);
writer.ToString()
.RemoveStyleElement()
.Should()
.BeEquivalentHtmlTo($"""
<details open="open" class="dni-treeview">
{Tags.SummaryTextBegin}(123, hello){Tags.SummaryTextEnd}
<div>
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr>
<td>Item1</td>
<td>
<div class="dni-plaintext">
<pre>123</pre>
</div>
</td>
</tr>
<tr>
<td>Item2</td>
<td>
<div class="dni-plaintext">
<pre>hello</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</details>
""");
}
[Fact]
public void Object_properties_are_formatted_using_nested_trees()
{
var writer = new StringWriter();
var instance = new
{
A = 123,
B = new { BA = 456 }
};
var formatter = HtmlFormatter.GetPreferredFormatterFor(instance.GetType());
formatter.Format(instance, writer);
var expected = $$"""
<details open="open" class="dni-treeview">
<summary><span class="dni-code-hint"><code>{ A = 123, B = { BA = 456 } }</code></span></summary>
<div>
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td>
<div class="dni-plaintext">
<pre>123</pre>
</div>
</td>
</tr>
<tr>
<td>B</td>
<td>
<details open="open" class="dni-treeview">
<summary><span class="dni-code-hint"><code>{ BA = 456 }</code></span></summary>
<div>
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr>
<td>BA</td>
<td>
<div class="dni-plaintext">
<pre>456</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</details>
</td>
</tr>
</tbody>
</table>
</div>
</details>
""";
var actual = writer.ToString().RemoveStyleElement();
actual
.Should()
.BeEquivalentHtmlTo(
expected);
}
[Fact]
public void Scalar_sequence_properties_are_formatted_using_plain_text_formatter()
{
var writer = new StringWriter();
var instance = new
{
PropertyA = 123,
PropertyB = Enumerable.Range(1, 3)
};
var formatter = HtmlFormatter.GetPreferredFormatterFor(instance.GetType());
formatter.Format(instance, writer);
var html = writer.ToString().RemoveStyleElement();
html
.Should()
.BeEquivalentHtmlTo(
$$"""
<details open="open" class="dni-treeview">
{{Tags.SummaryTextBegin}}{ PropertyA = 123, PropertyB = System.Linq.Enumerable+RangeIterator }{{Tags.SummaryTextEnd}}
<div>
<table>
<thead>
<tr></tr>
</thead>
<tbody>
<tr>
<td>PropertyA</td>
<td>
<div class="dni-plaintext">
<pre>123</pre>
</div>
</td>
</tr>
<tr>
<td>PropertyB</td>
<td>
<div class="dni-plaintext">
<pre>[ 1, 2, 3 ]</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</details>
""");
}
[Fact]
public void It_displays_exceptions_thrown_by_properties_in_the_property_value_cell()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(SomePropertyThrows));
var writer = new StringWriter();
var widget = new SomePropertyThrows();
formatter.Format(widget, writer);
var html = writer.ToString();
html
.Should()
.Contain("""
<tr>
<td>NotOk</td>
<td>
<details open="open" class="dni-treeview">
<summary><span class="dni-code-hint"><code>System.Exception: not ok
""".Crunch());
}
[Fact]
public void Type_instances_do_not_have_properties_expanded()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(Type));
var writer = new StringWriter();
formatter.Format(typeof(ClassNotInSystemNamespace), writer);
writer.ToString()
.Should()
.Be("Dummy.ClassNotInSystemNamespace");
}
[Fact]
public void Type_instances_have_link_added_for_System_namespace_type()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(Type));
var writer = new StringWriter();
formatter.Format(typeof(string), writer);
writer.ToString()
.Should()
.Be("<span><a href=\"https://docs.microsoft.com/dotnet/api/system.string?view=net-7.0\">System.String</a></span>");
}
[Fact]
public void Type_instances_have_link_added_for_Microsoft_namespace_type()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(Type));
var writer = new StringWriter();
formatter.Format(typeof(RuntimeBinderException), writer);
writer.ToString()
.Should()
.Be(
"<span><a href=\"https://docs.microsoft.com/dotnet/api/microsoft.csharp.runtimebinder.runtimebinderexception?view=net-7.0\">Microsoft.CSharp.RuntimeBinder.RuntimeBinderException</a></span>");
}
[Fact]
public void Enums_are_formatted_using_their_names()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(FileAccess));
var writer = new StringWriter();
formatter.Format(FileAccess.ReadWrite, writer);
writer.ToString().Should().Contain("ReadWrite");
}
[Fact]
public void TimeSpan_is_not_destructured()
{
var formatter = HtmlFormatter.GetPreferredFormatterFor(typeof(TimeSpan));
var writer = new StringWriter();
var timespan = 25.Milliseconds();
formatter.Format(timespan, writer);
writer.ToString().Should().Contain(timespan.ToString());
}
}
}