move sorted_template and sorted_json tests
This commit is contained in:
parent
f1a996c39b
commit
17c89239d9
5 changed files with 271 additions and 272 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
119
src/librustdoc/html/render/sorted_json/tests.rs
Normal file
119
src/librustdoc/html/render/sorted_json/tests.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -134,3 +134,6 @@ impl fmt::Display for Error {
|
|||
write!(f, "invalid template")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
|||
148
src/librustdoc/html/render/sorted_template/tests.rs
Normal file
148
src/librustdoc/html/render/sorted_template/tests.rs
Normal file
|
|
@ -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));
|
||||
}
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue