summaryrefslogtreecommitdiff
path: root/test/text.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/text.c')
-rw-r--r--test/text.c40
1 files changed, 36 insertions, 4 deletions
diff --git a/test/text.c b/test/text.c
index ec99890..9073553 100644
--- a/test/text.c
+++ b/test/text.c
@@ -1,4 +1,5 @@
#include "assert.h"
+#include "stdio.h"
#include "test.h"
#include "text.h"
@@ -7,7 +8,7 @@
#include <string.h>
#include <wchar.h>
-void assert_line_equal(struct txt_line *line) {}
+void assert_line_equal(struct text_chunk *line) {}
void test_add_text() {
uint32_t lines_added, cols_added;
@@ -25,8 +26,12 @@ void test_add_text() {
const char *txt2 = "This is line 2\n";
text_append(t, 1, 0, (uint8_t *)txt2, strlen(txt2), &lines_added,
&cols_added);
+ ASSERT(text_num_lines(t) == 3,
+ "Expected text to have three lines after second insertion");
ASSERT_STR_EQ((const char *)text_get_line(t, 1).text, "This is line 2",
"Expected line 2 to be line 2");
+
+ text_destroy(t);
}
void test_delete_text() {
@@ -49,13 +54,36 @@ void test_delete_text() {
const char *txt2 = "This is line 1\nThis is line 2\nThis is line 3";
text_append(t, 0, 0, (uint8_t *)txt2, strlen(txt2), &lines_added,
&cols_added);
+ ASSERT(text_num_lines(t) == 3,
+ "Expected to have three lines after inserting as many");
+
text_delete(t, 1, 11, 3);
ASSERT(text_line_length(t, 1) == 11,
"Expected line to contain 11 chars after deletion");
- struct txt_line line = text_get_line(t, 1);
+ struct text_chunk line = text_get_line(t, 1);
ASSERT(strncmp((const char *)line.text, "This is lin", line.nbytes) == 0,
"Expected deleted characters to be gone in the second line");
+ text_delete(t, 1, 0, text_line_length(t, 1) + 1);
+ ASSERT(text_num_lines(t) == 2,
+ "Expected to have two lines after deleting one");
+ struct text_chunk line2 = text_get_line(t, 1);
+ ASSERT(strncmp((const char *)line2.text, "This is line 3", line2.nbytes) == 0,
+ "Expected lines to have shifted upwards after deleting");
+
+ struct text *t3 = text_create(10);
+ const char *delete_me = "This is line๐ŸŽ™\nQ";
+ text_append(t3, 0, 0, (uint8_t *)delete_me, strlen(delete_me), &lines_added,
+ &cols_added);
+ text_delete(t3, 0, 13, 1);
+ struct text_chunk top_line = text_get_line(t3, 0);
+ ASSERT(strncmp((const char *)top_line.text, "This is line๐ŸŽ™Q",
+ top_line.nbytes) == 0,
+ "Expected text from second line to be appended to first line when "
+ "deleting newline");
+ ASSERT(text_num_lines(t3) == 1,
+ "Expected text to have one line after deleting newline");
+
// test utf-8
struct text *t2 = text_create(10);
const char *txt3 = "Emojis: ๐Ÿ‡ซ๐Ÿ‡ฎ ๐Ÿฎ\n";
@@ -71,9 +99,13 @@ void test_delete_text() {
text_delete(t2, 0, 10, 2);
ASSERT(text_line_length(t2, 0) == 10,
"Line length should be 10 after deleting the cow emoji and a space");
- struct txt_line line2 = text_get_line(t2, 0);
- ASSERT(strncmp((const char *)line2.text, "Emojis: ๐Ÿ‡ซ๐Ÿ‡ฎ", line2.nbytes) == 0,
+ struct text_chunk line3 = text_get_line(t2, 0);
+ ASSERT(strncmp((const char *)line3.text, "Emojis: ๐Ÿ‡ซ๐Ÿ‡ฎ", line3.nbytes) == 0,
"Expected cow emoji plus space to be deleted");
+
+ text_destroy(t);
+ text_destroy(t2);
+ text_destroy(t3);
}
void run_text_tests() {