Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testgen: stop copying exile tests and generate output assertions #70

Merged
merged 3 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions exile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,4 @@ readme = "README.md"
xdoc = { path = "../xdoc", version = "0.0.2" }

[build-dependencies]
cargo-readme = "3.2.0"
xtest = { path = "../xtest", version = "0.0.2" }

[dev-dependencies]
lazy_static = "1.4.0"
serde_json = "1.0.48"
serde_plain = "0.3.0"
xtest = { path = "../xtest", version = "0.0.2" }
cargo-readme = "3.2.0"
85 changes: 1 addition & 84 deletions exile/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

fn main() {
sir_watch_alot();
generate_readme();
generate_tests();
}

fn generate_readme() {
Expand Down Expand Up @@ -41,84 +39,3 @@ fn generate_readme() {
std::fs::copy(&this_readme_path, &top_readme_path).unwrap();
}
}

fn generate_tests() {
// Check for environment variable "SKIP_TEST_GENERATION". If it is set, skip test generation.
if env::var_os("EXILE_GENERATE_TESTS").is_none() {
println!("skipping test generation...");
return;
}
println!("generating tests...");
let test_file_path = integ_test_dir().join("parse_tests.rs");
xtest::gen::generate_tests(test_file_path);
}

fn integ_test_dir() -> PathBuf {
let mycrate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
mycrate_dir.join("tests")
}

fn xtest_test_dir() -> PathBuf {
let mut mycrate_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
mycrate_dir.pop();
mycrate_dir.join("xtest")
}

fn exile_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

// path walking helpers for watching files

fn list_dir(p: &Path) -> Vec<String> {
if !p.is_dir() {
panic!("{} is not a dir", p.display());
}
let mut vec = Vec::new();
let paths = std::fs::read_dir(p).unwrap();
for path in paths {
vec.append(&mut list_files_recursively(&path.unwrap().path()));
}
vec
}

fn list_files_recursively(p: &Path) -> Vec<String> {
let mut vec = Vec::new();
if p.is_file() {
if let Some(ext) = p.extension() {
let ext = ext.to_str().unwrap();
if ext == "rs" || ext == "json" || ext == "xml" {
vec.push(p.canonicalize().unwrap().to_str().unwrap().to_owned());
}
}
} else if p.is_dir() {
vec.append(&mut list_dir(p));
} else {
panic!("unknown path type {}", p.display());
}
vec
}

fn lib_rs() -> PathBuf {
exile_dir().join("src").join("lib.rs")
}

fn readme_template() -> PathBuf {
exile_dir().join("readme.template")
}

fn sir_watch_alot() {
let tests = xtest_test_dir();
if !tests.is_dir() {
// The tests aren't there, so there's nothing to watch, or to do.
return;
}
let mut vec = list_files_recursively(&tests);
vec.push(lib_rs().to_str().unwrap().to_owned());
vec.push(readme_template().to_str().unwrap().to_owned());
for file in &vec {
println!("cargo:rerun-if-changed={}", file);
}
println!("cargo:rerun-if-env-changed=EXILE_GENERATE_TESTS");
println!("cargo:rerun-if-env-changed=EXILE_GENERATE_README");
}
32 changes: 32 additions & 0 deletions exile/tests/generated/exile_angle_in_attribute_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// generated file, do not edit

use std::path::PathBuf;

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const INPUT_DATA: &str = "input_data";
const INPUT_FILE: &str = "exile_angle_in_attribute_value.xml";

fn path(filename: &str) -> PathBuf {
let p = PathBuf::from(MANIFEST_DIR)
.join("tests")
.join(INPUT_DATA)
.join(filename);
p.canonicalize()
.unwrap_or_else(|e| panic!("bad path: {}: {}", p.display(), e))
}

#[test]
/// unescaped angle bracket in an attribute value
fn angle_in_attribute_value_test() {
let result = exile::load(path(INPUT_FILE));
assert!(result.is_err());
let e = result.err().unwrap();
match e {
exile::error::Error::Parse(parse_error) => {
assert_eq!(51, parse_error.xml_site.position);
assert_eq!(2, parse_error.xml_site.line);
assert_eq!(12, parse_error.xml_site.column);
}
_ => panic!("expected parse error."),
}
}
33 changes: 22 additions & 11 deletions exile/tests/generated/exile_cd_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,44 @@ use xdoc::Version;

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const INPUT_DATA: &str = "input_data";
const FILENAME: &str = "exile_cd_catalog.xml";
const INPUT_FILE: &str = "exile_cd_catalog.xml";
const OUTPUT_FILE: &str = "exile_cd_catalog.output.xml";

fn path() -> PathBuf {
fn path(filename: &str) -> PathBuf {
let p = PathBuf::from(MANIFEST_DIR)
.join("tests")
.join(INPUT_DATA)
.join(FILENAME);
.join(filename);
p.canonicalize()
.unwrap_or_else(|e| panic!("bad path: {}: {}", p.display(), e))
}

#[test]
fn cd_catalog() {
let path = path();
let loaded = exile::load(&path).unwrap();
/// cd_catalog example from https://www.w3schools.com/xml/xml_examples.asp
fn cd_catalog_parse() {
let path = path(INPUT_FILE);
let actual = exile::load(&path).unwrap();
let expected = expected();
if loaded != expected {
let loaded_str = loaded.to_string();
if actual != expected {
let actual_str = actual.to_string();
let expected_str = expected.to_string();
if loaded_str != expected_str {
assert_eq!(loaded_str, expected_str);
if actual_str != expected_str {
assert_eq!(expected_str, actual_str);
} else {
assert_eq!(loaded, expected);
assert_eq!(expected, actual);
}
}
}

#[test]
/// Check that the serialization of this XML document matches what we expect.
fn cd_catalog_serialize() {
let doc = expected();
let actual = doc.to_string();
let expected = std::fs::read_to_string(path(OUTPUT_FILE)).unwrap();
assert_eq!(expected, actual);
}

fn expected() -> Document {
let mut doc = Document::new();
doc.set_declaration(Declaration {
Expand Down
23 changes: 12 additions & 11 deletions exile/tests/generated/exile_doctypes_comments_pis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@ use xdoc::Version;

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const INPUT_DATA: &str = "input_data";
const FILENAME: &str = "exile_doctypes_comments_pis.xml";
const INPUT_FILE: &str = "exile_doctypes_comments_pis.xml";

fn path() -> PathBuf {
fn path(filename: &str) -> PathBuf {
let p = PathBuf::from(MANIFEST_DIR)
.join("tests")
.join(INPUT_DATA)
.join(FILENAME);
.join(filename);
p.canonicalize()
.unwrap_or_else(|e| panic!("bad path: {}: {}", p.display(), e))
}

#[test]
fn doctypes_comments_pis() {
let path = path();
let loaded = exile::load(&path).unwrap();
/// a file with doctypes, processing instructions and comments
fn doctypes_comments_pis_parse() {
let path = path(INPUT_FILE);
let actual = exile::load(&path).unwrap();
let expected = expected();
if loaded != expected {
let loaded_str = loaded.to_string();
if actual != expected {
let actual_str = actual.to_string();
let expected_str = expected.to_string();
if loaded_str != expected_str {
assert_eq!(loaded_str, expected_str);
if actual_str != expected_str {
assert_eq!(expected_str, actual_str);
} else {
assert_eq!(loaded, expected);
assert_eq!(expected, actual);
}
}
}
Expand Down
33 changes: 22 additions & 11 deletions exile/tests/generated/exile_escapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,44 @@ use xdoc::Version;

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const INPUT_DATA: &str = "input_data";
const FILENAME: &str = "exile_escapes.xml";
const INPUT_FILE: &str = "exile_escapes.xml";
const OUTPUT_FILE: &str = "exile_escapes.output.xml";

fn path() -> PathBuf {
fn path(filename: &str) -> PathBuf {
let p = PathBuf::from(MANIFEST_DIR)
.join("tests")
.join(INPUT_DATA)
.join(FILENAME);
.join(filename);
p.canonicalize()
.unwrap_or_else(|e| panic!("bad path: {}: {}", p.display(), e))
}

#[test]
fn escapes() {
let path = path();
let loaded = exile::load(&path).unwrap();
/// escape sequences
fn escapes_parse() {
let path = path(INPUT_FILE);
let actual = exile::load(&path).unwrap();
let expected = expected();
if loaded != expected {
let loaded_str = loaded.to_string();
if actual != expected {
let actual_str = actual.to_string();
let expected_str = expected.to_string();
if loaded_str != expected_str {
assert_eq!(loaded_str, expected_str);
if actual_str != expected_str {
assert_eq!(expected_str, actual_str);
} else {
assert_eq!(loaded, expected);
assert_eq!(expected, actual);
}
}
}

#[test]
/// Check that the serialization of this XML document matches what we expect.
fn escapes_serialize() {
let doc = expected();
let actual = doc.to_string();
let expected = std::fs::read_to_string(path(OUTPUT_FILE)).unwrap();
assert_eq!(expected, actual);
}

fn expected() -> Document {
let mut doc = Document::new();
doc.set_declaration(Declaration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,44 @@ use xdoc::Version;

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const INPUT_DATA: &str = "input_data";
const FILENAME: &str = "exile_e_file.xml";
const INPUT_FILE: &str = "exile_ezfile.xml";
const OUTPUT_FILE: &str = "exile_ezfile.output.xml";

fn path() -> PathBuf {
fn path(filename: &str) -> PathBuf {
let p = PathBuf::from(MANIFEST_DIR)
.join("tests")
.join(INPUT_DATA)
.join(FILENAME);
.join(filename);
p.canonicalize()
.unwrap_or_else(|e| panic!("bad path: {}: {}", p.display(), e))
}

#[test]
fn e_file() {
let path = path();
let loaded = exile::load(&path).unwrap();
/// a simple, small, well-formed xml file
fn ezfile_parse() {
let path = path(INPUT_FILE);
let actual = exile::load(&path).unwrap();
let expected = expected();
if loaded != expected {
let loaded_str = loaded.to_string();
if actual != expected {
let actual_str = actual.to_string();
let expected_str = expected.to_string();
if loaded_str != expected_str {
assert_eq!(loaded_str, expected_str);
if actual_str != expected_str {
assert_eq!(expected_str, actual_str);
} else {
assert_eq!(loaded, expected);
assert_eq!(expected, actual);
}
}
}

#[test]
/// Check that the serialization of this XML document matches what we expect.
fn ezfile_serialize() {
let doc = expected();
let actual = doc.to_string();
let expected = std::fs::read_to_string(path(OUTPUT_FILE)).unwrap();
assert_eq!(expected, actual);
}

fn expected() -> Document {
let mut doc = Document::new();
doc.set_declaration(Declaration {
Expand Down
Loading