Skip to content

Commit

Permalink
add unit tests for digit_difference
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Nov 25, 2023
1 parent 3186493 commit ba08584
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/info/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ impl AuthorsInfo {

fn digit_difference(num1: usize, num2: usize) -> usize {
let count_digits = |num: usize| (num.checked_ilog10().unwrap_or(0) + 1) as usize;

count_digits(num1) - count_digits(num2)
count_digits(num1).abs_diff(count_digits(num2))
}

impl std::fmt::Display for AuthorsInfo {
Expand Down Expand Up @@ -129,6 +128,7 @@ mod test {
use crate::ui::text_colors::TextColors;
use insta::assert_snapshot;
use owo_colors::DynColors;
use rstest::rstest;

#[test]
fn test_display_author() {
Expand Down Expand Up @@ -276,4 +276,13 @@ mod test {

assert_snapshot!(buffer);
}

#[rstest]
#[case(456, 123, 0)]
#[case(456789, 123, 3)]
#[case(1, 12, 1)]
fn test_digit_difference(#[case] num1: usize, #[case] num2: usize, #[case] expected: usize) {
let result = digit_difference(num1, num2);
assert_eq!(result, expected);
}
}

0 comments on commit ba08584

Please sign in to comment.