summaryrefslogtreecommitdiff
path: root/test/minibuffer.c
blob: 7ac6c9b3b45f6eebc686428c7e7eae7ecb4510c7 (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
#include "assert.h"
#include "stdlib.h"
#include "test.h"

#include "dged/allocator.h"
#include "dged/buffer.h"
#include "dged/buffer_view.h"
#include "dged/command.h"
#include "dged/display.h"
#include "dged/minibuffer.h"
#include "dged/settings.h"

static struct buffer b = {0};

static struct frame_allocator *g_alloc = NULL;

void *alloc_fn(size_t sz) { return frame_allocator_alloc(g_alloc, sz); }

void init() {
  if (b.name == NULL) {
    settings_init(10);
    b = buffer_create("minibuffer");
  }

  minibuffer_init(&b);
}

void destroy() {
  if (b.name != NULL) {
    buffer_destroy(&b);
    settings_destroy();
  }
}

void test_minibuffer_echo() {
  struct buffer_view view = buffer_view_create(&b, false, false);

  // TODO: how to clear this?
  struct frame_allocator alloc = frame_allocator_create(1024 * 1024);
  g_alloc = &alloc;

  struct command_list *list =
      command_list_create(10, alloc_fn, 0, 0, "minibuffer");

  init();
  ASSERT(!minibuffer_displaying(),
         "Minibuffer should have nothing to display before echoing");

  minibuffer_echo("Test %s", "test");
  ASSERT(minibuffer_displaying(), "Minibuffer should now have text to display");

  minibuffer_clear();
  struct buffer_view_update_params p = {
      .commands = list,
      .frame_alloc = alloc_fn,
      .window_id = -1,
      .frame_time = 0,
      .width = 100,
      .height = 1,
      .window_x = 0,
      .window_y = 25,
  };
  buffer_view_update(&view, &p);
  ASSERT(!minibuffer_displaying(),
         "Minibuffer should have nothing to display after clearing");

  minibuffer_echo_timeout(0, "You will not see me");

  buffer_view_update(&view, &p);
  ASSERT(!minibuffer_displaying(),
         "A zero timeout echo should be cleared after first update");

  frame_allocator_destroy(&alloc);
  g_alloc = NULL;
  destroy();
}

int32_t fake(struct command_ctx ctx, int argc, const char *argv[]) { return 0; }

void test_minibuffer_prompt() {
  init();
  ASSERT(!minibuffer_focused(),
         "Minibuffer should not be focused without reason");

  struct command cmd = {
      .fn = fake,
      .name = "fake",
      .userdata = NULL,
  };
  struct command_ctx ctx = {.commands = NULL,
                            .active_window = NULL,
                            .buffers = NULL,
                            .userdata = NULL,
                            .self = &cmd};
  minibuffer_prompt(ctx, "prompt %s: ", "yes");

  ASSERT(minibuffer_focused(), "Minibuffer should get focused when prompting");

  minibuffer_abort_prompt();
  ASSERT(!minibuffer_focused(),
         "Minibuffer must not be focused after prompt has been aborted");
  destroy();
}

void run_minibuffer_tests() {
  run_test(test_minibuffer_echo);
  run_test(test_minibuffer_prompt);
}