From cb081a6bda35e0140e9982ea39a23ee395c3e67a Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Sun, 19 Jul 2020 18:31:10 +0200 Subject: [PATCH] Test that loading types from the include path works This uses the recently added "hooks" or APIs to pass an include path that's specified in the test case itself. --- tests/cases/external_type.60 | 2 ++ tests/driver/cpp.rs | 6 +++++- tests/driver/interpreter.rs | 7 ++++++- tests/driver/nodejs.rs | 2 ++ tests/driver_lib/lib.rs | 22 ++++++++++++++++++++++ tests/helper_components/test_button.60 | 3 +++ tests/rustdriver/build.rs | 17 ++++++++++++++++- 7 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 tests/cases/external_type.60 create mode 100644 tests/helper_components/test_button.60 diff --git a/tests/cases/external_type.60 b/tests/cases/external_type.60 new file mode 100644 index 000000000..9d994a7b0 --- /dev/null +++ b/tests/cases/external_type.60 @@ -0,0 +1,2 @@ +//include_path: ../helper_components +TestCase := TestButton {} \ No newline at end of file diff --git a/tests/driver/cpp.rs b/tests/driver/cpp.rs index 7118dd4ca..3b00d3a96 100644 --- a/tests/driver/cpp.rs +++ b/tests/driver/cpp.rs @@ -6,8 +6,12 @@ use std::ops::Deref; pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box> { let source = std::fs::read_to_string(&testcase.absolute_path)?; + let include_paths = &test_driver_lib::extract_include_paths(&source) + .map(std::path::PathBuf::from) + .collect::>(); + let (syntax_node, diag) = parser::parse(source.clone(), Some(&testcase.absolute_path)); - let compiler_config = CompilerConfiguration::default(); + let compiler_config = CompilerConfiguration { include_paths, ..Default::default() }; let (doc, mut diag) = compile_syntax_node(syntax_node, diag, &compiler_config); if diag.has_error() { diff --git a/tests/driver/interpreter.rs b/tests/driver/interpreter.rs index ac5710b56..0312c260d 100644 --- a/tests/driver/interpreter.rs +++ b/tests/driver/interpreter.rs @@ -3,7 +3,12 @@ use std::error::Error; pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box> { let source = std::fs::read_to_string(&testcase.absolute_path)?; - let component = match sixtyfps_interpreter::load(source, &testcase.absolute_path, &[]) { + let include_paths = &test_driver_lib::extract_include_paths(&source) + .map(std::path::PathBuf::from) + .collect::>(); + + let component = match sixtyfps_interpreter::load(source, &testcase.absolute_path, include_paths) + { Ok(c) => c, Err(diag) => { let vec = diag.to_string_vec(); diff --git a/tests/driver/nodejs.rs b/tests/driver/nodejs.rs index be82b7acc..38afdeb24 100644 --- a/tests/driver/nodejs.rs +++ b/tests/driver/nodejs.rs @@ -44,6 +44,7 @@ pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box> path = testcase.absolute_path.to_string_lossy() )?; let source = std::fs::read_to_string(&testcase.absolute_path)?; + let include_paths = test_driver_lib::extract_include_paths(&source); for x in test_driver_lib::extract_test_functions(&source).filter(|x| x.language_id == "js") { write!(main_js, "{{\n {}\n}}\n", x.source.replace("\n", "\n "))?; } @@ -52,6 +53,7 @@ pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box> .arg(dir.path().join("main.js")) .current_dir(dir.path()) .env("SIXTYFPS_NODE_NATIVE_LIB", native_lib) + .env("SIXTYFPS_INCLUDE_PATH", std::env::join_paths(include_paths).unwrap()) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) .output() diff --git a/tests/driver_lib/lib.rs b/tests/driver_lib/lib.rs index 1297c4cfd..3771c7b42 100644 --- a/tests/driver_lib/lib.rs +++ b/tests/driver_lib/lib.rs @@ -106,3 +106,25 @@ let yy = 0; assert_eq!(r2.language_id, "rust"); assert_eq!(r2.source, "let xx = 0;\nlet yy = 0;"); } + +/// Extract extra include paths from a comment in the source if present. +pub fn extract_include_paths(source: &str) -> impl Iterator { + lazy_static::lazy_static! { + static ref RX: Regex = Regex::new(r"//include_path:\s*(.+)\s*\n").unwrap(); + } + RX.captures_iter(source).map(|mat| mat.get(1).unwrap().as_str()) +} + +#[test] +fn test_extract_include_paths() { + assert!(extract_include_paths("something").next().is_none()); + + let source = r" + //include_path: ../first + //include_path: ../second + Blah {} +"; + + let r = extract_include_paths(source).collect::>(); + assert_eq!(r, ["../first", "../second"]); +} diff --git a/tests/helper_components/test_button.60 b/tests/helper_components/test_button.60 new file mode 100644 index 000000000..e59e2556f --- /dev/null +++ b/tests/helper_components/test_button.60 @@ -0,0 +1,3 @@ + +TestButton := Rectangle { +} \ No newline at end of file diff --git a/tests/rustdriver/build.rs b/tests/rustdriver/build.rs index fc9c7d35b..c92151379 100644 --- a/tests/rustdriver/build.rs +++ b/tests/rustdriver/build.rs @@ -21,7 +21,22 @@ fn main() -> std::io::Result<()> { let mut output = std::fs::File::create( Path::new(&std::env::var_os("OUT_DIR").unwrap()).join(format!("{}.rs", module_name)), )?; - output.write_all(b"sixtyfps::sixtyfps!{\n")?; + + let include_paths = test_driver_lib::extract_include_paths(&source); + + output.write_all(b"sixtyfps::sixtyfps!{")?; + + for path in include_paths { + let mut abs_path = testcase.absolute_path.clone(); + abs_path.pop(); + abs_path.push(path); + + output.write_all(b"#[include_path=\"")?; + output.write_all(abs_path.to_string_lossy().as_bytes())?; + output.write_all(b"\"]")?; + } + + output.write_all(b"\n")?; output.write_all(source.as_bytes())?; output.write_all(b"}\n")?;