1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "cctest.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33
34 CcTest* CcTest::last_ = NULL;
35
36
CcTest(TestFunction * callback,const char * test_file,const char * test_name,const char * test_dependency,bool test_is_enabled)37 CcTest::CcTest(TestFunction* callback, const char* test_file,
38 const char* test_name, const char* test_dependency,
39 bool test_is_enabled)
40 : callback_(callback), name_(test_name), dependency_(test_dependency),
41 prev_(last_) {
42 // Find the base name of this test (const_cast required on Windows).
43 char *basename = strrchr(const_cast<char *>(test_file), '/');
44 if (!basename) {
45 basename = strrchr(const_cast<char *>(test_file), '\\');
46 }
47 if (!basename) {
48 basename = strdup(test_file);
49 } else {
50 basename = strdup(basename + 1);
51 }
52 // Drop the extension, if there is one.
53 char *extension = strrchr(basename, '.');
54 if (extension) *extension = 0;
55 // Install this test in the list of tests
56 file_ = basename;
57 enabled_ = test_is_enabled;
58 prev_ = last_;
59 last_ = this;
60 }
61
62
PrintTestList(CcTest * current)63 static void PrintTestList(CcTest* current) {
64 if (current == NULL) return;
65 PrintTestList(current->prev());
66 if (current->dependency() != NULL) {
67 printf("%s/%s<%s\n",
68 current->file(), current->name(), current->dependency());
69 } else {
70 printf("%s/%s<\n", current->file(), current->name());
71 }
72 }
73
74
main(int argc,char * argv[])75 int main(int argc, char* argv[]) {
76 int tests_run = 0;
77 bool print_run_count = true;
78 for (int i = 1; i < argc; i++) {
79 char* arg = argv[i];
80 if (strcmp(arg, "--list") == 0) {
81 PrintTestList(CcTest::last());
82 print_run_count = false;
83
84 } else {
85 char* arg_copy = strdup(arg);
86 char* testname = strchr(arg_copy, '/');
87 if (testname) {
88 // Split the string in two by nulling the slash and then run
89 // exact matches.
90 *testname = 0;
91 char* file = arg_copy;
92 char* name = testname + 1;
93 CcTest* test = CcTest::last();
94 while (test != NULL) {
95 if (test->enabled()
96 && strcmp(test->file(), file) == 0
97 && strcmp(test->name(), name) == 0) {
98 test->Run();
99 tests_run++;
100 }
101 test = test->prev();
102 }
103
104 } else {
105 // Run all tests with the specified file or test name.
106 char* file_or_name = arg_copy;
107 CcTest* test = CcTest::last();
108 while (test != NULL) {
109 if (test->enabled()
110 && (strcmp(test->file(), file_or_name) == 0
111 || strcmp(test->name(), file_or_name) == 0)) {
112 test->Run();
113 tests_run++;
114 }
115 test = test->prev();
116 }
117 }
118 free(arg_copy);
119 }
120 }
121 if (print_run_count && tests_run != 1)
122 printf("Ran %i tests.\n", tests_run);
123 return 0;
124 }
125