Added testing for message::Parser

This commit is contained in:
Nathan Fisher 2023-06-07 16:32:51 -04:00
parent 676c7b34dc
commit 22972ed43d
3 changed files with 48 additions and 17 deletions

View file

@ -44,7 +44,7 @@ pub struct Message {
pub from: Mailbox,
pub senders: Vec<Mailbox>,
pub recipients: Vec<Mailbox>,
pub timstamp: Option<String>,
pub timestamp: Option<String>,
pub title: Option<String>,
pub body: String,
}
@ -61,7 +61,7 @@ impl fmt::Display for Message {
self.recipients.iter().try_for_each(|r| write!(f, " {r}"))?;
writeln!(f)?;
}
if let Some(ref t) = self.timstamp {
if let Some(ref t) = self.timestamp {
writeln!(f, "@ {t}")?;
}
write!(f, "{}\r\n", self.body)
@ -85,14 +85,4 @@ mod tests {
let rec: Recipients = REC.parse().unwrap();
assert_eq!(rec.to_string(), REC);
}
#[test]
fn parse_message() {
todo!()
}
#[test]
fn print_message() {
todo!()
}
}

View file

@ -45,27 +45,35 @@ impl Parser {
if let Some(t) = s.strip_prefix("###").map(|x| x.trim().to_string()) {
self.title = Some(t);
}
self.body.push_str(s);
if !self.body.is_empty() {
self.body.push('\n');
}
self.body.push_str(s);
}
s if s.starts_with("##") && self.title.is_none() => {
if let Some(t) = s.strip_prefix("##").map(|x| x.trim().to_string()) {
self.title = Some(t);
}
self.body.push_str(s);
if !self.body.is_empty() {
self.body.push('\n');
}
self.body.push_str(s);
}
s if s.starts_with("#") && self.title.is_none() => {
if let Some(t) = s.strip_prefix("#").map(|x| x.trim().to_string()) {
self.title = Some(t);
}
self.body.push_str(s);
if !self.body.is_empty() {
self.body.push('\n');
}
s => {
self.body.push_str(s);
}
s => {
if !self.body.is_empty() {
self.body.push('\n');
}
self.body.push_str(s);
}
}
}
if self.from.is_none() {
@ -76,10 +84,38 @@ impl Parser {
from: self.from.unwrap(),
senders: self.senders,
recipients: self.recipients.boxes,
timstamp: self.timestamp,
timestamp: self.timestamp,
title: self.title,
body: self.body,
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const RAW: &'static str = include_str!("../../test/ox42sc69.gmi");
#[test]
fn parse_message() {
let msg: Message = Parser::new("ox42sc69").parse(RAW).unwrap();
assert_eq!(msg.id, "ox42sc69");
assert_eq!(msg.from.to_string(), "joe@gemini.example.org");
assert_eq!(msg.timestamp.unwrap(), "2023-06-07T16:09:42Z");
assert!(msg.senders.is_empty());
assert!(msg.recipients.is_empty());
assert_eq!(msg.title.unwrap(), "How 'bout dose Bears?");
assert_eq!(msg.body, "# How 'bout dose Bears?\nWhen are they coming for dinner anyway?\n");
}
#[test]
fn print_message() {
let msg: Message = Parser::new("ox42sc69").parse(RAW).unwrap();
assert_eq!(
msg.to_string(),
"< joe@gemini.example.org\n@ 2023-06-07T16:09:42Z\n# How 'bout dose Bears?\nWhen are they coming for dinner anyway?\n\r\n"
);
}
}

5
test/ox42sc69.gmi Normal file
View file

@ -0,0 +1,5 @@
< joe@gemini.example.org
@ 2023-06-07T16:09:42Z
# How 'bout dose Bears?
When are they coming for dinner anyway?