use crate::element::{Component, Element};
use crate::layout::LayoutStyle;
#[derive(Default, Clone, PartialEq)]
pub struct NewlineProps {
pub count: usize,
}
impl NewlineProps {
pub fn new() -> Self {
Self { count: 1 }
}
pub fn with_count(count: usize) -> Self {
Self { count }
}
}
pub struct Newline;
impl Newline {
pub fn layout_style(props: &NewlineProps) -> LayoutStyle {
LayoutStyle {
height: Some(props.count.max(1) as f32),
width: Some(0.0),
..Default::default()
}
}
}
impl Component for Newline {
type Props = NewlineProps;
fn render(props: &Self::Props) -> Element {
if props.count <= 1 {
Element::text("")
} else {
Element::text("\n".repeat(props.count - 1))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_newline_props_default() {
let props = NewlineProps::new();
assert_eq!(props.count, 1);
}
#[test]
fn test_newline_props_with_count() {
let props = NewlineProps::with_count(3);
assert_eq!(props.count, 3);
}
#[test]
fn test_newline_layout_style() {
let props = NewlineProps::with_count(2);
let style = Newline::layout_style(&props);
assert_eq!(style.height, Some(2.0));
}
#[test]
fn test_newline_layout_style_default() {
let props = NewlineProps::new();
let style = Newline::layout_style(&props);
assert_eq!(style.height, Some(1.0));
assert_eq!(style.width, Some(0.0));
}
#[test]
fn test_newline_layout_style_zero_count() {
let props = NewlineProps::with_count(0);
let style = Newline::layout_style(&props);
assert_eq!(style.height, Some(1.0));
}
#[test]
fn test_newline_render() {
let props = NewlineProps::new();
let elem = Newline::render(&props);
assert!(elem.is_text());
}
#[test]
fn test_newline_default_trait() {
let props = NewlineProps::default();
assert_eq!(props.count, 0);
}
}