summaryrefslogtreecommitdiff
path: root/tests/iid.cpp
blob: 74d010d497c826c5e4ac15b8b80d7a174e5534c4 (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
#include "catch.hpp"

#include <irc/iid.hpp>
#include <irc/irc_user.hpp>

#include <config/config.hpp>

TEST_CASE("Irc user parsing")
{
  const std::map<char, char> prefixes{{'!', 'a'}, {'@', 'o'}};
  IrcUser user1("!nick!~some@host.bla", prefixes);
  CHECK(user1.nick == "nick");
  CHECK(user1.host == "~some@host.bla");
  CHECK(user1.modes.size() == 1);
  CHECK(user1.modes.find('a') != user1.modes.end());

  IrcUser user2("coucou!~other@host.bla", prefixes);
  CHECK(user2.nick == "coucou");
  CHECK(user2.host == "~other@host.bla");
  CHECK(user2.modes.empty());
  CHECK(user2.modes.find('a') == user2.modes.end());
}

TEST_CASE("multi-prefix")
{
  const std::map<char, char> prefixes{{'!', 'a'}, {'@', 'o'}, {'~', 'f'}};
  IrcUser user("!@~nick", prefixes);
  CHECK(user.nick == "nick");
  CHECK(user.modes.size() == 3);
  CHECK(user.modes.find('f') != user.modes.end());
}

/**
 * Let Catch know how to display Iid objects
 */
namespace Catch
{
  template<>
  struct StringMaker<Iid>
  {
    static std::string convert(const Iid& value)
    {
      return std::to_string(value);
    }
  };
}

TEST_CASE("Iid creation")
{
    Iid iid1("foo!irc.example.org");
    CHECK(std::to_string(iid1) == "foo!irc.example.org");
    CHECK(iid1.get_local() == "foo");
    CHECK(iid1.get_server() == "irc.example.org");
    CHECK(!iid1.is_channel);
    CHECK(iid1.is_user);

    Iid iid2("#test%irc.example.org");
    CHECK(std::to_string(iid2) == "#test%irc.example.org");
    CHECK(iid2.get_local() == "#test");
    CHECK(iid2.get_server() == "irc.example.org");
    CHECK(iid2.is_channel);
    CHECK(!iid2.is_user);

    Iid iid3("%irc.example.org");
    CHECK(std::to_string(iid3) == "%irc.example.org");
    CHECK(iid3.get_local() == "");
    CHECK(iid3.get_server() == "irc.example.org");
    CHECK(iid3.is_channel);
    CHECK(!iid3.is_user);

    Iid iid4("irc.example.org");
    CHECK(std::to_string(iid4) == "irc.example.org");
    CHECK(iid4.get_local() == "");
    CHECK(iid4.get_server() == "irc.example.org");
    CHECK(!iid4.is_channel);
    CHECK(!iid4.is_user);

    Iid iid5("nick!");
    CHECK(std::to_string(iid5) == "nick!");
    CHECK(iid5.get_local() == "nick");
    CHECK(iid5.get_server() == "");
    CHECK(!iid5.is_channel);
    CHECK(iid5.is_user);

    Iid iid6("##channel%");
    CHECK(std::to_string(iid6) == "##channel%");
    CHECK(iid6.get_local() == "##channel");
    CHECK(iid6.get_server() == "");
    CHECK(iid6.is_channel);
    CHECK(!iid6.is_user);
}

TEST_CASE("Iid creation in fixed_server mode")
{
    Config::set("fixed_irc_server", "fixed.example.com", false);

    Iid iid1("foo!irc.example.org");
    CHECK(std::to_string(iid1) == "foo!");
    CHECK(iid1.get_local() == "foo");
    CHECK(iid1.get_server() == "fixed.example.com");
    CHECK(!iid1.is_channel);
    CHECK(iid1.is_user);

    Iid iid2("#test%irc.example.org");
    CHECK(std::to_string(iid2) == "#test%irc.example.org");
    CHECK(iid2.get_local() == "#test%irc.example.org");
    CHECK(iid2.get_server() == "fixed.example.com");
    CHECK(iid2.is_channel);
    CHECK(!iid2.is_user);

    // Note that it is impossible to adress the IRC server directly, or to
    // use the virtual channel, in that mode

    // Iid iid3("%irc.example.org");
    // Iid iid4("irc.example.org");

    Iid iid5("nick!");
    CHECK(std::to_string(iid5) == "nick!");
    CHECK(iid5.get_local() == "nick");
    CHECK(iid5.get_server() == "fixed.example.com");
    CHECK(!iid5.is_channel);
    CHECK(iid5.is_user);

    Iid iid6("##channel%");
    CHECK(std::to_string(iid6) == "##channel%");
    CHECK(iid6.get_local() == "##channel%");
    CHECK(iid6.get_server() == "fixed.example.com");
    CHECK(iid6.is_channel);
    CHECK(!iid6.is_user);
}