Added constructor for Data, rewrote unit tests

This commit is contained in:
Xithrius 2021-09-04 02:59:30 -07:00
parent 792dbdbdfb
commit df0b0fc926
No known key found for this signature in database
GPG Key ID: 04B94D37B0B8C08F
2 changed files with 47 additions and 46 deletions

View File

@ -1,5 +1,5 @@
use textwrap;
use tui::style::{Color::Rgb, Style};
use tui::style::{Color, Color::Rgb, Style};
use tui::widgets::{Cell, Row};
#[derive(Debug, Clone)]
@ -10,13 +10,24 @@ pub struct Data {
}
impl Data {
pub fn to_row(&self) -> Row {
let user_bytes = self.author.as_bytes();
let user_color = Rgb(user_bytes[0] * 2, user_bytes[1] * 2, user_bytes[2] * 2);
pub fn new(time_sent: String, author: String, message: String) -> Self {
Data {
time_sent,
author,
message,
}
}
pub fn hash_username(&self) -> Color {
let user_bytes = self.author.as_bytes();
Rgb(user_bytes[0] * 2, user_bytes[1] * 2, user_bytes[2] * 2)
}
pub fn to_row(&self) -> Row {
Row::new(vec![
Cell::from(self.time_sent.to_string()),
Cell::from(self.author.to_string()).style(Style::default().fg(user_color)),
Cell::from(self.author.to_string()).style(Style::default().fg(self.hash_username())),
Cell::from(self.message.to_string()),
])
}
@ -32,18 +43,18 @@ impl Data {
if split_message.len() == 1 {
data_vec.push(self);
} else if split_message.len() > 1 {
data_vec.push(Data {
time_sent: self.time_sent,
author: self.author,
message: split_message[0].to_string(),
});
data_vec.push(Data::new(
self.time_sent,
self.author,
split_message[0].to_string(),
));
for index in 1..split_message.len() {
data_vec.push(Data {
time_sent: "".to_string(),
author: "".to_string(),
message: split_message[index].clone(),
});
data_vec.push(Data::new(
"".to_string(),
"".to_string(),
split_message[index].to_string(),
));
}
}
@ -54,39 +65,30 @@ impl Data {
#[cfg(test)]
mod tests {
use chrono::Local;
use tui::style::Color::Rgb;
use super::*;
fn generate_time() -> String {
Local::now().format("%c").to_string()
fn create_data() -> Data {
Data::new(
Local::now().format("%c").to_string(),
"human".to_string(),
"beep boop".to_string(),
)
}
#[test]
fn test_data_vec() {
let some_time = generate_time();
let var = Data {
time_sent: some_time.to_string(),
author: "A human".to_string(),
message: "beep boop".to_string(),
};
let var_vector_test = vec![
some_time.to_string(),
"A human".to_string(),
"beep boop".to_string(),
];
assert_eq!(var.to_vec(), var_vector_test);
fn test_username_hash() {
assert_eq!(
create_data().hash_username(),
Rgb(104 * 2, 117 * 2, 109 * 2)
);
}
#[test]
fn test_data_message_wrapping() {
let some_data = Data {
time_sent: generate_time(),
author: "A human".to_string(),
message: "asdf ".repeat(10),
};
let mut some_data = create_data();
some_data.message = "asdf ".repeat(10);
let some_vec = some_data.wrap_message(5);
assert_eq!(some_vec.len(), 10);

View File

@ -28,14 +28,13 @@ pub async fn twitch_irc(config: &CompleteConfig, tx: &Sender<Data>) {
Some(username) => username.to_string(),
None => "Undefined username".to_string(),
};
if let Err(e) = tx.send(Data {
time_sent: format!(
"{}",
Local::now().format(config.frontend.date_format.as_str())
),
author: user,
message: msg.to_string(),
}) {
if let Err(e) = tx.send(Data::new(
Local::now()
.format(config.frontend.date_format.as_str())
.to_string(),
user,
msg.to_string(),
)) {
println!("{}", e);
}
}