1
/*
2
Copyright (C) 2023 Kunal Mehta <legoktm@debian.org>
3

            
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8

            
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13

            
14
You should have received a copy of the GNU General Public License
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
use crate::image::HorizontalAlignment;
18
use crate::prelude::*;
19
use crate::tests::test_client;
20
use anyhow::Result;
21

            
22
#[tokio::test]
23
3
async fn test_image() -> Result<()> {
24
3
    let client = test_client::testwp_client();
25
3
    let wikitext = "[[File:Example.jpg]]";
26
3
    let code = client.transform_to_html(wikitext).await?.into_mutable();
27
3
    let images = code.filter_images();
28
3
    assert_eq!(images.len(), 1);
29
3
    assert_eq!(images[0].title(), "File:Example.jpg");
30
3
    assert_eq!(
31
3
        images[0].horizontal_alignment(),
32
3
        HorizontalAlignment::Unspecified
33
3
    );
34
3
    images[0].set_horizontal_alignment(HorizontalAlignment::Center);
35
3
    let centered = client.transform_to_wikitext(&code).await?;
36
3
    assert_eq!(&centered, "[[File:Example.jpg|center]]");
37
3
    images[0].set_horizontal_alignment(HorizontalAlignment::Left);
38
3
    let left = client.transform_to_wikitext(&code).await?;
39
3
    assert_eq!(&left, "[[File:Example.jpg|left]]");
40
3
    images[0].set_horizontal_alignment(HorizontalAlignment::Unspecified);
41
3
    let unspecified = client.transform_to_wikitext(&code).await?;
42
3
    assert_eq!(&unspecified, wikitext);
43
3
    Ok(())
44
2
}
45

            
46
#[tokio::test]
47
3
async fn test_missing_image() -> Result<()> {
48
3
    let client = test_client::testwp_client();
49
3
    let code = client.get("Mwbot-rs/Gallery3").await?.into_mutable();
50
3
    let galleries: Vec<_> = code
51
3
        .inclusive_descendants()
52
67
        .filter_map(|node| node.as_gallery())
53
3
        .collect();
54
3
    let images = galleries[0].images();
55
3
    assert_eq!(images[0].title(), "File:ThisFileDoesNotExist.jpg");
56
3
    let errors = images[0].error().unwrap();
57
3
    assert_eq!(errors[0].key, "apierror-filedoesnotexist");
58
3
    Ok(())
59
2
}