summaryrefslogtreecommitdiff
path: root/test/buffer.c
blob: f4aefc53c4c827abe3360afea09d9ec7730db2da (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
#include <string.h>

#include "dged/buffer.h"

#include "assert.h"
#include "test.h"

void test_add() {
  struct buffer b = buffer_create("test-buffer");
  ASSERT(buffer_num_lines(&b) == 0, "Expected buffer to have zero lines");

  const char *txt = "we are adding some text";
  struct location loc = buffer_add(&b, (struct location){.line = 1, .col = 0},
                                   (uint8_t *)txt, strlen(txt));

  ASSERT(loc.line == 1 && loc.col == strlen(txt),
         "Expected buffer to have one line with characters");
}

void test_word_at() {
  struct buffer b = buffer_create("test-word-at-buffer");
  const char *txt = "word1 (word2). Another";
  buffer_add(&b, (struct location){.line = 0, .col = 0}, (uint8_t *)txt,
             strlen(txt));

  struct region word1 =
      buffer_word_at(&b, (struct location){.line = 0, .col = 0});
  ASSERT(region_has_size(word1), "expected 0,0 to be a word");
  ASSERT(word1.begin.col == 0 && word1.end.col == 5,
         "Expected word to end at col 5");

  // test that dot can be in the middle of a word
  // and that '(' and ')' works as a delimiter
  struct region word2 =
      buffer_word_at(&b, (struct location){.line = 0, .col = 8});
  ASSERT(region_has_size(word2), "expected 0,8 to be in a word");
  ASSERT(word2.begin.col == 7 && word2.end.col == 12,
         "Expected word to span cols 7..12");

  // test that clamping works correctly
  struct region word3 =
      buffer_word_at(&b, (struct location){.line = 0, .col = 100});
  ASSERT(region_has_size(word3), "expected 0,100 to be in the last word");
  ASSERT(word3.begin.col == 15 && word3.end.col == 22,
         "Expected word to span cols 15..22");
}

void run_buffer_tests() {
  run_test(test_add);
  run_test(test_word_at);
}