ap: add tests for context
This commit is contained in:
parent
e568927d4b
commit
369780f48f
1 changed files with 88 additions and 13 deletions
|
@ -1,5 +1,5 @@
|
||||||
use serde::de::{MapAccess, SeqAccess};
|
use serde::de::{MapAccess, SeqAccess};
|
||||||
use serde::ser::SerializeSeq;
|
use serde::ser::{SerializeMap, SerializeSeq};
|
||||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
@ -7,6 +7,8 @@ use std::fmt;
|
||||||
use crate::core::*;
|
use crate::core::*;
|
||||||
|
|
||||||
/// The `@context` field in an ActivityPub document.
|
/// The `@context` field in an ActivityPub document.
|
||||||
|
#[cfg_attr(test, derive(PartialEq))]
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
entries: Vec<Entry>,
|
entries: Vec<Entry>,
|
||||||
}
|
}
|
||||||
|
@ -18,16 +20,16 @@ pub mod uri {
|
||||||
pub static TOOT: &'static str = "http://joinmastodon.org/ns#";
|
pub static TOOT: &'static str = "http://joinmastodon.org/ns#";
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Entry {
|
pub enum Entry {
|
||||||
Anon(AnonEntry),
|
Anon(AnonEntry),
|
||||||
Named(NamedEntry),
|
Named(NamedEntry),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct AnonEntry(String);
|
pub struct AnonEntry(String);
|
||||||
|
|
||||||
#[derive(Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct NamedEntry(String, String);
|
pub struct NamedEntry(String, String);
|
||||||
|
|
||||||
impl Entry {
|
impl Entry {
|
||||||
|
@ -60,9 +62,7 @@ impl PartialEq for Entry {
|
||||||
Entry::Anon(self_anon) => self_anon.0.eq(other.uri()),
|
Entry::Anon(self_anon) => self_anon.0.eq(other.uri()),
|
||||||
Entry::Named(self_named) => match other {
|
Entry::Named(self_named) => match other {
|
||||||
Entry::Anon(other_anon) => self_named.1.eq(&other_anon.0),
|
Entry::Anon(other_anon) => self_named.1.eq(&other_anon.0),
|
||||||
Entry::Named(other_named) => {
|
Entry::Named(other_named) => self_named.eq(other_named),
|
||||||
self_named.0.eq(&other_named.0) && self_named.1.eq(&other_named.1)
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
impl Serialize for Context {
|
||||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||||
where
|
where
|
||||||
|
@ -129,13 +137,11 @@ impl Serialize for Context {
|
||||||
serializer.serialize_str(self.entries[0].uri())
|
serializer.serialize_str(self.entries[0].uri())
|
||||||
} else {
|
} else {
|
||||||
let mut anons = Vec::new();
|
let mut anons = Vec::new();
|
||||||
let mut nameds = HashMap::new();
|
let mut nameds = Vec::new();
|
||||||
for e in &self.entries {
|
for e in &self.entries {
|
||||||
match e {
|
match e {
|
||||||
Entry::Anon(anon) => anons.push(anon),
|
Entry::Anon(anon) => anons.push(anon),
|
||||||
Entry::Named(named) => {
|
Entry::Named(named) => nameds.push(named),
|
||||||
nameds.insert(named.0.clone(), named.1.clone());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let (anons, nameds) = (anons, nameds);
|
let (anons, nameds) = (anons, nameds);
|
||||||
|
@ -146,7 +152,7 @@ impl Serialize for Context {
|
||||||
seq.serialize_element(&a.0)?;
|
seq.serialize_element(&a.0)?;
|
||||||
}
|
}
|
||||||
if nameds.len() > 0 {
|
if nameds.len() > 0 {
|
||||||
seq.serialize_element(&nameds)?;
|
seq.serialize_element(&NamedEntriesWrapper(nameds))?;
|
||||||
}
|
}
|
||||||
seq.end()
|
seq.end()
|
||||||
}
|
}
|
||||||
|
@ -275,8 +281,77 @@ impl<'de> de::Visitor<'de> for StupidEntryWrapperVisitor {
|
||||||
{
|
{
|
||||||
let mut entries = Vec::new();
|
let mut entries = Vec::new();
|
||||||
while let Some((k, v)) = map.next_entry()? {
|
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))
|
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,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue