use qpprint::tbl::{Align, Column, Data, Renderer}; // With no explicit width constraints, a long title should set the column // witdth: // // A long title // ~~~~~~~~~~~~ // short #[test] fn title_constrains_width() { let columns = [Column::new("A long title", ToString::to_string)]; let mut data = Data::new(columns.len()); data.add_row(vec!["short".into()]); let renderer = Renderer::new(&columns, &data).header(Some('~')); let res = renderer.to_string(); assert_eq!(res, "A long title\n~~~~~~~~~~~~\nshort \n"); } // With no explicit width constraints, a cell should set the column // witdth: // // Title // ~~~~~~~~~~~ // A long cell #[test] fn cell_constrains_width() { let columns = [Column::new("Title", ToString::to_string).title_align(Align::Left)]; let mut data = Data::new(columns.len()); data.add_row(vec!["A long cell".into()]); let renderer = Renderer::new(&columns, &data).header(Some('~')); let res = renderer.to_string(); assert_eq!(res, "Title \n~~~~~~~~~~~\nA long cell\n"); } #[test] fn left_align_title() { let columns = [Column::new("Id", ToString::to_string) .min_width(5) .title_align(Align::Left)]; let data = Data::new(columns.len()); let renderer = Renderer::new(&columns, &data).header(Some('~')); let res = renderer.to_string(); assert_eq!(res, "Id \n~~~~~\n"); } #[test] fn center_align_title() { let columns = [Column::new("I", ToString::to_string) .min_width(5) .title_align(Align::Center)]; let data = Data::new(columns.len()); let renderer = Renderer::new(&columns, &data).header(Some('~')); let res = renderer.to_string(); assert_eq!(res, " I \n~~~~~\n"); } #[test] fn right_align_title() { let columns = [Column::new("I", ToString::to_string) .min_width(5) .title_align(Align::Right)]; let data = Data::new(columns.len()); let renderer = Renderer::new(&columns, &data).header(Some('~')); let res = renderer.to_string(); assert_eq!(res, " I\n~~~~~\n"); } #[test] fn trunc_cells() { let idcol = Column::new("Id", ToString::to_string).cell_align(Align::Right); let namecol = Column::new("Name", ToString::to_string) .max_width(4) .trunc_style(2, '.'); let namecol2 = Column::new("Title", ToString::to_string) .max_width(4) .trunc_style(1, '…'); let columns = [idcol, namecol, namecol2]; let mut data = Data::new(columns.len()); data.add_row(vec![4.into(), "hell".into(), "hell".into()]); data.add_row(vec![42.into(), "hello".into(), "hello".into()]); data.add_row(vec![11.into(), "world".into(), "world".into()]); let renderer = Renderer::new(&columns, &data) .header(Some('=')) .column_spacing(1); let res = renderer.to_string(); assert_eq!( res, r"Id Name Tit… == ==== ==== 4 hell hell 42 he.. hel… 11 wo.. wor… " ); } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :