ap: add tests for context

This commit is contained in:
anna 2022-12-22 13:44:18 +01:00
parent e568927d4b
commit 369780f48f
Signed by: fef
GPG key ID: EC22E476DC2D3D84

View file

@ -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<Entry>,
}
@ -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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
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<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
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,
],
)
}
}