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
|
#define _POSIX_C_SOURCE 200809L
#include "test_core.h"
#include "completion.h"
#include "ecex.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
void test_completion_helpers(void) {
assert(ecex_ascii_strncasecmp("Alpha", "alpha", 5) == 0);
assert(ecex_ascii_strncasecmp("Alpha", "Alpine", 4) != 0);
assert(ecex_ascii_contains_ci("compile-buffer", "PILE") == 1);
assert(ecex_ascii_contains_ci("compile-buffer", "missing") == 0);
assert(ecex_fuzzy_score("find-file", "ff") > 0);
assert(ecex_fuzzy_score("find-file", "zz") < 0);
assert(ecex_fuzzy_score("find-file", "find") > ecex_fuzzy_score("other-find", "find"));
ecex_completion_item_t items[] = {
{"z-file", 10, 0, 0},
{"a-dir", 10, 1, 1},
{"b-file", 30, 0, 2},
};
qsort(items, 3, sizeof(items[0]), ecex_completion_item_compare);
assert(strcmp(items[0].value, "b-file") == 0);
assert(strcmp(items[1].value, "a-dir") == 0);
assert(strcmp(items[2].value, "z-file") == 0);
}
void test_path_helpers(void) {
char small[4];
assert(ecex_path_copy(small, sizeof(small), "abcd") != 0);
assert(strcmp(small, "abc") == 0);
char *joined = ecex_path_join("/tmp", "ecex-path-test.txt");
assert(joined);
assert(strcmp(joined, "/tmp/ecex-path-test.txt") == 0);
free(joined);
char *absolute = ecex_path_join("/tmp", "/var/log");
assert(absolute);
assert(strcmp(absolute, "/var/log") == 0);
free(absolute);
char *dir = ecex_path_dirname("/tmp/ecex/file.txt");
assert(dir);
assert(strcmp(dir, "/tmp/ecex") == 0);
free(dir);
char *base = ecex_path_basename_dup("/tmp/ecex/file.txt");
assert(base);
assert(strcmp(base, "file.txt") == 0);
free(base);
assert(ecex_path_is_image("photo.PNG") == 1);
assert(ecex_path_is_video("clip.MKV") == 1);
assert(ecex_path_is_media("notes.txt") == 0);
char root_dir[256];
snprintf(root_dir, sizeof(root_dir), "/tmp/ecex-project-test-%ld", (long)getpid());
char src_dir[320];
char nested_dir[384];
char marker[384];
char source_file[448];
snprintf(src_dir, sizeof(src_dir), "%s/src", root_dir);
snprintf(nested_dir, sizeof(nested_dir), "%s/nested", src_dir);
snprintf(marker, sizeof(marker), "%s/.ecex-project", root_dir);
snprintf(source_file, sizeof(source_file), "%s/main.c", nested_dir);
assert(mkdir(root_dir, 0700) == 0);
assert(mkdir(src_dir, 0700) == 0);
assert(mkdir(nested_dir, 0700) == 0);
FILE *file = fopen(marker, "wb");
assert(file);
fclose(file);
char *project_root = ecex_project_root_for_file(source_file);
char *normal_root = ecex_path_normalize(root_dir);
assert(project_root && normal_root);
assert(strcmp(project_root, normal_root) == 0);
free(project_root);
free(normal_root);
unlink(marker);
rmdir(nested_dir);
rmdir(src_dir);
rmdir(root_dir);
}
|