|
| 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