2
This file is part of hugin.
4
hugin is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 2 of the License, or
7
(at your option) any later version.
9
hugin is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with hugin. If not, see <http://www.gnu.org/licenses/>.
19
* @file test_makefilelib.cpp
20
* @brief Tests all features of the lib.
21
* Created on: Jul 24, 2010
22
* @author Florian Achleitner <florian.achleitner.2.6.31@gmail.com>
25
#include "char_type.h"
32
#include "VariableDef.h"
33
#include "VariableRef.h"
34
#include "MakefileItem.h"
36
#include "AutoVariable.h"
39
#include "Conditional.h"
40
#include "StringAdapter.h"
43
#include "test_util.h"
45
#include <boost/iostreams/stream.hpp>
46
#include <boost/iostreams/device/file_descriptor.hpp>
48
using namespace makefile;
49
namespace fs = boost::filesystem;
50
namespace io = boost::iostreams;
52
namespace makefile { namespace tester {
55
ostream& cout = std::wcout;
56
ostream& cerr = std::wcerr;
58
ostream& cout = std::cout;
59
ostream& cerr = std::cerr;
62
struct TestComment : public Test
68
Comment* comment = mgr.own(new Comment(cstr("First line")));
69
comment->appendLine(cstr("second line"));
70
comment->appendLine(cstr("third line\nfourth\r line"));
75
struct TestRule : public Test
79
:Test("Rule", "cp 1.in 1.out\ncp 2.in 2.out\nHello Make\n")
81
Rule& rule = *mgr.own(new Rule());
82
Rule& rule2 = *mgr.own(new Rule());
83
rule.addTarget(cstr("all"));
84
rule.addPrereq(cstr("1.out"));
85
rule.addPrereq(cstr("2.out"));
86
rule.addCommand(cstr("@echo Hello Make"));
89
rule2.addTarget(cstr("%.out"));
90
rule2.addPrereq(cstr("%.in"));
91
rule2.addCommand(cstr("cp $*.in $@"));
94
std::ofstream in1("1.in"); in1.close();
95
std::ofstream in2("2.in"); in2.close();
100
return fs::exists("1.out") && fs::exists("2.out");
104
fs::remove("1.in"); fs::remove("2.in"); fs::remove("1.out"); fs::remove("2.out");
108
struct TestConditional : public Test
120
Variable& var1 = *mgr.own(new Variable(cstr("VAR1"), cstr("equal")));
121
Variable& var2 = *mgr.own(new Variable(cstr("VAR2"), cstr("equal")));
122
Variable& var3 = *mgr.own(new Variable(cstr("VAR3"), cstr("nequal")));
123
Variable& ifvar1 = *mgr.own(new Variable(cstr("TESTVAR1"), cstr("cond1 is true")));
124
Variable& ifvar2 = *mgr.own(new Variable(cstr("TESTVAR2"), cstr("cond2 is true")));
125
Variable& ifvar3 = *mgr.own(new Variable(cstr("TESTVAR3"), cstr("cond3 is true")));
126
Variable& ifvar4 = *mgr.own(new Variable(cstr("TESTVAR4"), cstr("cond4 is true")));
127
Variable& elsevar1 = *mgr.own(new Variable(ifvar1.getName(), cstr("cond1 is false")));
128
Variable& elsevar2 = *mgr.own(new Variable(ifvar2.getName(), cstr("cond2 is false")));
129
Variable& elsevar3 = *mgr.own(new Variable(ifvar3.getName(), cstr("cond3 is false")));
130
Variable& elsevar4 = *mgr.own(new Variable(ifvar4.getName(), cstr("cond4 is false")));
131
ConditionalDEF& cdef1 = *mgr.own(new ConditionalDEF(var1.getName()));
132
ConditionalDEF& cdef2 = *mgr.own(new ConditionalDEF(cstr("THISISNOTDEFINED")));
133
ConditionalEQ& ceq1 = *mgr.own(new ConditionalEQ(var1.getRef(), var2.getRef()));
134
ConditionalEQ& ceq2 = *mgr.own(new ConditionalEQ(var1.getRef(), var3.getRef()));
135
Rule& rule = *mgr.own(new Rule());
140
rule.addTarget(cstr("all"));
141
rule.addCommand(cstr("@echo Results:"));
142
rule.addCommand(cstr("@echo ") + ifvar1.getRef());
143
rule.addCommand(cstr("@echo ") + ifvar2.getRef());
144
rule.addCommand(cstr("@echo ") + ifvar3.getRef());
145
rule.addCommand(cstr("@echo ") + ifvar4.getRef());
147
cdef1.addToIf(ifvar1.getDef());
148
cdef1.addToElse(elsevar1.getDef());
149
cdef2.addToIf(ifvar2.getDef());
150
cdef2.addToElse(elsevar2.getDef());
152
ceq1.addToIf(ifvar3.getDef());
153
ceq1.addToElse(elsevar3.getDef());
154
ceq2.addToIf(ifvar4.getDef());
155
ceq2.addToElse(elsevar4.getDef());
163
// ofstream mf("test.mk");
164
// Makefile::getSingleton().writeMakefile(mf);
168
struct TestVariable : public Test
171
const std::vector<string> values;
176
"a single word__another word__value3\n")
178
Variable* v1 = mgr.own(new Variable(cstr("VAR1"), cstr("a single value")));
179
Variable* v2 = mgr.own(new Variable(cstr("VAR2"), 3.14592));
180
std::vector<string> values;
181
values.push_back("a single word");
182
values.push_back("another word");
183
values.push_back("value3");
184
Variable* v3 = mgr.own(new Variable(cstr("VAR3"), values.begin(), values.end(),
185
Makefile::SHELL, cstr("__")));
187
v1->getDef().add(); v2->getDef().add(); v3->getDef().add();
189
Rule* rule = mgr.own_add(new Rule());
190
rule->addTarget(cstr("all"));
191
rule->addCommand(cstr("@echo ") + v1->getRef());
192
rule->addCommand(cstr("@echo ") + v2->getRef());
193
rule->addCommand(cstr("@echo ") + v3->getRef());
197
void do_test(bool& result, Test* test)
199
result &= test->run();
203
using namespace makefile::tester;
205
int main(int argc, char *argv[])
208
do_test(result, new TestComment);
209
do_test(result, new TestRule);
210
do_test(result, new TestConditional);
211
do_test(result, new TestVariable);
212
return !result; // return 0 on success (= !true)