test_makefilelib.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of hugin.
00003 
00004 hugin is free software: you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation, either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 hugin is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with hugin.  If not, see <http://www.gnu.org/licenses/>.
00016 */
00017 
00025 #include "char_type.h"
00026 #include <iostream>
00027 #include <stdexcept>
00028 #include <cstdio>
00029 #include <fstream>
00030 #include "Comment.h"
00031 #include "Variable.h"
00032 #include "VariableDef.h"
00033 #include "VariableRef.h"
00034 #include "MakefileItem.h"
00035 #include "Makefile.h"
00036 #include "AutoVariable.h"
00037 #include "Newline.h"
00038 #include "Rule.h"
00039 #include "Conditional.h"
00040 #include "StringAdapter.h"
00041 #include "Manager.h"
00042 
00043 #include "test_util.h"
00044 
00045 #include <boost/iostreams/stream.hpp>
00046 #include <boost/iostreams/device/file_descriptor.hpp>
00047 
00048 using namespace makefile;
00049 namespace fs = boost::filesystem;
00050 namespace io = boost::iostreams;
00051 
00052 namespace makefile { namespace tester {
00053 
00054 #ifdef USE_WCHAR
00055 ostream& cout =  std::wcout;
00056 ostream& cerr =  std::wcerr;
00057 #else
00058 ostream& cout =  std::cout;
00059 ostream& cerr =  std::cerr;
00060 #endif
00061 
00062 struct TestComment : public Test
00063 {
00064         Manager mgr;
00065         TestComment()
00066         :Test("Comment", "")
00067         {
00068                 Comment* comment = mgr.own(new Comment(cstr("First line")));
00069                 comment->appendLine(cstr("second line"));
00070                 comment->appendLine(cstr("third line\nfourth\r line"));
00071                 comment->add();
00072         }
00073 };
00074 
00075 struct TestRule : public Test
00076 {
00077         Manager mgr;
00078         TestRule()
00079         :Test("Rule", "cp 1.in 1.out\ncp 2.in 2.out\nHello Make\n")
00080         {
00081                 Rule& rule = *mgr.own(new Rule());
00082                 Rule& rule2 = *mgr.own(new Rule());
00083                 rule.addTarget(cstr("all"));
00084                 rule.addPrereq(cstr("1.out"));
00085                 rule.addPrereq(cstr("2.out"));
00086                 rule.addCommand(cstr("@echo Hello Make"));
00087                 rule.add();
00088 
00089                 rule2.addTarget(cstr("%.out"));
00090                 rule2.addPrereq(cstr("%.in"));
00091                 rule2.addCommand(cstr("cp $*.in $@"));
00092                 rule2.add();
00093 
00094                 std::ofstream in1("1.in"); in1.close();
00095                 std::ofstream in2("2.in"); in2.close();
00096         }
00097 
00098         bool precond()
00099         {
00100                 return fs::exists("1.out") && fs::exists("2.out");
00101         }
00102         ~TestRule()
00103         {
00104                 fs::remove("1.in"); fs::remove("2.in"); fs::remove("1.out"); fs::remove("2.out");
00105         }
00106 };
00107 
00108 struct TestConditional : public Test
00109 {
00110         Manager mgr;
00111 
00112         TestConditional()
00113         :Test("Conditional",
00114                         "Results:\n"
00115                         "cond1 is true\n"
00116                         "cond2 is false\n"
00117                         "cond3 is true\n"
00118                         "cond4 is false\n")
00119         {
00120                 Variable& var1 = *mgr.own(new Variable(cstr("VAR1"), cstr("equal")));
00121                 Variable& var2 = *mgr.own(new Variable(cstr("VAR2"), cstr("equal")));
00122                 Variable& var3 = *mgr.own(new Variable(cstr("VAR3"), cstr("nequal")));
00123                 Variable& ifvar1 = *mgr.own(new Variable(cstr("TESTVAR1"), cstr("cond1 is true")));
00124                 Variable& ifvar2 = *mgr.own(new Variable(cstr("TESTVAR2"), cstr("cond2 is true")));
00125                 Variable& ifvar3 = *mgr.own(new Variable(cstr("TESTVAR3"), cstr("cond3 is true")));
00126                 Variable& ifvar4 = *mgr.own(new Variable(cstr("TESTVAR4"), cstr("cond4 is true")));
00127                 Variable& elsevar1 = *mgr.own(new Variable(ifvar1.getName(), cstr("cond1 is false")));
00128                 Variable& elsevar2 = *mgr.own(new Variable(ifvar2.getName(), cstr("cond2 is false")));
00129                 Variable& elsevar3 = *mgr.own(new Variable(ifvar3.getName(), cstr("cond3 is false")));
00130                 Variable& elsevar4 = *mgr.own(new Variable(ifvar4.getName(), cstr("cond4 is false")));
00131                 ConditionalDEF& cdef1 = *mgr.own(new ConditionalDEF(var1.getName()));
00132                 ConditionalDEF& cdef2 = *mgr.own(new ConditionalDEF(cstr("THISISNOTDEFINED")));
00133                 ConditionalEQ& ceq1 = *mgr.own(new ConditionalEQ(var1.getRef(), var2.getRef()));
00134                 ConditionalEQ& ceq2 = *mgr.own(new ConditionalEQ(var1.getRef(), var3.getRef()));
00135                 Rule& rule = *mgr.own(new Rule());
00136 
00137                 var1.getDef().add();
00138                 var2.getDef().add();
00139                 var3.getDef().add();
00140                 rule.addTarget(cstr("all"));
00141                 rule.addCommand(cstr("@echo Results:"));
00142                 rule.addCommand(cstr("@echo ") + ifvar1.getRef());
00143                 rule.addCommand(cstr("@echo ") + ifvar2.getRef());
00144                 rule.addCommand(cstr("@echo ") + ifvar3.getRef());
00145                 rule.addCommand(cstr("@echo ") + ifvar4.getRef());
00146 
00147                 cdef1.addToIf(ifvar1.getDef());
00148                 cdef1.addToElse(elsevar1.getDef());
00149                 cdef2.addToIf(ifvar2.getDef());
00150                 cdef2.addToElse(elsevar2.getDef());
00151 
00152                 ceq1.addToIf(ifvar3.getDef());
00153                 ceq1.addToElse(elsevar3.getDef());
00154                 ceq2.addToIf(ifvar4.getDef());
00155                 ceq2.addToElse(elsevar4.getDef());
00156 
00157                 cdef1.add();
00158                 cdef2.add();
00159                 ceq1.add();
00160                 ceq2.add();
00161                 rule.add();
00162 
00163 //              ofstream mf("test.mk");
00164 //              Makefile::getSingleton().writeMakefile(mf);
00165         }
00166 };
00167 
00168 struct TestVariable : public Test
00169 {
00170         Manager mgr;
00171         const std::vector<string> values;
00172         TestVariable()
00173         : Test("Variable",
00174                 "a single value\n"
00175                 "3.14592\n"
00176                 "a single word__another word__value3\n")
00177         {
00178                 Variable* v1 = mgr.own(new Variable(cstr("VAR1"), cstr("a single value")));
00179                 Variable* v2 = mgr.own(new Variable(cstr("VAR2"), 3.14592));
00180                 std::vector<string> values;
00181                 values.push_back("a single word");
00182                 values.push_back("another word");
00183                 values.push_back("value3");
00184                 Variable* v3 = mgr.own(new Variable(cstr("VAR3"), values.begin(), values.end(),
00185                                 Makefile::SHELL, cstr("__")));
00186 
00187                 v1->getDef().add(); v2->getDef().add(); v3->getDef().add();
00188 
00189                 Rule* rule = mgr.own_add(new Rule());
00190                 rule->addTarget(cstr("all"));
00191                 rule->addCommand(cstr("@echo ") + v1->getRef());
00192                 rule->addCommand(cstr("@echo ") + v2->getRef());
00193                 rule->addCommand(cstr("@echo ") + v3->getRef());
00194         }
00195 };
00196 
00197 void do_test(bool& result, Test* test)
00198 {
00199         result &= test->run();
00200         delete test;
00201 }
00202 }} // namespace
00203 using namespace makefile::tester;
00204 
00205 int main(int argc, char *argv[])
00206 {
00207         bool result = true;
00208         do_test(result, new TestComment);
00209         do_test(result, new TestRule);
00210         do_test(result, new TestConditional);
00211         do_test(result, new TestVariable);
00212         return !result;         // return 0 on success (= !true)
00213 }
00214 
00215 

Generated on Wed Jul 30 01:25:44 2014 for Hugintrunk by  doxygen 1.3.9.1