mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-30 05:44:52 +00:00
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.
This commit is contained in:
parent
0aaa163058
commit
cb081a6bda
7 changed files with 56 additions and 3 deletions
2
tests/cases/external_type.60
Normal file
2
tests/cases/external_type.60
Normal file
|
@ -0,0 +1,2 @@
|
|||
//include_path: ../helper_components
|
||||
TestCase := TestButton {}
|
|
@ -6,8 +6,12 @@ use std::ops::Deref;
|
|||
pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box<dyn Error>> {
|
||||
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::<Vec<_>>();
|
||||
|
||||
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() {
|
||||
|
|
|
@ -3,7 +3,12 @@ use std::error::Error;
|
|||
pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box<dyn Error>> {
|
||||
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::<Vec<_>>();
|
||||
|
||||
let component = match sixtyfps_interpreter::load(source, &testcase.absolute_path, include_paths)
|
||||
{
|
||||
Ok(c) => c,
|
||||
Err(diag) => {
|
||||
let vec = diag.to_string_vec();
|
||||
|
|
|
@ -44,6 +44,7 @@ pub fn test(testcase: &test_driver_lib::TestCase) -> Result<(), Box<dyn Error>>
|
|||
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<dyn Error>>
|
|||
.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()
|
||||
|
|
|
@ -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<Item = &'_ str> {
|
||||
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::<Vec<_>>();
|
||||
assert_eq!(r, ["../first", "../second"]);
|
||||
}
|
||||
|
|
3
tests/helper_components/test_button.60
Normal file
3
tests/helper_components/test_button.60
Normal file
|
@ -0,0 +1,3 @@
|
|||
|
||||
TestButton := Rectangle {
|
||||
}
|
|
@ -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")?;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue