Skip to main content

api_testing_core/
markdown.rs

1use crate::Result;
2use nils_common::markdown as common_markdown;
3
4/// Format JSON similar to `jq -S .` (stable key order, pretty printed).
5pub fn format_json_pretty_sorted(value: &serde_json::Value) -> Result<String> {
6    Ok(common_markdown::format_json_pretty_sorted(value)?)
7}
8
9pub fn heading(level: u8, text: &str) -> String {
10    common_markdown::heading(level, text)
11}
12
13pub fn code_block(lang: &str, body: &str) -> String {
14    common_markdown::code_block(lang, body)
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    use pretty_assertions::assert_eq;
21
22    #[test]
23    fn markdown_code_block_is_newline_stable() {
24        assert_eq!(code_block("json", "{ }"), "```json\n{ }\n```\n");
25        assert_eq!(code_block("json", "{ }\n"), "```json\n{ }\n```\n");
26    }
27
28    #[test]
29    fn markdown_heading_trims_and_clamps_level() {
30        assert_eq!(heading(1, " Title "), "# Title\n");
31        assert_eq!(heading(9, "Title"), "###### Title\n");
32    }
33
34    #[test]
35    fn json_format_sorts_keys_recursively() {
36        let v = serde_json::json!({"b": 1, "a": {"d": 4, "c": 3}});
37        let s = format_json_pretty_sorted(&v).unwrap();
38        assert_eq!(
39            s,
40            "{\n  \"a\": {\n    \"c\": 3,\n    \"d\": 4\n  },\n  \"b\": 1\n}"
41        );
42    }
43}