Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
Summary:
Make `Echo` component less generic and not use state.

Less generic code is easier to understand, and state is removed in the following diff D44821871.

Differential Revision: D44821872

fbshipit-source-id: a2899fd52dd0f221320f2d241c6dea44c22f01c8
  • Loading branch information
stepancheg authored and facebook-github-bot committed Apr 13, 2023
1 parent fb5ed9a commit e2093cb
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 305 deletions.
108 changes: 43 additions & 65 deletions src/components/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,45 +147,38 @@ mod tests {

#[test]
fn test_align_left_unjustified() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Top,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(),
vec!["pretty normal test"].try_into().unwrap(),
]);
let msg = Msg(original.clone());
state.insert(&msg);
let component = Aligned::new(
Echo(original.clone()),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Top,
);
let dimensions = Dimensions::new(20, 20);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = original;

assert_eq!(actual, expected);
assert_eq!(actual, original);
}

#[test]
fn test_align_left_justified() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Left(true),
VerticalAlignmentKind::Top,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(), // 11 chars
vec!["pretty normal test"].try_into().unwrap(), // 18 chars
vec!["short"].try_into().unwrap(), // 5 chars
]);
let msg = Msg(original);
state.insert(&msg);
let component = Aligned::new(
Echo(original),
HorizontalAlignmentKind::Left(true),
VerticalAlignmentKind::Top,
);
let dimensions = Dimensions::new(20, 20);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = Lines(vec![
vec!["hello world", &" ".repeat(18 - 11)]
Expand All @@ -200,22 +193,19 @@ mod tests {

#[test]
fn test_align_col_center() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Center,
VerticalAlignmentKind::Top,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(), // 11 chars
vec!["pretty normal testss"].try_into().unwrap(), // 20 chars
vec!["shorts"].try_into().unwrap(), // 6 chars
]);
let msg = Msg(original);
state.insert(&msg);
let component = Aligned::new(
Echo(original),
HorizontalAlignmentKind::Center,
VerticalAlignmentKind::Top,
);
let dimensions = Dimensions::new(20, 20);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = Lines(vec![
vec![" ".repeat(4).as_ref(), "hello world", &" ".repeat(5)]
Expand All @@ -232,22 +222,19 @@ mod tests {

#[test]
fn test_align_right() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Right,
VerticalAlignmentKind::Top,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(), // 11 chars
vec!["pretty normal testsss"].try_into().unwrap(), // 21 chars
vec!["shorts"].try_into().unwrap(), // 6 chars
]);
let msg = Msg(original);
state.insert(&msg);
let component = Aligned::new(
Echo(original),
HorizontalAlignmentKind::Right,
VerticalAlignmentKind::Top,
);
let dimensions = Dimensions::new(20, 20);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = Lines(vec![
vec![" ".repeat(9).as_ref(), "hello world"]
Expand All @@ -262,22 +249,19 @@ mod tests {

#[test]
fn test_align_top() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Top,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(), // 11 chars
vec!["pretty normal testsss"].try_into().unwrap(), // 21 chars
vec!["shorts"].try_into().unwrap(), // 6 chars
]);
let msg = Msg(original);
state.insert(&msg);
let component = Aligned::new(
Echo(original),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Top,
);
let dimensions = Dimensions::new(20, 20);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = Lines(vec![
vec!["hello world"].try_into().unwrap(),
Expand All @@ -290,22 +274,19 @@ mod tests {

#[test]
fn test_align_row_center() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Center,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(), // 11 chars
vec!["pretty normal testsss"].try_into().unwrap(), // 21 chars
vec!["shorts"].try_into().unwrap(), // 6 chars
]);
let msg = Msg(original);
state.insert(&msg);
let component = Aligned::new(
Echo(original),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Center,
);
let dimensions = Dimensions::new(20, 10);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = Lines(vec![
Line::default(),
Expand All @@ -325,22 +306,19 @@ mod tests {

#[test]
fn test_align_bottom() {
let mut state = State::new();
let component = Aligned::new(
Box::new(Echo::<Msg>::new(false)),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Bottom,
);
let original = Lines(vec![
vec!["hello world"].try_into().unwrap(), // 11 chars
vec!["pretty normal testsss"].try_into().unwrap(), // 21 chars
vec!["shorts"].try_into().unwrap(), // 6 chars
]);
let msg = Msg(original);
state.insert(&msg);
let component = Aligned::new(
Echo(original),
HorizontalAlignmentKind::Left(false),
VerticalAlignmentKind::Bottom,
);
let dimensions = Dimensions::new(20, 10);
let actual = component
.draw(&state, dimensions, DrawMode::Normal)
.draw(&State::new(), dimensions, DrawMode::Normal)
.unwrap();
let expected = Lines(vec![
Line::default(),
Expand Down
23 changes: 5 additions & 18 deletions src/components/blank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,28 @@ mod tests {
use crate::Component;
use crate::Dimensions;
use crate::Lines;
use crate::State;

#[derive(AsRef, Debug)]
struct EchoMsg(Lines);

#[test]
fn test_echo_empty() {
let echo: Echo<EchoMsg> = Echo::new(false);

let test = EchoMsg(Lines::new());

let output = echo
.draw(
&crate::state!(&test),
Dimensions::new(10, 10),
DrawMode::Normal,
)
let output = Echo(Lines::new())
.draw(&State::new(), Dimensions::new(10, 10), DrawMode::Normal)
.unwrap();
assert_eq!(output, Lines::new());
}

#[test]
fn test_echo() {
let echo: Echo<EchoMsg> = Echo::new(false);
let output = Lines(vec![
vec!["Line 1"].try_into().unwrap(),
vec!["Line 2"].try_into().unwrap(),
]);
let state = EchoMsg(output.clone());

let test_output = echo
.draw(
&crate::state!(&state),
Dimensions::new(10, 10),
DrawMode::Final,
)
let test_output = Echo(output.clone())
.draw(&State::new(), Dimensions::new(10, 10), DrawMode::Final)
.unwrap();
assert_eq!(output, test_output);
}
Expand Down
48 changes: 19 additions & 29 deletions src/components/bordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,16 @@ mod tests {

#[test]
fn test_basic() -> anyhow::Result<()> {
let component = Bordered::new(Box::new(Echo::<Msg>::new(true)), BorderedSpec::default());

let msg = Msg(Lines(vec![
let msg = Lines(vec![
vec!["Test"].try_into()?, // 4 chars
vec!["Longer"].try_into()?, // 6 chars
vec!["Even Longer", "ok"].try_into()?, // 13 chars
Line::default(),
]));
let output = component.draw(
&crate::state![&msg],
Dimensions::new(14, 5),
DrawMode::Normal,
)?;
]);

let component = Bordered::new(Echo(msg), BorderedSpec::default());

let output = component.draw(&State::new(), Dimensions::new(14, 5), DrawMode::Normal)?;

// A single character on the right side of the message gets truncated to make way for side padding
let expected = Lines(vec![
Expand All @@ -179,8 +176,15 @@ mod tests {

#[test]
fn test_complex() -> anyhow::Result<()> {
let msg = Lines(vec![
vec!["Test"].try_into()?, // 4 chars
vec!["Longer"].try_into()?, // 6 chars
vec!["Even Longer", "ok"].try_into()?, // 13 chars
Line::default(),
]);

let component = Bordered::new(
Box::new(Echo::<Msg>::new(true)),
Echo(msg),
BorderedSpec {
top: Some("@@@".try_into()?),
left: None,
Expand All @@ -189,17 +193,7 @@ mod tests {
},
);

let msg = Msg(Lines(vec![
vec!["Test"].try_into()?, // 4 chars
vec!["Longer"].try_into()?, // 6 chars
vec!["Even Longer", "ok"].try_into()?, // 13 chars
Line::default(),
]));
let output = component.draw(
&crate::state![&msg],
Dimensions::new(13, 7),
DrawMode::Normal,
)?;
let output = component.draw(&State::new(), Dimensions::new(13, 7), DrawMode::Normal)?;

// A single character on the right side of the message gets truncated to make way for side padding
let expected = Lines(vec![
Expand All @@ -221,8 +215,10 @@ mod tests {
fn test_multi_width_unicode() -> anyhow::Result<()> {
let multi_width = "🦶";

let msg = Lines(vec![vec!["Tested"].try_into()?]);

let component = Bordered::new(
Box::new(Echo::<Msg>::new(true)),
Echo(msg),
BorderedSpec {
top: Some(multi_width.try_into()?),
left: None,
Expand All @@ -231,13 +227,7 @@ mod tests {
},
);

let msg = Msg(Lines(vec![vec!["Tested"].try_into()?]));

let output = component.draw(
&crate::state![&msg],
Dimensions::new(13, 7),
DrawMode::Normal,
)?;
let output = component.draw(&State::new(), Dimensions::new(13, 7), DrawMode::Normal)?;
let expected = Lines(vec![vec!["🦶🦶🦶"].try_into()?, vec!["Tested"].try_into()?]);

assert_eq!(output, expected);
Expand Down
19 changes: 8 additions & 11 deletions src/components/bounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,31 @@ mod tests {

#[test]
fn test_no_bounding() -> anyhow::Result<()> {
let test = Bounded::new(Box::new(Echo::<Msg>::new(false)), Some(40), Some(40));
let msg = Msg(Lines(vec![Line::from_iter([Span::new_unstyled(
"hello world",
)?])]));
let msg = Lines(vec![Line::from_iter([Span::new_unstyled("hello world")?])]);
let test = Bounded::new(Echo(msg.clone()), Some(40), Some(40));
let output = test.draw(
&crate::state![&msg],
&State::new(),
Dimensions {
width: 50,
height: 50,
},
DrawMode::Normal,
)?;
let expected = msg.0;

assert_eq!(output, expected);
assert_eq!(output, msg);

Ok(())
}

#[test]
fn test_bounding() -> anyhow::Result<()> {
let test = Bounded::new(Box::new(Echo::<Msg>::new(false)), Some(2), Some(1));
let msg = Msg(Lines(vec![
let msg = Lines(vec![
Line::from_iter([Span::new_unstyled("hello world")?]),
Line::from_iter([Span::new_unstyled("hello world")?]),
]));
]);
let test = Bounded::new(Echo(msg), Some(2), Some(1));
let output = test.draw(
&crate::state![&msg],
&State::new(),
Dimensions {
width: 50,
height: 50,
Expand Down
Loading

0 comments on commit e2093cb

Please sign in to comment.