feature(test): add tests

This commit is contained in:
VC
2022-04-22 17:10:08 +02:00
parent 080218f385
commit 6363c12460
9 changed files with 222 additions and 26 deletions

View File

@@ -1,6 +1,5 @@
// auto imports
use crate::config::MastodonConfig;
use crate::twitter::decode_urls;
// std
use std::{
@@ -16,7 +15,7 @@ use htmlescape::decode_html;
// egg-mode
use egg_mode::{
tweet::Tweet,
entities::MentionEntity,
entities::{UrlEntity, MentionEntity},
};
// elefren
@@ -39,6 +38,20 @@ fn twitter_mentions(ums: &Vec<MentionEntity>) -> HashMap<String, String> {
decoded_mentions
}
/// Decodes urls from UrlEntities
fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
let mut decoded_urls = HashMap::new();
for url in urls {
if url.expanded_url.is_some() {
// unwrap is safe here as we just verified that there is something inside expanded_url
decoded_urls.insert(String::from(&url.url), String::from(url.expanded_url.as_deref().unwrap()));
}
}
decoded_urls
}
/// Gets Mastodon Data
pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
let data = Data {
@@ -104,3 +117,52 @@ pub fn register(host: &str) {
println!("Please insert the following block at the end of your configuration file:\n[mastodon]\n{}", toml);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_twitter_mentions() {
let mention_entity = MentionEntity {
id: 12345,
range: (1, 3),
name: String::from("Ta Mere l0l"),
screen_name: String::from("tamerelol"),
};
let twitter_ums = vec![mention_entity];
let mut expected_mentions = HashMap::new();
expected_mentions.insert(String::from("@tamerelol"), String::from("@tamerelol@twitter.com"));
let decoded_mentions = twitter_mentions(&twitter_ums);
assert_eq!(expected_mentions, decoded_mentions);
}
#[test]
fn test_decode_urls() {
let url_entity1 = UrlEntity {
display_url: String::from("tamerelol"),
expanded_url: Some(String::from("https://www.nintendojo.fr/dojobar")),
range: (1, 3),
url: String::from("https://t.me/tamerelol"),
};
let url_entity2 = UrlEntity {
display_url: String::from("tamerelol"),
expanded_url: None,
range: (1, 3),
url: String::from("https://t.me/tamerelol"),
};
let twitter_urls = vec![url_entity1, url_entity2];
let mut expected_urls = HashMap::new();
expected_urls.insert(String::from("https://t.me/tamerelol"), String::from("https://www.nintendojo.fr/dojobar"));
let decoded_urls = decode_urls(&twitter_urls);
assert_eq!(expected_urls, decoded_urls);
}
}