aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/tests.rs
blob: 35a29ba4c9d26f6131c025c7df8f4b4cf1039fe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{ObjectBuffer, Tag, TypedTag};
use std::marker::PhantomData;

const NAME: TypedTag<&str> = TypedTag(Tag(15), PhantomData);
const AGE: TypedTag<u32> = TypedTag(Tag(13), PhantomData);
const FRIEND: TypedTag<&str> = TypedTag(Tag(54321), PhantomData);

fn test_object() -> ObjectBuffer {
    ObjectBuffer::new(&mut [
        (NAME.0, &"Bob"),
        (AGE.0, &35_u32),
        (FRIEND.0, &"Alice"),
        (FRIEND.0, &"Charlie"),
    ])
}

#[test]
fn read_single_field() {
    let bob = test_object();
    let bob = bob.as_object();
    assert_eq!(bob.get(NAME), Some("Bob"));
    assert_eq!(bob.get(AGE), Some(35));
}

#[test]
fn read_multi_field() {
    let bob = test_object();
    let bob = bob.as_object();

    let mut friends = bob.iter(FRIEND);
    assert_eq!(friends.next(), Some("Alice"));
    assert_eq!(friends.next(), Some("Charlie"));
    assert_eq!(friends.next(), None);
}