#[cfg(test)]
mod tests {
use r3bl_rs_utils_core::*;
use r3bl_rs_utils_macro::style;
use crate::*;
#[test]
fn test_surface_2_col_complex() -> CommonResult<()> {
throws!({
let mut surface = Surface {
stylesheet: dsl_stylesheet()?,
..Default::default()
};
surface.surface_start(SurfaceProps {
pos: position!(col_index:0, row_index:0),
size: size!(col_count:500, row_count:500),
})?;
create_main_container(&mut surface)?;
surface.surface_end()?;
println!("{:?}", &surface.render_pipeline);
println!(
"{}",
serde_json::to_string_pretty(&surface.render_pipeline).unwrap()
);
});
}
fn create_main_container(surface: &mut Surface) -> CommonResult<()> {
throws!({
surface.box_start(FlexBoxProps {
id: FlexBoxId::from(0),
dir: LayoutDirection::Horizontal,
requested_size_percent: requested_size_percent!(width:100, height:100),
maybe_styles: get_styles! { @from: surface.stylesheet, [0] },
})?;
make_container_assertions(surface)?;
create_left_col(surface)?;
create_right_col(surface)?;
surface.box_end()?;
});
fn make_container_assertions(surface: &Surface) -> CommonResult<()> {
throws!({
let layout_item = surface.stack_of_boxes.first().unwrap();
assert_eq2!(layout_item.id, FlexBoxId::from(0));
assert_eq2!(layout_item.dir, LayoutDirection::Horizontal);
assert_eq2!(layout_item.origin_pos, position!(col_index:0, row_index:0));
assert_eq2!(layout_item.bounds_size, size!(col_count:500, row_count:500));
assert_eq2!(
layout_item.style_adjusted_origin_pos,
position!(col_index:1, row_index:1)
); assert_eq2!(
layout_item.style_adjusted_bounds_size,
size!(col_count:498, row_count:498)
);
assert_eq2!(
layout_item.requested_size_percent,
requested_size_percent!(width:100, height:100)
);
assert_eq2!(
layout_item.insertion_pos_for_next_box,
position!(col_index:0, row_index:0).into()
);
assert!(layout_item.get_computed_style().is_some());
assert_eq2!(
layout_item.get_computed_style().unwrap().padding,
Some(ch!(1))
);
});
}
}
fn create_left_col(surface: &mut Surface) -> CommonResult<()> {
throws!({
box_start! {
in: surface,
id: FlexBoxId::from(1),
dir: LayoutDirection::Vertical,
requested_size_percent: requested_size_percent!(width:50, height:100),
styles: [1]
}
make_left_col_assertions(surface)?;
box_end!(in: surface);
});
fn make_left_col_assertions(surface: &Surface) -> CommonResult<()> {
throws!({
let layout_item = surface.stack_of_boxes.last().unwrap();
assert_eq2!(layout_item.id, FlexBoxId::from(1));
assert_eq2!(layout_item.dir, LayoutDirection::Vertical);
assert_eq2!(layout_item.origin_pos, position!(col_index:0, row_index:0));
assert_eq2!(layout_item.bounds_size, size!(col_count:250, row_count:500));
debug!(layout_item);
assert_eq2!(
layout_item.style_adjusted_origin_pos,
position!(col_index:3, row_index:3)
); assert_eq2!(
layout_item.style_adjusted_bounds_size,
size!(col_count:244, row_count:494)
);
assert_eq2!(
layout_item.requested_size_percent,
requested_size_percent!(width:50, height:100)
);
assert_eq2!(layout_item.insertion_pos_for_next_box, None);
assert_ne!(
layout_item.get_computed_style(),
Stylesheet::compute(&surface.stylesheet.find_styles_by_ids(vec![1]))
);
});
}
}
fn create_right_col(surface: &mut Surface) -> CommonResult<()> {
throws!({
surface.box_start(FlexBoxProps {
maybe_styles: get_styles! { @from: surface.stylesheet, [2] },
id: FlexBoxId::from(2),
dir: LayoutDirection::Vertical,
requested_size_percent: requested_size_percent!(width:50, height:100),
})?;
make_right_col_assertions(surface)?;
surface.box_end()?;
});
fn make_right_col_assertions(surface: &Surface) -> CommonResult<()> {
throws!({
let current_box = surface.stack_of_boxes.last().unwrap();
assert_eq2!(current_box.id, FlexBoxId::from(2));
assert_eq2!(current_box.dir, LayoutDirection::Vertical);
assert_eq2!(
current_box.origin_pos,
position!(col_index: 250, row_index: 0)
);
assert_eq2!(current_box.bounds_size, size!(col_count:250, row_count:500));
assert_eq2!(
current_box.style_adjusted_origin_pos,
position!(col_index: 254, row_index: 4)
); assert_eq2!(
current_box.style_adjusted_bounds_size,
size! (col_count:242, row_count:492)
);
assert_eq2!(
current_box.requested_size_percent,
requested_size_percent!(width:50, height:100)
);
assert_eq2!(current_box.insertion_pos_for_next_box, None);
assert_ne!(
current_box.get_computed_style(),
Stylesheet::compute(&surface.stylesheet.find_styles_by_ids(vec![2]))
);
});
}
}
fn dsl_stylesheet() -> CommonResult<Stylesheet> {
throws_with_return!({
stylesheet! {
style! {
id: 0
padding: 1
},
style! {
id: 1
attrib: [dim, bold]
padding: 2
color_fg: TuiColor::Rgb (RgbValue{ red: 255, green: 255, blue: 0 })
color_bg: TuiColor::Rgb (RgbValue{ red: 128, green: 128, blue: 128 })
},
style! {
id: 2
attrib: [underline, strikethrough]
padding: 3
color_fg: TuiColor::Rgb (RgbValue{ red: 0, green: 0, blue: 0 })
color_bg: TuiColor::Rgb (RgbValue{ red: 255, green: 255, blue: 255 })
}
}
})
}
}