wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-markdown/src/parser/inline/tests/emphasis.rs@337ba67f65eaa17b44e371af7c0f0c761d6aa914

raw
Date Commit Message Author Files + -
2026-02-23 21:59 beginning on port over some of the changes from markdown-ppp since the f... wayverd 11 527 15
...

1use crate::{ast::*, parser::parse_markdown};
2
3#[test]
4fn emphasis1() {
5    let doc = parse_markdown("*foo bar*").unwrap();
6    assert_eq!(
7        doc,
8        Document {
9            blocks: vec![Block::Paragraph(vec![Inline::Emphasis(vec![
10                Inline::Text("foo bar".to_string())
11            ])])],
12        }
13    );
14}
15
16#[test]
17fn emphasis2() {
18    let doc = parse_markdown("* a *").unwrap();
19    assert_eq!(
20        doc,
21        Document {
22            blocks: vec![Block::List(List {
23                kind: ListKind::Bullet(ListBulletKind::Star),
24                items: vec![ListItem {
25                    task: None,
26                    blocks: vec![Block::Paragraph(vec![Inline::Text("a *".to_owned())])]
27                }]
28            })]
29        }
30    );
31}
32
33#[test]
34fn emphasis3() {
35    let doc = parse_markdown("foo ___bar___").unwrap();
36    assert_eq!(
37        doc,
38        Document {
39            blocks: vec![Block::Paragraph(vec![
40                Inline::Text("foo ".to_owned()),
41                Inline::Strong(vec![Inline::Emphasis(vec![Inline::Text("bar".to_owned())])])
42            ])]
43        }
44    );
45}
46
47#[test]
48fn emphasis4() {
49    let doc = parse_markdown("**foo ___bar___ baz**").unwrap();
50    assert_eq!(
51        doc,
52        Document {
53            blocks: vec![Block::Paragraph(vec![Inline::Strong(vec![
54                Inline::Text("foo ".to_owned()),
55                Inline::Strong(vec![Inline::Emphasis(vec![Inline::Text("bar".to_owned())])]),
56                Inline::Text(" baz".to_owned())
57            ])])]
58        }
59    );
60}
61
62#[test]
63fn emphasis_with_underscores_in_words() {
64    // Test case: PKG_CONFIG_PATH should not be parsed as PKG*CONFIG_PATH
65    let doc =
66        parse_markdown("Note that we set PKG_CONFIG_PATH only if it's not _already_ set").unwrap();
67
68    // Debug output
69    println!("Parsed document: {doc:?}");
70
71    // Expected: _already_ should be emphasized, PKG_CONFIG_PATH should remain as is
72    assert_eq!(
73        doc,
74        Document {
75            blocks: vec![Block::Paragraph(vec![
76                Inline::Text("Note that we set PKG_CONFIG_PATH only if it's not ".to_string()),
77                Inline::Emphasis(vec![Inline::Text("already".to_string())]),
78                Inline::Text(" set".to_string())
79            ])],
80        }
81    );
82}
83
84#[test]
85fn test_simple_underscore() {
86    let doc = parse_markdown("_already_").unwrap();
87
88    println!("Simple underscore: {doc:?}");
89
90    assert_eq!(
91        doc,
92        Document {
93            blocks: vec![Block::Paragraph(vec![Inline::Emphasis(vec![
94                Inline::Text("already".to_string())
95            ])])],
96        }
97    );
98}
99
100#[test]
101fn test_pkg_config() {
102    let doc = parse_markdown("PKG_CONFIG_PATH").unwrap();
103
104    println!("PKG_CONFIG_PATH: {doc:?}");
105
106    assert_eq!(
107        doc,
108        Document {
109            blocks: vec![Block::Paragraph(vec![Inline::Text(
110                "PKG_CONFIG_PATH".to_string()
111            )])],
112        }
113    );
114}
115
116#[test]
117fn test_multiple_env_vars_with_emphasis() {
118    let doc =
119        parse_markdown("Set PATH_TO_FILE and CMAKE_BUILD_TYPE to _debug_ for testing").unwrap();
120
121    assert_eq!(
122        doc,
123        Document {
124            blocks: vec![Block::Paragraph(vec![
125                Inline::Text("Set PATH_TO_FILE and CMAKE_BUILD_TYPE to ".to_string()),
126                Inline::Emphasis(vec![Inline::Text("debug".to_string())]),
127                Inline::Text(" for testing".to_string())
128            ])],
129        }
130    );
131}
132
133#[test]
134fn test_env_var_mixed_case() {
135    let doc = parse_markdown("Use my_custom_var for configuration").unwrap();
136
137    assert_eq!(
138        doc,
139        Document {
140            blocks: vec![Block::Paragraph(vec![Inline::Text(
141                "Use my_custom_var for configuration".to_string()
142            )])],
143        }
144    );
145}
146
147#[test]
148fn test_false_positive_prevention() {
149    // These should NOT be parsed as environment variables
150    let doc = parse_markdown("Text with _emphasis_ and __strong__ formatting").unwrap();
151
152    assert_eq!(
153        doc,
154        Document {
155            blocks: vec![Block::Paragraph(vec![
156                Inline::Text("Text with ".to_string()),
157                Inline::Emphasis(vec![Inline::Text("emphasis".to_string())]),
158                Inline::Text(" and ".to_string()),
159                Inline::Strong(vec![Inline::Text("strong".to_string())]),
160                Inline::Text(" formatting".to_string())
161            ])],
162        }
163    );
164}
165