-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCallOutTests.cs
More file actions
544 lines (472 loc) · 14 KB
/
CallOutTests.cs
File metadata and controls
544 lines (472 loc) · 14 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.IO.Abstractions.TestingHelpers;
using AwesomeAssertions;
using Elastic.Markdown.Myst.CodeBlocks;
using Elastic.Markdown.Tests.Inline;
using JetBrains.Annotations;
namespace Elastic.Markdown.Tests.CodeBlocks;
public abstract class CodeBlockCallOutTests(
ITestOutputHelper output,
string language,
[LanguageInjection("csharp")] string code,
[LanguageInjection("markdown")] string? markdown = null
)
: BlockTest<EnhancedCodeBlock>(output,
$$"""
```{{language}}
{{code}}
```
{{markdown}}
"""
)
{
[Fact]
public void ParsesAdmonitionBlock() => Block.Should().NotBeNull();
[Fact]
public void SetsLanguage() => Block!.Language.Should().Be("csharp");
}
public class MagicCalOuts(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; // this is a callout
//this is not a callout
var y = x - 2;
var z = y - 2; // another callout
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.NotContain(c => c.Text.Contains("not a callout"));
[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
}
public class MagicCallOutWithFormatting(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; // this uses `formatting` and a [link](testing/req.md)
"""
)
{
protected override void AddToFileSystem(MockFileSystem fileSystem) =>
fileSystem.AddFile("docs/testing/req.md", new MockFileData("# Requirements"));
[Fact]
public void RendersFormattedInlineMarkdown() =>
Html.ShouldContainHtml(
"""
<ol class="code-callouts">
<li>this uses <code>formatting</code> and a <a href="/docs/testing/req" hx-get="/docs/testing/req" hx-select-oob="#content-container,#toc-nav" hx-swap="none" hx-push-url="true" hx-indicator="#htmx-indicator" preload="mousedown">link</a></li>
</ol>
"""
);
[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
}
public class ClassicCallOutsRequiresContent(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void RequiresContentToFollow() => Collector.Diagnostics.Should().HaveCount(1)
.And.OnlyContain(c => c.Message.StartsWith("Code block with annotations is not followed by any content"));
}
public class ClassicCallOutsNotFollowedByList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
## hello world
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void RequiresContentToFollow() => Collector.Diagnostics.Should().HaveCount(1)
.And.OnlyContain(c => c.Message.StartsWith("Code block with annotations is not followed by a list"));
}
public class ClassicCallOutsFollowedByAListWithOneParagraph(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
**OUTPUT:**
1. Marking the first callout
2. Marking the second callout
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void RendersExpectedHtml() =>
Html.ShouldBeHtml(
"""
<div class="highlight-csharp notranslate">
<div class="highlight">
<pre><code class="language-csharp">var x = 1;<span style="display: inline-block; width: 1ch"></span><span class="code-callout" data-index="1"></span>
var y = x - 2;
var z = y - 2;<span style="display: inline-block; width: 1ch"></span><span class="code-callout" data-index="2"></span>
</code></pre>
</div>
</div>
<p><strong>OUTPUT:</strong></p>
<ol class="code-callouts">
<li>Marking the first callout</li>
<li>Marking the second callout</li>
</ol>
"""
);
[Fact]
public void AllowsAParagraphInBetween() => Collector.Diagnostics.Should().BeEmpty();
}
public class ClassicCallOutsFollowedByListButWithTwoParagraphs(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
**OUTPUT:**
BLOCK TWO
1. Marking the first callout
2. Marking the second callout
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void RequiresContentToFollow() => Collector.Diagnostics.Should().HaveCount(1)
.And.OnlyContain(c => c.Message.StartsWith("More than one content block between code block with annotations and its list"));
}
public class ClassicCallOutsFollowedByListWithWrongCoung(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
1. Only marking the first callout
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void RequiresContentToFollow() => Collector.Diagnostics.Should().HaveCount(1)
.And.OnlyContain(c => c.Message.StartsWith("Code block has 2 callouts but the following list only has 1"));
}
public class ClassicCallOutsReuseHighlights(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2; <2>
var z = y - 2; <2>
""",
"""
1. The first
2. The second appears twice
"""
)
{
[Fact]
public void SeesTwoUniqueCallouts() => Block!.UniqueCallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void ParsesAllForLineInformation() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(3)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void RequiresContentToFollow() => Collector.Diagnostics.Should().BeEmpty();
}
public class ClassicCallOutWithTheRightListItems(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
receivers: <1>
# ...
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors: <2>
# ...
memory_limiter:
check_interval: 1s
limit_mib: 2000
batch:
exporters:
debug:
verbosity: detailed <3>
otlp: <4>
# Elastic APM server https endpoint without the "https://" prefix
endpoint: "${env:ELASTIC_APM_SERVER_ENDPOINT}" <5> <7>
headers:
# Elastic APM Server secret token
Authorization: "Bearer ${env:ELASTIC_APM_SECRET_TOKEN}" <6> <7>
service:
pipelines:
traces:
receivers: [otlp]
processors: [..., memory_limiter, batch]
exporters: [debug, otlp]
metrics:
receivers: [otlp]
processors: [..., memory_limiter, batch]
exporters: [debug, otlp]
logs: <8>
receivers: [otlp]
processors: [..., memory_limiter, batch]
exporters: [debug, otlp]
""",
"""
1. The receivers, like the OTLP receiver, that forward data emitted by APM agents, or the host metrics receiver.
2. We recommend using the Batch processor and the memory limiter processor. For more information, see recommended processors.
3. The debug exporter is helpful for troubleshooting, and supports configurable verbosity levels: basic (default), normal, and detailed.
4. Elastic {observability} endpoint configuration. APM Server supports a ProtoBuf payload via both the OTLP protocol over gRPC transport (OTLP/gRPC) and the OTLP protocol over HTTP transport (OTLP/HTTP). To learn more about these exporters, see the OpenTelemetry Collector documentation: OTLP/HTTP Exporter or OTLP/gRPC exporter. When adding an endpoint to an existing configuration an optional name component can be added, like otlp/elastic, to distinguish endpoints as described in the OpenTelemetry Collector Configuration Basics.
5. Hostname and port of the APM Server endpoint. For example, elastic-apm-server:8200.
6. Credential for Elastic APM secret token authorization (Authorization: "Bearer a_secret_token") or API key authorization (Authorization: "ApiKey an_api_key").
7. Environment-specific configuration parameters can be conveniently passed in as environment variables documented here (e.g. ELASTIC_APM_SERVER_ENDPOINT and ELASTIC_APM_SECRET_TOKEN).
8. [preview] To send OpenTelemetry logs to {stack} version 8.0+, declare a logs pipeline.
"""
)
{
[Fact]
public void ParsesClassicCallouts()
{
Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(9)
.And.OnlyContain(c => c.Text.StartsWith('<'));
Block!.UniqueCallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(8);
}
[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
}
public class MultipleCalloutsInOneLine(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; // <1>
var y = x - 2;
var z = y - 2; // <1> <2>
""",
"""
1. First callout
2. Second callout
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(3)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
}
public class CodeBlockWithChevronInsideCode(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
app.UseFilter<StopwatchFilter>(); <1>
app.UseFilter<CatchExceptionFilter>(); <2>
var x = 1; <1>
var y = x - 2;
var z = y - 2; <1> <2>
""",
"""
1. First callout
2. Second callout
"""
)
{
[Fact]
public void ParsesMagicCallOuts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(5)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
}
public class CodeBlockWithCommentBlocksThenList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
% TEST[s/"basque_keywords",//]
% TEST[s/\n$/\nstartyaml\n - compare_analyzers: {index: basque_example, first: basque, second: rebuilt_basque}\nendyaml\n/]
1. First callout
2. Second callout
"""
)
{
[Fact]
public void ParsesCallouts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2)
.And.OnlyContain(c => c.Text.StartsWith('<'));
[Fact]
public void HandlesCommentBlocksCorrectly() => Collector.Diagnostics.Should().BeEmpty();
[Fact]
public void RenderedHtmlContainsCallouts() =>
Html.ShouldContainHtml(
"""
<ol class="code-callouts">
<li>First callout</li>
<li>Second callout</li>
</ol>
"""
);
}
public class CodeBlockWithMultipleCommentTypesThenList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
% This is an HTML-style comment that starts with %
% TEST[catch:bad_request]
1. First callout
2. Second callout
"""
)
{
[Fact]
public void ParsesCallouts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2);
[Fact]
public void HandlesCommentBlocksCorrectly() => Collector.Diagnostics.Should().BeEmpty();
}
public class CodeBlockWithCommentBlocksParagraphThenList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
% TEST[s/"basque_keywords",//]
% TEST[catch:bad_request]
**This is an intermediate paragraph**
1. First callout
2. Second callout
"""
)
{
[Fact]
public void ParsesCallouts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2);
[Fact]
public void HandlesCommentBlocksAndParagraphCorrectly() => Collector.Diagnostics.Should().BeEmpty();
[Fact]
public void RendersIntermediateParagraph() =>
Html.ShouldContainHtml(
"""
<p><strong>This is an intermediate paragraph</strong></p>
<ol class="code-callouts">
<li>First callout</li>
<li>Second callout</li>
</ol>
"""
);
}
public class CodeBlockWithCommentBlocksTwoParagraphsThenList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
% TEST[s/"basque_keywords",//]
**This is an intermediate paragraph**
**This is a second paragraph which should cause an error**
1. First callout
2. Second callout
"""
)
{
[Fact]
public void ParsesCallouts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2);
[Fact]
public void EmitsErrorForTooManyParagraphs() => Collector.Diagnostics.Should().HaveCount(1)
.And.OnlyContain(c => c.Message.StartsWith("More than one content block between code block with annotations and its list"));
}
public class CodeBlockWithManyCommentBlocksNoList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
% TEST[s/"basque_keywords",//]
% TEST[s/\n$/\nstartyaml\n - compare_analyzers: {index: basque_example, first: basque, second: rebuilt_basque}\nendyaml\n/]
% TEST[catch:bad_request]
"""
)
{
[Fact]
public void ParsesCallouts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2);
[Fact]
public void EmitsErrorForNoList() => Collector.Diagnostics.Should().HaveCount(1)
.And.OnlyContain(c => c.Message.StartsWith("Code block with annotations is not followed by a list"));
}
public class CodeBlockWithCommentsAfterList(ITestOutputHelper output) : CodeBlockCallOutTests(output, "csharp",
"""
var x = 1; <1>
var y = x - 2;
var z = y - 2; <2>
""",
"""
1. First callout
2. Second callout
% TEST[s/"basque_keywords",//]
"""
)
{
[Fact]
public void ParsesCallouts() => Block!.CallOuts
.Should().NotBeNullOrEmpty()
.And.HaveCount(2);
[Fact]
public void HandlesCommentsCorrectly() => Collector.Diagnostics.Should().BeEmpty();
[Fact]
public void RenderedHtmlDoesNotContainComments() =>
Html.Should().NotContain("basque_keywords");
}