diff options
| -rw-r--r-- | src/librustdoc/clean/types.rs | 2 | ||||
| -rw-r--r-- | src/librustdoc/html/render/sorted_json/tests.rs | 119 | ||||
| -rw-r--r-- | src/librustdoc/html/render/sorted_template.rs | 3 | ||||
| -rw-r--r-- | src/librustdoc/html/render/sorted_template/tests.rs | 148 | ||||
| -rw-r--r-- | src/librustdoc/html/render/tests.rs | 271 |
5 files changed, 271 insertions, 272 deletions
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 542e810b5cf..4850500a1bf 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -128,7 +128,7 @@ pub(crate) struct ExternalCrate { } impl ExternalCrate { - pub(crate) const LOCAL: Self = Self { crate_num: LOCAL_CRATE }; + const LOCAL: Self = Self { crate_num: LOCAL_CRATE }; #[inline] pub(crate) fn def_id(&self) -> DefId { diff --git a/src/librustdoc/html/render/sorted_json/tests.rs b/src/librustdoc/html/render/sorted_json/tests.rs new file mode 100644 index 00000000000..1e72c6f614c --- /dev/null +++ b/src/librustdoc/html/render/sorted_json/tests.rs @@ -0,0 +1,119 @@ +use super::super::sorted_json::*; + +fn check(json: SortedJson, serialized: &str) { + assert_eq!(json.to_string(), serialized); + assert_eq!(serde_json::to_string(&json).unwrap(), serialized); + + let json = json.to_string(); + let json: SortedJson = serde_json::from_str(&json).unwrap(); + + assert_eq!(json.to_string(), serialized); + assert_eq!(serde_json::to_string(&json).unwrap(), serialized); + + let json = serde_json::to_string(&json).unwrap(); + let json: SortedJson = serde_json::from_str(&json).unwrap(); + + assert_eq!(json.to_string(), serialized); + assert_eq!(serde_json::to_string(&json).unwrap(), serialized); +} + +// Test this basic are needed because we are testing that our Display impl + serialize impl don't +// nest everything in extra level of string. We also are testing round trip. +#[test] +fn escape_json_number() { + let json = SortedJson::serialize(3); + let json = EscapedJson::from(json); + assert_eq!(format!("{json}"), "3"); +} + +#[test] +fn escape_json_single_quote() { + let json = SortedJson::serialize("he's"); + let json = EscapedJson::from(json); + assert_eq!(format!("{json}"), r#""he\'s""#); +} + +#[test] +fn escape_json_array() { + let json = SortedJson::serialize([1, 2, 3]); + let json = EscapedJson::from(json); + assert_eq!(format!("{json}"), r#"[1,2,3]"#); +} + +#[test] +fn escape_json_string() { + let json = SortedJson::serialize(r#"he"llo"#); + let json = EscapedJson::from(json); + assert_eq!(format!("{json}"), r#""he\\\"llo""#); +} + +#[test] +fn escape_json_string_escaped() { + let json = SortedJson::serialize(r#"he\"llo"#); + let json = EscapedJson::from(json); + assert_eq!(format!("{json}"), r#""he\\\\\\\"llo""#); +} + +#[test] +fn escape_json_string_escaped_escaped() { + let json = SortedJson::serialize(r#"he\\"llo"#); + let json = EscapedJson::from(json); + assert_eq!(format!("{json}"), r#""he\\\\\\\\\\\"llo""#); +} + +#[test] +fn number() { + let json = SortedJson::serialize(3); + let serialized = "3"; + check(json, serialized); +} + +#[test] +fn boolean() { + let json = SortedJson::serialize(true); + let serialized = "true"; + check(json, serialized); +} + +#[test] +fn string() { + let json = SortedJson::serialize("he\"llo"); + let serialized = r#""he\"llo""#; + check(json, serialized); +} + +#[test] +fn serialize_array() { + let json = SortedJson::serialize([3, 1, 2]); + let serialized = "[3,1,2]"; + check(json, serialized); +} + +#[test] +fn sorted_array() { + let items = ["c", "a", "b"]; + let serialized = r#"["a","b","c"]"#; + let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect(); + let json = SortedJson::array(items); + check(json, serialized); +} + +#[test] +fn nested_array() { + let a = SortedJson::serialize(3); + let b = SortedJson::serialize(2); + let c = SortedJson::serialize(1); + let d = SortedJson::serialize([1, 3, 2]); + let json = SortedJson::array([a, b, c, d]); + let serialized = r#"[1,2,3,[1,3,2]]"#; + check(json, serialized); +} + +#[test] +fn array_unsorted() { + let items = ["c", "a", "b"]; + let serialized = r#"["c","a","b"]"#; + let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect(); + let json = SortedJson::array_unsorted(items); + check(json, serialized); +} diff --git a/src/librustdoc/html/render/sorted_template.rs b/src/librustdoc/html/render/sorted_template.rs index 95240616b01..8e0a2ee0fd4 100644 --- a/src/librustdoc/html/render/sorted_template.rs +++ b/src/librustdoc/html/render/sorted_template.rs @@ -134,3 +134,6 @@ impl fmt::Display for Error { write!(f, "invalid template") } } + +#[cfg(test)] +mod tests; diff --git a/src/librustdoc/html/render/sorted_template/tests.rs b/src/librustdoc/html/render/sorted_template/tests.rs new file mode 100644 index 00000000000..04553f65a21 --- /dev/null +++ b/src/librustdoc/html/render/sorted_template/tests.rs @@ -0,0 +1,148 @@ +use super::super::sorted_template::*; +use std::str::FromStr; + +fn is_comment_js(s: &str) -> bool { + s.starts_with("//") +} + +fn is_comment_html(s: &str) -> bool { + // not correct but good enough for these tests + s.starts_with("<!--") && s.ends_with("-->") +} + +#[test] +fn html_from_empty() { + let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>hello</p>", "<p>world</p>"]; + let mut template = SortedTemplate::<Html>::before_after("", ""); + for insert in inserts { + template.append(insert.to_string()); + } + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, "<p>hello</p><p>kind</p><p>world</p>"); + assert!(is_comment_html(end)); + assert!(!end.contains("\n")); +} + +#[test] +fn html_page() { + let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>world</p>"]; + let before = "<html><head></head><body>"; + let after = "</body>"; + let mut template = SortedTemplate::<Html>::before_after(before, after); + for insert in inserts { + template.append(insert.to_string()); + } + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, format!("{before}{}{after}", inserts.join(""))); + assert!(is_comment_html(end)); + assert!(!end.contains("\n")); +} + +#[test] +fn js_from_empty() { + let inserts = ["1", "2", "2", "2", "3", "1"]; + let mut template = SortedTemplate::<Js>::before_after("", ""); + for insert in inserts { + template.append(insert.to_string()); + } + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, "1,2,3"); + assert!(is_comment_js(end)); + assert!(!end.contains("\n")); +} + +#[test] +fn js_empty_array() { + let template = SortedTemplate::<Js>::before_after("[", "]"); + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, format!("[]")); + assert!(is_comment_js(end)); + assert!(!end.contains("\n")); +} + +#[test] +fn js_number_array() { + let inserts = ["1", "2", "3"]; + let mut template = SortedTemplate::<Js>::before_after("[", "]"); + for insert in inserts { + template.append(insert.to_string()); + } + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, format!("[1,2,3]")); + assert!(is_comment_js(end)); + assert!(!end.contains("\n")); +} + +#[test] +fn magic_js_number_array() { + let inserts = ["1", "1"]; + let mut template = SortedTemplate::<Js>::magic("[#]", "#").unwrap(); + for insert in inserts { + template.append(insert.to_string()); + } + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, format!("[1]")); + assert!(is_comment_js(end)); + assert!(!end.contains("\n")); +} + +#[test] +fn round_trip_js() { + let inserts = ["1", "2", "3"]; + let mut template = SortedTemplate::<Js>::before_after("[", "]"); + for insert in inserts { + template.append(insert.to_string()); + } + let template1 = format!("{template}"); + let mut template = SortedTemplate::<Js>::from_str(&template1).unwrap(); + assert_eq!(template1, format!("{template}")); + template.append("4".to_string()); + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, "[1,2,3,4]"); + assert!(is_comment_js(end)); +} + +#[test] +fn round_trip_html() { + let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>world</p>", "<p>kind</p>"]; + let before = "<html><head></head><body>"; + let after = "</body>"; + let mut template = SortedTemplate::<Html>::before_after(before, after); + template.append(inserts[0].to_string()); + template.append(inserts[1].to_string()); + let template = format!("{template}"); + let mut template = SortedTemplate::<Html>::from_str(&template).unwrap(); + template.append(inserts[2].to_string()); + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, format!("{before}<p>hello</p><p>kind</p><p>world</p>{after}")); + assert!(is_comment_html(end)); +} + +#[test] +fn blank_js() { + let inserts = ["1", "2", "3"]; + let template = SortedTemplate::<Js>::before_after("", ""); + let template = format!("{template}"); + let (t, _) = template.rsplit_once("\n").unwrap(); + assert_eq!(t, ""); + let mut template = SortedTemplate::<Js>::from_str(&template).unwrap(); + for insert in inserts { + template.append(insert.to_string()); + } + let template1 = format!("{template}"); + let mut template = SortedTemplate::<Js>::from_str(&template1).unwrap(); + assert_eq!(template1, format!("{template}")); + template.append("4".to_string()); + let template = format!("{template}"); + let (template, end) = template.rsplit_once("\n").unwrap(); + assert_eq!(template, "1,2,3,4"); + assert!(is_comment_js(end)); +} diff --git a/src/librustdoc/html/render/tests.rs b/src/librustdoc/html/render/tests.rs index 16e67b0f118..4a9724a6f84 100644 --- a/src/librustdoc/html/render/tests.rs +++ b/src/librustdoc/html/render/tests.rs @@ -52,274 +52,3 @@ fn test_all_types_prints_header_once() { assert_eq!(1, buffer.into_inner().matches("List of all items").count()); } - -mod sorted_json { - use super::super::sorted_json::*; - - fn check(json: SortedJson, serialized: &str) { - assert_eq!(json.to_string(), serialized); - assert_eq!(serde_json::to_string(&json).unwrap(), serialized); - - let json = json.to_string(); - let json: SortedJson = serde_json::from_str(&json).unwrap(); - - assert_eq!(json.to_string(), serialized); - assert_eq!(serde_json::to_string(&json).unwrap(), serialized); - - let json = serde_json::to_string(&json).unwrap(); - let json: SortedJson = serde_json::from_str(&json).unwrap(); - - assert_eq!(json.to_string(), serialized); - assert_eq!(serde_json::to_string(&json).unwrap(), serialized); - } - - #[test] - fn escape_json_number() { - let json = SortedJson::serialize(3); - let json = EscapedJson::from(json); - assert_eq!(format!("{json}"), "3"); - } - - #[test] - fn escape_json_single_quote() { - let json = SortedJson::serialize("he's"); - let json = EscapedJson::from(json); - assert_eq!(format!("{json}"), r#""he\'s""#); - } - - #[test] - fn escape_json_array() { - let json = SortedJson::serialize([1, 2, 3]); - let json = EscapedJson::from(json); - assert_eq!(format!("{json}"), r#"[1,2,3]"#); - } - - #[test] - fn escape_json_string() { - let json = SortedJson::serialize(r#"he"llo"#); - let json = EscapedJson::from(json); - assert_eq!(format!("{json}"), r#""he\\\"llo""#); - } - - #[test] - fn escape_json_string_escaped() { - let json = SortedJson::serialize(r#"he\"llo"#); - let json = EscapedJson::from(json); - assert_eq!(format!("{json}"), r#""he\\\\\\\"llo""#); - } - - #[test] - fn escape_json_string_escaped_escaped() { - let json = SortedJson::serialize(r#"he\\"llo"#); - let json = EscapedJson::from(json); - assert_eq!(format!("{json}"), r#""he\\\\\\\\\\\"llo""#); - } - - #[test] - fn number() { - let json = SortedJson::serialize(3); - let serialized = "3"; - check(json, serialized); - } - - #[test] - fn boolean() { - let json = SortedJson::serialize(true); - let serialized = "true"; - check(json, serialized); - } - - #[test] - fn string() { - let json = SortedJson::serialize("he\"llo"); - let serialized = r#""he\"llo""#; - check(json, serialized); - } - - #[test] - fn serialize_array() { - let json = SortedJson::serialize([3, 1, 2]); - let serialized = "[3,1,2]"; - check(json, serialized); - } - - #[test] - fn sorted_array() { - let items = ["c", "a", "b"]; - let serialized = r#"["a","b","c"]"#; - let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect(); - let json = SortedJson::array(items); - check(json, serialized); - } - - #[test] - fn nested_array() { - let a = SortedJson::serialize(3); - let b = SortedJson::serialize(2); - let c = SortedJson::serialize(1); - let d = SortedJson::serialize([1, 3, 2]); - let json = SortedJson::array([a, b, c, d]); - let serialized = r#"[1,2,3,[1,3,2]]"#; - check(json, serialized); - } - - #[test] - fn array_unsorted() { - let items = ["c", "a", "b"]; - let serialized = r#"["c","a","b"]"#; - let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect(); - let json = SortedJson::array_unsorted(items); - check(json, serialized); - } -} - -mod sorted_template { - use super::super::sorted_template::*; - use std::str::FromStr; - - fn is_comment_js(s: &str) -> bool { - s.starts_with("//") - } - - fn is_comment_html(s: &str) -> bool { - // not correct but good enough for these tests - s.starts_with("<!--") && s.ends_with("-->") - } - - #[test] - fn html_from_empty() { - let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>hello</p>", "<p>world</p>"]; - let mut template = SortedTemplate::<Html>::before_after("", ""); - for insert in inserts { - template.append(insert.to_string()); - } - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, "<p>hello</p><p>kind</p><p>world</p>"); - assert!(is_comment_html(end)); - assert!(!end.contains("\n")); - } - - #[test] - fn html_page() { - let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>world</p>"]; - let before = "<html><head></head><body>"; - let after = "</body>"; - let mut template = SortedTemplate::<Html>::before_after(before, after); - for insert in inserts { - template.append(insert.to_string()); - } - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, format!("{before}{}{after}", inserts.join(""))); - assert!(is_comment_html(end)); - assert!(!end.contains("\n")); - } - - #[test] - fn js_from_empty() { - let inserts = ["1", "2", "2", "2", "3", "1"]; - let mut template = SortedTemplate::<Js>::before_after("", ""); - for insert in inserts { - template.append(insert.to_string()); - } - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, "1,2,3"); - assert!(is_comment_js(end)); - assert!(!end.contains("\n")); - } - - #[test] - fn js_empty_array() { - let template = SortedTemplate::<Js>::before_after("[", "]"); - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, format!("[]")); - assert!(is_comment_js(end)); - assert!(!end.contains("\n")); - } - - #[test] - fn js_number_array() { - let inserts = ["1", "2", "3"]; - let mut template = SortedTemplate::<Js>::before_after("[", "]"); - for insert in inserts { - template.append(insert.to_string()); - } - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, format!("[1,2,3]")); - assert!(is_comment_js(end)); - assert!(!end.contains("\n")); - } - - #[test] - fn magic_js_number_array() { - let inserts = ["1", "1"]; - let mut template = SortedTemplate::<Js>::magic("[#]", "#").unwrap(); - for insert in inserts { - template.append(insert.to_string()); - } - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, format!("[1]")); - assert!(is_comment_js(end)); - assert!(!end.contains("\n")); - } - - #[test] - fn round_trip_js() { - let inserts = ["1", "2", "3"]; - let mut template = SortedTemplate::<Js>::before_after("[", "]"); - for insert in inserts { - template.append(insert.to_string()); - } - let template1 = format!("{template}"); - let mut template = SortedTemplate::<Js>::from_str(&template1).unwrap(); - assert_eq!(template1, format!("{template}")); - template.append("4".to_string()); - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, "[1,2,3,4]"); - assert!(is_comment_js(end)); - } - - #[test] - fn round_trip_html() { - let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>world</p>", "<p>kind</p>"]; - let before = "<html><head></head><body>"; - let after = "</body>"; - let mut template = SortedTemplate::<Html>::before_after(before, after); - template.append(inserts[0].to_string()); - template.append(inserts[1].to_string()); - let template = format!("{template}"); - let mut template = SortedTemplate::<Html>::from_str(&template).unwrap(); - template.append(inserts[2].to_string()); - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, format!("{before}<p>hello</p><p>kind</p><p>world</p>{after}")); - assert!(is_comment_html(end)); - } - - #[test] - fn blank_js() { - let inserts = ["1", "2", "3"]; - let template = SortedTemplate::<Js>::before_after("", ""); - let template = format!("{template}"); - let (t, _) = template.rsplit_once("\n").unwrap(); - assert_eq!(t, ""); - let mut template = SortedTemplate::<Js>::from_str(&template).unwrap(); - for insert in inserts { - template.append(insert.to_string()); - } - let template1 = format!("{template}"); - let mut template = SortedTemplate::<Js>::from_str(&template1).unwrap(); - assert_eq!(template1, format!("{template}")); - template.append("4".to_string()); - let template = format!("{template}"); - let (template, end) = template.rsplit_once("\n").unwrap(); - assert_eq!(template, "1,2,3,4"); - assert!(is_comment_js(end)); - } -} |
