kakoune/src/unit_tests.cc

104 lines
2.9 KiB
C++
Raw Normal View History

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"
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
{
Buffer buffer("test", Buffer::Flags::None, "allo ?\nmais 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;
assert(buffer.line_and_column_at(i) == BufferCoord{0 COMMA 6});
i += 1;
assert(buffer.line_and_column_at(i) == BufferCoord{1 COMMA 0});
--i;
assert(buffer.line_and_column_at(i) == BufferCoord{0 COMMA 6});
++i;
assert(buffer.line_and_column_at(i) == BufferCoord{1 COMMA 0});
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");
// 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");
2012-03-21 20:27:36 +01:00
}
2012-04-01 17:39:53 +02:00
void test_editor()
{
Buffer buffer("test", Buffer::Flags::None, "test\n\nyoupi\n");
2012-04-01 17:39:53 +02:00
Editor editor(buffer);
using namespace std::placeholders;
editor.select(select_whole_buffer);
2012-07-16 21:14:52 +02:00
editor.multi_select(std::bind(select_all_matches, _1, "\\n\\h*"));
2012-04-01 17:39:53 +02:00
for (auto& sel : editor.selections())
{
assert(*sel.begin() == '\n');
editor.buffer().erase(sel.begin(), sel.end());
2012-04-01 17:39:53 +02:00
}
}
void test_incremental_inserter()
{
Buffer buffer("test", Buffer::Flags::None, "test\n\nyoupi\nmatin\n");
Editor editor(buffer);
editor.select(buffer.begin());
{
IncrementalInserter inserter(editor, InsertMode::OpenLineAbove);
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);
}
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");
}
2012-03-21 20:27:36 +01:00
void run_unit_tests()
{
2012-10-22 01:05:56 +02:00
test_utf8();
test_string();
2012-03-21 20:27:36 +01:00
test_buffer();
2012-04-01 17:39:53 +02:00
test_editor();
test_incremental_inserter();
2012-03-21 20:27:36 +01:00
}