feature: add test for build_basic_status() fn

This commit is contained in:
VC
2022-04-24 14:06:46 +02:00
parent 6c0383d9d0
commit 734f03f5a9
4 changed files with 83 additions and 24 deletions

View File

@@ -2,11 +2,10 @@ use crate::config::MastodonConfig;
use std::{
borrow::Cow,
error::Error,
collections::HashMap,
io::stdin,
};
use htmlescape::decode_html;
use html_escape::decode_html_entities;
use egg_mode::{
tweet::Tweet,
entities::{UrlEntity, MentionEntity},
@@ -48,24 +47,18 @@ pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
}
/// Builds toot text from tweet
pub fn build_basic_status(tweet: &Tweet) -> Result<String, Box<dyn Error>> {
pub fn build_basic_status(tweet: &Tweet) -> String {
let mut toot = String::from(&tweet.text);
let decoded_urls = decode_urls(&tweet.entities.urls);
for decoded_url in decoded_urls {
for decoded_url in decode_urls(&tweet.entities.urls) {
toot = toot.replace(&decoded_url.0, &decoded_url.1);
}
let decoded_mentions = twitter_mentions(&tweet.entities.user_mentions);
for decoded_mention in decoded_mentions {
for decoded_mention in twitter_mentions(&tweet.entities.user_mentions) {
toot = toot.replace(&decoded_mention.0, &decoded_mention.1);
}
if let Ok(t) = decode_html(&toot) {
toot = t;
}
Ok(toot)
decode_html_entities(&toot).to_string()
}
/// Generic register function
@@ -101,6 +94,9 @@ pub fn register(host: &str) {
mod tests {
use super::*;
use egg_mode::tweet::TweetEntities;
use chrono::prelude::*;
#[test]
fn test_twitter_mentions() {
let mention_entity = MentionEntity {
@@ -145,5 +141,63 @@ mod tests {
assert_eq!(expected_urls, decoded_urls);
}
#[test]
fn test_build_basic_status() {
let t = Tweet {
coordinates: None,
created_at: Utc::now(),
current_user_retweet: None,
display_text_range: None,
entities: TweetEntities {
hashtags: vec![],
symbols: vec![],
urls: vec![
UrlEntity {
display_url: "youtube.com/watch?v=w5TrSa…".to_string(),
expanded_url: Some("https://www.youtube.com/watch?v=w5TrSaoYmZ8".to_string()),
range: (93, 116),
url: "https://t.co/zXw0FfX2Nt".to_string(),
}
],
user_mentions: vec![
MentionEntity {
id: 491500016,
range: (80, 95),
name: "Nintendo France".to_string(),
screen_name: "NintendoFrance".to_string(),
}
],
media: None,
},
extended_entities: None,
favorite_count: 0,
favorited: None,
filter_level: None,
id: 1491541246984306693,
in_reply_to_user_id: None,
in_reply_to_screen_name: None,
in_reply_to_status_id: None,
lang: None,
place: None,
possibly_sensitive: None,
quoted_status: None,
quoted_status_id: None,
retweet_count: 0,
retweeted: None,
retweeted_status: None,
source: None,
text: "Mother 1 &amp; 2 sur le NES/SNES online !\nDispo maintenant. cc @NintendoFrance https://t.co/zXw0FfX2Nt".to_string(),
truncated: false,
user: None,
withheld_copyright: false,
withheld_in_countries: None,
withheld_scope: None,
};
let t_out = build_basic_status(&t);
assert_eq!(&t_out, "Mother 1 & 2 sur le NES/SNES online !\nDispo maintenant. cc @NintendoFrance@twitter.com https://www.youtube.com/watch?v=w5TrSaoYmZ8");
}
}