-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbuffer_set_lines.c
185 lines (147 loc) · 5.41 KB
/
buffer_set_lines.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "libvim.h"
#include "minunit.h"
static int updateCount = 0;
static int lastLnum = 0;
static int lastLnume = 0;
static long lastXtra = 0;
static long lastVersionAtUpdateTime = 0;
void onBufferUpdate(bufferUpdate_T update)
{
lastLnum = update.lnum;
lastLnume = update.lnume;
lastXtra = update.xtra;
lastVersionAtUpdateTime = vimBufferGetLastChangedTick(curbuf);
updateCount++;
}
void test_setup(void)
{
vimKey("<esc>");
vimKey("<esc>");
vimExecute("e!");
vimInput("g");
vimInput("g");
updateCount = 0;
lastLnum = 0;
lastLnume = 0;
lastXtra = 0;
}
void test_teardown(void) {}
MU_TEST(test_append_before_buffer)
{
char_u *lines[] = {"one"};
vimBufferSetLines(curbuf, 0, 0, lines, 1);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "one") == 0);
printf("LINE 2: %s\n", vimBufferGetLine(curbuf, 2));
mu_check(strcmp(vimBufferGetLine(curbuf, 2), "This is the first line of a test file") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 4);
}
MU_TEST(test_append_after_buffer)
{
char_u *lines[] = {"after"};
vimBufferSetLines(curbuf, 3, 4, lines, 1);
printf("LINE 3: %s\n", vimBufferGetLine(curbuf, 3));
printf("LINE 4: %s\n", vimBufferGetLine(curbuf, 4));
mu_check(strcmp(vimBufferGetLine(curbuf, 4), "after") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 3), "This is the third line of a test file") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 4);
}
MU_TEST(test_append_after_first_line)
{
char_u *lines[] = {"after first line"};
vimBufferSetLines(curbuf, 1, 1, lines, 1);
printf("LINE 1: %s\n", vimBufferGetLine(curbuf, 1));
printf("LINE 2: %s\n", vimBufferGetLine(curbuf, 2));
printf("LINE 3: %s\n", vimBufferGetLine(curbuf, 3));
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "This is the first line of a test file") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 2), "after first line") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 3), "This is the second line of a test file") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 4);
}
MU_TEST(test_replace_second_line_multiple_lines)
{
char_u *lines[] = {"new first line", "new second line"};
vimBufferSetLines(curbuf, 1, 1, lines, 2);
printf("LINE 1: %s\n", vimBufferGetLine(curbuf, 1));
printf("LINE 2: %s\n", vimBufferGetLine(curbuf, 2));
printf("LINE 3: %s\n", vimBufferGetLine(curbuf, 3));
printf("LINE 4: %s\n", vimBufferGetLine(curbuf, 4));
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "This is the first line of a test file") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 2), "new first line") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 3), "new second line") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 4), "This is the second line of a test file") == 0);
}
MU_TEST(test_replace_entire_buffer_from_zero)
{
char_u *lines[] = {"abc"};
vimBufferSetLines(curbuf, 0, 3, lines, 1);
mu_check(vimBufferGetLineCount(curbuf) == 1);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "abc") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 1);
}
MU_TEST(test_replace_entire_buffer_after_first_line)
{
char_u *lines[] = {"abc"};
vimBufferSetLines(curbuf, 1, 3, lines, 1);
mu_check(vimBufferGetLineCount(curbuf) == 2);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "This is the first line of a test file") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 2), "abc") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 2);
}
MU_TEST(test_replace_entire_buffer_with_more_lines)
{
char_u *lines[] = {"line1", "line2", "line3", "line4", "line5"};
vimBufferSetLines(curbuf, 0, 3, lines, 5);
mu_check(vimBufferGetLineCount(curbuf) == 5);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "line1") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 5), "line5") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 5);
}
MU_TEST(test_replace_entire_buffer_with_more_lines_again)
{
char_u *lines[] = {"line1", "line2", "line3", "line4", "line5"};
vimBufferSetLines(curbuf, 0, -1, lines, 5);
mu_check(vimBufferGetLineCount(curbuf) == 5);
mu_check(strcmp(vimBufferGetLine(curbuf, 1), "line1") == 0);
mu_check(strcmp(vimBufferGetLine(curbuf, 5), "line5") == 0);
mu_check(vimBufferGetLineCount(curbuf) == 5);
}
MU_TEST(test_version_is_incremented)
{
int initialVersion = vimBufferGetLastChangedTick(curbuf);
char_u *lines[] = {"one"};
vimBufferSetLines(curbuf, 0, 0, lines, 1);
int afterVersion = vimBufferGetLastChangedTick(curbuf);
mu_check(afterVersion > initialVersion);
}
MU_TEST(test_modified_is_set)
{
mu_check(vimBufferGetModified(curbuf) == FALSE);
char_u *lines[] = {"one"};
vimBufferSetLines(curbuf, 0, 0, lines, 1);
mu_check(vimBufferGetModified(curbuf) == TRUE);
}
MU_TEST_SUITE(test_suite)
{
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);
MU_RUN_TEST(test_append_before_buffer);
MU_RUN_TEST(test_append_after_buffer);
MU_RUN_TEST(test_append_after_first_line);
MU_RUN_TEST(test_replace_second_line_multiple_lines);
MU_RUN_TEST(test_replace_entire_buffer_from_zero);
MU_RUN_TEST(test_replace_entire_buffer_after_first_line);
MU_RUN_TEST(test_replace_entire_buffer_with_more_lines);
MU_RUN_TEST(test_replace_entire_buffer_with_more_lines_again);
MU_RUN_TEST(test_version_is_incremented);
MU_RUN_TEST(test_modified_is_set);
}
int main(int argc, char **argv)
{
vimInit(argc, argv);
vimSetBufferUpdateCallback(&onBufferUpdate);
win_setwidth(5);
win_setheight(100);
vimBufferOpen("collateral/testfile.txt", 1, 0);
MU_RUN_SUITE(test_suite);
MU_REPORT();
MU_RETURN();
}