From b629e93b124fcad6635a508e47c7776bb0891d1b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 20 Jan 2019 05:02:13 +0100 Subject: Start with custom emoji (binds) and tests --- tests/main.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/main.cpp (limited to 'tests/main.cpp') diff --git a/tests/main.cpp b/tests/main.cpp new file mode 100644 index 0000000..719a9ea --- /dev/null +++ b/tests/main.cpp @@ -0,0 +1,111 @@ +#include "../include/MessageComposer.hpp" +#include +#include +#include + +#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) +{ + std::vector 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 expectText = + { + "abc ", + "cool", + " def" + }; + + int index = 0; + const char *str = "abc [emoji](cool) def"; + + 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, 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"; + dchat::compose(str, 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)"; + dchat::compose(str, 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"; + dchat::compose(str, compareFunc); + } + return 0; +} -- cgit v1.2.3