Made notepad uses NLS properly.
[wine] / programs / winetest / wtmain.c
1 /*
2  * Main routine for Wine C unit tests.
3  *
4  * Copyright 2002 Alexandre Julliard
5  * Copyright 2002 Andriy Palamarchuk
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* debug level */
27 int winetest_debug = 1;
28
29 /* current platform */
30 const char *winetest_platform = "windows";
31
32 struct test
33 {
34     const char  *name;
35     void       (*func)(void);
36 };
37
38 extern const struct test winetest_testlist[];
39 static const struct test *current_test; /* test currently being run */
40
41 static int successes;         /* number of successful tests */
42 static int failures;          /* number of failures */
43 static int todo_successes;    /* number of successful tests inside todo block */
44 static int todo_failures;     /* number of failures inside todo block */
45 static int todo_level;        /* current todo nesting level */
46
47 /*
48  * Checks condition.
49  * Parameters:
50  *   - condition - condition to check;
51  *   - msg test description;
52  *   - file - test application source code file name of the check
53  *   - line - test application source code file line number of the check
54  */
55 void winetest_ok( int condition, const char *msg, const char *file, int line )
56 {
57     if (todo_level)
58     {
59         if (condition)
60         {
61             fprintf( stderr, "%s:%d: Test succeeded inside todo block", file, line );
62             if (msg && msg[0]) fprintf( stderr, ": %s", msg );
63             fputc( '\n', stderr );
64             todo_failures++;
65         }
66         else todo_successes++;
67     }
68     else
69     {
70         if (!condition)
71         {
72             fprintf( stderr, "%s:%d: Test failed", file, line );
73             if (msg && msg[0]) fprintf( stderr, ": %s", msg );
74             fputc( '\n', stderr );
75             failures++;
76         }
77         else successes++;
78     }
79 }
80
81
82 /* Find a test by name */
83 static const struct test *find_test( const char *name )
84 {
85     const struct test *test;
86     const char *p;
87     int len;
88
89     if ((p = strrchr( name, '/' ))) name = p + 1;
90     if ((p = strrchr( name, '\\' ))) name = p + 1;
91     len = strlen(name);
92     if (len > 2 && !strcmp( name + len - 2, ".c" )) len -= 2;
93
94     for (test = winetest_testlist; test->name; test++)
95     {
96         if (!strncmp( test->name, name, len ) && !test->name[len]) break;
97     }
98     return test->name ? test : NULL;
99 }
100
101
102 /* Run a named test, and return exit status */
103 static int run_test( const char *name )
104 {
105     const struct test *test;
106     int status;
107
108     if (!(test = find_test( name )))
109     {
110         fprintf( stderr, "Fatal: test '%s' does not exist.\n", name );
111         exit(1);
112     }
113     successes = failures = todo_successes = todo_failures = 0;
114     todo_level = 0;
115     current_test = test;
116     test->func();
117
118     if (winetest_debug)
119     {
120         fprintf( stderr, "%s: %d tests executed, %d marked as todo, %d %s.\n",
121                  name, successes + failures + todo_successes + todo_failures,
122                  todo_successes, failures + todo_failures,
123                  (failures + todo_failures != 1) ? "failures" : "failure" );
124     }
125     status = (failures + todo_failures < 255) ? failures + todo_failures : 255;
126     return status;
127 }
128
129
130 /* main function */
131 int main( int argc, char **argv )
132 {
133     char *p;
134
135     if ((p = getenv( "WINETEST_PLATFORM" ))) winetest_platform = p;
136     if ((p = getenv( "WINETEST_DEBUG" ))) winetest_debug = atoi(p);
137     if (!argv[1])
138     {
139         fprintf( stderr, "Usage: %s test_name\n", argv[0] );
140         exit(1);
141     }
142     exit( run_test(argv[1]) );
143 }