-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathyank.c
187 lines (148 loc) · 3.67 KB
/
yank.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
186
187
#include "libvim.h"
#include "minunit.h"
#include "vim.h"
static int yankCount = 0;
static int lastYankLineCount = -1;
static int lastRegname = 0;
static char **lastYankLines = NULL;
static pos_T lastStart;
static pos_T lastEnd;
static int lastYankType = -1;
static int lastOpChar = -1;
void disposeLastYankInfo(void)
{
if (lastYankLines != NULL)
{
for (int i = 0; i < lastYankLineCount; i++)
{
vim_free(lastYankLines[i]);
}
vim_free(lastYankLines);
}
lastYankLineCount = -1;
lastYankLines = NULL;
lastRegname = 0;
lastOpChar = 0;
}
void onYank(yankInfo_T *yankInfo)
{
disposeLastYankInfo();
lastYankLineCount = yankInfo->numLines;
lastYankLines = alloc(sizeof(char_u *) * yankInfo->numLines);
lastStart = yankInfo->start;
lastEnd = yankInfo->end;
lastYankType = yankInfo->blockType;
lastOpChar = yankInfo->op_char;
lastRegname = yankInfo->regname;
for (int i = 0; i < lastYankLineCount; i++)
{
int len = strlen(yankInfo->lines[i]);
char_u *sz = alloc(sizeof(char_u) * (len + 1));
strcpy(sz, yankInfo->lines[i]);
lastYankLines[i] = sz;
};
yankCount++;
};
void test_setup(void)
{
vimKey("<esc>");
vimKey("<esc>");
vimExecute("e!");
vimInput("g");
vimInput("g");
yankCount = 0;
}
void test_teardown(void)
{
disposeLastYankInfo();
}
MU_TEST(test_yank_line)
{
vimInput("y");
vimInput("y");
mu_check(yankCount == 1);
mu_check(lastYankLineCount == 1);
mu_check(lastOpChar == 'y');
mu_check(lastYankType == MLINE);
mu_check(lastRegname == 0);
mu_check(strcmp(lastYankLines[0], "This is the first line of a test file") == 0);
};
MU_TEST(test_yank_register)
{
vimInput("\"");
vimInput("c");
vimInput("y");
vimInput("y");
mu_check(yankCount == 1);
mu_check(lastYankLineCount == 1);
mu_check(lastOpChar == 'y');
mu_check(lastYankType == MLINE);
mu_check(lastRegname == 'c');
mu_check(strcmp(lastYankLines[0], "This is the first line of a test file") == 0);
};
MU_TEST(test_clipboard_registers)
{
vimInput("\"");
vimInput("+");
vimInput("y");
vimInput("y");
printf("LASTREGNAME: %c|%d\n", lastRegname, lastRegname);
mu_check(yankCount == 1);
mu_check(lastRegname == '+');
vimInput("\"");
vimInput("*");
vimInput("y");
vimInput("y");
mu_check(yankCount == 2);
mu_check(lastRegname == '*');
};
MU_TEST(test_delete_line)
{
vimInput("d");
vimInput("d");
mu_check(yankCount == 1);
mu_check(lastYankLineCount == 1);
mu_check(lastYankType == MLINE);
mu_check(strcmp(lastYankLines[0], "This is the first line of a test file") == 0);
};
MU_TEST(test_delete_two_lines)
{
vimInput("d");
vimInput("j");
mu_check(yankCount == 1);
mu_check(lastYankLineCount == 2);
mu_check(lastYankType == MLINE);
mu_check(lastOpChar == 'd');
mu_check(strcmp(lastYankLines[0], "This is the first line of a test file") == 0);
mu_check(strcmp(lastYankLines[1], "This is the second line of a test file") == 0);
};
MU_TEST(test_delete_char)
{
vimInput("x");
mu_check(yankCount == 1);
mu_check(lastYankLineCount == 1);
mu_check(lastYankType == MCHAR);
mu_check(lastOpChar == 'd');
mu_check(strcmp(lastYankLines[0], "T") == 0);
};
MU_TEST_SUITE(test_suite)
{
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);
MU_RUN_TEST(test_delete_char);
MU_RUN_TEST(test_delete_line);
MU_RUN_TEST(test_delete_two_lines);
MU_RUN_TEST(test_yank_line);
MU_RUN_TEST(test_yank_register);
MU_RUN_TEST(test_clipboard_registers);
}
int main(int argc, char **argv)
{
vimInit(argc, argv);
vimSetYankCallback(&onYank);
win_setwidth(5);
win_setheight(100);
vimBufferOpen("collateral/testfile.txt", 1, 0);
MU_RUN_SUITE(test_suite);
MU_REPORT();
return minunit_status;
}