parser: test_context

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-04-21 18:30:41 +02:00
parent 1987b44315
commit 4688712904

View file

@ -189,26 +189,15 @@ fn parse_client(s: Span) -> IResult<Span, (Name, Client)> {
Ok((s, (String::from(name), client))) Ok((s, (String::from(name), client)))
} }
fn parse_clients(s: Span) -> IResult<Span, HashMap<Name, Client>> { fn parse_context(s: Span) -> IResult<Span, Context> {
let (s, clients) = many0(parse_client)(s)?; let (s, clients) = many0(parse_client)(s)?;
let mut map: HashMap<Name, Client> = HashMap::new(); let mut map: Context = HashMap::new();
for (name, client) in clients { for (name, client) in clients {
map.insert(name, client); map.insert(name, Entity::Client(client));
} }
Ok((s, map)) Ok((s, map))
} }
fn parse_context(s: Span) -> IResult<Span, Context> {
let (s, clients) = parse_clients(s)?;
Ok((
s,
clients
.into_iter()
.map(|(name, client)| (name, Entity::Client(client)))
.collect::<HashMap<_, _>>(),
))
}
fn parse_sep(s: Span) -> IResult<Span, Token> { fn parse_sep(s: Span) -> IResult<Span, Token> {
let (s, (pos, _)) = delimited(allspaces, tuple((position, many1(tag("-")))), allspaces)(s)?; let (s, (pos, _)) = delimited(allspaces, tuple((position, many1(tag("-")))), allspaces)(s)?;
Ok((s, Token { position: pos })) Ok((s, Token { position: pos }))
@ -458,7 +447,7 @@ mod tests {
} }
#[test] #[test]
fn test_clients() { fn test_context() {
let buf1 = r#" let buf1 = r#"
[Client] louise [Client] louise
jid: louise@localhost jid: louise@localhost
@ -469,20 +458,26 @@ mod tests {
password: password password: password
"#; "#;
let mut clients: HashMap<Name, Client> = HashMap::new(); let mut context: HashMap<Name, Entity> = HashMap::new();
clients.insert( context.insert(
String::from("louise"), String::from("louise"),
Client::new(Jid::from_str("louise@localhost").unwrap(), "password"), Entity::Client(Client::new(
Jid::from_str("louise@localhost").unwrap(),
"password",
)),
); );
clients.insert( context.insert(
String::from("須賀子"), String::from("須賀子"),
Client::new(Jid::from_str("sugako@localhost").unwrap(), "password"), Entity::Client(Client::new(
Jid::from_str("sugako@localhost").unwrap(),
"password",
)),
); );
assert_eq!( assert_eq!(
parse_clients(buf1.into()), parse_context(buf1.into()),
Ok(( Ok((
unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) }, unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) },
clients context
)) ))
); );
} }