From 369780f48ff3df9e41503a76351ebcc1c4290c34 Mon Sep 17 00:00:00 2001 From: fef Date: Thu, 22 Dec 2022 13:44:18 +0100 Subject: [PATCH] ap: add tests for context --- src/ap/context.rs | 101 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/src/ap/context.rs b/src/ap/context.rs index 2735b11..41cae55 100644 --- a/src/ap/context.rs +++ b/src/ap/context.rs @@ -1,5 +1,5 @@ use serde::de::{MapAccess, SeqAccess}; -use serde::ser::SerializeSeq; +use serde::ser::{SerializeMap, SerializeSeq}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::collections::HashMap; use std::fmt; @@ -7,6 +7,8 @@ use std::fmt; use crate::core::*; /// The `@context` field in an ActivityPub document. +#[cfg_attr(test, derive(PartialEq))] +#[derive(Debug)] pub struct Context { entries: Vec, } @@ -18,16 +20,16 @@ pub mod uri { pub static TOOT: &'static str = "http://joinmastodon.org/ns#"; } -#[derive(Clone)] +#[derive(Debug, Clone)] pub enum Entry { Anon(AnonEntry), Named(NamedEntry), } -#[derive(Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct AnonEntry(String); -#[derive(Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct NamedEntry(String, String); impl Entry { @@ -60,9 +62,7 @@ impl PartialEq for Entry { Entry::Anon(self_anon) => self_anon.0.eq(other.uri()), Entry::Named(self_named) => match other { Entry::Anon(other_anon) => self_named.1.eq(&other_anon.0), - Entry::Named(other_named) => { - self_named.0.eq(&other_named.0) && self_named.1.eq(&other_named.1) - } + Entry::Named(other_named) => self_named.eq(other_named), }, } } @@ -118,6 +118,14 @@ impl Context { } } +impl Default for Context { + fn default() -> Self { + let mut ctx = Context::new(); + ctx.add_entry(Entry::anon(uri::ACTIVITY_STREAMS)); + ctx + } +} + impl Serialize for Context { fn serialize(&self, serializer: S) -> std::result::Result where @@ -129,13 +137,11 @@ impl Serialize for Context { serializer.serialize_str(self.entries[0].uri()) } else { let mut anons = Vec::new(); - let mut nameds = HashMap::new(); + let mut nameds = Vec::new(); for e in &self.entries { match e { Entry::Anon(anon) => anons.push(anon), - Entry::Named(named) => { - nameds.insert(named.0.clone(), named.1.clone()); - } + Entry::Named(named) => nameds.push(named), } } let (anons, nameds) = (anons, nameds); @@ -146,7 +152,7 @@ impl Serialize for Context { seq.serialize_element(&a.0)?; } if nameds.len() > 0 { - seq.serialize_element(&nameds)?; + seq.serialize_element(&NamedEntriesWrapper(nameds))?; } seq.end() } @@ -275,8 +281,77 @@ impl<'de> de::Visitor<'de> for StupidEntryWrapperVisitor { { let mut entries = Vec::new(); while let Some((k, v)) = map.next_entry()? { - entries.push(Entry::named(k, v)); + entries.push(Entry::Named(NamedEntry(k, v))); } Ok(StupidEntryWrapper::Multi(entries)) } } + +/// We can't use a HashMap to serialize named entries because that doesn't +/// produce deterministic results, so we have to use our own serializer +struct NamedEntriesWrapper<'a>(Vec<&'a NamedEntry>); + +impl<'a> Serialize for NamedEntriesWrapper<'a> { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + let mut map = serializer.serialize_map(Some(self.0.len()))?; + for entry in &self.0 { + //let entry = *entry; + map.serialize_entry(&entry.0, &entry.1)?; + } + map.end() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_test::{assert_tokens, Token}; + + #[test] + fn default() { + let ctx = Context::default(); + + assert_tokens(&ctx, &[Token::String(uri::ACTIVITY_STREAMS)]); + } + + #[test] + fn multi_anon() { + let mut ctx = Context::default(); + ctx.add_entry(Entry::anon(uri::NYANO)); + + assert_tokens( + &ctx, + &[ + Token::Seq { len: Some(2) }, + Token::String(uri::ACTIVITY_STREAMS), + Token::String(uri::NYANO), + Token::SeqEnd, + ], + ); + } + + #[test] + fn multi_named() { + let mut ctx = Context::default(); + ctx.add_entry(Entry::named("nyano", uri::NYANO)); + ctx.add_entry(Entry::named("toot", uri::TOOT)); + + assert_tokens( + &ctx, + &[ + Token::Seq { len: Some(2) }, + Token::String(uri::ACTIVITY_STREAMS), + Token::Map { len: Some(2) }, + Token::String("nyano"), + Token::String(uri::NYANO), + Token::String("toot"), + Token::String(uri::TOOT), + Token::MapEnd, + Token::SeqEnd, + ], + ) + } +}