Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add some unit test on notification #65

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ maxminddb = "0.23.0"
confy = "0.5.1"
serde = { version = "1.0.152", default_features = false, features = ["derive"] }
rodio = { version = "0.16.0", default_features = false, features = ["mp3"] }

[dev-dependencies]
rstest = "0.16.0"
19 changes: 19 additions & 0 deletions src/enums/byte_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,22 @@ pub fn from_char_to_multiple(ch: char) -> ByteMultiple {
_ => ByteMultiple::B,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_interpret_suffix_correctly() {
assert_eq!(from_char_to_multiple('B'), ByteMultiple::B);
assert_eq!(from_char_to_multiple('k'), ByteMultiple::KB);
assert_eq!(from_char_to_multiple('M'), ByteMultiple::MB);
assert_eq!(from_char_to_multiple('g'), ByteMultiple::GB);
}

#[test]
fn test_interpret_unknown_suffix_correctly() {
assert_eq!(from_char_to_multiple('T'), ByteMultiple::B);
assert_eq!(from_char_to_multiple('p'), ByteMultiple::B);
}
}
4 changes: 2 additions & 2 deletions src/enums/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ pub enum Message {
LanguageSelection(Language),
/// Set packets notification
UpdatePacketsNotification(PacketsNotification, bool),
/// Set packets notification
/// Set bytes notification
UpdateBytesNotification(BytesNotification, bool),
/// Set packets notification
/// Set favorite notification
UpdateFavoriteNotification(FavoriteNotification, bool),
/// Clear all received notifications
ClearAllNotifications,
Expand Down
86 changes: 14 additions & 72 deletions src/gui/pages/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::enums::byte_multiple::{from_char_to_multiple, ByteMultiple};
use crate::enums::element_type::ElementType;
use crate::enums::message::Message;
use crate::enums::overlay::MyOverlay;
Expand Down Expand Up @@ -388,23 +387,14 @@ fn get_favorite_notify(
favorite_notification_translation(language),
favorite_notification.notify_on_favorite,
move |toggled| {
if toggled {
Message::UpdateFavoriteNotification(
FavoriteNotification {
notify_on_favorite: true,
..favorite_notification
},
false,
)
} else {
Message::UpdateFavoriteNotification(
FavoriteNotification {
notify_on_favorite: false,
..favorite_notification
},
false,
)
}
Message::UpdateFavoriteNotification(
if toggled {
FavoriteNotification::on(favorite_notification.sound)
} else {
FavoriteNotification::off(favorite_notification.sound)
},
false,
)
},
)
.size(18)
Expand Down Expand Up @@ -462,21 +452,9 @@ fn input_group_packets(
curr_threshold_str
},
move |value| {
let new_threshold = if value.is_empty() {
0
} else {
value
.parse()
.unwrap_or(packets_notification.previous_threshold)
};
Message::UpdatePacketsNotification(
PacketsNotification {
threshold: Some(new_threshold),
previous_threshold: new_threshold,
..packets_notification
},
false,
)
let packets_notification =
PacketsNotification::from(&value, Some(packets_notification));
Message::UpdatePacketsNotification(packets_notification, false)
},
)
.padding(1)
Expand Down Expand Up @@ -519,45 +497,9 @@ fn input_group_bytes(
&curr_threshold_str
},
move |value| {
let mut byte_multiple_inserted = ByteMultiple::B;
let new_threshold = if value.is_empty() {
0
} else if !value.chars().map(char::is_numeric).any(|x| !x) {
// no multiple
value
.parse::<u64>()
.unwrap_or(bytes_notification.previous_threshold)
} else {
// multiple
let last_char = value.chars().last().unwrap();
byte_multiple_inserted = from_char_to_multiple(last_char);
let without_multiple = value[0..value.len() - 1].to_string();
if without_multiple.parse::<u64>().is_ok()
&& TryInto::<u64>::try_into(
without_multiple.parse::<u128>().unwrap()
* u128::from(byte_multiple_inserted.get_multiplier()),
)
.is_ok()
{
without_multiple.parse::<u64>().unwrap()
* byte_multiple_inserted.get_multiplier()
} else if without_multiple.is_empty() {
byte_multiple_inserted = ByteMultiple::B;
0
} else {
byte_multiple_inserted = bytes_notification.byte_multiple;
bytes_notification.previous_threshold
}
};
Message::UpdateBytesNotification(
BytesNotification {
threshold: Some(new_threshold),
previous_threshold: new_threshold,
byte_multiple: byte_multiple_inserted,
..bytes_notification
},
false,
)
let bytes_notification =
BytesNotification::from(&value, Some(bytes_notification));
Message::UpdateBytesNotification(bytes_notification, false)
},
)
.padding(1)
Expand Down
Loading