aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-01-24 23:10:59 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:38:23 +0200
commit2446736bc775b22cf5aaae88c2c69c9504e5eb17 (patch)
tree54f1b892dbd3c38b05dcd81c33ffe7ae9c84d6e7 /tests
parent8fb1062b263c90c22b2cd6a22e9031eef71240ef (diff)
Move message composer from dchat_gtk to dchat_core
Diffstat (limited to 'tests')
-rw-r--r--tests/main.cpp132
1 files changed, 130 insertions, 2 deletions
diff --git a/tests/main.cpp b/tests/main.cpp
index 9ad80a6..9f33a05 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -1,7 +1,135 @@
+#include <dchat/MessageComposer.hpp>
+#include <vector>
#include <stdio.h>
+#include <string.h>
+
+#define REQUIRE(expr) do { if(!(expr)) { fprintf(stderr, "%s:%d: Assert failed: %s\n", __FILE__, __LINE__, #expr); exit(EXIT_FAILURE); } } while(0)
+static void requireEqualValues(int a, int b, const char *file, int line)
+{
+ if(a != b)
+ {
+ fprintf(stderr, "%s:%d: Assert failed: %d == %d\n", file, line, a, b);
+ exit(EXIT_FAILURE);
+ }
+}
+#define REQUIRE_EQUAL(a, b) do { requireEqualValues((a), (b), __FILE__, __LINE__); } while(0)
int main(int argc, char **argv)
{
- printf("hello, world!\n");
+ std::vector<dchat::MessagePart> expect =
+ {
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } },
+ dchat::MessagePart { dchat::MessagePart::Type::EMOJI, dchat::Range{ 12, 16 } },
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 17, 21 } }
+ };
+
+ std::vector<const char*> expectText =
+ {
+ "abc ",
+ "cool",
+ " def"
+ };
+
+ int index = 0;
+ const char *str = "abc [emoji](cool) def";
+ dchat::usize strSize = strlen(str);
+
+ auto compareFunc = [&str, &index, &expect, &expectText](dchat::MessagePart messagePart)
+ {
+ REQUIRE(index < expect.size());
+ REQUIRE_EQUAL((int)messagePart.type, (int)expect[index].type);
+ REQUIRE_EQUAL(messagePart.textRange.start, expect[index].textRange.start);
+ REQUIRE_EQUAL(messagePart.textRange.end, expect[index].textRange.end);
+ int length = messagePart.textRange.end - messagePart.textRange.start;
+ if(strncmp(str + messagePart.textRange.start, expectText[index], length) != 0)
+ {
+ fprintf(stderr, "Assert failed: |%.*s| == |%.*s|\n", length, str + messagePart.textRange.start, length, expectText[index]);
+ exit(EXIT_FAILURE);
+ }
+ ++index;
+ };
+
+ dchat::compose(str, strSize, compareFunc);
+
+ {
+ expect =
+ {
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } },
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 4, 20 } }
+ };
+
+ expectText =
+ {
+ "abc ",
+ "[emoji](cool def"
+ };
+
+ index = 0;
+ str = "abc [emoji](cool def";
+ strSize = strlen(str);
+ dchat::compose(str, strSize, compareFunc);
+ }
+
+ {
+ expect =
+ {
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } },
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 4, 11 } },
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 11, 22 } }
+ };
+
+ expectText =
+ {
+ "abc ",
+ "[emoji]",
+ " (cool def)"
+ };
+
+ index = 0;
+ str = "abc [emoji] (cool def)";
+ strSize = strlen(str);
+ dchat::compose(str, strSize, compareFunc);
+ }
+
+ {
+ expect =
+ {
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } },
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 4, 20 } },
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 20, 24 } }
+ };
+
+ expectText =
+ {
+ "abc ",
+ "[notemoji](cool)",
+ " def"
+ };
+
+ index = 0;
+ str = "abc [notemoji](cool) def";
+ strSize = strlen(str);
+ dchat::compose(str, strSize, compareFunc);
+ }
+
+ {
+ expect =
+ {
+ dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 12 } },
+ dchat::MessagePart { dchat::MessagePart::Type::EMOJI, dchat::Range{ 20, 71 } }
+ };
+
+ expectText =
+ {
+ "Hello world ",
+ "https://discordemoji.com/assets/emoji/PeepoHide.png"
+ };
+
+ index = 0;
+ str = "Hello world [emoji](https://discordemoji.com/assets/emoji/PeepoHide.png)";
+ strSize = strlen(str);
+ dchat::compose(str, strSize, compareFunc);
+ }
+
return 0;
-}
+} \ No newline at end of file