瀏覽代碼

Tests enhancement

Yann Weber 8 年之前
父節點
當前提交
c46b166b0e
共有 3 個文件被更改,包括 128 次插入17 次删除
  1. 5
    5
      tests/Makefile.am
  2. 123
    9
      tests/ttail_argparse_check.c
  3. 0
    3
      tests/ttail_init_check.h

+ 5
- 5
tests/Makefile.am 查看文件

1
-TESTS = ttail_init_check
2
-check_PROGRAMS = ttail_init_check
1
+TESTS = ttail_argparse_check
2
+check_PROGRAMS = ttail_argparse_check
3
 
3
 
4
-ttail_init_check_SOURCES = ttail_init_check.c
5
-ttail_init_check_CFLAGS = @CHECK_CFLAGS@
6
-ttail_init_check_LDADD =  $(top_builddir)/src/libttail.a @CHECK_LIBS@
4
+ttail_argparse_check_SOURCES = ttail_argparse_check.c
5
+ttail_argparse_check_CFLAGS = @CHECK_CFLAGS@
6
+ttail_argparse_check_LDADD =  $(top_builddir)/src/libttail.a @CHECK_LIBS@

+ 123
- 9
tests/ttail_argparse_check.c 查看文件

1
 #include <check.h>
1
 #include <check.h>
2
+#include <errno.h>
3
+#include <stdio.h>
4
+#include <unistd.h>
2
 
5
 
3
-#include "../src/include/ttail.h"
6
+
7
+#include "ttail.h"
4
 
8
 
5
 ttail_t *ttail;
9
 ttail_t *ttail;
10
+#define __Fname_sz 5
11
+#define FNAME_NO_EXIST 0
12
+#define FNAME_EXIST 1
13
+char *fname[__Fname_sz];
6
 
14
 
7
 void teardown_ttail(void)
15
 void teardown_ttail(void)
8
 {
16
 {
56
  * file argument parsing
64
  * file argument parsing
57
  */
65
  */
58
 
66
 
59
-START_TEST (test_init_file_non_exist)
67
+void setup_fname(void)
68
+{
69
+	int i;
70
+	FILE *fp;
71
+	for(i=0;i<__Fname_sz;i++)
72
+	{
73
+		fname[i] = NULL;
74
+	}
75
+
76
+	for(i=0; i<__Fname_sz; i++)
77
+	{
78
+		fname[i] = tempnam(NULL, "ttail_check");
79
+		if(i%2)
80
+		{
81
+			if((fp=fopen(fname[i], "w+")) == NULL)
82
+			{
83
+				perror("Unable to create file for testing");
84
+				ck_abort_msg("Unable to create file for testing");
85
+			} else { printf("Created : %s\n", fname[i]);}
86
+			if(fclose(fp))
87
+			{
88
+				perror("Unable to close file for testing");
89
+				ck_abort_msg("Unable to close file for testing");
90
+
91
+			}
92
+		}
93
+	}
94
+}
95
+
96
+void teardown_fname(void)
97
+{
98
+	int i;
99
+	for(i=0; i<__Fname_sz; i++)
100
+	{
101
+		unlink(fname[i]);
102
+		if(fname[i] != NULL)
103
+		{
104
+			free(fname[i]);
105
+		}
106
+	}
107
+}
108
+
109
+START_TEST (test_argparse_file_non_exist)
60
 {
110
 {
61
 	ttail_t *t;
111
 	ttail_t *t;
62
-	char *args[3] = {"foo", "-f", "__non__existing__"};
112
+	char *args[3] = {"foo", "-f", NULL};
63
 
113
 
114
+	args[2] = fname[FNAME_NO_EXIST];
64
 	t = ttail_init(3, args);
115
 	t = ttail_init(3, args);
65
 
116
 
66
 	ck_assert_msg(t->logfile_sz == 1,
117
 	ck_assert_msg(t->logfile_sz == 1,
75
 }
126
 }
76
 END_TEST
127
 END_TEST
77
 
128
 
129
+START_TEST (test_argparse_file_exist)
130
+{
131
+	ttail_t *t;
132
+	char *args[3] = {"foo", "-f", NULL};
133
+
134
+	args[2] = fname[FNAME_EXIST];
135
+
136
+	t = ttail_init(3, args);
137
+
138
+	ck_assert_msg(t->logfile_sz == 1,
139
+		"ttail_t.logfile_sz should be 1");
140
+	ck_assert_msg(strcmp(t->logfile_name[0], args[2]) == 0,
141
+		"ttail_t.logfile_name[0] does not contain the filename");
142
+	ck_assert_msg(t->logfile[0] != NULL,
143
+		"ttail_t.logfile[0] shouldn't be NULL");
144
+	
145
+	ttail_free(t);
146
+
147
+}
148
+END_TEST
149
+
150
+START_TEST (test_argparse_file_multiple)
151
+{
152
+	printf("MULTIPLE\n");
153
+	ttail_t *t;
154
+	int i;
155
+	char *args[11] = {"foo", "-f", NULL, "-f", NULL,
156
+		"-f", NULL, "-f", NULL, "-f", NULL};
157
+	
158
+	for(i=0; i<__Fname_sz; i++)
159
+	{
160
+		args[(i*2)+2] = fname[i];
161
+	}
162
+
163
+	t = ttail_init(11, args);
164
+	
165
+	ck_assert_msg(t->logfile_sz == __Fname_sz,
166
+		"ttail_t.logfile_sz doesn't have the good value");
167
+
168
+	for(i=0;i<__Fname_sz; i++)
169
+	{
170
+		ck_assert_msg(strcmp(t->logfile_name[i], args[(i*2)+2]) == 0,
171
+			"some filename corrupted in ttail_t.logfile_name");
172
+		if(i%2)
173
+		{
174
+			ck_assert_msg(t->logfile[i] != NULL,
175
+				"logfile not opened");
176
+		} else {
177
+			ck_assert_msg(t->logfile[i] == NULL,
178
+				"logfile should be NULL");
179
+
180
+		}
181
+	}
182
+
183
+	
184
+}
185
+END_TEST
186
+
78
 Suite * ttail_init_suite(void)
187
 Suite * ttail_init_suite(void)
79
 {
188
 {
80
 	Suite *s;
189
 	Suite *s;
81
 	TCase *tc_argparse_empty;
190
 	TCase *tc_argparse_empty;
82
-	TCase *tc_init_files;
191
+	TCase *tc_argparse_files;
83
 
192
 
84
-	s = suite_create("ttail init methods tests");
193
+	s = suite_create("ttail argument parsing checks");
85
 
194
 
86
-	tc_argparse_empty = tcase_create("Empty arguments parsing");
195
+	tc_argparse_empty = tcase_create("empty arguments parsing");
87
 	tcase_add_checked_fixture(tc_argparse_empty,
196
 	tcase_add_checked_fixture(tc_argparse_empty,
88
 		setup_ttail_empty, teardown_ttail);
197
 		setup_ttail_empty, teardown_ttail);
89
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilename);
198
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_logfilename);
93
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
202
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
94
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
203
 	tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
95
 
204
 
96
-	tc_init_files = tcase_create("files options handling");
97
-	tcase_add_test(tc_init_files, test_init_file_non_exist);
205
+	tc_argparse_files = tcase_create("files options parse");
206
+	tcase_add_checked_fixture(tc_argparse_files,
207
+		setup_fname, teardown_fname);
208
+	tcase_add_test(tc_argparse_files, test_argparse_file_non_exist);
209
+	tcase_add_test(tc_argparse_files, test_argparse_file_exist);
210
+	tcase_add_test(tc_argparse_files, test_argparse_file_multiple);
98
 
211
 
99
-	suite_add_tcase(s, tc_init_files);
212
+	suite_add_tcase(s, tc_argparse_files);
100
 	suite_add_tcase(s, tc_argparse_empty);
213
 	suite_add_tcase(s, tc_argparse_empty);
101
 	return s;
214
 	return s;
102
 }
215
 }
107
 	SRunner *sr;
220
 	SRunner *sr;
108
 
221
 
109
 	sr = srunner_create(ttail_init_suite());
222
 	sr = srunner_create(ttail_init_suite());
223
+	srunner_set_fork_status(sr, CK_FORK);
110
 
224
 
111
 	srunner_run_all(sr,CK_VERBOSE);
225
 	srunner_run_all(sr,CK_VERBOSE);
112
 	number_failed = srunner_ntests_failed(sr);
226
 	number_failed = srunner_ntests_failed(sr);

+ 0
- 3
tests/ttail_init_check.h 查看文件

1
-#include <check.h>
2
-
3
-Suite * ttail_init_suite(void);

Loading…
取消
儲存