summary refs log tree commit diff stats
path: root/src/data/effective/tests.rs
blob: dab503f2678be5dc99d7c098b59babb2902ed299 (plain) (blame)
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
101
102
103
104
105
106
107
108
// This file is part of GAnarchy - decentralized development hub
// Copyright (C) 2021  Soni L.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Unit tests for the effective module.

use std::collections::BTreeSet;
use std::error;
use std::time::Duration;

use impl_trait::impl_trait;
use url::Url;

use crate::data::{
    DataSource,
    DataSourceBase,
};
use crate::data::kinds::{
    InstanceBaseUrl,
    InstanceTitle,
    ProjectFork,
    RepoListUrl,
};
use super::{EffectiveDataSource, EffectiveKind};

/// A helper to test [`EffectiveDataSource`] and [`EffectiveKind`].
struct EffectiveTester;

impl_trait! {
    impl EffectiveTester {
        /// Always updates successfully, with an unknown refresh interval.
        impl trait DataSourceBase {
            fn update(&mut self) -> (
                Option<Duration>,
                Result<(), Box<dyn error::Error + Send + Sync + 'static>>,
            ) {
                (None, Ok(()))
            }

            fn exists(&self) -> bool {
                true
            }
        }

        /// [`InstanceTitle`] effectiveness tester.
        impl trait DataSource<InstanceTitle> {
            fn get_values(&self) -> Option<InstanceTitle> {
                Some(String::new().into())
            }
        }

        /// [`InstanceBaseUrl`] effectiveness tester.
        impl trait DataSource<InstanceBaseUrl> {
            fn get_values(&self) -> Option<InstanceBaseUrl> {
                None
            }
        }

        /// [`RepoListUrl`] effectiveness tester.
        impl trait DataSource<RepoListUrl> {
            fn get_values(&self) -> Vec<RepoListUrl> {
                // TWO for the same url
                vec![
                    RepoListUrl::new_for({
                        Url::parse("https://example.org").unwrap()
                    }),
                    RepoListUrl::new_for({
                        Url::parse("https://example.org").unwrap()
                    }),
                ]
            }
        }

        /// [`ProjectFork`] effectiveness tester.
        impl trait DataSource<EffectiveKind<ProjectFork>> {
            fn get_values(&self) -> BTreeSet<EffectiveKind<ProjectFork>> {
                vec![
                ].into_iter().collect()
            }
        }

        impl trait std::fmt::Display {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "tester")
            }
        }
    }
}

/// Tests that the `Option` data sources pass through.
#[test]
fn test_options() {
    let eds = EffectiveDataSource(EffectiveTester);
    assert_eq!(None, DataSource::<InstanceBaseUrl>::get_values(&eds));
    assert_eq!("", DataSource::<InstanceTitle>::get_values(&eds).unwrap().0);
}