diff options
| author | Philipp A | 2019-11-12 22:57:02 +0100 |
|---|---|---|
| committer | Philipp A | 2019-11-12 22:57:02 +0100 |
| commit | 271cb95ec3e12d6f97852491bc723c5060ce4b75 (patch) | |
| tree | 78cb094fb882fd6c56733ff9e4d1a9377713d393 | |
| parent | 18fba4a71eeebf3a2df91a6bcfda7f13e6c3f561 (diff) | |
| download | rust-rst-271cb95ec3e12d6f97852491bc723c5060ce4b75.tar.bz2 | |
Run python tests
| -rw-r--r-- | .travis.yml | 2 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | build.rs | 22 | ||||
| -rw-r--r-- | build_tests.py | 27 | ||||
| -rw-r--r-- | src/bin.rs | 2 | ||||
| -rw-r--r-- | src/renderer.rs | 2 | ||||
| -rw-r--r-- | src/renderer/html.rs | 12 | ||||
| -rw-r--r-- | src/renderer/html_tests.rs | 15 |
8 files changed, 82 insertions, 4 deletions
diff --git a/.travis.yml b/.travis.yml index ec58734..58e9cb1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: rust cache: cargo rust: - - beta + - stable @@ -11,6 +11,7 @@ homepage = 'https://github.com/flying-sheep/rust-rst' repository = 'https://github.com/flying-sheep/rust-rst.git' edition = '2018' +build = 'build.rs' [lib] name = 'rst' @@ -37,3 +38,6 @@ serde-xml-rs = '0.3.1' quicli = '0.4.0' structopt = '0.2.15' clap = '2.32.0' + +[dev-dependencies] +pretty_assertions = '0.6.1' diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..88b6214 --- /dev/null +++ b/build.rs @@ -0,0 +1,22 @@ + +use std::env; +use std::io::{self, Write}; +use std::path::Path; +use std::process::Command; + +fn main() { + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + + let input = Path::new(&manifest_dir).join("build_tests.py"); + let output = Path::new(&env::var("OUT_DIR").unwrap()).join("html_tests.rs"); + println!("cargo:rerun-if-changed={}", input.display()); + + let output = Command::new("python3") + .arg("build_tests.py") + .arg(format!("{}", output.display())) + .output() + .expect("failed to execute process"); + io::stdout().write_all(&output.stdout).unwrap(); + io::stderr().write_all(&output.stderr).unwrap(); + assert!(output.status.success()); +} diff --git a/build_tests.py b/build_tests.py new file mode 100644 index 0000000..3fe4e9a --- /dev/null +++ b/build_tests.py @@ -0,0 +1,27 @@ +import sys +import json +from ast import literal_eval +from urllib.request import urlopen + +out = sys.argv[1] +url_base = 'https://sourceforge.net/p/docutils/code/HEAD/tree/trunk/docutils' + +with urlopen(f'{url_base}/test/test_writers/test_html5_polyglot_parts.py?format=raw') as con: + code = con.read().decode() + +code = code[code.find('totest ='):code.find('if __name__')] +exec(code) +with open(out, 'w') as f: + t = 0 + for k, (opts, tests) in totest.items(): + for rst, result_code in tests: + result = literal_eval(result_code)['fragment'] + rst, result = (r.replace('"', r'\"') for r in (rst, result)) + f.write(f'''\ +#[test] +fn test_{t:02}() {{ + check_renders_to("{rst}", "{result.strip()}"); +}} +''') + t += 1 + @@ -49,7 +49,7 @@ fn main() -> CliResult { match args.format { Format::json => render_json(&document, stdout)?, Format::xml => render_xml (&document, stdout)?, - Format::html => render_html(&document, stdout)?, + Format::html => render_html(&document, stdout, true)?, } Ok(()) } diff --git a/src/renderer.rs b/src/renderer.rs index fed670b..82a5826 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,4 +1,6 @@ mod html; +#[cfg(test)] +pub mod html_tests; use std::io::Write; diff --git a/src/renderer/html.rs b/src/renderer/html.rs index 56e3175..869444f 100644 --- a/src/renderer/html.rs +++ b/src/renderer/html.rs @@ -15,8 +15,16 @@ use crate::document_tree::{ // static FOOTNOTE_SYMBOLS: [char; 10] = ['*', '†', '‡', '§', '¶', '#', '♠', '♥', '♦', '♣']; -pub fn render_html<W>(document: &Document, mut stream: W) -> Result<(), Error> where W: Write { - document.render_html(stream.by_ref()) +pub fn render_html<W>(document: &Document, mut stream: W, standalone: bool) -> Result<(), Error> where W: Write { + if standalone { + document.render_html(stream.by_ref()) + } else { + let stream = stream.by_ref(); + for c in document.children() { + (*c).render_html(stream)?; + } + Ok(()) + } } trait HTMLRender { diff --git a/src/renderer/html_tests.rs b/src/renderer/html_tests.rs new file mode 100644 index 0000000..a91dd80 --- /dev/null +++ b/src/renderer/html_tests.rs @@ -0,0 +1,15 @@ +use pretty_assertions::assert_eq; + +use crate::parser::parse; +use super::html::render_html; + +fn check_renders_to(rst: &str, expected: &str) { + println!("Rendering:\n{}\n---", rst); + let doc = parse(rst).expect("Cannot parse"); + let mut result_data: Vec<u8> = vec![]; + render_html(&doc, &mut result_data, false).expect("Render error"); + let result = String::from_utf8(result_data).expect("Could not decode"); + assert_eq!(result.as_str().trim(), expected); +} + +include!(concat!(env!("OUT_DIR"), "/html_tests.rs")); |
