1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#![allow(clippy::needless_update)]
use std::{
mem::take,
path::{Path, PathBuf},
};
use swc_common::{FileName, Span};
use swc_xml_ast::*;
use swc_xml_codegen::{
writer::basic::{BasicXmlWriter, BasicXmlWriterConfig, IndentType, LineFeed},
CodeGenerator, CodegenConfig, Emit,
};
use swc_xml_parser::{parse_file_as_document, parser::ParserConfig};
use swc_xml_visit::{VisitMut, VisitMutWith};
use testing::{assert_eq, run_test2, NormalizedOutput};
fn print_document(
input: &Path,
parser_config: Option<ParserConfig>,
writer_config: Option<BasicXmlWriterConfig>,
codegen_config: Option<CodegenConfig>,
) {
let dir = input.parent().unwrap();
let parser_config = parser_config.unwrap_or_default();
let writer_config = writer_config.unwrap_or_default();
let codegen_config = codegen_config.unwrap_or_default();
let output = if codegen_config.minify {
dir.join(format!(
"output.min.{}",
input.extension().unwrap().to_string_lossy()
))
} else {
dir.join(format!(
"output.{}",
input.extension().unwrap().to_string_lossy()
))
};
run_test2(false, |cm, handler| {
let fm = cm.load_file(input).unwrap();
let mut errors = Vec::new();
let mut document: Document =
parse_file_as_document(&fm, parser_config, &mut errors).unwrap();
for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
}
let mut xml_str = String::new();
let wr = BasicXmlWriter::new(&mut xml_str, None, writer_config);
let mut gen = CodeGenerator::new(wr, codegen_config);
gen.emit(&document).unwrap();
let fm_output = cm.load_file(&output).unwrap();
NormalizedOutput::new_raw(xml_str)
.compare_to_file(output)
.unwrap();
let mut errors = Vec::new();
let mut document_parsed_again =
parse_file_as_document(&fm_output, parser_config, &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
for error in take(&mut errors) {
error.to_diagnostics(&handler).emit();
}
document.visit_mut_with(&mut DropSpan);
document_parsed_again.visit_mut_with(&mut DropSpan);
assert_eq!(document, document_parsed_again);
Ok(())
})
.unwrap();
}
fn verify_document(
input: &Path,
parser_config: Option<ParserConfig>,
writer_config: Option<BasicXmlWriterConfig>,
codegen_config: Option<CodegenConfig>,
ignore_errors: bool,
) {
let parser_config = parser_config.unwrap_or_default();
let writer_config = writer_config.unwrap_or_default();
let codegen_config = codegen_config.unwrap_or_default();
testing::run_test2(false, |cm, handler| {
let fm = cm.load_file(input).unwrap();
let mut errors = Vec::new();
let mut document =
parse_file_as_document(&fm, parser_config, &mut errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
if !ignore_errors {
for err in take(&mut errors) {
err.to_diagnostics(&handler).emit();
}
}
let mut xml_str = String::new();
let wr = BasicXmlWriter::new(&mut xml_str, None, writer_config);
let mut gen = CodeGenerator::new(wr, codegen_config);
gen.emit(&document).unwrap();
let new_fm = cm.new_source_file(FileName::Anon.into(), xml_str);
let mut parsed_errors = Vec::new();
let mut document_parsed_again =
parse_file_as_document(&new_fm, parser_config, &mut parsed_errors).map_err(|err| {
err.to_diagnostics(&handler).emit();
})?;
if !ignore_errors {
for err in parsed_errors {
err.to_diagnostics(&handler).emit();
}
}
document.visit_mut_with(&mut DropSpan);
document_parsed_again.visit_mut_with(&mut DropSpan);
assert_eq!(document, document_parsed_again);
Ok(())
})
.unwrap();
}
struct DropSpan;
impl VisitMut for DropSpan {
fn visit_mut_document_type(&mut self, n: &mut DocumentType) {
n.visit_mut_children_with(self);
n.raw = None;
}
fn visit_mut_comment(&mut self, n: &mut Comment) {
n.visit_mut_children_with(self);
n.raw = None;
}
fn visit_mut_text(&mut self, n: &mut Text) {
n.visit_mut_children_with(self);
n.raw = None;
}
fn visit_mut_attribute(&mut self, n: &mut Attribute) {
n.visit_mut_children_with(self);
n.raw_name = None;
n.raw_value = None;
}
fn visit_mut_span(&mut self, n: &mut Span) {
*n = Default::default()
}
}
#[testing::fixture("tests/fixture/**/input.xml")]
fn test_document(input: PathBuf) {
print_document(
&input,
None,
None,
Some(CodegenConfig {
scripting_enabled: false,
minify: false,
..Default::default()
}),
);
print_document(
&input,
None,
None,
Some(CodegenConfig {
scripting_enabled: false,
minify: true,
..Default::default()
}),
);
}
#[testing::fixture("tests/options/indent_type/**/input.xml")]
fn test_indent_type_option(input: PathBuf) {
print_document(
&input,
None,
Some(BasicXmlWriterConfig {
indent_type: IndentType::Tab,
indent_width: 2,
linefeed: LineFeed::default(),
}),
None,
);
}
#[testing::fixture("../swc_xml_parser/tests/fixture/**/*.xml")]
fn parser_verify(input: PathBuf) {
verify_document(&input, None, None, None, false);
verify_document(
&input,
None,
None,
Some(CodegenConfig {
scripting_enabled: false,
minify: true,
..Default::default()
}),
false,
);
verify_document(
&input,
None,
None,
Some(CodegenConfig {
scripting_enabled: false,
minify: true,
..Default::default()
}),
false,
);
}
#[testing::fixture("../swc_xml_parser/tests/recovery/**/*.xml")]
fn parser_recovery_verify(input: PathBuf) {
verify_document(
&input,
None,
None,
Some(CodegenConfig {
scripting_enabled: false,
minify: true,
..Default::default()
}),
true,
);
verify_document(
&input,
None,
None,
Some(CodegenConfig {
scripting_enabled: false,
minify: true,
..Default::default()
}),
true,
);
}
|