summaryrefslogtreecommitdiffstats
path: root/tests/test_escape.c
blob: e749443a869444a62d124c7220cad10810e42a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;
}