summaryrefslogtreecommitdiffstats
path: root/tests/test_escape.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_escape.c')
-rw-r--r--tests/test_escape.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/tests/test_escape.c b/tests/test_escape.c
new file mode 100644
index 0000000..e749443
--- /dev/null
+++ b/tests/test_escape.c
@@ -0,0 +1,113 @@
+// Tests for escape.h - ANSI escape code macros and functions
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#define ESCAPE_IMPLEMENTATION
+#include "../include/escape.h"
+
+#define ASSERT(cond, msg) \
+ do { \
+ if (!(cond)) { \
+ fprintf(stderr, "FAIL: %s (test_escape.c:%d)\n", msg, __LINE__); \
+ failures++; \
+ } \
+ } while (0)
+
+static int failures = 0;
+
+void test_macros() {
+ ASSERT(ESC_CURSOR_UP(5)[0] == '\x1B', "cursor up starts with ESC");
+ ASSERT(ESC_CURSOR_DOWN(3)[0] == '\x1B', "cursor down starts with ESC");
+ ASSERT(ESC_CURSOR_LEFT(2)[0] == '\x1B', "cursor left starts with ESC");
+ ASSERT(ESC_CURSOR_RIGHT(2)[0] == '\x1B', "cursor right starts with ESC");
+ ASSERT(ESC_CLEAR_SCREEN[0] == '\x1B', "clear screen starts with ESC");
+ ASSERT(ESC_CLEAR_LINE[0] == '\x1B', "clear line starts with ESC");
+ ASSERT(ESC_RESET[0] == '\x1B', "reset starts with ESC");
+ ASSERT(ESC_BOLD[0] == '\x1B', "bold starts with ESC");
+ ASSERT(ESC_UNDERLINE[0] == '\x1B', "underline starts with ESC");
+ ASSERT(ESC_BLINK[0] == '\x1B', "blink starts with ESC");
+ ASSERT(ESC_REVERSE[0] == '\x1B', "reverse starts with ESC");
+ ASSERT(ESC_CONCEAL[0] == '\x1B', "conceal starts with ESC");
+ ASSERT(ESC_REVEAL[0] == '\x1B', "reveal starts with ESC");
+ ASSERT(ESC_STRIKETHROUGH[0] == '\x1B', "strikethrough starts with ESC");
+ ASSERT(ESC_OVERLINE[0] == '\x1B', "overline starts with ESC");
+}
+
+void test_color_macros() {
+ ASSERT(ESC_COLOR_FG(1)[0] == '\x1B', "fg color starts with ESC");
+ ASSERT(ESC_COLOR_BG(4)[0] == '\x1B', "bg color starts with ESC");
+ ASSERT(ESC_COLOR_FG_BRIGHT(3)[0] == '\x1B', "fg bright starts with ESC");
+ ASSERT(ESC_COLOR_BG_BRIGHT(5)[0] == '\x1B', "bg bright starts with ESC");
+ ASSERT(ESC_COLOR_FG_RGB(255, 128, 0)[0] == '\x1B', "fg rgb starts with ESC");
+ ASSERT(ESC_COLOR_BG_RGB(0, 128, 255)[0] == '\x1B', "bg rgb starts with ESC");
+ ASSERT(ESC_COLOR_FG_256(196)[0] == '\x1B', "fg 256 starts with ESC");
+ ASSERT(ESC_COLOR_BG_256(21)[0] == '\x1B', "bg 256 starts with ESC");
+}
+
+void test_cursor_position_macro() {
+ ASSERT(ESC_CURSOR_POSITION(10, 20)[0] == '\x1B', "cursor position starts with ESC");
+}
+
+void test_scroll_macros() {
+ ASSERT(ESC_SCROLL_UP(3)[0] == '\x1B', "scroll up starts with ESC");
+ ASSERT(ESC_SCROLL_DOWN(2)[0] == '\x1B', "scroll down starts with ESC");
+}
+
+void test_clear_macros() {
+ ASSERT(ESC_CLEAR_SCREEN_START[0] == '\x1B', "clear screen start");
+ ASSERT(ESC_CLEAR_SCREEN_END[0] == '\x1B', "clear screen end");
+ ASSERT(ESC_CLEAR_LINE_START[0] == '\x1B', "clear line start");
+ ASSERT(ESC_CLEAR_LINE_END[0] == '\x1B', "clear line end");
+ ASSERT(ESC_CURSOR_LINE_NEXT(2)[0] == '\x1B', "cursor line next");
+}
+
+void test_csi() {
+ ASSERT(CSI[0] == '\x1B', "CSI starts with ESC");
+ ASSERT(CSI[1] == '[', "CSI second char is [");
+}
+
+void test_fvariants() {
+ esc_fcursor_up(stdout, 3);
+
+ esc_fclear_line(stdout);
+
+ esc_fcolor_fg_256(stdout, 196);
+
+ esc_fcursor_position(stdout, 10, 20);
+
+ esc_fscroll_up(stdout, 2);
+
+ esc_fscroll_down(stdout, 1);
+
+ esc_fclear_screen(stdout);
+
+ esc_fcolor_fg_rgb(stdout, 255, 128, 0);
+
+ esc_fcolor_bg_rgb(stdout, 0, 128, 255);
+
+ esc_fcursor_down(stdout, 5);
+ esc_fcursor_left(stdout, 2);
+ esc_fcursor_right(stdout, 2);
+ esc_fcursor_line_next(stdout, 1);
+ esc_fclear_screen_start(stdout);
+ esc_fclear_screen_end(stdout);
+ esc_fclear_line_start(stdout);
+ esc_fclear_line_end(stdout);
+ esc_fcolor_bg_256(stdout, 21);
+}
+
+int main() {
+ test_macros();
+ test_color_macros();
+ test_cursor_position_macro();
+ test_scroll_macros();
+ test_clear_macros();
+ test_csi();
+ test_fvariants();
+
+ printf("test_escape: %d failures\n", failures);
+ return failures;
+}