File: select_path.rs

package info (click to toggle)
rust-ruma-common 0.10.5-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,208 kB
  • sloc: makefile: 2
file content (100 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download | duplicates (2)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use assert_matches::assert_matches;
use http::Method;
use ruma_common::api::{
    error::IntoHttpError,
    select_path,
    MatrixVersion::{V1_0, V1_1, V1_2},
    Metadata,
};

const BASE: Metadata = Metadata {
    description: "",
    method: Method::GET,
    name: "test_endpoint",
    unstable_path: Some("/unstable/path"),
    r0_path: Some("/r0/path"),
    stable_path: Some("/stable/path"),
    rate_limited: false,
    authentication: ruma_common::api::AuthScheme::None,
    added: None,
    deprecated: None,
    removed: None,
};

const U: &str = "u";
const S: &str = "s";
const R: &str = "r";

// TODO add test that can hook into tracing and verify the deprecation warning is emitted

#[test]
fn select_stable() {
    let meta = Metadata { added: Some(V1_1), ..BASE };

    let res = select_path(&[V1_0, V1_1], &meta, None, None, Some(format_args!("{S}")))
        .unwrap()
        .to_string();

    assert_eq!(res, S);
}

#[test]
fn select_unstable() {
    let meta = BASE;

    let res =
        select_path(&[V1_0], &meta, Some(format_args!("{U}")), None, None).unwrap().to_string();

    assert_eq!(res, U);
}

#[test]
fn select_r0() {
    let meta = Metadata { added: Some(V1_0), ..BASE };

    let res =
        select_path(&[V1_0], &meta, None, Some(format_args!("{R}")), Some(format_args!("{S}")))
            .unwrap()
            .to_string();

    assert_eq!(res, R);
}

#[test]
fn select_removed_err() {
    let meta = Metadata { added: Some(V1_0), deprecated: Some(V1_1), removed: Some(V1_2), ..BASE };

    let res = select_path(
        &[V1_2],
        &meta,
        Some(format_args!("{U}")),
        Some(format_args!("{R}")),
        Some(format_args!("{S}")),
    )
    .unwrap_err();

    assert_matches!(res, IntoHttpError::EndpointRemoved(V1_2));
}

#[test]
fn partially_removed_but_stable() {
    let meta = Metadata { added: Some(V1_0), deprecated: Some(V1_1), removed: Some(V1_2), ..BASE };

    let res =
        select_path(&[V1_1], &meta, None, Some(format_args!("{R}")), Some(format_args!("{S}")))
            .unwrap()
            .to_string();

    assert_eq!(res, S);
}

#[test]
fn no_unstable() {
    let meta = Metadata { added: Some(V1_1), ..BASE };

    let res =
        select_path(&[V1_0], &meta, None, Some(format_args!("{R}")), Some(format_args!("{S}")))
            .unwrap_err();

    assert_matches!(res, IntoHttpError::NoUnstablePath);
}