commit 84bd772636647be68110269f1b09725e82037380
parent 19d630d8313595d57cba4c8bd9da6c0a37a113f2
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Fri, 4 Oct 2019 17:09:23 -0400
Add tests for Entry and clean up formatting adjustments
Diffstat:
2 files changed, 70 insertions(+), 5 deletions(-)
diff --git a/src/entry.rs b/src/entry.rs
@@ -1,8 +1,8 @@
-use chrono::{DateTime, Duration, FixedOffset, Local, SecondsFormat};
+use chrono::{offset::TimeZone, DateTime, Duration, FixedOffset, Local, SecondsFormat};
use std::fmt;
-#[derive(Ord, PartialOrd, PartialEq, Eq)]
+#[derive(Ord, PartialOrd, PartialEq, Eq, Debug)]
pub struct Entry {
pub timestamp: DateTime<FixedOffset>,
pub author: Option<String>,
@@ -87,7 +87,7 @@ impl fmt::Display for Entry {
write!(
formatter,
- "\n@{} {}\n{}",
+ "@{} {}\n{}",
self.author.as_ref().unwrap_or(&"".to_string()),
×tamp,
&self.content
@@ -100,6 +100,46 @@ mod tests {
use super::*;
#[test]
+ fn test_new() {
+ let start: DateTime<FixedOffset> = Local::now().into();
+ let entry = Entry::new("content".to_string(), Some("author".to_string()));
+ let end: DateTime<FixedOffset> = Local::now().into();
+
+ assert_eq!(start.cmp(&entry.timestamp), std::cmp::Ordering::Less);
+ assert_eq!(end.cmp(&entry.timestamp), std::cmp::Ordering::Greater);
+ assert_eq!(entry.content, "content");
+ assert_eq!(entry.author, Some("author".to_string()));
+ }
+
+ #[test]
+ fn test_parse() {
+ assert_eq!(Entry::parse("This is not valid twtxt.\t", None), Err(()));
+ assert_eq!(
+ Entry::parse("2016-02-04T13:30:01+01:00\tThis is valid twtxt.", None),
+ Ok(Entry {
+ timestamp: FixedOffset::east(3600).ymd(2016, 02, 04).and_hms(13, 30, 1),
+ author: None,
+ content: "This is valid twtxt.".to_string(),
+ })
+ );
+ }
+
+ #[test]
+ fn test_to_twtxt() {
+ assert_eq!(
+ Entry {
+ timestamp: FixedOffset::east(3600)
+ .ymd(2016, 02, 04)
+ .and_hms_milli(13, 30, 1, 238),
+ author: None,
+ content: "Hello world!".to_string(),
+ }
+ .to_twtxt(),
+ "2016-02-04T13:30:01+01:00\tHello world!\n"
+ );
+ }
+
+ #[test]
fn test_format_duration() {
assert_eq!(
Entry::format_duration(Duration::days(365 * 2)),
@@ -124,4 +164,29 @@ mod tests {
assert_eq!(Entry::format_duration(Duration::minutes(1)), "1 minute ago");
assert_eq!(Entry::format_duration(Duration::seconds(30)), "just now");
}
+
+ #[test]
+ fn test_display() {
+ let timestamp = (Local::now() - Duration::weeks(1)).into();
+ let entry = Entry {
+ timestamp,
+ author: Some("anonymous".to_string()),
+ content: "Hello world!".to_string(),
+ };
+
+ assert_eq!(
+ format!("{}", entry),
+ format!("@anonymous 1 week ago\nHello world!",)
+ );
+
+ assert_eq!(
+ format!("{:#}", entry),
+ format!(
+ "@anonymous {}\nHello world!",
+ timestamp
+ .with_timezone(&Local)
+ .to_rfc3339_opts(SecondsFormat::Secs, true)
+ )
+ );
+ }
}
diff --git a/src/timeline.rs b/src/timeline.rs
@@ -24,9 +24,9 @@ pub fn timeline(config: &Config, _subcommand: &ArgMatches) {
for _ in 0..config.limit_timeline {
if let Some(tweet) = all_tweets.pop() {
if config.use_abs_time {
- println!("{:#}", tweet);
+ println!("\n{:#}", tweet);
} else {
- println!("{}", tweet);
+ println!("\n{}", tweet);
}
}
}