aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-01-24 22:15:21 +0100
committerdec05eba <dec05eba@protonmail.com>2020-08-19 00:14:40 +0200
commitd525a7ac28dd6403e46e8df6fa15b723b8c09503 (patch)
tree51254e6e94bb099484c2d2c01fa4f397c4e70287
parentb629e93b124fcad6635a508e47c7776bb0891d1b (diff)
Add more tests, add apache license
-rw-r--r--LICENSE13
m---------depends/dchat_core0
-rw-r--r--src/MessageComposer.cpp8
-rw-r--r--src/Window.cpp2
-rw-r--r--tests/main.cpp81
5 files changed, 98 insertions, 6 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..9d3f9f8
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,13 @@
+Copyright 2019 dec05eba
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/depends/dchat_core b/depends/dchat_core
-Subproject b2e9deb0e6733c0d79d6992809882e214dee7ed
+Subproject c070f5f9edc0acb0abe853036dcd9157ca24fc1
diff --git a/src/MessageComposer.cpp b/src/MessageComposer.cpp
index 56ff925..3ff0d76 100644
--- a/src/MessageComposer.cpp
+++ b/src/MessageComposer.cpp
@@ -33,7 +33,6 @@ namespace dchat
EnclosedType parseEnclosedData(char endSymbol)
{
- ++index;
bool foundEndOfType = false;
while(index < length)
@@ -47,7 +46,7 @@ namespace dchat
}
}
- if(!foundEndOfType || index == length)
+ if(!foundEndOfType)
return EnclosedType::TEXT;
return EnclosedType::DATA;
@@ -62,6 +61,7 @@ namespace dchat
if(c == '[')
{
usize start = index;
+ ++index;
if(parseEnclosedData(']') == EnclosedType::TEXT)
{
identifierRange.start = start;
@@ -69,8 +69,7 @@ namespace dchat
return Token::TEXT;
}
- c = getChar();
- if(c != '(')
+ if(index == length || getChar() != '(')
{
identifierRange.start = start;
identifierRange.end = index;
@@ -81,6 +80,7 @@ namespace dchat
typeRange.end = index - 1;
typeDataRange.start = index + 1;
+ ++index;
switch(parseEnclosedData(')'))
{
case EnclosedType::TEXT:
diff --git a/src/Window.cpp b/src/Window.cpp
index 8a6652e..1344429 100644
--- a/src/Window.cpp
+++ b/src/Window.cpp
@@ -272,7 +272,7 @@ namespace dchat
}
prevTimeMillis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count() - 5000;
//drawBackgroundConnection = signal_draw().connect(sigc::mem_fun(*this, &Window::drawBackground));
- //set_size_request(640, 480);
+ set_size_request(640, 480);
//set_app_paintable(true);
}
diff --git a/tests/main.cpp b/tests/main.cpp
index 719a9ea..b18626c 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -1,7 +1,13 @@
#include "../include/MessageComposer.hpp"
+#include "../include/DynamicImage.hpp"
#include <stdio.h>
#include <vector>
#include <string.h>
+#include <gtkmm/application.h>
+#include <gtkmm/window.h>
+#include <gtkmm/grid.h>
+#include <gtkmm/textview.h>
+#include <assert.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)
@@ -14,6 +20,60 @@ 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)
+static void applyRichText(Gtk::TextView *textView, const Glib::ustring &text)
+{
+ auto buffer = textView->get_buffer();
+ buffer->set_text("");
+ Gtk::TextIter iter = buffer->get_iter_at_offset(0);
+
+ dchat::compose(text, [textView, &text, &iter, &buffer](dchat::MessagePart messagePart)
+ {
+ switch(messagePart.type)
+ {
+ case dchat::MessagePart::Type::TEXT:
+ {
+ iter = buffer->insert(iter, text.data() + messagePart.textRange.start, text.data() + messagePart.textRange.end);
+ break;
+ }
+ case dchat::MessagePart::Type::EMOJI:
+ {
+ auto anchor = Gtk::TextChildAnchor::create();
+ iter = buffer->insert_child_anchor(iter, anchor);
+ auto image = Gtk::manage(new dchat::DynamicImage());
+ image->url = text.substr(messagePart.textRange.start, messagePart.textRange.length());
+ image->set_size_request(35, 35);
+ textView->add_child_at_anchor(*image, anchor);
+ break;
+ }
+ default:
+ assert(false);
+ break;
+ }
+ });
+}
+
+static int testVisual(int argc, char **argv)
+{
+ auto app = Gtk::Application::create(argc, argv, "dec05eba.dchat", Gio::APPLICATION_NON_UNIQUE);
+ Gtk::Window window;
+ window.set_border_width(0);
+ window.set_size_request(640, 480);
+
+ auto grid = Gtk::manage(new Gtk::Grid());
+ grid->set_vexpand(true);
+ grid->set_hexpand(true);
+ window.add(*grid);
+
+ auto textView = Gtk::manage(new Gtk::TextView());
+ textView->set_size_request(640, 480);
+ textView->set_wrap_mode(Gtk::WRAP_WORD_CHAR);
+ applyRichText(textView, "Hello world [emoji](https://discordemoji.com/assets/emoji/PeepoHide.png)[emoji](https://discordemoji.com/assets/emoji/PeepoHide.png)");
+ grid->attach(*textView, 0, 0, 1, 1);
+
+ window.show_all();
+ return app->run(window);
+}
+
int main(int argc, char **argv)
{
std::vector<dchat::MessagePart> expect =
@@ -107,5 +167,24 @@ int main(int argc, char **argv)
str = "abc [notemoji](cool) def";
dchat::compose(str, compareFunc);
}
- return 0;
+
+ {
+ 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)";
+ dchat::compose(str, compareFunc);
+ }
+
+ return testVisual(argc, argv);
}