Added testing for message::Parser
This commit is contained in:
parent
676c7b34dc
commit
22972ed43d
3 changed files with 48 additions and 17 deletions
|
@ -44,7 +44,7 @@ pub struct Message {
|
||||||
pub from: Mailbox,
|
pub from: Mailbox,
|
||||||
pub senders: Vec<Mailbox>,
|
pub senders: Vec<Mailbox>,
|
||||||
pub recipients: Vec<Mailbox>,
|
pub recipients: Vec<Mailbox>,
|
||||||
pub timstamp: Option<String>,
|
pub timestamp: Option<String>,
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
pub body: String,
|
pub body: String,
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ impl fmt::Display for Message {
|
||||||
self.recipients.iter().try_for_each(|r| write!(f, " {r}"))?;
|
self.recipients.iter().try_for_each(|r| write!(f, " {r}"))?;
|
||||||
writeln!(f)?;
|
writeln!(f)?;
|
||||||
}
|
}
|
||||||
if let Some(ref t) = self.timstamp {
|
if let Some(ref t) = self.timestamp {
|
||||||
writeln!(f, "@ {t}")?;
|
writeln!(f, "@ {t}")?;
|
||||||
}
|
}
|
||||||
write!(f, "{}\r\n", self.body)
|
write!(f, "{}\r\n", self.body)
|
||||||
|
@ -85,14 +85,4 @@ mod tests {
|
||||||
let rec: Recipients = REC.parse().unwrap();
|
let rec: Recipients = REC.parse().unwrap();
|
||||||
assert_eq!(rec.to_string(), REC);
|
assert_eq!(rec.to_string(), REC);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn parse_message() {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn print_message() {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,27 +45,35 @@ impl Parser {
|
||||||
if let Some(t) = s.strip_prefix("###").map(|x| x.trim().to_string()) {
|
if let Some(t) = s.strip_prefix("###").map(|x| x.trim().to_string()) {
|
||||||
self.title = Some(t);
|
self.title = Some(t);
|
||||||
}
|
}
|
||||||
self.body.push_str(s);
|
if !self.body.is_empty() {
|
||||||
self.body.push('\n');
|
self.body.push('\n');
|
||||||
}
|
}
|
||||||
|
self.body.push_str(s);
|
||||||
|
}
|
||||||
s if s.starts_with("##") && self.title.is_none() => {
|
s if s.starts_with("##") && self.title.is_none() => {
|
||||||
if let Some(t) = s.strip_prefix("##").map(|x| x.trim().to_string()) {
|
if let Some(t) = s.strip_prefix("##").map(|x| x.trim().to_string()) {
|
||||||
self.title = Some(t);
|
self.title = Some(t);
|
||||||
}
|
}
|
||||||
self.body.push_str(s);
|
if !self.body.is_empty() {
|
||||||
self.body.push('\n');
|
self.body.push('\n');
|
||||||
}
|
}
|
||||||
|
self.body.push_str(s);
|
||||||
|
}
|
||||||
s if s.starts_with("#") && self.title.is_none() => {
|
s if s.starts_with("#") && self.title.is_none() => {
|
||||||
if let Some(t) = s.strip_prefix("#").map(|x| x.trim().to_string()) {
|
if let Some(t) = s.strip_prefix("#").map(|x| x.trim().to_string()) {
|
||||||
self.title = Some(t);
|
self.title = Some(t);
|
||||||
}
|
}
|
||||||
self.body.push_str(s);
|
if !self.body.is_empty() {
|
||||||
self.body.push('\n');
|
self.body.push('\n');
|
||||||
}
|
}
|
||||||
s => {
|
|
||||||
self.body.push_str(s);
|
self.body.push_str(s);
|
||||||
|
}
|
||||||
|
s => {
|
||||||
|
if !self.body.is_empty() {
|
||||||
self.body.push('\n');
|
self.body.push('\n');
|
||||||
}
|
}
|
||||||
|
self.body.push_str(s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.from.is_none() {
|
if self.from.is_none() {
|
||||||
|
@ -76,10 +84,38 @@ impl Parser {
|
||||||
from: self.from.unwrap(),
|
from: self.from.unwrap(),
|
||||||
senders: self.senders,
|
senders: self.senders,
|
||||||
recipients: self.recipients.boxes,
|
recipients: self.recipients.boxes,
|
||||||
timstamp: self.timestamp,
|
timestamp: self.timestamp,
|
||||||
title: self.title,
|
title: self.title,
|
||||||
body: self.body,
|
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
5
test/ox42sc69.gmi
Normal 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?
|
||||||
|
|
Loading…
Add table
Reference in a new issue