Skip to content

Commit

Permalink
feat: add range for gen function
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 30, 2022
1 parent 2d1a203 commit c8f5d52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions fkl_cli/src/mock/fake_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,34 @@ impl RandomValue {

pub fn range_number(min: i64, max: i64) -> MockType {
let mut rng = rand::thread_rng();

if min > max {
panic!("min must be less than max")
}

let n: i64 = rng.gen_range(min..max);

MockType::Integer(n)
}

pub fn float() -> MockType {
let mut rng = rand::thread_rng();
let n: f64 = rng.gen();

MockType::Float(n)
}

pub fn range_float(min: f64, max: f64) -> MockType {
let mut rng = rand::thread_rng();

if min > max {
panic!("min must be less than max")
}

let n: f64 = rng.gen_range(min..max);

MockType::Float(n)
}
}

#[cfg(test)]
Expand All @@ -55,4 +79,11 @@ mod tests {
let n = RandomValue::range_number(1, 10);
assert!(n.integer() >= 1);
}

#[test]
fn test_range_float() {
let n = RandomValue::range_float(1.0, 10.0);
println!("{:?}", n);
assert!(n.float() >= 1.0);
}
}
7 changes: 7 additions & 0 deletions fkl_cli/src/mock/mock_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ impl MockType {
_ => panic!("cannot convert to integer"),
}
}

pub fn float(&self) -> f64 {
match self {
MockType::Float(f) => *f,
_ => panic!("cannot convert to float"),
}
}
}

0 comments on commit c8f5d52

Please sign in to comment.