Page MenuHomePhorge

No OneTemporary

Size
10 KB
Referenced Files
None
Subscribers
None
diff --git a/src/tests/client/matrix-uri-test.cpp b/src/tests/client/matrix-uri-test.cpp
index 5bf7070..a166e85 100644
--- a/src/tests/client/matrix-uri-test.cpp
+++ b/src/tests/client/matrix-uri-test.cpp
@@ -1,337 +1,365 @@
/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2022 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <catch2/catch.hpp>
#include <matrix-uri.hpp>
using namespace Kazv;
TEST_CASE("MatrixUri::parse()", "[client][matrix-uri]")
{
SECTION("room by alias")
{
auto uri = MatrixUri::parse("matrix:r/somewhere:example.org");
REQUIRE(uri.isValid());
REQUIRE(uri.isRoom());
REQUIRE(uri.id() == "#somewhere:example.org");
}
SECTION("room by id")
{
auto uri = MatrixUri::parse("matrix:roomid/somewhere:example.org?via=elsewhere.ca");
REQUIRE(uri.isValid());
REQUIRE(uri.isRoom());
REQUIRE(uri.id() == "!somewhere:example.org");
REQUIRE(uri.routing() == immer::flex_vector<std::string>{"elsewhere.ca"});
}
SECTION("room by alias with event")
{
auto uri = MatrixUri::parse("matrix:r/somewhere:example.org/e/event");
REQUIRE(uri.isValid());
REQUIRE(uri.isEvent());
REQUIRE(uri.userOrRoom() == "#somewhere:example.org");
REQUIRE(uri.id() == "$event");
}
SECTION("room by id with event")
{
auto uri = MatrixUri::parse("matrix:roomid/somewhere:example.org/e/event?via=elsewhere.ca");
REQUIRE(uri.isValid());
REQUIRE(uri.isEvent());
REQUIRE(uri.userOrRoom() == "!somewhere:example.org");
REQUIRE(uri.id() == "$event");
REQUIRE(uri.routing() == immer::flex_vector<std::string>{"elsewhere.ca"});
}
SECTION("multiple via")
{
auto uri = MatrixUri::parse("matrix:roomid/somewhere:example.org/e/event?via=a&via=b&via=c");
REQUIRE(uri.isValid());
REQUIRE(uri.isEvent());
REQUIRE(uri.routing() == immer::flex_vector<std::string>{"a", "b", "c"});
}
SECTION("user")
{
auto uri = MatrixUri::parse("matrix:u/alice:example.org?action=chat");
REQUIRE(uri.isValid());
REQUIRE(uri.isUser());
REQUIRE(uri.userOrRoom() == "@alice:example.org");
REQUIRE(uri.id() == "@alice:example.org");
REQUIRE(uri.action() == "chat");
}
SECTION("custom query")
{
auto uri = MatrixUri::parse("matrix:u/alice:example.org?lol=mew&lol=wow&foo=bar");
REQUIRE(uri.isValid());
REQUIRE(uri.isUser());
REQUIRE(uri.query("lol") == "wow");
REQUIRE(uri.query("foo") == "bar");
}
SECTION("fragment")
{
auto uri = MatrixUri::parse("matrix:u/alice:example.org#aaabbb");
REQUIRE(uri.isValid());
REQUIRE(uri.isUser());
REQUIRE(uri.fragment() == "aaabbb");
}
SECTION("legacy entity types")
{
REQUIRE(MatrixUri::parse("matrix:user/alice:example.org").isUser());
REQUIRE(MatrixUri::parse("matrix:room/alice:example.org").isRoom());
REQUIRE(MatrixUri::parse("matrix:roomid/alice:example.org/event/aaa").isEvent());
REQUIRE(MatrixUri::parse("matrix:room/alice:example.org/event/aaa").isEvent());
}
SECTION("invalid")
{
SECTION("proto")
{
REQUIRE(!MatrixUri::parse("mx:u/alice:example.org").isValid());
}
SECTION("path is not 2 or 4 parts")
{
REQUIRE(!MatrixUri::parse("matrix:u/alice:example.org/aaa").isValid());
REQUIRE(!MatrixUri::parse("matrix:u").isValid());
REQUIRE(!MatrixUri::parse("matrix:u/alice:example.org/e/aaa/f/xxx").isValid());
}
SECTION("entity type")
{
REQUIRE(!MatrixUri::parse("matrix:foo/alice:example.org").isValid());
REQUIRE(!MatrixUri::parse("matrix:r/alice:example.org/foo/xxx").isValid());
}
SECTION("user with event")
{
REQUIRE(!MatrixUri::parse("matrix:u/alice:example.org/e/xxx").isValid());
}
SECTION("query without =")
{
REQUIRE(!MatrixUri::parse("matrix:u/alice:example.org?a").isValid());
}
}
}
TEST_CASE("desigilify()", "[client][matrix-uri]")
{
SECTION("It removes #, !, @ and $")
{
REQUIRE(desigilify("#a") == "a");
REQUIRE(desigilify("!a") == "a");
REQUIRE(desigilify("@a") == "a");
REQUIRE(desigilify("$a") == "a");
}
SECTION("It does not remove other symbols")
{
REQUIRE(desigilify("*a") == "*a");
REQUIRE(desigilify("ua") == "ua");
REQUIRE(desigilify("喵喵") == "喵喵");
}
SECTION("It does not crash with empty identifiers")
{
REQUIRE(desigilify("@") == "");
REQUIRE(desigilify("") == "");
}
}
TEST_CASE("MatrixUri is copyable and movable", "[client][matrix-uri]")
{
auto a = MatrixUri::withRoomById("!aaa:example.org");
auto b = a;
REQUIRE(b.id() == "!aaa:example.org");
REQUIRE(a.id() == "!aaa:example.org");
a = b;
REQUIRE(b.id() == "!aaa:example.org");
REQUIRE(a.id() == "!aaa:example.org");
auto c = std::move(a);
REQUIRE(!a.isValid());
REQUIRE(c.id() == "!aaa:example.org");
a = std::move(c);
REQUIRE(!c.isValid());
REQUIRE(a.id() == "!aaa:example.org");
}
TEST_CASE("MatrixUri::withRoomByAlias()", "[client][matrix-uri]")
{
auto uri = MatrixUri::withRoomByAlias("#aaa:example.org");
REQUIRE(uri.isValid());
REQUIRE(uri.isRoom());
REQUIRE(uri.userOrRoom() == "#aaa:example.org");
REQUIRE(uri.id() == "#aaa:example.org");
}
TEST_CASE("MatrixUri::withRoomById()", "[client][matrix-uri]")
{
auto uri = MatrixUri::withRoomById("!aaa:example.org");
REQUIRE(uri.isValid());
REQUIRE(uri.isRoom());
REQUIRE(uri.userOrRoom() == "!aaa:example.org");
REQUIRE(uri.id() == "!aaa:example.org");
}
TEST_CASE("MatrixUri::withUser()", "[client][matrix-uri]")
{
auto uri = MatrixUri::withUser("@aaa:example.org");
REQUIRE(uri.isValid());
REQUIRE(uri.isUser());
REQUIRE(uri.userOrRoom() == "@aaa:example.org");
REQUIRE(uri.id() == "@aaa:example.org");
}
TEST_CASE("MatrixUri::withEvent()", "[client][matrix-uri]")
{
SECTION("in room by id")
{
auto uri = MatrixUri::withRoomById("!aaa:example.org").withEvent("$bbb");
REQUIRE(uri.isValid());
REQUIRE(!uri.isRoom());
REQUIRE(uri.isEvent());
REQUIRE(uri.userOrRoom() == "!aaa:example.org");
REQUIRE(uri.id() == "$bbb");
}
SECTION("in room by alias")
{
auto uri = MatrixUri::withRoomByAlias("#aaa:example.org").withEvent("$bbb");
REQUIRE(uri.isValid());
REQUIRE(!uri.isRoom());
REQUIRE(uri.isEvent());
REQUIRE(uri.userOrRoom() == "#aaa:example.org");
REQUIRE(uri.id() == "$bbb");
}
SECTION("in user")
{
auto uri = MatrixUri::withUser("@aaa:example.org").withEvent("$bbb");
REQUIRE(!uri.isValid());
}
}
TEST_CASE("MatrixUri::toString()", "[client][matrix-uri]")
{
SECTION("room by id")
{
auto uri = MatrixUri::withRoomById("!aaa:example.org");
REQUIRE(uri.toString() == "matrix:roomid/aaa:example.org");
SECTION("event")
{
REQUIRE(uri.withEvent("$bbb").toString() == "matrix:roomid/aaa:example.org/e/bbb");
}
}
SECTION("room by alias")
{
auto uri = MatrixUri::withRoomByAlias("#aaa:example.org");
REQUIRE(uri.toString() == "matrix:r/aaa:example.org");
SECTION("event")
{
REQUIRE(uri.withEvent("$bbb").toString() == "matrix:r/aaa:example.org/e/bbb");
}
}
SECTION("user")
{
auto uri = MatrixUri::withUser("@aaa:example.org");
REQUIRE(uri.toString() == "matrix:u/aaa:example.org");
}
SECTION("query params")
{
auto uri =
MatrixUri::withUser("@aaa:example.org")
.withQuery("via", "a.org")
.withQuery("via", "b.org")
.withAction("chat")
.withQuery("foo", "1")
.withQuery("foo", "2");
REQUIRE(uri.toString() == "matrix:u/aaa:example.org?action=chat&via=a.org&via=b.org&foo=2");
}
SECTION("fragments")
{
auto uri =
MatrixUri::withUser("@aaa:example.org")
.withFragment("ccc");
REQUIRE(uri.toString() == "matrix:u/aaa:example.org#ccc");
REQUIRE(uri.withFragment("").toString() == "matrix:u/aaa:example.org");
}
SECTION("all combined")
{
auto uri =
MatrixUri::withRoomByAlias("#aaa:example.org")
.withFragment("ccc")
.withQuery("via", "a.org")
.withEvent("bbb")
.withQuery("via", "b.org")
.withAction("chat")
.withQuery("foo", "1")
.withQuery("foo", "2");
REQUIRE(uri.toString() == "matrix:r/aaa:example.org/e/bbb?action=chat&via=a.org&via=b.org&foo=2#ccc");
}
}
TEST_CASE("MatrixUri::query()", "[client][matrix-uri]")
{
SECTION("via")
{
auto uri =
MatrixUri::withUser("@a:example.org")
.withVia("foo.org")
.withVia("bar.org");
REQUIRE(uri.query("via") == "bar.org");
uri =
MatrixUri::withUser("@a:example.org")
.withQuery("via", "foo.org")
.withQuery("via", "bar.org");
REQUIRE(uri.query("via") == "bar.org");
}
SECTION("action")
{
auto uri =
MatrixUri::withUser("@a:example.org")
.withAction("chat");
REQUIRE(uri.query("action") == "chat");
uri =
MatrixUri::withUser("@a:example.org")
.withQuery("action", "chat");
REQUIRE(uri.query("action") == "chat");
}
SECTION("custom param")
{
auto uri =
MatrixUri::withUser("@a:example.org")
.withQuery("foo", "bar")
.withQuery("foo", "bar2");
REQUIRE(uri.query("foo") == "bar2");
}
}
+
+TEST_CASE("MatrixUri::action()", "[client][matrix-uri]")
+{
+ auto uri =
+ MatrixUri::withUser("@alice:example.org")
+ .withAction("foo");
+
+ REQUIRE(uri.action() == "foo");
+}
+
+TEST_CASE("MatrixUri::routing()", "[client][matrix-uri]")
+{
+ auto uri =
+ MatrixUri::withUser("@alice:example.org")
+ .withVia("foo.org")
+ .withVia("bar.org");
+
+ REQUIRE(uri.routing() == immer::flex_vector<std::string>{"foo.org", "bar.org"});
+}
+
+TEST_CASE("MatrixUri::withVia(RangeT)", "[client][matrix-uri]")
+{
+ auto uri =
+ MatrixUri::withUser("@alice:example.org")
+ .withVia(std::vector<std::string>{"foo.org", "bar.org"});
+
+ REQUIRE(uri.routing() == immer::flex_vector<std::string>{"foo.org", "bar.org"});
+}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 11:43 PM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55518
Default Alt Text
(10 KB)

Event Timeline