diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-01-27 02:09:50 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:38:23 +0200 |
commit | 6778e3b87cc9a6f5d195a2c80e5b499e3d94558b (patch) | |
tree | d37e25d48168465bdf0fb29559fe6e186a15e1d7 /tests | |
parent | 2446736bc775b22cf5aaae88c2c69c9504e5eb17 (diff) |
Add binds to emoji parsing, refactor
Diffstat (limited to 'tests')
-rw-r--r-- | tests/main.cpp | 206 |
1 files changed, 176 insertions, 30 deletions
diff --git a/tests/main.cpp b/tests/main.cpp index 9f33a05..64a0523 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,4 +1,5 @@ -#include <dchat/MessageComposer.hpp> +#include <dchat/IncomingMessage.hpp> +#include <dchat/OutgoingMessage.hpp> #include <vector> #include <stdio.h> #include <string.h> @@ -14,48 +15,48 @@ static void requireEqualValues(int a, int b, const char *file, int line) } #define REQUIRE_EQUAL(a, b) do { requireEqualValues((a), (b), __FILE__, __LINE__); } while(0) -int main(int argc, char **argv) +static void testIncomingMessage() { - std::vector<dchat::MessagePart> expect = + std::vector<dchat::IncomingMessagePart> 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 } } + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::EMOJI, dchat::Range{ 12, 16 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 17, 21 } } }; - std::vector<const char*> expectText = + std::vector<std::string> expectText = { "abc ", "cool", " def" }; - int index = 0; + dchat::usize index = 0; const char *str = "abc [emoji](cool) def"; dchat::usize strSize = strlen(str); - auto compareFunc = [&str, &index, &expect, &expectText](dchat::MessagePart messagePart) + auto compareFunc = [&str, &index, &expect, &expectText](dchat::IncomingMessagePart incomingMessagePart) { 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) + REQUIRE_EQUAL((int)incomingMessagePart.type, (int)expect[index].type); + REQUIRE_EQUAL(incomingMessagePart.textRange.start, expect[index].textRange.start); + REQUIRE_EQUAL(incomingMessagePart.textRange.end, expect[index].textRange.end); + int length = incomingMessagePart.textRange.end - incomingMessagePart.textRange.start; + if(length != expectText[index].size() || strncmp(str + incomingMessagePart.textRange.start, expectText[index].c_str(), length) != 0) { - fprintf(stderr, "Assert failed: |%.*s| == |%.*s|\n", length, str + messagePart.textRange.start, length, expectText[index]); + fprintf(stderr, "Assert failed: |%.*s| == |%.*s|\n", length, str + incomingMessagePart.textRange.start, expectText[index].size(), expectText[index].c_str()); exit(EXIT_FAILURE); } ++index; }; - dchat::compose(str, strSize, compareFunc); + dchat::parseIncomingMessage(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::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 4, 20 } } }; expectText = @@ -67,15 +68,15 @@ int main(int argc, char **argv) index = 0; str = "abc [emoji](cool def"; strSize = strlen(str); - dchat::compose(str, strSize, compareFunc); + dchat::parseIncomingMessage(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 } } + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 4, 11 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 11, 22 } } }; expectText = @@ -88,15 +89,15 @@ int main(int argc, char **argv) index = 0; str = "abc [emoji] (cool def)"; strSize = strlen(str); - dchat::compose(str, strSize, compareFunc); + dchat::parseIncomingMessage(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 } } + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 4, 20 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 20, 24 } } }; expectText = @@ -109,14 +110,14 @@ int main(int argc, char **argv) index = 0; str = "abc [notemoji](cool) def"; strSize = strlen(str); - dchat::compose(str, strSize, compareFunc); + dchat::parseIncomingMessage(str, strSize, compareFunc); } { expect = { - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 12 } }, - dchat::MessagePart { dchat::MessagePart::Type::EMOJI, dchat::Range{ 20, 71 } } + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::TEXT, dchat::Range{ 0, 12 } }, + dchat::IncomingMessagePart { dchat::IncomingMessagePart::Type::EMOJI, dchat::Range{ 20, 71 } } }; expectText = @@ -128,8 +129,153 @@ int main(int argc, char **argv) index = 0; str = "Hello world [emoji](https://discordemoji.com/assets/emoji/PeepoHide.png)"; strSize = strlen(str); - dchat::compose(str, strSize, compareFunc); + dchat::parseIncomingMessage(str, strSize, compareFunc); + } +} + +static void testOutgoingMessage() +{ + const dchat::EmojiBindMap binds = { + { "thinking", "https://thinking" }, + { "sad", "https://sad" } + }; + + std::vector<dchat::OutgoingMessagePart> expect; + std::vector<std::string> expectText; + + dchat::usize index = 0; + const char *str = nullptr; + dchat::usize size = 0; + auto callbackFunc = [&expect, &expectText, &index, &str](dchat::OutgoingMessagePart outgoingMessagePart) + { + REQUIRE(index < expect.size()); + REQUIRE_EQUAL((int)outgoingMessagePart.type, (int)expect[index].type); + switch(outgoingMessagePart.type) + { + case dchat::OutgoingMessagePart::Type::TEXT: + { + REQUIRE_EQUAL(outgoingMessagePart.textRange.start, expect[index].textRange.start); + REQUIRE_EQUAL(outgoingMessagePart.textRange.end, expect[index].textRange.end); + int length = outgoingMessagePart.textRange.end - outgoingMessagePart.textRange.start; + if(length != expectText[index].size() || strncmp(str + outgoingMessagePart.textRange.start, expectText[index].c_str(), length) != 0) + { + fprintf(stderr, "Assert failed: |%.*s| == |%.*s|\n", length, str + outgoingMessagePart.textRange.start, expectText[index].size(), expectText[index].c_str()); + exit(EXIT_FAILURE); + } + break; + } + case dchat::OutgoingMessagePart::Type::EMOJI: + { + if(*outgoingMessagePart.emoji != expectText[index]) + { + fprintf(stderr, "Assert failed: |%s| == |%s|\n", outgoingMessagePart.emoji->c_str(), expectText[index].c_str()); + exit(EXIT_FAILURE); + } + break; + } + } + ++index; + }; + + { + expect = + { + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::EMOJI, nullptr }, + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 14, 18 } } + }; + + expectText = + { + "aaa ", + "https://thinking", + " bbb" + }; + + index = 0; + str = "aaa :thinking: bbb"; + size = strlen(str); + dchat::parseOutgoingMessage(str, size, binds, callbackFunc); + } + + { + expect = + { + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::EMOJI, nullptr }, + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 5, 9 } } + }; + + expectText = + { + "https://sad", + " bbb" + }; + + index = 0; + str = ":sad: bbb"; + size = strlen(str); + dchat::parseOutgoingMessage(str, size, binds, callbackFunc); + } + + { + expect = + { + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::EMOJI, nullptr }, + }; + + expectText = + { + "https://sad", + }; + + index = 0; + str = ":sad:"; + size = strlen(str); + dchat::parseOutgoingMessage(str, size, binds, callbackFunc); + } + + { + expect = + { + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 0, 9 } } + }; + + expectText = + { + ":nothing:", + }; + + index = 0; + str = ":nothing:"; + size = strlen(str); + dchat::parseOutgoingMessage(str, size, binds, callbackFunc); } + { + expect = + { + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 4, 13 } }, + dchat::OutgoingMessagePart { dchat::OutgoingMessagePart::Type::TEXT, dchat::Range{ 13, 17 } } + }; + + expectText = + { + "aaa ", + ":nothing:", + " bbb" + }; + + index = 0; + str = "aaa :nothing: bbb"; + size = strlen(str); + dchat::parseOutgoingMessage(str, size, binds, callbackFunc); + } +} + +int main(int argc, char **argv) +{ + testIncomingMessage(); + testOutgoingMessage(); return 0; }
\ No newline at end of file |