aboutsummaryrefslogtreecommitdiff
path: root/tests/main.cpp
blob: 5063c2d612927854061631d0c65fb8ca1e85e5a0 (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
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
#include <stdio.h>
#include <string.h>
#include "../include/NetUtils.hpp"
#include "../plugins/utils/EpisodeNameParser.hpp"
#include "../plugins/Matrix.hpp"
#include "../generated/Emoji.hpp"

#define assert_fail(str) do { fprintf(stderr, "Assert failed on line %d, reason: %s\n", __LINE__, (str)); exit(1); } while(0)
#define assert_equals(a, b) do { if((a) != (b)) { fprintf(stderr, "Assert failed on line %d, %s == %s\n", __LINE__, #a, #b); exit(1); } } while(0)

using namespace QuickMedia;

int main() {
    std::vector<std::string> urls;
    const char *str;

    str = "example.com";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "example.com");

    str = "example.com, is where I like to go";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "example.com");

    str = "The website I like to go to is example.com";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "example.com");

    str = "example.com. Is also a website";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "example.com");

    str = "example.com: the best test website";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "example.com");

    str = "is it example.com? or not?";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "example.com");

    str = "these. are. not. websites.";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 0);

    str = "This is not an url: example.";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 0);

    str = "the.se/~#423-_/2f.no/3df a.re considered sub.websit.es, this.is.not";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 3);
    assert_equals(urls[0], "the.se/~#423-_/2f.no/3df");
    assert_equals(urls[1], "a.re");
    assert_equals(urls[2], "sub.websit.es");

    str = "(see https://emojipedia.org/emoji/%23%EF%B8%8F%E2%83%A3/)";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "https://emojipedia.org/emoji/%23%EF%B8%8F%E2%83%A3/");

    str = "[sneed](https://sneedville.com)";
    urls = ranges_get_strings(str, extract_urls(str));
    assert_equals(urls.size(), 1);
    assert_equals(urls[0], "https://sneedville.com");

    std::string html_unescaped_str = "hello &#039; world";
    html_unescape_sequences(html_unescaped_str);
    assert_equals(html_unescaped_str, "hello ' world");

    html_unescaped_str = "hello &#x27; world";
    html_unescape_sequences(html_unescaped_str);
    assert_equals(html_unescaped_str, "hello ' world");

    std::optional<EpisodeNameParts> name_parts1 = episode_name_extract_parts("[SubsPlease] Shikkakumon no Saikyou Kenja - 07 (1080p) [83EFD76A].mkv");
    assert_equals(name_parts1.has_value(), true);
    assert_equals(name_parts1->group, "SubsPlease");
    assert_equals(name_parts1->anime, "Shikkakumon no Saikyou Kenja");
    assert_equals(name_parts1->season.size(), 0);
    assert_equals(name_parts1->episode, "07");

    std::optional<EpisodeNameParts> name_parts2 = episode_name_extract_parts("[SubsPlease] Shingeki no Kyojin (The Final Season) - 81 (1080p) [601A33BD].mkv");
    assert_equals(name_parts2.has_value(), true);
    assert_equals(name_parts2->group, "SubsPlease");
    assert_equals(name_parts2->anime, "Shingeki no Kyojin (The Final Season)");
    assert_equals(name_parts2->season.size(), 0);
    assert_equals(name_parts2->episode, "81");

    std::optional<EpisodeNameParts> name_parts3 = episode_name_extract_parts("[SubsPlease] Lupin III - Part 6 - 18 (1080p) [98204042].mkv");
    assert_equals(name_parts3.has_value(), true);
    assert_equals(name_parts3->group, "SubsPlease");
    assert_equals(name_parts3->anime, "Lupin III");
    assert_equals(name_parts3->season, "Part 6");
    assert_equals(name_parts3->episode, "18");

    std::optional<EpisodeNameParts> name_parts4 = episode_name_extract_parts("[SubsPlease] Kimetsu no Yaiba - Yuukaku-hen - 11 (1080p) [BE15F231].mkv");
    assert_equals(name_parts4.has_value(), true);
    assert_equals(name_parts4->group, "SubsPlease");
    assert_equals(name_parts4->anime, "Kimetsu no Yaiba");
    assert_equals(name_parts4->season, "Yuukaku-hen");
    assert_equals(name_parts4->episode, "11");

    uint32_t emoji_sequence[32];
    size_t emoji_sequence_length = 0;
    size_t emoji_sequence_byte_length = 0;
    const char *emoji = "\xF0\x9F\x8F\xB4\xE2\x80\x8D\xE2\x98\xA0\xEF\xB8\x8F";
    assert_equals(match_emoji_sequence((const unsigned char*)emoji, strlen(emoji), emoji_sequence, emoji_sequence_length, emoji_sequence_byte_length), true);
    assert_equals(emoji_sequence_length, 4);
    assert_equals(emoji_sequence_byte_length, 13);

    assert_equals(extract_user_name_from_user_id("@dec05eba:domail.com"), "dec05eba");
    assert_equals(extract_user_name_from_user_id("dec05eba@domain.com"), "");
    assert_equals(extract_user_name_from_user_id("dec05eba"), "");

    assert_equals(extract_user_name_from_email("dec05eba@domain.com"), "dec05eba");
    assert_equals(extract_user_name_from_email("@dec05eba:domain.com"), "");
    assert_equals(extract_user_name_from_email("dec05eba"), "");

    auto room_ids = matrix_extract_room_ids("hello #sneed:matrix.org world #feed:midov.pl");
    assert_equals(room_ids.size(), 2);
    assert_equals(room_ids[0], "#sneed:matrix.org");
    assert_equals(room_ids[1], "#feed:midov.pl");

    room_ids = matrix_extract_room_ids("#sneed:matrix.org");
    assert_equals(room_ids.size(), 1);
    assert_equals(room_ids[0], "#sneed:matrix.org");

    room_ids = matrix_extract_room_ids("#sneedmatrix.org");
    assert_equals(room_ids.size(), 0);

    room_ids = matrix_extract_room_ids("!sneed:matrix.org");
    assert_equals(room_ids.size(), 0);
    //assert_equals(room_ids.size(), 1);
    //assert_equals(room_ids[0], "!sneed:matrix.org");

    room_ids = matrix_extract_room_ids("@sneed:matrix.org");
    assert_equals(room_ids.size(), 0);

    Matrix matrix(false);
    std::string formatted_body = matrix.body_to_formatted_body(nullptr, "hello world");
    assert_equals(formatted_body, "hello world");

    std::string qm_text = formatted_text_to_qm_text(&matrix, formatted_body.c_str(), formatted_body.size(), true);
    assert_equals(qm_text, "hello world");

    return 0;
}