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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * includes/includer/debug.rs
 *
 * ftml - Library to parse Wikidot text
 * Copyright (C) 2019-2021 Wikijump Team
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

use super::prelude::*;
use crate::includes::IncludeVariables;
use std::fmt::{self, Display};
use void::Void;

#[derive(Debug)]
pub struct DebugIncluder;

impl<'t> Includer<'t> for DebugIncluder {
    type Error = Void;

    #[inline]
    fn include_pages(
        &mut self,
        includes: &[IncludeRef<'t>],
    ) -> Result<Vec<FetchedPage<'t>>, Void> {
        let mut first = true;
        let mut pages = Vec::new();

        for include in includes {
            let content = if first && includes.len() > 1 {
                // If the requested inclusions are greater than one,
                // then have the list be a missing page.
                //
                // This lets us test the no_such_include() method,
                // without it affecting typical single-include test cases.

                first = false;
                None
            } else {
                let content = format!(
                    "<INCLUDED-PAGE {} {}>",
                    include.page_ref(),
                    MapWrap(include.variables()),
                );

                Some(Cow::Owned(content))
            };

            let page_ref = include.page_ref().clone();

            pages.push(FetchedPage { page_ref, content });
        }

        Ok(pages)
    }

    #[inline]
    fn no_such_include(&mut self, page_ref: &PageRef<'t>) -> Result<Cow<'t, str>, Void> {
        Ok(Cow::Owned(format!("<MISSING-PAGE {}>", page_ref)))
    }
}

/// Rendering a `HashMap` as a string, sorted alphabetically.
///
/// Avoids the uncertain key-value pair ordering inherent in the `Debug`
/// implementation, which could cause tests to be flakey or system-dependent.
#[derive(Debug)]
struct MapWrap<'m, 't>(&'m IncludeVariables<'t>);

impl<'t> Display for MapWrap<'_, 't> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Get all entries and sort by key
        let mut entries: Vec<(&Cow<'t, str>, &Cow<'t, str>)> = self.0.iter().collect();
        entries.sort_by(|(key1, _), (key2, _)| key1.cmp(key2));

        // Write all entries
        write!(f, "{{")?;

        for (i, (key, value)) in entries.iter().enumerate() {
            write!(f, "{:?} => {:?}", key, value)?;

            if i < entries.len() - 1 {
                write!(f, ", ")?;
            }
        }

        write!(f, "}}")?;

        // Return
        Ok(())
    }
}

#[test]
fn map_wrap() {
    macro_rules! test {
        ($input:expr, $expected:expr,) => {
            test!($input, $expected)
        };

        ($input:expr, $expected:expr) => {{
            // Get what was actually specified as the input,
            // stripping out the "hashmap!".
            let raw_input = &stringify!($input)[9..];

            // Convert string literals into Cows
            let input = {
                let original = $input;
                let mut map = HashMap::new();

                for (key, value) in original {
                    let key = Cow::Borrowed(key);
                    let value = Cow::Borrowed(value);

                    map.insert(key, value);
                }

                map
            };

            let actual = MapWrap(&input).to_string();

            println!("Input:    {}", raw_input);
            println!("Actual:   {}", actual);
            println!("Expected: {}", $expected);
            println!();

            assert_eq!(
                &actual, $expected,
                "Actual format string didn't match expected"
            );
        }};
    }

    test!(hashmap! {}, "{}");
    test!(hashmap! { "apple" => "1" }, r#"{"apple" => "1"}"#);
    test!(
        hashmap! { "apple" => "1", "banana" => "2" },
        r#"{"apple" => "1", "banana" => "2"}"#,
    );
    test!(
        hashmap! { "banana" => "2", "apple" => "1" },
        r#"{"apple" => "1", "banana" => "2"}"#,
    );
    test!(
        hashmap! { "apple" => "1", "banana" => "2", "cherry" => "3" },
        r#"{"apple" => "1", "banana" => "2", "cherry" => "3"}"#,
    );
    test!(
        hashmap! { "banana" => "2", "apple" => "1", "cherry" => "3" },
        r#"{"apple" => "1", "banana" => "2", "cherry" => "3"}"#,
    );
    test!(
        hashmap! { "cherry" => "3", "banana" => "2", "apple" => "1" },
        r#"{"apple" => "1", "banana" => "2", "cherry" => "3"}"#,
    );
    test!(
        hashmap! { "apple" => "1", "cherry" => "3", "banana" => "2" },
        r#"{"apple" => "1", "banana" => "2", "cherry" => "3"}"#,
    );
}