Skip to content

Commit

Permalink
Add snapshot tests for classed highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOnlyMrCat committed Jul 5, 2023
1 parent c1a26ad commit a493573
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 24 deletions.
95 changes: 71 additions & 24 deletions components/markdown/tests/codeblocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ use config::Config;

mod common;

fn render_codeblock(content: &str, highlight_code: bool) -> String {
enum HighlightMode {
None,
Inlined,
Classed,
}

fn render_codeblock(content: &str, highlight_mode: HighlightMode) -> String {
let mut config = Config::default_for_test();
config.markdown.highlight_code = highlight_code;
match highlight_mode {
HighlightMode::None => {}
HighlightMode::Inlined => {
config.markdown.highlight_code = true;
}
HighlightMode::Classed => {
config.markdown.highlight_code = true;
config.markdown.highlight_theme = "css".to_owned();
}
}
common::render_with_config(content, config).unwrap().body
}

Expand All @@ -17,7 +32,7 @@ foo
bar
```
"#,
false,
HighlightMode::None,
);
insta::assert_snapshot!(body);
}
Expand All @@ -33,7 +48,7 @@ baz
bat
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -49,7 +64,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -65,7 +80,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -81,7 +96,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -97,7 +112,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
let body2 = render_codeblock(
r#"
Expand All @@ -108,7 +123,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
assert_eq!(body, body2);
}
Expand All @@ -124,7 +139,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -140,7 +155,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -156,7 +171,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -172,7 +187,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -188,7 +203,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -204,7 +219,7 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -220,7 +235,23 @@ bar
baz
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}

#[test]
fn can_highlight_with_classes() {
let body = render_codeblock(
r#"
```html,hl_lines=3-4
<div id="custom-attr">
<a href="javascript:void(0);"
>With a line break in the middle of the tag</a>
</div>
```
"#,
HighlightMode::Classed,
);
insta::assert_snapshot!(body);
}
Expand All @@ -234,14 +265,14 @@ foo
bar
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}

#[test]
fn can_add_line_numbers_windows_eol() {
let body = render_codeblock("```linenos\r\nfoo\r\nbar\r\n```\r\n", true);
let body = render_codeblock("```linenos\r\nfoo\r\nbar\r\n```\r\n", HighlightMode::Inlined);
insta::assert_snapshot!(body);
}

Expand All @@ -254,7 +285,7 @@ foo
bar
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -268,7 +299,23 @@ foo
bar
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}

#[test]
fn can_add_line_numbers_with_classes() {
let body = render_codeblock(
r#"
```html,linenos
<div id="custom-attr">
<a href="javascript:void(0);"
>With a line break in the middle of the tag</a>
</div>
```
"#,
HighlightMode::Classed,
);
insta::assert_snapshot!(body);
}
Expand All @@ -283,7 +330,7 @@ fn can_render_shortcode_in_codeblock() {
</div>
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -300,7 +347,7 @@ text2
text3
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -323,7 +370,7 @@ A quote
<!-- end text goes here -->
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Expand All @@ -337,7 +384,7 @@ foo
bar
```
"#,
true,
HighlightMode::Inlined,
);
insta::assert_snapshot!(body);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: components/markdown/tests/codeblocks.rs
expression: body
---
<pre data-linenos data-lang="html" class="language-html z-code"><code class="language-html" data-lang="html"><table><tbody><tr><td>1</td><td><span class="z-text z-html z-basic"><span class="z-meta z-tag z-block z-any z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;</span><span class="z-entity z-name z-tag z-block z-any z-html">div</span> <span class="z-meta z-attribute-with-value z-id z-html"><span class="z-entity z-other z-attribute-name z-id z-html">id</span><span class="z-punctuation z-separator z-key-value z-html">=</span><span class="z-string z-quoted z-double z-html"><span class="z-punctuation z-definition z-string z-begin z-html">&quot;</span></span><span class="z-string z-quoted z-double z-html"><span class="z-meta z-toc-list z-id z-html">custom-attr</span><span class="z-punctuation z-definition z-string z-end z-html">&quot;</span></span></span><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>
</span></td></tr><tr><td>2</td><td><span class="z-text z-html z-basic"><span class="z-meta z-tag z-inline z-a z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;</span><span class="z-entity z-name z-tag z-inline z-a z-html">a</span> <span class="z-meta z-attribute-with-value z-html"><span class="z-entity z-other z-attribute-name z-html">href</span><span class="z-punctuation z-separator z-key-value z-html">=</span><span class="z-string z-quoted z-double z-html"><span class="z-punctuation z-definition z-string z-begin z-html">&quot;</span>javascript:void(0);<span class="z-punctuation z-definition z-string z-end z-html">&quot;</span></span></span>
</span></span></td></tr><tr><td>3</td><td><span class="z-text z-html z-basic"><span class="z-meta z-tag z-inline z-a z-html"><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>With a line break in the middle of the tag<span class="z-meta z-tag z-inline z-a z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;/</span><span class="z-entity z-name z-tag z-inline z-a z-html">a</span><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>
</span></td></tr><tr><td>4</td><td><span class="z-text z-html z-basic"><span class="z-meta z-tag z-block z-any z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;/</span><span class="z-entity z-name z-tag z-block z-any z-html">div</span><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>
</span></td></tr></tbody></table></code></pre>

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: components/markdown/tests/codeblocks.rs
expression: body
---
<pre data-lang="html" class="language-html z-code"><code class="language-html" data-lang="html"><span class="z-text z-html z-basic"><span class="z-meta z-tag z-block z-any z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;</span><span class="z-entity z-name z-tag z-block z-any z-html">div</span> <span class="z-meta z-attribute-with-value z-id z-html"><span class="z-entity z-other z-attribute-name z-id z-html">id</span><span class="z-punctuation z-separator z-key-value z-html">=</span><span class="z-string z-quoted z-double z-html"><span class="z-punctuation z-definition z-string z-begin z-html">&quot;</span></span><span class="z-string z-quoted z-double z-html"><span class="z-meta z-toc-list z-id z-html">custom-attr</span><span class="z-punctuation z-definition z-string z-end z-html">&quot;</span></span></span><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>
</span><span class="z-text z-html z-basic"><span class="z-meta z-tag z-inline z-a z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;</span><span class="z-entity z-name z-tag z-inline z-a z-html">a</span> <span class="z-meta z-attribute-with-value z-html"><span class="z-entity z-other z-attribute-name z-html">href</span><span class="z-punctuation z-separator z-key-value z-html">=</span><span class="z-string z-quoted z-double z-html"><span class="z-punctuation z-definition z-string z-begin z-html">&quot;</span>javascript:void(0);<span class="z-punctuation z-definition z-string z-end z-html">&quot;</span></span></span>
</span></span><mark><span class="z-text z-html z-basic"><span class="z-meta z-tag z-inline z-a z-html"><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>With a line break in the middle of the tag<span class="z-meta z-tag z-inline z-a z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;/</span><span class="z-entity z-name z-tag z-inline z-a z-html">a</span><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>
</span></mark><mark><span class="z-text z-html z-basic"><span class="z-meta z-tag z-block z-any z-html"><span class="z-punctuation z-definition z-tag z-begin z-html">&lt;/</span><span class="z-entity z-name z-tag z-block z-any z-html">div</span><span class="z-punctuation z-definition z-tag z-end z-html">&gt;</span></span>
</span></mark></code></pre>

0 comments on commit a493573

Please sign in to comment.