slint/tests/driver/interpreter/interpreter.rs
Olivier Goffart 862f65c151 Fix panic in the interpreter when accessing uninitialized global
As the type of the Value wouldn't match the expected type

Fixes #173

(Also added a way to test the interpreter by making the interpreter
make sure the property "test", if it exists, is true)
2021-04-19 11:41:36 +02:00

47 lines
1.6 KiB
Rust

/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
use itertools::Itertools;
use sixtyfps_interpreter::{Value, ValueType};
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 include_paths = test_driver_lib::extract_include_paths(&source)
.map(std::path::PathBuf::from)
.collect::<Vec<_>>();
let mut compiler = sixtyfps_interpreter::ComponentCompiler::new();
compiler.set_include_paths(include_paths);
let component =
spin_on::spin_on(compiler.build_from_source(source, testcase.absolute_path.clone()));
let component = match component {
None => {
sixtyfps_interpreter::print_diagnostics(&compiler.diagnostics());
return Err(compiler
.diagnostics()
.into_iter()
.map(|d| d.to_string())
.join("\n")
.into());
}
Some(c) => c,
};
let instance = component.create();
if let Some((_, ty)) = component.properties().find(|x| x.0 == "test") {
if ty == ValueType::Bool {
assert_eq!(instance.get_property("test"), Ok(Value::Bool(true)));
}
}
Ok(())
}