Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmTestGenerator.cxx
blobb062e824acb89a5e1b65ce92282b1b1ba60ae2f2
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTestGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-16 14:51:23 $
7 Version: $Revision: 1.3 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmTestGenerator.h"
19 #include "cmLocalGenerator.h"
20 #include "cmMakefile.h"
21 #include "cmSystemTools.h"
22 #include "cmTarget.h"
23 #include "cmTest.h"
25 //----------------------------------------------------------------------------
26 cmTestGenerator
27 ::cmTestGenerator(cmTest* test,
28 std::vector<std::string> const& configurations):
29 cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations),
30 Test(test)
32 this->ActionsPerConfig = !test->GetOldStyle();
33 this->TestGenerated = false;
36 //----------------------------------------------------------------------------
37 cmTestGenerator
38 ::~cmTestGenerator()
42 //----------------------------------------------------------------------------
43 void cmTestGenerator::GenerateScriptConfigs(std::ostream& os,
44 Indent const& indent)
46 // First create the tests.
47 this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
49 // Now generate the test properties.
50 if(this->TestGenerated)
52 cmTest* test = this->Test;
53 std::ostream& fout = os;
54 cmPropertyMap::const_iterator pit;
55 cmPropertyMap* mpit = &test->GetProperties();
56 if ( mpit->size() )
58 fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
59 for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
61 fout << " " << pit->first.c_str() << " \"";
62 const char* value = pit->second.GetValue();
63 for ( ; *value; ++ value )
65 switch ( *value )
67 case '\\':
68 case '"':
69 case ' ':
70 case '#':
71 case '(':
72 case ')':
73 case '$':
74 case '^':
75 fout << "\\" << *value;
76 break;
77 case '\t':
78 fout << "\\t";
79 break;
80 case '\n':
81 fout << "\\n";
82 break;
83 case '\r':
84 fout << "\\r";
85 break;
86 default:
87 fout << *value;
90 fout << "\"";
92 fout << ")" << std::endl;
97 //----------------------------------------------------------------------------
98 void cmTestGenerator::GenerateScriptActions(std::ostream& os,
99 Indent const& indent)
101 if(this->ActionsPerConfig)
103 // This is the per-config generation in a single-configuration
104 // build generator case. The superclass will call our per-config
105 // method.
106 this->cmScriptGenerator::GenerateScriptActions(os, indent);
108 else
110 // This is an old-style test, so there is only one config.
111 //assert(this->Test->GetOldStyle());
112 this->GenerateOldStyle(os, indent);
116 //----------------------------------------------------------------------------
117 void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
118 const char* config,
119 Indent const& indent)
121 this->TestGenerated = true;
123 // Start the test command.
124 os << indent << "ADD_TEST(" << this->Test->GetName() << " ";
126 // Get the test command line to be executed.
127 std::vector<std::string> const& command = this->Test->GetCommand();
129 // Check whether the command executable is a target whose name is to
130 // be translated.
131 std::string exe = command[0];
132 cmMakefile* mf = this->Test->GetMakefile();
133 cmTarget* target = mf->FindTargetToUse(exe.c_str());
134 if(target && target->GetType() == cmTarget::EXECUTABLE)
136 // Use the target file on disk.
137 exe = target->GetFullPath(config);
139 else
141 // Use the command name given.
142 cmSystemTools::ConvertToUnixSlashes(exe);
145 // Generate the command line with full escapes.
146 cmLocalGenerator* lg = mf->GetLocalGenerator();
147 os << lg->EscapeForCMake(exe.c_str());
148 for(std::vector<std::string>::const_iterator ci = command.begin()+1;
149 ci != command.end(); ++ci)
151 os << " " << lg->EscapeForCMake(ci->c_str());
154 // Finish the test command.
155 os << ")\n";
158 //----------------------------------------------------------------------------
159 void cmTestGenerator::GenerateOldStyle(std::ostream& fout,
160 Indent const& indent)
162 this->TestGenerated = true;
164 // Get the test command line to be executed.
165 std::vector<std::string> const& command = this->Test->GetCommand();
167 std::string exe = command[0];
168 cmSystemTools::ConvertToUnixSlashes(exe);
169 fout << indent;
170 fout << "ADD_TEST(";
171 fout << this->Test->GetName() << " \"" << exe << "\"";
173 for(std::vector<std::string>::const_iterator argit = command.begin()+1;
174 argit != command.end(); ++argit)
176 // Just double-quote all arguments so they are re-parsed
177 // correctly by the test system.
178 fout << " \"";
179 for(std::string::const_iterator c = argit->begin();
180 c != argit->end(); ++c)
182 // Escape quotes within arguments. We should escape
183 // backslashes too but we cannot because it makes the result
184 // inconsistent with previous behavior of this command.
185 if((*c == '"'))
187 fout << '\\';
189 fout << *c;
191 fout << "\"";
193 fout << ")" << std::endl;