]>
git.proxmox.com Git - rustc.git/blob - vendor/pulldown-cmark-0.7.2/build.rs
2 generate_tests_from_spec()
5 // If the "gen-tests" feature is absent,
6 // this function will be compiled down to nothing
7 #[cfg(not(feature = "gen-tests"))]
8 fn generate_tests_from_spec() {}
10 // If the feature is present, generate tests
11 // from any .txt file present in the specs/ directory
13 // Test cases are present in the files in the
16 // ```````````````````````````````` example
19 // expected html output
20 // ````````````````````````````````
21 #[cfg(feature = "gen-tests")]
22 fn generate_tests_from_spec() {
23 use std
::fs
::{self, File}
;
24 use std
::io
::{Read, Write}
;
25 use std
::path
::PathBuf
;
27 // This is a hardcoded path to the CommonMark spec because it is not situated in
28 // the specs/ directory. It's in an array to easily chain it to the other iterator
29 // and make it easy to eventually add other hardcoded paths in the future if needed
31 "./third_party/CommonMark/spec.txt",
32 "./third_party/GitHub/gfm_table.txt",
33 "./third_party/GitHub/gfm_strikethrough.txt",
34 "./third_party/GitHub/gfm_tasklist.txt",
36 let hardcoded_iter
= hardcoded
.into_iter().map(PathBuf
::from
);
38 // Create an iterator over the files in the specs/ directory that have a .txt extension
39 let spec_files
= fs
::read_dir("./specs")
40 .expect("Could not find the 'specs' directory")
41 .filter_map(Result
::ok
)
43 .filter(|p
| p
.extension().map(|e
| e
.to_owned()).is_some())
44 .chain(hardcoded_iter
)
47 for file_path
in &spec_files
{
48 let mut raw_spec
= String
::new();
50 File
::open(&file_path
)
51 .and_then(|mut f
| f
.read_to_string(&mut raw_spec
))
52 .expect("Could not read the spec file");
54 let rs_test_file
= PathBuf
::from("./tests/suite/")
55 .join(file_path
.file_name().expect("Invalid filename"))
56 .with_extension("rs");
59 File
::create(&rs_test_file
).expect(&format
!("Could not create {:?}", rs_test_file
));
61 let spec_name
= file_path
.file_stem().unwrap().to_str().unwrap();
63 let spec
= Spec
::new(&raw_spec
);
67 .write(b
"// This file is auto-generated by the build script\n")
70 .write(b
"// Please, do not modify it manually\n")
73 .write(b
"\nuse super::test_markdown_html;\n")
76 for (i
, testcase
) in spec
.enumerate() {
78 .write_fmt(format_args
!(
82 let original = r##"{original}"##;
83 let expected = r##"{expected}"##;
85 test_markdown_html(original, expected);
90 original
= testcase
.original
,
91 expected
= testcase
.expected
99 "cargo:warning=Generated {} tests in {:?}",
100 n_tests
, rs_test_file
104 // write mods to suite/mod.rs
105 let suite_mod_file
= PathBuf
::from("./tests/suite/mod").with_extension("rs");
108 File
::create(&suite_mod_file
).expect(&format
!("Could not create {:?}", &suite_mod_file
));
111 .write(b
"// This file is auto-generated by the build script\n")
114 .write(b
"// Please, do not modify it manually\n")
117 .write(b
"\npub use super::test_markdown_html;\n\n")
120 for file_path
in &spec_files
{
121 let mod_name
= file_path
.file_stem().unwrap().to_str().unwrap();
122 mod_rs
.write(b
"mod ").unwrap();
123 mod_rs
.write(mod_name
.as_bytes()).unwrap();
124 mod_rs
.write(b
";\n").unwrap();
128 #[cfg(feature = "gen-tests")]
129 pub struct Spec
<'a
> {
133 #[cfg(feature = "gen-tests")]
135 pub fn new(spec
: &'a
str) -> Self {
140 #[cfg(feature = "gen-tests")]
141 pub struct TestCase
{
142 pub original
: String
,
143 pub expected
: String
,
146 #[cfg(feature = "gen-tests")]
147 impl<'a
> Iterator
for Spec
<'a
> {
148 type Item
= TestCase
;
150 fn next(&mut self) -> Option
<TestCase
> {
151 let spec
= self.spec
;
153 let i_start
= match self
155 .find("```````````````````````````````` example\n")
162 let i_end
= match self.spec
[i_start
..]
164 .map(|pos
| (pos
+ 1) + i_start
)
170 let e_end
= match self.spec
[i_end
+ 2..]
171 .find("````````````````````````````````\n")
172 .map(|pos
| pos
+ i_end
+ 2)
178 self.spec
= &self.spec
[e_end
+ 33..];
180 let test_case
= TestCase
{
181 original
: spec
[i_start
..i_end
].to_string().replace("→", "\t"),
182 expected
: spec
[i_end
+ 2..e_end
].to_string().replace("→", "\t"),