Skip to content

Commit

Permalink
refactor: simplify building of options in tests
Browse files Browse the repository at this point in the history
- Replace the global constant `DEFAULT_SYNC_OPTIONS` with a factory
  function to obtain a new instance of the options object
- Rework tests using custom options to start with the default options
  and modify only the fields relevant in the test.

With this improvement in place, when we add a new sync option in the
future, existing tests should not need any changes.

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed May 4, 2020
1 parent fd2edac commit d917c91
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,6 @@ mod tests {
use egg_mode::tweet::{ExtendedTweetEntities, TweetEntities, TweetSource};
use egg_mode::user::{TwitterUser, UserEntities, UserEntityDetail};

static DEFAULT_SYNC_OPTIONS: SyncOptions = SyncOptions {
sync_reblogs: true,
sync_retweets: true,
};

#[test]
fn tweet_shortening() {
let toot = "#MASTODON POST PRIVACY - who can see your post?
Expand Down Expand Up @@ -484,7 +479,7 @@ UNLISTED 🔓 ✅ Tagged people

let tweets = vec![tweet];
let statuses = vec![status];
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());
assert!(posts.toots.is_empty());
assert!(posts.tweets.is_empty());
}
Expand All @@ -506,7 +501,7 @@ UNLISTED 🔓 ✅ Tagged people

let tweets = vec![tweet];
let statuses = vec![status];
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());
assert!(posts.toots.is_empty());
assert!(posts.tweets.is_empty());
}
Expand All @@ -517,7 +512,7 @@ UNLISTED 🔓 ✅ Tagged people
fn mastodon_html_decode() {
let mut status = get_mastodon_status();
status.content = "<p>You &amp; me!</p>".to_string();
let posts = determine_posts(&vec![status], &Vec::new(), &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&vec![status], &Vec::new(), &get_sync_options());
assert_eq!(posts.tweets[0].text, "You & me!");
}

Expand All @@ -527,7 +522,7 @@ UNLISTED 🔓 ✅ Tagged people
fn twitter_html_decode() {
let mut status = get_twitter_status();
status.text = "You &amp; me!".to_string();
let posts = determine_posts(&Vec::new(), &vec![status], &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&Vec::new(), &vec![status], &get_sync_options());
assert_eq!(posts.toots[0].text, "You & me!");
}

Expand All @@ -541,7 +536,7 @@ UNLISTED 🔓 ✅ Tagged people
status.reblog = Some(Box::new(reblog));
status.reblogged = Some(true);

let posts = determine_posts(&vec![status], &Vec::new(), &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&vec![status], &Vec::new(), &get_sync_options());
assert_eq!(posts.tweets[0].text, "RT example: Some example toooot!");
}

Expand All @@ -555,7 +550,7 @@ UNLISTED 🔓 ✅ Tagged people
status.reblog = Some(Box::new(reblog));
status.reblogged = Some(true);

let posts = determine_posts(&vec![status], &Vec::new(), &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&vec![status], &Vec::new(), &get_sync_options());
assert_eq!(posts.tweets[0].text, "RT example: longer than 280 characters longer than 280 characters longer than 280 characters longer than 280 characters longer than 280 characters longer than 280 characters longer than 280 characters longer than… https://example.com/a/b/c/5");
}

Expand All @@ -574,7 +569,7 @@ UNLISTED 🔓 ✅ Tagged people

let tweets = vec![tweet];
let statuses = vec![status];
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());
assert!(posts.toots.is_empty());
assert!(posts.tweets.is_empty());
}
Expand Down Expand Up @@ -605,7 +600,7 @@ UNLISTED 🔓 ✅ Tagged people
status.content = "@Test Hello! http://example.com".to_string();
let tweets = Vec::new();
let statuses = vec![status];
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());
assert!(posts.toots.is_empty());
assert!(posts.tweets.is_empty());
}
Expand All @@ -617,7 +612,7 @@ UNLISTED 🔓 ✅ Tagged people
status.content = "Österreich".to_string();
let tweets = Vec::new();
let statuses = vec![status];
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());
assert!(posts.toots.is_empty());
assert_eq!(posts.tweets[0].text, "Österreich");
}
Expand Down Expand Up @@ -655,7 +650,7 @@ UNLISTED 🔓 ✅ Tagged people
fn pictures_in_tweet() {
let tweets = vec![get_twitter_status_media()];
let statuses = Vec::new();
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());

let status = &posts.toots[0];
assert_eq!(status.text, "Verhalten bei #Hausdurchsuchung");
Expand All @@ -675,7 +670,7 @@ UNLISTED 🔓 ✅ Tagged people
let tweet = get_twitter_status_video();
let tweets = vec![tweet];
let statuses = Vec::new();
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());

let status = &posts.toots[0];
assert_eq!(status.text, "Verhalten bei #Hausdurchsuchung");
Expand All @@ -695,7 +690,7 @@ UNLISTED 🔓 ✅ Tagged people
fn pictures_in_toot() {
let statuses = vec![get_mastodon_status_media()];
let tweets = Vec::new();
let posts = determine_posts(&statuses, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&statuses, &tweets, &get_sync_options());

let tweet = &posts.tweets[0];
assert_eq!(tweet.text, "test image");
Expand All @@ -720,7 +715,7 @@ UNLISTED 🔓 ✅ Tagged people

let tweets = vec![retweet];
let toots = Vec::new();
let posts = determine_posts(&toots, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&toots, &tweets, &get_sync_options());

let sync_toot = &posts.toots[0];
assert_eq!(
Expand All @@ -742,7 +737,7 @@ UNLISTED 🔓 ✅ Tagged people

let tweets = Vec::new();
let toots = vec![boost];
let posts = determine_posts(&toots, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&toots, &tweets, &get_sync_options());

let sync_tweet = &posts.tweets[0];
assert_eq!(sync_tweet.text, "RT example: test image");
Expand Down Expand Up @@ -777,7 +772,7 @@ UNLISTED 🔓 ✅ Tagged people

let tweets = vec![quote_tweet];
let toots = Vec::new();
let posts = determine_posts(&toots, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&toots, &tweets, &get_sync_options());

let sync_toot = &posts.toots[0];
assert_eq!(
Expand Down Expand Up @@ -817,7 +812,7 @@ QT test123: Original text"

let tweets = vec![quote_tweet];
let toots = Vec::new();
let posts = determine_posts(&toots, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&toots, &tweets, &get_sync_options());

let sync_toot = &posts.toots[0];
assert_eq!(
Expand Down Expand Up @@ -859,7 +854,7 @@ QT test123: Original text"

let tweets = vec![quote_tweet];
let toots = Vec::new();
let posts = determine_posts(&toots, &tweets, &DEFAULT_SYNC_OPTIONS);
let posts = determine_posts(&toots, &tweets, &get_sync_options());

let sync_toot = &posts.toots[0];
assert_eq!(
Expand Down Expand Up @@ -888,10 +883,8 @@ QT test123: Verhalten bei #Hausdurchsuchung"

let tweets = vec![retweet];
let toots = Vec::new();
let options = SyncOptions {
sync_reblogs: true,
sync_retweets: false,
};
let mut options = get_sync_options();
options.sync_retweets = false;

let posts = determine_posts(&toots, &tweets, &options);
assert!(posts.toots.is_empty());
Expand All @@ -912,10 +905,9 @@ QT test123: Verhalten bei #Hausdurchsuchung"

let tweets = vec![quote_tweet];
let toots = Vec::new();
let options = SyncOptions {
sync_reblogs: true,
sync_retweets: false,
};
let mut options = get_sync_options();
options.sync_retweets = false;

let posts = determine_posts(&toots, &tweets, &options);

let sync_toot = &posts.toots[0];
Expand All @@ -937,10 +929,8 @@ QT test123: Original text"

let tweets = Vec::new();
let toots = vec![boost];
let options = SyncOptions {
sync_reblogs: false,
sync_retweets: true,
};
let mut options = get_sync_options();
options.sync_reblogs = false;

let posts = determine_posts(&toots, &tweets, &options);
assert!(posts.toots.is_empty());
Expand Down Expand Up @@ -1203,4 +1193,15 @@ QT test123: Original text"
withheld_scope: None,
}
}

/**
* Create default sync options to be used as the base configuration
* in tests.
*/
fn get_sync_options() -> SyncOptions {
return SyncOptions {
sync_reblogs: true,
sync_retweets: true,
};
}
}

0 comments on commit d917c91

Please sign in to comment.