2012-03-21 20:27:36 +01:00
|
|
|
#include "assert.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "buffer.hh"
|
2012-04-01 17:39:53 +02:00
|
|
|
#include "editor.hh"
|
2013-01-30 19:16:36 +01:00
|
|
|
#include "keys.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "selectors.hh"
|
2012-03-21 20:27:36 +01:00
|
|
|
|
|
|
|
using namespace Kakoune;
|
|
|
|
|
2012-04-01 17:39:53 +02:00
|
|
|
void test_buffer()
|
2012-03-21 20:27:36 +01:00
|
|
|
{
|
2012-11-28 20:10:49 +01:00
|
|
|
Buffer empty_buffer("empty", Buffer::Flags::None, {});
|
|
|
|
|
2012-11-23 18:42:07 +01:00
|
|
|
Buffer buffer("test", Buffer::Flags::None, { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" });
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(buffer.line_count() == 4);
|
2012-03-21 20:27:36 +01:00
|
|
|
|
2013-06-06 19:39:53 +02:00
|
|
|
BufferIterator pos = buffer.begin();
|
|
|
|
kak_assert(*pos == 'a');
|
|
|
|
pos += 6;
|
|
|
|
kak_assert(pos.coord() == BufferCoord{0 COMMA 6});
|
|
|
|
++pos;
|
|
|
|
kak_assert(pos.coord() == BufferCoord{1 COMMA 0});
|
|
|
|
--pos;
|
|
|
|
kak_assert(pos.coord() == BufferCoord{0 COMMA 6});
|
|
|
|
pos += 1;
|
|
|
|
kak_assert(pos.coord() == BufferCoord{1 COMMA 0});
|
2013-06-04 19:23:11 +02:00
|
|
|
buffer.insert(pos, "tchou kanaky\n");
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(buffer.line_count() == 5);
|
2012-03-21 20:27:36 +01:00
|
|
|
|
2013-06-04 19:23:11 +02:00
|
|
|
String str = buffer.string({ 4, 1 }, buffer.next({ 4, 5 }));
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(str == "youpi");
|
2012-09-10 19:26:17 +02:00
|
|
|
|
|
|
|
// check insert at end behaviour: auto add end of line if necessary
|
2013-06-06 19:39:53 +02:00
|
|
|
pos = buffer.end()-1;
|
2013-06-04 19:23:11 +02:00
|
|
|
buffer.insert(pos, "tchou");
|
2013-06-06 19:39:53 +02:00
|
|
|
kak_assert(buffer.string(pos.coord(), buffer.end_coord()) == "tchou\n");
|
2012-09-10 19:26:17 +02:00
|
|
|
|
2013-06-06 19:39:53 +02:00
|
|
|
pos = buffer.end()-1;
|
|
|
|
buffer.insert(buffer.end(), "kanaky\n");
|
|
|
|
kak_assert(buffer.string((pos+1).coord(), buffer.end_coord()) == "kanaky\n");
|
2013-01-23 14:27:21 +01:00
|
|
|
|
2013-02-20 14:20:16 +01:00
|
|
|
buffer.commit_undo_group();
|
2013-06-06 19:39:53 +02:00
|
|
|
buffer.erase(pos+1, buffer.end());
|
|
|
|
buffer.insert(buffer.end(), "mutch\n");
|
2013-02-20 14:20:16 +01:00
|
|
|
buffer.commit_undo_group();
|
2013-01-23 14:27:21 +01:00
|
|
|
buffer.undo();
|
2013-06-04 19:23:11 +02:00
|
|
|
kak_assert(buffer.string(buffer.advance(buffer.end_coord(), -7), buffer.end_coord()) == "kanaky\n");
|
2013-01-31 18:59:23 +01:00
|
|
|
buffer.redo();
|
2013-06-04 19:23:11 +02:00
|
|
|
kak_assert(buffer.string(buffer.advance(buffer.end_coord(), -6), buffer.end_coord()) == "mutch\n");
|
2012-03-21 20:27:36 +01:00
|
|
|
}
|
|
|
|
|
2013-06-06 19:54:13 +02:00
|
|
|
void test_undo_group_optimizer()
|
|
|
|
{
|
|
|
|
std::vector<String> lines = { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" };
|
|
|
|
Buffer buffer("test", Buffer::Flags::None, lines);
|
|
|
|
auto pos = buffer.insert(buffer.end(), "kanaky\n");
|
|
|
|
buffer.erase(pos, buffer.end());
|
|
|
|
buffer.insert(buffer.iterator_at(2_line), "tchou\n");
|
|
|
|
buffer.insert(buffer.iterator_at(2_line), "mutch\n");
|
|
|
|
buffer.erase(buffer.iterator_at({2, 1}), buffer.iterator_at({2, 5}));
|
|
|
|
buffer.erase(buffer.iterator_at(2_line), buffer.end());
|
|
|
|
buffer.insert(buffer.end(), "youpi");
|
|
|
|
buffer.undo();
|
|
|
|
buffer.redo();
|
|
|
|
buffer.undo();
|
|
|
|
|
|
|
|
kak_assert((int)buffer.line_count() == lines.size());
|
|
|
|
for (size_t i = 0; i < lines.size(); ++i)
|
|
|
|
kak_assert(lines[i] == buffer[LineCount((int)i)]);
|
|
|
|
}
|
|
|
|
|
2012-04-01 17:39:53 +02:00
|
|
|
void test_editor()
|
|
|
|
{
|
2013-06-01 14:22:57 +02:00
|
|
|
using namespace std::placeholders;
|
2012-11-23 18:42:07 +01:00
|
|
|
Buffer buffer("test", Buffer::Flags::None, { "test\n", "\n", "youpi\n" });
|
2012-04-01 17:39:53 +02:00
|
|
|
Editor editor(buffer);
|
|
|
|
|
|
|
|
{
|
2013-03-13 14:26:20 +01:00
|
|
|
scoped_edition edition{editor};
|
|
|
|
editor.select(select_whole_buffer);
|
2013-06-01 14:22:57 +02:00
|
|
|
editor.multi_select(std::bind(select_all_matches, _1, _2, Regex{"\\n\\h*"}));
|
2013-03-13 14:26:20 +01:00
|
|
|
for (auto& sel : editor.selections())
|
|
|
|
{
|
2013-06-03 18:58:09 +02:00
|
|
|
kak_assert(buffer.byte_at(sel.min()) == '\n');
|
|
|
|
erase(buffer, sel);
|
2013-03-13 14:26:20 +01:00
|
|
|
}
|
2012-04-01 17:39:53 +02:00
|
|
|
}
|
2013-03-13 14:26:20 +01:00
|
|
|
editor.undo();
|
|
|
|
|
2013-06-04 19:23:11 +02:00
|
|
|
Selection sel{ 2_line, buffer.back_coord() };
|
2013-03-13 14:26:20 +01:00
|
|
|
editor.select(sel, SelectMode::Replace);
|
|
|
|
editor.insert("",InsertMode::Replace);
|
2013-06-03 18:58:09 +02:00
|
|
|
kak_assert(not buffer.is_end(editor.main_selection().first()));
|
2012-04-01 17:39:53 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 23:46:04 +02:00
|
|
|
void test_incremental_inserter()
|
|
|
|
{
|
2013-07-30 20:09:36 +02:00
|
|
|
Buffer buffer("test", Buffer::Flags::None, { "test\n", "\n", "yoüpi\n", "matin\n" });
|
2012-09-03 23:46:04 +02:00
|
|
|
Editor editor(buffer);
|
|
|
|
|
2013-06-04 19:23:11 +02:00
|
|
|
editor.select({0,0});
|
2012-09-03 23:46:04 +02:00
|
|
|
{
|
2012-09-26 14:22:24 +02:00
|
|
|
IncrementalInserter inserter(editor, InsertMode::OpenLineAbove);
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(editor.is_editing());
|
|
|
|
kak_assert(editor.selections().size() == 1);
|
2013-06-04 19:23:11 +02:00
|
|
|
kak_assert(editor.selections().front().first() == BufferCoord{0 COMMA 0});
|
|
|
|
kak_assert(editor.selections().front().last() == BufferCoord{0 COMMA 0});
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(*buffer.begin() == L'\n');
|
2012-09-03 23:46:04 +02:00
|
|
|
}
|
2013-07-30 20:09:36 +02:00
|
|
|
// check utf-8 erase
|
|
|
|
editor.select({3,4});
|
|
|
|
{
|
|
|
|
IncrementalInserter inserter(editor, InsertMode::Insert);
|
|
|
|
inserter.erase();
|
|
|
|
kak_assert(editor.selections().back().last() == BufferCoord{3 COMMA 2});
|
|
|
|
}
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(not editor.is_editing());
|
2012-09-03 23:46:04 +02:00
|
|
|
}
|
2012-10-22 01:05:56 +02:00
|
|
|
|
|
|
|
void test_utf8()
|
|
|
|
{
|
|
|
|
String str = "maïs mélange bientôt";
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(utf8::distance(str.begin(), str.end()) == 20);
|
|
|
|
kak_assert(utf8::codepoint(str.begin() + 2) == 0x00EF);
|
2012-10-22 01:05:56 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
void test_string()
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(String("youpi ") + "matin" == "youpi matin");
|
2012-05-29 07:19:50 +02:00
|
|
|
|
2013-07-24 22:37:17 +02:00
|
|
|
std::vector<String> splited = split("youpi:matin::tchou\\:kanaky:hihi\\:", ':', '\\');
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(splited[0] == "youpi");
|
|
|
|
kak_assert(splited[1] == "matin");
|
|
|
|
kak_assert(splited[2] == "");
|
2013-07-24 22:37:17 +02:00
|
|
|
kak_assert(splited[3] == "tchou:kanaky");
|
|
|
|
kak_assert(splited[4] == "hihi:");
|
|
|
|
|
|
|
|
String escaped = escape("youpi:matin:tchou:", ':', '\\');
|
|
|
|
kak_assert(escaped == "youpi\\:matin\\:tchou\\:");
|
2012-05-29 07:19:50 +02:00
|
|
|
}
|
|
|
|
|
2013-01-30 19:16:36 +01:00
|
|
|
void test_keys()
|
|
|
|
{
|
|
|
|
std::vector<Key> keys{
|
|
|
|
{ ' ' },
|
|
|
|
{ 'c' },
|
|
|
|
{ Key::Modifiers::Alt, 'j' },
|
|
|
|
{ Key::Modifiers::Control, 'r' }
|
|
|
|
};
|
|
|
|
String keys_as_str;
|
|
|
|
for (auto& key : keys)
|
|
|
|
keys_as_str += key_to_str(key);
|
|
|
|
auto parsed_keys = parse_keys(keys_as_str);
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(keys == parsed_keys);
|
2013-01-30 19:16:36 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 20:27:36 +01:00
|
|
|
void run_unit_tests()
|
|
|
|
{
|
2012-10-22 01:05:56 +02:00
|
|
|
test_utf8();
|
2012-05-29 07:19:50 +02:00
|
|
|
test_string();
|
2013-01-30 19:16:36 +01:00
|
|
|
test_keys();
|
2012-03-21 20:27:36 +01:00
|
|
|
test_buffer();
|
2013-06-06 19:54:13 +02:00
|
|
|
test_undo_group_optimizer();
|
2012-04-01 17:39:53 +02:00
|
|
|
test_editor();
|
2012-09-03 23:46:04 +02:00
|
|
|
test_incremental_inserter();
|
2012-03-21 20:27:36 +01:00
|
|
|
}
|