From 28609f436966f731f91e84d10c1d7d0621b4abe8 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 5 May 2024 12:57:18 +0200 Subject: Tests --- test-ymake.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 11 deletions(-) (limited to 'test-ymake.cpp') diff --git a/test-ymake.cpp b/test-ymake.cpp index a4d9aa4..fcd424f 100644 --- a/test-ymake.cpp +++ b/test-ymake.cpp @@ -15,16 +15,16 @@ namespace { const fs::path testpath{"testdir1"}; } -class BuildTest: public ::testing::Test +class ymakeTest: public ::testing::Test { private: fs::path original_path; protected: - BuildTest() { + ymakeTest() { } - ~BuildTest() override { + ~ymakeTest() override { } void SetUp() override { @@ -45,6 +45,10 @@ protected: }; +class yscanTest: public ymakeTest +{ +}; + void create_file(const fs::path& path, const std::string& contents) { Reichwein::File::setFile(path, contents); @@ -55,19 +59,27 @@ int run_command(const std::string& command) return bp::system(command, bp::std_out > bp::null, bp::std_err > bp::null); } -int run_command(const std::string& command, std::vector& output) +int run_command(const std::string& command, std::string& output) { bp::ipstream is; bp::ipstream es; int result = bp::system(command, bp::std_out > is, bp::std_err > es); - std::string output_string(std::istreambuf_iterator(is), {}); - output_string += std::string(std::istreambuf_iterator(es), {}); - output = Reichwein::Stringhelper::split(output_string, "\r\n"); + output = std::string(std::istreambuf_iterator(is), {}); + output += std::string(std::istreambuf_iterator(es), {}); + + return result; +} +int run_command(const std::string& command, std::vector& output) +{ + std::string output_string; + int result = run_command(command, output_string); + + output = Reichwein::Stringhelper::split(output_string, "\r\n"); return result; } -TEST_F(BuildTest, build_one_cpp) +TEST_F(ymakeTest, build_one_cpp) { create_file("hello.cpp", R"(int main(int argc, char* argv[]) { @@ -90,7 +102,7 @@ TEST_F(BuildTest, build_one_cpp) EXPECT_EQ(output.size(), 2); // compile, link } -TEST_F(BuildTest, build_one_cpp_compile_error) +TEST_F(ymakeTest, build_one_cpp_compile_error) { create_file("hello.cpp", R"(int main(int argc, char* argv[]) { @@ -110,7 +122,7 @@ TEST_F(BuildTest, build_one_cpp_compile_error) EXPECT_NE(result, 0); } -TEST_F(BuildTest, clean_one_cpp) +TEST_F(ymakeTest, clean_one_cpp) { create_file("hello.cpp", R"(int main(int argc, char* argv[]) { @@ -140,7 +152,7 @@ TEST_F(BuildTest, clean_one_cpp) ASSERT_EQ(result, 0); } -TEST_F(BuildTest, YMakefile_missing) +TEST_F(ymakeTest, YMakefile_missing) { int result = run_command("../ymake"); EXPECT_NE(result, 0); @@ -149,3 +161,78 @@ TEST_F(BuildTest, YMakefile_missing) EXPECT_NE(result, 0); } +TEST_F(ymakeTest, build_three_cpp) +{ + create_file("hello.cpp", R"(int main(int argc, char* argv[]) +{ + return 0; +})"); + create_file("second.cpp", R"( +#include "second.h" +)"); + create_file("second.h", R"( +#pragma once +)"); + create_file("third.cpp", R"( +)"); + + create_file("YMakefile", R"( + + + hello + hello.cpp + second.cpp + third.cpp + + +)"); + + std::vector output; + int result = run_command("../ymake", output); + EXPECT_EQ(result, 0); + + EXPECT_EQ(output.size(), 4); // compile 3, link 1 +} + +// TODO: test .c .cc .cpp +// TODO: multiple builds +// TODO: test tests + +TEST_F(yscanTest, no_cpp_file) +{ + std::string output; + int result = run_command("../yscan", output); + EXPECT_EQ(result, 0); + + EXPECT_EQ(output, R"( + + testdir1 + + +)"); +} + +TEST_F(yscanTest, one_cpp_file) +{ + create_file("hello.cpp", R"(int main(int argc, char* argv[]) +{ + return 0; +})"); + + std::string output; + int result = run_command("../yscan", output); + EXPECT_EQ(result, 0); + + EXPECT_EQ(output, R"( + + testdir1 + hello.cpp + + +)"); +} + +// TODO: test multiple files +// TODO: test multiple builds +// TODO: test tests + -- cgit v1.2.3