2012-03-21 20:27:36 +01:00
|
|
|
#include "buffer.hh"
|
|
|
|
#include "assert.hh"
|
2012-04-01 17:39:53 +02:00
|
|
|
#include "editor.hh"
|
|
|
|
#include "selectors.hh"
|
2013-01-30 19:16:36 +01:00
|
|
|
#include "keys.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" });
|
2012-03-21 20:27:36 +01:00
|
|
|
assert(buffer.line_count() == 4);
|
|
|
|
|
|
|
|
BufferIterator i = buffer.begin();
|
|
|
|
assert(*i == 'a');
|
|
|
|
i += 6;
|
2012-11-22 18:54:37 +01:00
|
|
|
assert(i.coord() == BufferCoord{0 COMMA 6});
|
2012-03-21 20:27:36 +01:00
|
|
|
i += 1;
|
2012-11-22 18:54:37 +01:00
|
|
|
assert(i.coord() == BufferCoord{1 COMMA 0});
|
2012-03-21 20:27:36 +01:00
|
|
|
--i;
|
2012-11-22 18:54:37 +01:00
|
|
|
assert(i.coord() == BufferCoord{0 COMMA 6});
|
2012-03-21 20:27:36 +01:00
|
|
|
++i;
|
2012-11-22 18:54:37 +01:00
|
|
|
assert(i.coord() == BufferCoord{1 COMMA 0});
|
2012-08-10 19:12:43 +02:00
|
|
|
buffer.insert(i, "tchou kanaky\n");
|
2012-03-21 20:27:36 +01:00
|
|
|
assert(buffer.line_count() == 5);
|
|
|
|
|
|
|
|
BufferIterator begin = buffer.iterator_at({ 4, 1 });
|
|
|
|
BufferIterator end = buffer.iterator_at({ 4, 5 }) + 1;
|
|
|
|
String str = buffer.string(begin, end);
|
|
|
|
assert(str == "youpi");
|
2012-09-10 19:26:17 +02:00
|
|
|
|
|
|
|
// check insert at end behaviour: auto add end of line if necessary
|
|
|
|
begin = buffer.end() - 1;
|
|
|
|
buffer.insert(buffer.end(), "tchou");
|
|
|
|
assert(buffer.string(begin+1, buffer.end()) == "tchou\n");
|
|
|
|
|
|
|
|
begin = buffer.end() - 1;
|
|
|
|
buffer.insert(buffer.end(), "kanaky\n");
|
|
|
|
assert(buffer.string(begin+1, buffer.end()) == "kanaky\n");
|
2013-01-23 14:27:21 +01:00
|
|
|
|
2013-02-20 14:20:16 +01:00
|
|
|
buffer.commit_undo_group();
|
2013-01-23 14:27:21 +01:00
|
|
|
buffer.erase(begin+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();
|
|
|
|
assert(buffer.string(buffer.end() - 7, buffer.end()) == "kanaky\n");
|
2013-01-31 18:59:23 +01:00
|
|
|
buffer.redo();
|
|
|
|
assert(buffer.string(buffer.end() - 6, buffer.end()) == "mutch\n");
|
2012-03-21 20:27:36 +01:00
|
|
|
}
|
|
|
|
|
2012-04-01 17:39:53 +02:00
|
|
|
void test_editor()
|
|
|
|
{
|
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);
|
|
|
|
editor.multi_select(std::bind(select_all_matches, std::placeholders::_1, "\\n\\h*"));
|
|
|
|
for (auto& sel : editor.selections())
|
|
|
|
{
|
|
|
|
assert(*sel.begin() == '\n');
|
|
|
|
editor.buffer().erase(sel.begin(), sel.end());
|
|
|
|
}
|
2012-04-01 17:39:53 +02:00
|
|
|
}
|
2013-03-13 14:26:20 +01:00
|
|
|
editor.undo();
|
|
|
|
|
|
|
|
Selection sel{ buffer.iterator_at_line_begin(2_line), buffer.end() };
|
|
|
|
editor.select(sel, SelectMode::Replace);
|
|
|
|
editor.insert("",InsertMode::Replace);
|
2013-03-15 18:20:35 +01:00
|
|
|
assert(not editor.main_selection().first().is_end());
|
2012-04-01 17:39:53 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 23:46:04 +02:00
|
|
|
void test_incremental_inserter()
|
|
|
|
{
|
2012-11-23 18:42:07 +01:00
|
|
|
Buffer buffer("test", Buffer::Flags::None, { "test\n", "\n", "youpi\n", "matin\n" });
|
2012-09-03 23:46:04 +02:00
|
|
|
Editor editor(buffer);
|
|
|
|
|
|
|
|
editor.select(buffer.begin());
|
|
|
|
{
|
2012-09-26 14:22:24 +02:00
|
|
|
IncrementalInserter inserter(editor, InsertMode::OpenLineAbove);
|
2012-09-03 23:46:04 +02:00
|
|
|
assert(editor.is_editing());
|
|
|
|
assert(editor.selections().size() == 1);
|
|
|
|
assert(editor.selections().front().first() == buffer.begin());
|
|
|
|
assert(editor.selections().front().last() == buffer.begin());
|
|
|
|
assert(*buffer.begin() == L'\n');
|
|
|
|
}
|
|
|
|
assert(not editor.is_editing());
|
|
|
|
}
|
2012-10-22 01:05:56 +02:00
|
|
|
|
|
|
|
void test_utf8()
|
|
|
|
{
|
|
|
|
String str = "maïs mélange bientôt";
|
|
|
|
assert(utf8::distance(str.begin(), str.end()) == 20);
|
|
|
|
assert(utf8::codepoint(str.begin() + 2) == 0x00EF);
|
|
|
|
}
|
|
|
|
|
2012-05-29 07:19:50 +02:00
|
|
|
void test_string()
|
|
|
|
{
|
|
|
|
assert(int_to_str(124) == "124");
|
|
|
|
assert(int_to_str(-129) == "-129");
|
|
|
|
assert(int_to_str(0) == "0");
|
|
|
|
|
|
|
|
assert(String("youpi ") + "matin" == "youpi matin");
|
|
|
|
|
|
|
|
std::vector<String> splited = split("youpi:matin::tchou", ':');
|
|
|
|
assert(splited[0] == "youpi");
|
|
|
|
assert(splited[1] == "matin");
|
|
|
|
assert(splited[2] == "");
|
|
|
|
assert(splited[3] == "tchou");
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
assert(keys == parsed_keys);
|
|
|
|
}
|
|
|
|
|
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();
|
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
|
|
|
}
|