#[macro_export]
macro_rules! widget_test_suit {
(
$widget_fn: ident,
wnd_size = $size: expr,
$({
path = $path: expr,
$(x == $x_expect: expr,)?
$(y == $y_expect: expr,)?
$(width == $width_expect: expr,)?
$(height == $height_expect: expr,)?
$(rect == $rect_expect: expr,)?
$(size == $size_expect: expr,)?
})+
$(comparison = $comparison: expr)?
) => {
widget_layout_test!(
$widget_fn,
wnd_size = $size,
$({
path = $path,
$(x == $x_expect,)?
$(y == $y_expect,)?
$(width == $width_expect,)?
$(height == $height_expect,)?
$(rect == $rect_expect,)?
$(size == $size_expect,)?
})+
);
widget_image_test!($widget_fn, wnd_size = $size $(,comparison = $comparison)?);
};
(
$widget_fn: ident,
wnd_size = $size: expr,
$(x == $x_expect: expr,)?
$(y == $y_expect: expr,)?
$(width == $width_expect: expr,)?
$(height == $height_expect: expr,)?
$(rect == $rect_expect: expr,)?
$(size == $size_expect: expr,)?
$(comparison = $comparison: expr)?
) =>{
widget_test_suit!(
$widget_fn,
wnd_size = $size,
{
path = [0],
$(x == $x_expect,)?
$(y == $y_expect,)?
$(width == $width_expect,)?
$(height == $height_expect,)?
$(rect == $rect_expect,)?
$(size == $size_expect,)?
}
$(comparison = $comparison)?
);
};
(
$widget_fn: ident,
$($t:tt)+
) => {
widget_test_suit!($widget_fn, wnd_size = Size::new(1024., 1024.), $($t)+);
};
}
#[macro_export]
macro_rules! widget_layout_test {
(
$widget_fn: ident,
wnd_size = $size: expr,
$({
path = $path: expr,
$(x == $x_expect: expr,)?
$(y == $y_expect: expr,)?
$(width == $width_expect: expr,)?
$(height == $height_expect: expr,)?
$(rect == $rect_expect: expr,)?
$(size == $size_expect: expr,)?
})+
) => {
paste::paste! {
#[test]
fn [<$widget_fn _layout>]() {
let _scope = unsafe { AppCtx::new_lock_scope() };
let mut wnd = TestWindow::new_with_size($widget_fn(), $size);
wnd.draw_frame();
assert_layout_result_by_path!(
wnd,
$({
path = $path,
$(x == $x_expect,)?
$(y == $y_expect,)?
$(width == $width_expect,)?
$(height == $height_expect,)?
$(rect == $rect_expect,)?
$(size == $size_expect,)?
})+
);
}
}
};
(
$widget_fn: ident,
wnd_size = $size: expr,
$(x == $x_expect: expr,)?
$(y == $y_expect: expr,)?
$(width == $width_expect: expr,)?
$(height == $height_expect: expr,)?
$(rect == $rect_expect: expr,)?
$(size == $size_expect: expr,)?
) =>{
widget_layout_test!(
$widget_fn,
wnd_size = $size,
{
path = [0],
$(x == $x_expect,)?
$(y == $y_expect,)?
$(width == $width_expect,)?
$(height == $height_expect,)?
$(rect == $rect_expect,)?
$(size == $size_expect,)?
}
);
};
(
$widget_fn: ident,
$($t:tt)+
) => {
widget_layout_test!($widget_fn, wnd_size = Size::new(1024., 1024.), $($t)+);
};
}
#[cfg(not(target_arch = "wasm32"))]
#[macro_export]
macro_rules! widget_image_test {
($widget_fn:ident, wnd_size = $size:expr $(,comparison = $comparison:expr)?) => {
paste::paste! {
#[test]
fn [<$widget_fn _with_default_by_wgpu>]() {
let _scope = unsafe { AppCtx::new_lock_scope() };
let mut wnd = TestWindow::new_with_size($widget_fn(), $size);
wnd.draw_frame();
let Frame { commands, viewport, surface} = wnd.take_last_frame().unwrap();
let viewport = viewport.to_i32().cast_unit();
let img = wgpu_render_commands(&commands, viewport, surface);
let name = format!("{}_with_default_by_wgpu", std::stringify!($widget_fn));
let file_path = test_case_name!(name, "png");
ImageTest::new(img, &file_path)
$(.with_comparison($comparison))?
.test();
}
#[test]
fn [<$widget_fn _with_material_by_wgpu>]() {
let _scope = unsafe { AppCtx::new_lock_scope() };
unsafe { AppCtx::set_app_theme(ribir_material::purple::light()) };
let mut wnd = TestWindow::new_with_size($widget_fn(), $size);
wnd.draw_frame();
let Frame { commands, viewport, surface} = wnd.take_last_frame().unwrap();
let viewport = viewport.to_i32().cast_unit();
let img = wgpu_render_commands(&commands, viewport, surface);
let name = format!("{}_with_material_by_wgpu", std::stringify!($widget_fn));
let file_path = test_case_name!(name, "png");
ImageTest::new(img, &file_path)
$(.with_comparison($comparison))?
.test();
}
}
};
($widget_fn:ident $(,)?) => {
widget_image_test!($widget_fn, wnd_size = Size::new(128., 128.),);
};
}
#[allow(clippy::test_attr_in_doctest)]
#[macro_export]
macro_rules! assert_layout_result_by_path {
(
$test_wnd: ident,
$({
path = $path: expr,
$(x == $x_expect: expr,)?
$(y == $y_expect: expr,)?
$(width == $width_expect: expr,)?
$(height == $height_expect: expr,)?
$(rect == $rect_expect: expr,)?
$(size == $size_expect: expr,)?
})+
) => {
$(
let info = $test_wnd.layout_info_by_path(&$path).unwrap();
$(assert_eq!(info.pos.x, $x_expect, "unexpected x");)?
$(assert_eq!(info.pos.y, $y_expect, "unexpected y");)?
$(assert_eq!(info.size.unwrap().width, $width_expect, "unexpected width");)?
$(assert_eq!(info.size.unwrap().height, $height_expect, "unexpected height");)?
$(assert_eq!(info.size.unwrap(), $size_expect, "unexpected size");)?
$(
let size = info.size.unwrap();
assert_eq!(Rect::new(info.pos, size), $rect_expect, "unexpected rect");
)?
)+
};
}