summaryrefslogtreecommitdiff
path: root/src/tests/elementary/elm_code_test_indent.c
blob: 9a35b7ce1f72fed9efe61cd06cf319c879db39c8 (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
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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#define ELM_INTERNAL_API_ARGESFSDFEFC

#include "elm_suite.h"
#include "Elementary.h"
#include "elm_code_indent.h"

static void
_indent_check(Elm_Code_File *file, const char *prev, const char *expected)
{
   Elm_Code_Line *line;
   char *str;

   elm_code_file_clear(file);

   elm_code_file_line_append(file, prev, strlen(prev), NULL);
   elm_code_file_line_append(file, "", 0, NULL);
   line = elm_code_file_line_get(file, 2);

   str = elm_code_line_indent_get(line);
   ck_assert_str_eq(expected, str);

   free(str);
}

START_TEST (elm_code_indent_whitespace_test)
{
   Elm_Code *code;
   Elm_Code_File *file;

   elm_init(1, NULL);
   code = elm_code_create();
   file = elm_code_file_new(code);

   _indent_check(file, "", "");
   _indent_check(file, "  ", "  ");
   _indent_check(file, "\t", "\t");
   _indent_check(file, "\t  ", "\t  ");

   elm_shutdown();
}
END_TEST

START_TEST (elm_code_indent_comments_test)
{
   Elm_Code *code;
   Elm_Code_File *file;

   elm_init(1, NULL);
   code = elm_code_create();
   file = elm_code_file_new(code);

   _indent_check(file, " /**", "  * ");
   _indent_check(file, "  * ", "  * ");
   _indent_check(file, "  */", " ");
   _indent_check(file, "\t//", "\t//");

   // test these are not comments
   _indent_check(file, " / ", " ");
   _indent_check(file, " hi//", " ");

   elm_shutdown();
}
END_TEST

START_TEST (elm_code_indent_simple_braces)
{
   Elm_Code *code;
   Elm_Code_File *file;

   elm_init(1, NULL);
   code = elm_code_create();
   file = elm_code_file_new(code);

   _indent_check(file, "if() {", "     ");
   _indent_check(file, "}", "");

   _indent_check(file, "  {", "     ");
   _indent_check(file, "  }", "");

   elm_shutdown();
}
END_TEST

START_TEST (elm_code_indent_matching_braces)
{
   Elm_Code_File *file;
   Elm_Code_Line *line;
   Elm_Code *code;
   const char *str;
   unsigned int str_len;

   elm_init(1, NULL);
   code = elm_code_create();
   file = elm_code_file_new(code);

   elm_code_file_line_append(file, "", 8, NULL);
   line = elm_code_file_line_get(file, 1);

   elm_code_file_line_insert(file, 1, "   if ()", 8, NULL);
   str = elm_code_line_indent_matching_braces_get(line, &str_len);
   ck_assert_strn_eq(str, "", str_len);

   elm_code_file_line_insert(file, 2, "     {", 6, NULL);
   str = elm_code_line_indent_matching_braces_get(line, &str_len);
   ck_assert_strn_eq(str, "     ", str_len);

   elm_code_file_line_insert(file, 3, "        if (){", 14, NULL);
   str = elm_code_line_indent_matching_braces_get(line, &str_len);
   ck_assert_strn_eq(str, "        ", str_len);

   elm_code_file_line_insert(file, 4, "        }", 9, NULL);
   str = elm_code_line_indent_matching_braces_get(line, &str_len);
   ck_assert_strn_eq(str, "     ", str_len);

   elm_code_file_line_insert(file, 5, "     }", 6, NULL);
   str = elm_code_line_indent_matching_braces_get(line, &str_len);
   ck_assert_strn_eq(str, "", str_len);

   elm_code_free(code);
   elm_shutdown();
}
END_TEST

START_TEST (elm_code_indent_startswith_keyword)
{
   Elm_Code_File *file;
   Elm_Code *code;

   elm_init(1, NULL);
   code = elm_code_create();
   file = elm_code_file_new(code);

   _indent_check(file, "if ()", "  ");
   _indent_check(file, "else", "  ");
   _indent_check(file, "else if ()", "  ");
   _indent_check(file, "for ()", "  ");
   _indent_check(file, "while ()", "  ");
   _indent_check(file, "do", "  ");
   _indent_check(file, "do {", "     ");

   _indent_check(file, "  switch ()", "    ");
   _indent_check(file, "   case a:", "     ");
   _indent_check(file, "   default:", "     ");

   _indent_check(file, "if ();", "");
   _indent_check(file, "  for ();", "  ");

   _indent_check(file, "  iffy()", "  ");
   _indent_check(file, "  fi()", "  ");
   _indent_check(file, "  elihw", "  ");

   _indent_check(file, "   if", "   ");
   _indent_check(file, "   while", "   ");

   elm_code_free(code);
   elm_shutdown();
}
END_TEST

void elm_code_test_indent(TCase *tc)
{
   tcase_add_test(tc, elm_code_indent_whitespace_test);
   tcase_add_test(tc, elm_code_indent_comments_test);
   tcase_add_test(tc, elm_code_indent_simple_braces);
   tcase_add_test(tc, elm_code_indent_matching_braces);
   tcase_add_test(tc, elm_code_indent_startswith_keyword);
}