Skip to content

Commit 91ad4a2

Browse files
committed
move sorted_template and sorted_json tests
1 parent 47e3169 commit 91ad4a2

File tree

5 files changed

+271
-272
lines changed

5 files changed

+271
-272
lines changed

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub(crate) struct ExternalCrate {
128128
}
129129

130130
impl ExternalCrate {
131-
pub(crate) const LOCAL: Self = Self { crate_num: LOCAL_CRATE };
131+
const LOCAL: Self = Self { crate_num: LOCAL_CRATE };
132132

133133
#[inline]
134134
pub(crate) fn def_id(&self) -> DefId {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use super::super::sorted_json::*;
2+
3+
fn check(json: SortedJson, serialized: &str) {
4+
assert_eq!(json.to_string(), serialized);
5+
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
6+
7+
let json = json.to_string();
8+
let json: SortedJson = serde_json::from_str(&json).unwrap();
9+
10+
assert_eq!(json.to_string(), serialized);
11+
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
12+
13+
let json = serde_json::to_string(&json).unwrap();
14+
let json: SortedJson = serde_json::from_str(&json).unwrap();
15+
16+
assert_eq!(json.to_string(), serialized);
17+
assert_eq!(serde_json::to_string(&json).unwrap(), serialized);
18+
}
19+
20+
// Test this basic are needed because we are testing that our Display impl + serialize impl don't
21+
// nest everything in extra level of string. We also are testing round trip.
22+
#[test]
23+
fn escape_json_number() {
24+
let json = SortedJson::serialize(3);
25+
let json = EscapedJson::from(json);
26+
assert_eq!(format!("{json}"), "3");
27+
}
28+
29+
#[test]
30+
fn escape_json_single_quote() {
31+
let json = SortedJson::serialize("he's");
32+
let json = EscapedJson::from(json);
33+
assert_eq!(format!("{json}"), r#""he\'s""#);
34+
}
35+
36+
#[test]
37+
fn escape_json_array() {
38+
let json = SortedJson::serialize([1, 2, 3]);
39+
let json = EscapedJson::from(json);
40+
assert_eq!(format!("{json}"), r#"[1,2,3]"#);
41+
}
42+
43+
#[test]
44+
fn escape_json_string() {
45+
let json = SortedJson::serialize(r#"he"llo"#);
46+
let json = EscapedJson::from(json);
47+
assert_eq!(format!("{json}"), r#""he\\\"llo""#);
48+
}
49+
50+
#[test]
51+
fn escape_json_string_escaped() {
52+
let json = SortedJson::serialize(r#"he\"llo"#);
53+
let json = EscapedJson::from(json);
54+
assert_eq!(format!("{json}"), r#""he\\\\\\\"llo""#);
55+
}
56+
57+
#[test]
58+
fn escape_json_string_escaped_escaped() {
59+
let json = SortedJson::serialize(r#"he\\"llo"#);
60+
let json = EscapedJson::from(json);
61+
assert_eq!(format!("{json}"), r#""he\\\\\\\\\\\"llo""#);
62+
}
63+
64+
#[test]
65+
fn number() {
66+
let json = SortedJson::serialize(3);
67+
let serialized = "3";
68+
check(json, serialized);
69+
}
70+
71+
#[test]
72+
fn boolean() {
73+
let json = SortedJson::serialize(true);
74+
let serialized = "true";
75+
check(json, serialized);
76+
}
77+
78+
#[test]
79+
fn string() {
80+
let json = SortedJson::serialize("he\"llo");
81+
let serialized = r#""he\"llo""#;
82+
check(json, serialized);
83+
}
84+
85+
#[test]
86+
fn serialize_array() {
87+
let json = SortedJson::serialize([3, 1, 2]);
88+
let serialized = "[3,1,2]";
89+
check(json, serialized);
90+
}
91+
92+
#[test]
93+
fn sorted_array() {
94+
let items = ["c", "a", "b"];
95+
let serialized = r#"["a","b","c"]"#;
96+
let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect();
97+
let json = SortedJson::array(items);
98+
check(json, serialized);
99+
}
100+
101+
#[test]
102+
fn nested_array() {
103+
let a = SortedJson::serialize(3);
104+
let b = SortedJson::serialize(2);
105+
let c = SortedJson::serialize(1);
106+
let d = SortedJson::serialize([1, 3, 2]);
107+
let json = SortedJson::array([a, b, c, d]);
108+
let serialized = r#"[1,2,3,[1,3,2]]"#;
109+
check(json, serialized);
110+
}
111+
112+
#[test]
113+
fn array_unsorted() {
114+
let items = ["c", "a", "b"];
115+
let serialized = r#"["c","a","b"]"#;
116+
let items: Vec<SortedJson> = items.into_iter().map(SortedJson::serialize).collect();
117+
let json = SortedJson::array_unsorted(items);
118+
check(json, serialized);
119+
}

src/librustdoc/html/render/sorted_template.rs

+3
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ impl fmt::Display for Error {
134134
write!(f, "invalid template")
135135
}
136136
}
137+
138+
#[cfg(test)]
139+
mod tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
use super::super::sorted_template::*;
2+
use std::str::FromStr;
3+
4+
fn is_comment_js(s: &str) -> bool {
5+
s.starts_with("//")
6+
}
7+
8+
fn is_comment_html(s: &str) -> bool {
9+
// not correct but good enough for these tests
10+
s.starts_with("<!--") && s.ends_with("-->")
11+
}
12+
13+
#[test]
14+
fn html_from_empty() {
15+
let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>hello</p>", "<p>world</p>"];
16+
let mut template = SortedTemplate::<Html>::before_after("", "");
17+
for insert in inserts {
18+
template.append(insert.to_string());
19+
}
20+
let template = format!("{template}");
21+
let (template, end) = template.rsplit_once("\n").unwrap();
22+
assert_eq!(template, "<p>hello</p><p>kind</p><p>world</p>");
23+
assert!(is_comment_html(end));
24+
assert!(!end.contains("\n"));
25+
}
26+
27+
#[test]
28+
fn html_page() {
29+
let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>world</p>"];
30+
let before = "<html><head></head><body>";
31+
let after = "</body>";
32+
let mut template = SortedTemplate::<Html>::before_after(before, after);
33+
for insert in inserts {
34+
template.append(insert.to_string());
35+
}
36+
let template = format!("{template}");
37+
let (template, end) = template.rsplit_once("\n").unwrap();
38+
assert_eq!(template, format!("{before}{}{after}", inserts.join("")));
39+
assert!(is_comment_html(end));
40+
assert!(!end.contains("\n"));
41+
}
42+
43+
#[test]
44+
fn js_from_empty() {
45+
let inserts = ["1", "2", "2", "2", "3", "1"];
46+
let mut template = SortedTemplate::<Js>::before_after("", "");
47+
for insert in inserts {
48+
template.append(insert.to_string());
49+
}
50+
let template = format!("{template}");
51+
let (template, end) = template.rsplit_once("\n").unwrap();
52+
assert_eq!(template, "1,2,3");
53+
assert!(is_comment_js(end));
54+
assert!(!end.contains("\n"));
55+
}
56+
57+
#[test]
58+
fn js_empty_array() {
59+
let template = SortedTemplate::<Js>::before_after("[", "]");
60+
let template = format!("{template}");
61+
let (template, end) = template.rsplit_once("\n").unwrap();
62+
assert_eq!(template, format!("[]"));
63+
assert!(is_comment_js(end));
64+
assert!(!end.contains("\n"));
65+
}
66+
67+
#[test]
68+
fn js_number_array() {
69+
let inserts = ["1", "2", "3"];
70+
let mut template = SortedTemplate::<Js>::before_after("[", "]");
71+
for insert in inserts {
72+
template.append(insert.to_string());
73+
}
74+
let template = format!("{template}");
75+
let (template, end) = template.rsplit_once("\n").unwrap();
76+
assert_eq!(template, format!("[1,2,3]"));
77+
assert!(is_comment_js(end));
78+
assert!(!end.contains("\n"));
79+
}
80+
81+
#[test]
82+
fn magic_js_number_array() {
83+
let inserts = ["1", "1"];
84+
let mut template = SortedTemplate::<Js>::magic("[#]", "#").unwrap();
85+
for insert in inserts {
86+
template.append(insert.to_string());
87+
}
88+
let template = format!("{template}");
89+
let (template, end) = template.rsplit_once("\n").unwrap();
90+
assert_eq!(template, format!("[1]"));
91+
assert!(is_comment_js(end));
92+
assert!(!end.contains("\n"));
93+
}
94+
95+
#[test]
96+
fn round_trip_js() {
97+
let inserts = ["1", "2", "3"];
98+
let mut template = SortedTemplate::<Js>::before_after("[", "]");
99+
for insert in inserts {
100+
template.append(insert.to_string());
101+
}
102+
let template1 = format!("{template}");
103+
let mut template = SortedTemplate::<Js>::from_str(&template1).unwrap();
104+
assert_eq!(template1, format!("{template}"));
105+
template.append("4".to_string());
106+
let template = format!("{template}");
107+
let (template, end) = template.rsplit_once("\n").unwrap();
108+
assert_eq!(template, "[1,2,3,4]");
109+
assert!(is_comment_js(end));
110+
}
111+
112+
#[test]
113+
fn round_trip_html() {
114+
let inserts = ["<p>hello</p>", "<p>kind</p>", "<p>world</p>", "<p>kind</p>"];
115+
let before = "<html><head></head><body>";
116+
let after = "</body>";
117+
let mut template = SortedTemplate::<Html>::before_after(before, after);
118+
template.append(inserts[0].to_string());
119+
template.append(inserts[1].to_string());
120+
let template = format!("{template}");
121+
let mut template = SortedTemplate::<Html>::from_str(&template).unwrap();
122+
template.append(inserts[2].to_string());
123+
let template = format!("{template}");
124+
let (template, end) = template.rsplit_once("\n").unwrap();
125+
assert_eq!(template, format!("{before}<p>hello</p><p>kind</p><p>world</p>{after}"));
126+
assert!(is_comment_html(end));
127+
}
128+
129+
#[test]
130+
fn blank_js() {
131+
let inserts = ["1", "2", "3"];
132+
let template = SortedTemplate::<Js>::before_after("", "");
133+
let template = format!("{template}");
134+
let (t, _) = template.rsplit_once("\n").unwrap();
135+
assert_eq!(t, "");
136+
let mut template = SortedTemplate::<Js>::from_str(&template).unwrap();
137+
for insert in inserts {
138+
template.append(insert.to_string());
139+
}
140+
let template1 = format!("{template}");
141+
let mut template = SortedTemplate::<Js>::from_str(&template1).unwrap();
142+
assert_eq!(template1, format!("{template}"));
143+
template.append("4".to_string());
144+
let template = format!("{template}");
145+
let (template, end) = template.rsplit_once("\n").unwrap();
146+
assert_eq!(template, "1,2,3,4");
147+
assert!(is_comment_js(end));
148+
}

0 commit comments

Comments
 (0)