Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Dec 16, 2023
1 parent d65e056 commit dc57ef2
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/Cmd/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ struct BuildConfig {
for (const auto& var : sortedVars) {
os << var << " = " << variables.at(var) << '\n';
}
if (!sortedVars.empty()) {

if (!sortedVars.empty() && !targets.empty()) {
os << '\n';
}

Expand Down Expand Up @@ -231,6 +232,7 @@ void test_cycle_vars() {
std::stringstream ss;
config.emitMakefile(ss);
} catch (const std::runtime_error& e) {
assert(std::string(e.what()) == "too complex build graph");
return;
}

Expand All @@ -248,7 +250,34 @@ void test_simple_vars() {

assert(ss.str() == "a = 1\n"
"b = 2\n"
"c = 3\n\n");
"c = 3\n");
}

void test_depend_on_unregistered_var() {
BuildConfig config;
config.defineVariable("a", "1", {"b"});

std::stringstream ss;
config.emitMakefile(ss);

assert(ss.str() == "a = 1\n");
}

void test_cycle_targets() {
BuildConfig config;
config.defineTarget("a", {"echo a"}, {"b"});
config.defineTarget("b", {"echo b"}, {"c"});
config.defineTarget("c", {"echo c"}, {"a"});

try {
std::stringstream ss;
config.emitMakefile(ss);
} catch (const std::runtime_error& e) {
assert(std::string(e.what()) == "too complex build graph");
return;
}

assert(false && "should not reach here");
}

void test_simple_targets() {
Expand All @@ -271,9 +300,24 @@ void test_simple_targets() {
"\n");
}

void test_depend_on_unregistered_target() {
BuildConfig config;
config.defineTarget("a", {"echo a"}, {"b"});

std::stringstream ss;
config.emitMakefile(ss);

assert(ss.str() == "a: b\n"
"\techo a\n"
"\n");
}

int main() {
test_cycle_vars();
test_simple_vars();
test_depend_on_unregistered_var();
test_cycle_targets();
test_simple_targets();
test_depend_on_unregistered_target();
}
#endif

0 comments on commit dc57ef2

Please sign in to comment.