Skip to content

Commit

Permalink
test(term): add test to direction
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Jun 13, 2024
1 parent f6b9afe commit 90f8492
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jsonwebtoken = { version = "9.3.0" }
kvsd = { version = "0.1.3", default-features = false }
moka = { version = "0.12.7", features = ["future"] }
parse_duration = { version = "2.1.1" }
proptest = { version = "1.4.0" }
rand = { version = "0.8.5" }
reqwest = { version = "0.11.24", default-features = false, features = ["rustls-tls", "json"] }
serde = { version = "1", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions crates/synd_term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ axum-server = { workspace = true }
fake = { workspace = true }
insta = { workspace = true }
kvsd = { workspace = true }
proptest = { workspace = true }
serial_test = { version = "3.1.1", default_features = false, features = ["async", "file_locks"] }
tempfile = { workspace = true }
tokio-stream = "0.1.15"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 41acc663e9b0dfdd2bb6fbc916e18a1842a5821e4857e4e440d48899199d5719 # shrinks to dir = Up, index = 1, len = 0, out = Wrapping
48 changes: 46 additions & 2 deletions crates/synd_term/src/application/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) enum Direction {
Right,
}

#[derive(PartialEq, Eq, Clone, Copy)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) enum IndexOutOfRange {
Wrapping,
#[allow(dead_code)]
Expand All @@ -21,7 +21,7 @@ impl Direction {
)]
pub(crate) fn apply(self, index: usize, len: usize, out: IndexOutOfRange) -> usize {
if len == 0 {
return index;
return 0;
}
let diff = match self {
Direction::Up | Direction::Left => -1,
Expand All @@ -44,3 +44,47 @@ impl Direction {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::{prop_oneof, proptest, Just, ProptestConfig, Strategy};

proptest! {
#![proptest_config(ProptestConfig::default())]
#[test]
#[allow(clippy::cast_possible_wrap)]
fn apply(
dir in direction_strategy(),
index in 0..10_usize,
len in 0..10_usize,
out in index_out_of_range_strategy())
{
let apply = dir.apply(index, len,out) as i64;
let index = index as i64;
let len = len as i64;
assert!(
(apply - index).abs() == 1 ||
apply == 0 ||
apply == len-1
);
}


}
fn direction_strategy() -> impl Strategy<Value = Direction> {
prop_oneof![
Just(Direction::Up),
Just(Direction::Down),
Just(Direction::Left),
Just(Direction::Right),