Repository navigation
Expand file tree
/
Copy pathdocstring.rs
More file actions
1666 lines (1376 loc) · 49.3 KB
/
Copy pathdocstring.rs
File metadata and controls
1666 lines (1376 loc) · 49.3 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Docstring parsing utilities for language server features.
//!
//! This module provides functionality for extracting structured information from
//! Python docstrings, including parameter documentation for signature help.
//! Supports Google-style, NumPy-style, and reST/Sphinx-style docstrings.
//! There are no formal specifications for any of these formats, so the parsing
//! logic needs to be tolerant of variations.
mod document;
mod markdown;
use ruff_python_trivia::{PythonWhitespace, expand_tabs, leading_indentation};
use ruff_source_file::UniversalNewlines;
use crate::{FxIndexMap, MarkupKind};
/// A docstring which hasn't yet been interpreted or rendered
///
/// Used to ensure handlers of docstrings select a rendering mode.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Docstring(String);
impl Docstring {
/// Create a new docstring from the raw string literal contents
pub fn new(raw: String) -> Self {
Docstring(raw)
}
/// Render the docstring to the given markup format
pub fn render(&self, kind: MarkupKind) -> String {
match kind {
MarkupKind::PlainText => self.render_plaintext(),
MarkupKind::Markdown => self.render_markdown(),
}
}
/// Render the docstring for plaintext display
pub fn render_plaintext(&self) -> String {
documentation_trim(&self.0)
}
/// Render the docstring for markdown display
pub fn render_markdown(&self) -> String {
let trimmed = documentation_trim(&self.0);
markdown::render(&trimmed)
}
/// Extract parameter documentation from popular docstring formats.
/// Returns a map of parameter names to their documentation.
pub fn parameter_documentation(&self) -> FxIndexMap<String, String> {
let normalized_source = documentation_trim(&self.0);
document::parameter_documentation(&normalized_source)
}
}
/// Text extracted from within a larger docstring.
///
/// Unlike a complete docstring, a fragment has already had its surrounding indentation removed.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct DocstringFragment(String);
impl DocstringFragment {
pub fn new(raw: &str) -> Self {
Self(documentation_fragment_trim(raw))
}
pub fn render(&self, kind: MarkupKind) -> String {
match kind {
MarkupKind::PlainText => self.0.clone(),
MarkupKind::Markdown => self.render_markdown(),
}
}
}
/// Normalizes an extracted docstring fragment without removing meaningful relative indentation.
fn documentation_fragment_trim(docs: &str) -> String {
let expanded = expand_tabs(docs.trim_end());
let mut output = String::with_capacity(expanded.len());
for line in expanded.universal_newlines() {
output.push_str(line.as_str().trim_whitespace_end());
output.push('\n');
}
output
}
/// Normalizes tabs and trims a docstring as specified in PEP-0257
///
/// See: <https://peps.python.org/pep-0257/#handling-docstring-indentation>
fn documentation_trim(docs: &str) -> String {
// First apply tab expansion as we don't want tabs in our output. Python advances tabs to the
// next eight-column tab stop.
//
// We also trim off all trailing whitespace here to eliminate trailing newlines so we
// don't need to handle trailing blank lines later. We can't trim away leading
// whitespace yet, because we need to identify the first line and handle it specially.
let expanded = expand_tabs(docs.trim_end());
// Compute the minimum indention of all non-empty non-first lines
// and statistics about leading blank lines to help trim them later.
let mut min_indent = usize::MAX;
let mut leading_blank_lines = 0;
let mut is_first_line = true;
let mut found_non_blank_line = false;
for line_obj in expanded.universal_newlines() {
let line = line_obj.as_str();
let indent = leading_indentation(line);
if indent == line {
// Blank line
if !found_non_blank_line {
leading_blank_lines += 1;
}
} else {
// Non-blank line
found_non_blank_line = true;
// First line doesn't affect min-indent
if !is_first_line {
min_indent = min_indent.min(indent.len());
}
}
is_first_line = false;
}
let mut output = String::new();
let mut lines = expanded.universal_newlines();
// If the first line is non-blank then we need to include it *fully* trimmed
// As its indentation is ignored (effectively treated as having min_indent).
if leading_blank_lines == 0 {
if let Some(first_line) = lines.next() {
output.push_str(first_line.as_str().trim_whitespace());
output.push('\n');
}
}
// For the rest of the lines remove the minimum indent (if possible) and trailing whitespace.
//
// We computed min_indent by only counting python whitespace, and all python whitespace
// is ascii, so we can just remove that many bytes from the front.
for line_obj in lines.skip(leading_blank_lines) {
let line = line_obj.as_str();
let trimmed_line = line[min_indent.min(line.len())..].trim_whitespace_end();
output.push_str(trimmed_line);
output.push('\n');
}
output
}
#[cfg(test)]
mod tests {
use insta::Settings;
use insta::assert_snapshot;
use super::*;
fn bind_docstring_snapshot_filters() -> impl Drop {
let mut settings = Settings::clone_current();
// Markdown hard breaks are encoded as trailing spaces (`" \n"`), but many editors
// trim trailing whitespace in string literals. Replace them with `<HB>` in snapshots
// so tests are stable and the expected output stays readable.
settings.add_filter(" \n", "<HB>\n");
settings.bind_to_scope()
}
#[test]
fn expands_tabs_to_tab_stops_when_trimming_documentation() {
assert_snapshot!(
documentation_trim(
"\
Summary.
baseline
\tindented",
),
@"
Summary.
baseline
indented
"
);
}
// A nice doctest that is surrounded by prose
#[test]
fn dunder_escape() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Here _this_ and ___that__ should be escaped
Here *this* and **that** should be untouched
Here `this` and ``that`` should be untouched
Here `_this_` and ``__that__`` should be untouched
Here `_this_` ``__that__`` should be untouched
`_this_too_should_be_untouched_`
Here `_this_```__that__`` should be untouched but this_is_escaped
Here ``_this_```__that__` should be untouched but this_is_escaped
Here `_this_ and _that_ should be escaped (but isn't)
Here _this_ and _that_` should be escaped
`Here _this_ and _that_ should be escaped (but isn't)
Here _this_ and _that_ should be escaped`
Here ```_is_``__a__`_balanced_``_mess_```
Here ```_is_`````__a__``_random_````_mess__````
```_is_`````__a__``_random_````_mess__````
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r"
Here \_this\_ and \_\_\_that\_\_ should be escaped<HB>
Here *this* and **that** should be untouched<HB>
Here `this` and ``that`` should be untouched<HB>
<HB>
Here `_this_` and ``__that__`` should be untouched<HB>
Here `_this_` ``__that__`` should be untouched<HB>
`_this_too_should_be_untouched_`<HB>
<HB>
Here `_this_```__that__`` should be untouched but this\_is\_escaped<HB>
Here ``_this_```__that__` should be untouched but this\_is\_escaped<HB>
<HB>
Here `_this_ and _that_ should be escaped (but isn't)<HB>
Here \_this\_ and \_that\_` should be escaped<HB>
`Here _this_ and _that_ should be escaped (but isn't)<HB>
Here \_this\_ and \_that\_ should be escaped`<HB>
<HB>
Here ```_is_``__a__`_balanced_``_mess_```<HB>
Here ```_is_`````__a__``\_random\_````_mess__````<HB>
```_is_`````__a__``\_random\_````_mess__````
");
}
#[test]
fn html_escape() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Parse a URL into 6 components:
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
Markdown code fences keep literal HTML:
```text
<tag attr="value">content</tag>
```
So does `inline <code>`.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Parse a URL into 6 components:<HB>
<scheme>://<netloc>/<path>;<params>?<query>#<fragment><HB>
<HB>
Markdown code fences keep literal HTML:<HB>
<HB>
```text
<tag attr="value">content</tag>
```<HB>
<HB>
So does `inline <code>`.
"#);
}
// A literal block where the `::` is flush with the paragraph
// and should become `:`
#[test]
fn literal_colon() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Check out this great example code::
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
You love to see it.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Check out this great example code: <HB>
```````````python
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
```````````
You love to see it.
"#);
}
// A literal block where the `::` with the paragraph
// and should be erased
#[test]
fn literal_space() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Check out this great example code ::
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
You love to see it.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Check out this great example code <HB>
```````````python
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
```````````
You love to see it.
"#);
}
// A literal block where the `::` is floating
// and the whole line should be deleted
#[test]
fn literal_own_line() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Check out this great example code
::
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
You love to see it.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Check out this great example code<HB>
<HB>
```````````python
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
```````````
You love to see it.
"#);
}
// A literal block where the blank lines are missing
// and I have no idea what Should happen but let's record what Does
#[test]
fn literal_squeezed() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Check out this great example code::
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
You love to see it.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Check out this great example code:<HB>
```````````python
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
```````````
You love to see it.
"#);
}
// A literal block where the docstring just ends
// and we should tidy up
#[test]
fn literal_flush() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Check out this great example code::
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Check out this great example code: <HB>
```````````python
x_y = "hello"
if len(x_y) > 4:
print(x_y)
else:
print("too short :(")
print("done")
```````````
"#);
}
// `warning` and several other directives are special languages that should actually
// still be shown as text and not ```code```.
#[test]
fn warning_block() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
The thing you need to understand is that computers are hard.
.. warning::
Now listen here buckaroo you might have seen me say computers are hard,
and though "yeah I know computers are hard but NO you DON'T KNOW.
Listen:
- Computers
- Are
- Hard
Ok!?!?!?
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
The thing you need to understand is that computers are hard.<HB>
<HB>
**Warning:**<HB>
Now listen here buckaroo you might have seen me say computers are hard,<HB>
and though "yeah I know computers are hard but NO you DON'T KNOW.<HB>
<HB>
Listen:<HB>
<HB>
- Computers<HB>
- Are<HB>
- Hard<HB>
<HB>
Ok!?!?!?
"#);
}
// `warning` and several other directives are special languages that should actually
// still be shown as text and not ```code```.
#[test]
fn version_blocks() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Some much-updated docs
.. version-added:: 3.0
Function added
.. version-changed:: 4.0
The `spam` argument was added
.. version-changed:: 4.1
The `spam` argument is considered evil now.
You really shouldnt use it
And that's the docs
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
Some much-updated docs<HB>
<HB>
**Added in version 3.0:**<HB>
Function added<HB>
<HB>
**Changed in version 4.0:**<HB>
The `spam` argument was added<HB>
**Changed in version 4.1:**<HB>
The `spam` argument is considered evil now.<HB>
<HB>
You really shouldnt use it<HB>
<HB>
And that's the docs
");
}
// I don't know if this is valid syntax but we preserve stuff before non-code blocks like
// `..deprecated ::`
#[test]
fn deprecated_prefix_gunk() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
wow this is some changes .. deprecated:: 1.2.3
x = 2
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
**wow this is some changes Deprecated since version 1.2.3:**<HB>
x = 2
");
}
// We should not parse the contents of a markdown codefence
#[test]
fn explicit_markdown_block_with_ps1_contents() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
```python
>>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing
```
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func:<HB>
<HB>
```python
>>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing
```
");
}
// We should not parse the contents of a markdown codefence
#[test]
fn explicit_markdown_block_with_underscore_contents_tick() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
`````python
x_y = thing_do();
``` # this should't close the fence!
a_b = other_thing();
`````
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func:<HB>
<HB>
`````python
x_y = thing_do();
``` # this should't close the fence!
a_b = other_thing();
`````
");
}
// `~~~` also starts a markdown codefence
#[test]
fn explicit_markdown_block_with_underscore_contents_tilde() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
~~~~~python
x_y = thing_do();
~~~ # this should't close the fence!
a_b = other_thing();
~~~~~
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func:<HB>
<HB>
~~~~~python
x_y = thing_do();
~~~ # this should't close the fence!
a_b = other_thing();
~~~~~
");
}
// If an explicit markdown codefence is indented, eat the indent so it renders
// "the way the user expects" (as written this is basically invalid markdown,
// but it's nice if we handle it anyway because it makes visual sense).
#[test]
fn explicit_markdown_block_with_indent_tick() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func...
Returns:
Some details
`````python
x_y = thing_do();
``` # this should't close the fence!
a_b = other_thing();
`````
And so on.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func...
## Returns
Some details
`````python
x_y = thing_do();
``` # this should't close the fence!
a_b = other_thing();
`````<HB>
And so on.
");
}
// If an explicit markdown codefence is indented, eat the indent so it renders
// "the way the user expects" (as written this is basically invalid markdown,
// but it's nice if we handle it anyway because it makes visual sense).
#[test]
fn explicit_markdown_block_with_indent_tilde() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func...
Returns:
Some details
~~~~~~python
x_y = thing_do();
~~~ # this should't close the fence!
a_b = other_thing();
~~~~~~
And so on.
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func...
## Returns
Some details
~~~~~~python
x_y = thing_do();
~~~ # this should't close the fence!
a_b = other_thing();
~~~~~~<HB>
And so on.
");
}
// What do we do when we hit the end of the docstring with an unclosed markdown block?
#[test]
fn explicit_markdown_block_with_unclosed_fence_tick() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
````python
x_y = thing_do();
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func:<HB>
<HB>
````python
x_y = thing_do();
````
");
}
// What do we do when we hit the end of the docstring with an unclosed markdown block?
#[test]
fn explicit_markdown_block_with_unclosed_fence_tilde() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
~~~~~python
x_y = thing_do();
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func:<HB>
<HB>
~~~~~python
x_y = thing_do();
~~~~~
");
}
// Demonstration of where we're unreasonably lax about markdown block parsing.
// It's fine to break this test, it's not particularly intentional behaviour.
#[test]
fn explicit_markdown_block_messy_corners_tick() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
``````we still think this is a codefence```
x_y = thing_do();
```````````` and are sloppy as heck with indentation and closing shrugggg
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r"
My cool func:<HB>
<HB>
``````we still think this is a codefence```<HB>
x\_y = thing\_do();<HB>
```````````` and are sloppy as heck with indentation and closing shrugggg
````````````
");
}
// Demonstration of where we're unreasonably lax about markdown block parsing.
// It's fine to break this test, it's not particularly intentional behaviour.
#[test]
fn explicit_markdown_block_messy_corners_tilde() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
My cool func:
~~~~~~we still think this is a codefence~~~
x_y = thing_do();
~~~~~~~~~~~~~ and are sloppy as heck with indentation and closing shrugggg
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
My cool func:<HB>
<HB>
~~~~~~we still think this is a codefence~~~
x_y = thing_do();
~~~~~~~~~~~~~ and are sloppy as heck with indentation and closing shrugggg
");
}
// `.. code::` is a literal block and the `.. code::` should be deleted
#[test]
fn code_block() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Here's some code!
.. code::
def main() {
print("hello world!")
}
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Here's some code!<HB>
<HB>
<HB>
```````````python
def main() {
print("hello world!")
}
```````````
"#);
}
// `.. code:: rust` is a literal block with rust syntax highlighting
#[test]
fn code_block_lang() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Here's some Rust code!
.. code:: rust
fn main() {
println!("hello world!");
}
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Here's some Rust code!<HB>
<HB>
<HB>
```````````rust
fn main() {
println!("hello world!");
}
```````````
"#);
}
// I don't know if this is valid syntax but we preserve stuff before `..code ::`
#[test]
fn code_block_prefix_gunk() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
wow this is some code.. code:: abc
x = 2
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
wow this is some code<HB>
```````````abc
x = 2
```````````
");
}
// `.. asdgfhjkl-unknown::` is treated the same as `.. code::`
#[test]
fn unknown_block() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Here's some code!
.. asdgfhjkl-unknown::
fn main() {
println!("hello world!");
}
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Here's some code!<HB>
<HB>
<HB>
```````````python
fn main() {
println!("hello world!");
}
```````````
"#);
}
// `.. asdgfhjkl-unknown:: rust` is treated the same as `.. code:: rust`
#[test]
fn unknown_block_lang() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
Here's some Rust code!
.. asdgfhjkl-unknown:: rust
fn main() {
print("hello world!")
}
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @r#"
Here's some Rust code!<HB>
<HB>
<HB>
```````````rust
fn main() {
print("hello world!")
}
```````````
"#);
}
// A nice doctest that is surrounded by prose
#[test]
fn doctest_simple() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
This is a function description
>>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing
As you can see it did the thing!
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
This is a function description<HB>
<HB>
```````````python
>>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing
```````````<HB>
As you can see it did the thing!
");
}
// A nice doctest that is surrounded by prose with an indent
#[test]
fn doctest_simple_indent() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#"
This is a function description
>>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing
As you can see it did the thing!
"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
This is a function description<HB>
<HB>
```````````python
>>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing
```````````<HB>
As you can see it did the thing!
");
}
// A doctest that has nothing around it
#[test]
fn doctest_flush() {
let _snap = bind_docstring_snapshot_filters();
let docstring = r#">>> thing.do_thing()
wow it did the thing
>>> thing.do_other_thing()
it sure did the thing"#;
let docstring = Docstring::new(docstring.to_owned());
assert_snapshot!(docstring.render_markdown(), @"
```````````python
>>> thing.do_thing()