Skip to content

Commit

Permalink
tests: add coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masscry committed May 2, 2024
1 parent 8833975 commit 26ee1f0
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 4 deletions.
48 changes: 44 additions & 4 deletions tests/test_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST_F(ApplicationTest, RunReadFromZero) {
for (auto c : data) {
sum += c;
}
co_await std::move(file).close();
co_await file.close();
co_return sum;
});
EXPECT_EQ(result, 0);
Expand All @@ -54,7 +54,7 @@ TEST_F(ApplicationTest, RunWriteToNull) {
c = 42;
}
co_await file.write(0, data);
co_await std::move(file).close();
co_await file.close();
co_return 0;
});
EXPECT_EQ(result, 0);
Expand All @@ -66,7 +66,7 @@ TEST_F(ApplicationTest, RunReadFromNonExistent) {
auto file = co_await corey::File::open("/nonexistent", O_RDONLY);
std::array<char, 100> data;
co_await file.read(0, data);
co_await std::move(file).close();
co_await file.close();
co_return 0;
});
} catch (const std::system_error& e) {
Expand All @@ -86,11 +86,51 @@ TEST_F(ApplicationTest, RunWriteToReadOnly) {
} catch (const std::system_error& e) {
EXPECT_EQ(e.code().value(), EBADF);
}
co_await std::move(file).close();
co_await file.close();
co_return 0;
});
}

TEST_F(ApplicationTest, RunFileFsync) {
auto result = app.run([](const corey::ParseResult&) -> corey::Future<int> {
auto file = co_await corey::File::open("/dev/zero", O_RDONLY);
try {
co_await file.fsync();
} catch (const std::system_error& e) {
EXPECT_EQ(e.code().value(), EINVAL);
}
co_await file.close();
co_return 0;
});
EXPECT_EQ(result, 0);
}

TEST_F(ApplicationTest, RunFileFdatasync) {
auto result = app.run([](const corey::ParseResult&) -> corey::Future<int> {
auto file = co_await corey::File::open("/dev/zero", O_RDONLY);
try {
co_await file.fdatasync();
} catch (const std::system_error& e) {
EXPECT_EQ(e.code().value(), EINVAL);
}
co_await file.close();
co_return 0;
});
EXPECT_EQ(result, 0);
}

TEST_F(ApplicationTest, RunFileMoveAssignmentOperator) {
auto result = app.run([](const corey::ParseResult&) -> corey::Future<int> {
auto file = co_await corey::File::open("/dev/zero", O_RDONLY);
corey::File file2;

file2 = std::move(file);
co_await file2.close();
co_return 0;
});
EXPECT_EQ(result, 0);
}

TEST_F(ApplicationTest, RunYield) {
auto result = app.run([](const corey::ParseResult&) -> corey::Future<int> {
co_await corey::yield();
Expand Down
32 changes: 32 additions & 0 deletions tests/test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,38 @@ TEST(LogTest, Simple) {
test.info("hello!");
}

TEST(LogTest, LogLevelTextFormatting) {
auto debug = fmt::format("{}", corey::Log::Level::debug);
EXPECT_EQ(debug, "debug");

auto info = fmt::format("{}", corey::Log::Level::info);
EXPECT_EQ(info, "info");

auto warn = fmt::format("{}", corey::Log::Level::warn);
EXPECT_EQ(warn, "warn");

auto error = fmt::format("{}", corey::Log::Level::error);
EXPECT_EQ(error, "error");
}

TEST(LogTest, LogConstructor) {
corey::Log test("test");
EXPECT_EQ(test.get_name(), "test");
EXPECT_EQ(test.get_level(), corey::Log::Level::info);

corey::Log test1("test1", corey::Log::Level::warn);
EXPECT_EQ(test1.get_name(), "test1");
EXPECT_EQ(test1.get_level(), corey::Log::Level::warn);

corey::Log test2("test2", corey::Sink<void>::make<MockConsole>());
EXPECT_EQ(test2.get_name(), "test2");
EXPECT_EQ(test2.get_level(), corey::Log::Level::info);

corey::Log test3("test3", corey::Log::Level::error, corey::Sink<void>::make<MockConsole>());
EXPECT_EQ(test3.get_name(), "test3");
EXPECT_EQ(test3.get_level(), corey::Log::Level::error);
}

TEST(LogTest, Assert) {
EXPECT_DEATH({ corey::panic("at the disco"); }, ".*");
EXPECT_DEATH({ std::ignore = COREY_ASSERT(false); }, ".*");
Expand Down
69 changes: 69 additions & 0 deletions tests/test_future_promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
#include <stdexcept>
#include <tuple>

TEST(PromiseFutureTest, CheckFutureExceptionWhatText) {
auto broken_promise = corey::BrokenPromise();
EXPECT_STREQ(broken_promise.what(), "broken promise");

auto already_retrived = corey::FutureAlreadyRetrived();
EXPECT_STREQ(already_retrived.what(), "already retrived");

auto already_satisfied = corey::PromiseAlreadySatisfied();
EXPECT_STREQ(already_satisfied.what(), "already satisfied");

auto future_not_ready = corey::FutureNotReady();
EXPECT_STREQ(future_not_ready.what(), "not ready");
}

TEST(PromiseFutureTest, PromiseFutureSet) {
corey::Promise<int> test;

Expand Down Expand Up @@ -57,6 +71,28 @@ TEST(PromiseFutureTest, BrokenPromiseFuture) {
}, corey::BrokenPromise);
}

TEST(PromiseFutureTest, GetNonReadyFuture) {
corey::Promise<int> test;

auto fut = test.get_future();

EXPECT_FALSE(fut.is_ready());
EXPECT_FALSE(fut.has_failed());

EXPECT_THROW({ std::ignore = fut.get(); }, corey::FutureNotReady);
}

TEST(PromiseFutureTest, GetNonReadyFutureVoid) {
corey::Promise<> test;

auto fut = test.get_future();

EXPECT_FALSE(fut.is_ready());
EXPECT_FALSE(fut.has_failed());

EXPECT_THROW({ fut.get(); }, corey::FutureNotReady);
}

TEST(PromiseFutureTest, PromiseSetExceptionFuture) {
corey::Promise<int> test;

Expand Down Expand Up @@ -234,3 +270,36 @@ TEST(PromiseFutureTest, PromiseSetNonCopyableClassException) {

EXPECT_THROW({ std::ignore = fut.get(); }, std::runtime_error);
}

TEST(PromiseFutureTest, MakeExceptionFuture) {
auto fut = corey::make_exception_future<int>(std::make_exception_ptr(std::runtime_error("test")));

EXPECT_TRUE(fut.is_ready());
EXPECT_TRUE(fut.has_failed());

EXPECT_THROW({ std::ignore = fut.get(); }, std::runtime_error);
}

TEST(PromiseFutureTest, MakeExceptionFutureVoid) {
auto fut = corey::make_exception_future<>(std::make_exception_ptr(std::runtime_error("test")));

EXPECT_TRUE(fut.is_ready());
EXPECT_TRUE(fut.has_failed());

EXPECT_THROW({ fut.get(); }, std::runtime_error);
}

TEST(PromiseFutureTest, MakeFutureVoidMove) {

auto fut = corey::make_ready_future<>();
EXPECT_TRUE(fut.is_ready());
EXPECT_FALSE(fut.has_failed());

auto fut2 = corey::make_exception_future<>(std::make_exception_ptr(std::runtime_error("test")));
EXPECT_TRUE(fut2.is_ready());
EXPECT_TRUE(fut2.has_failed());

fut2 = std::move(fut);

EXPECT_NO_THROW({ fut2.get(); });
}
10 changes: 10 additions & 0 deletions tests/test_reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,13 @@ TEST_F(ReactorIOTest, IoEngineOpenReadOnly) {
close(fd);
}

TEST(Reactor, ReactorTwiceInit) {
corey::Reactor reactor;
EXPECT_DEATH({ corey::Reactor reactor2; }, ".*");
}

TEST(Reactor, IoEngineTwiceInit) {
corey::Reactor reactor;
corey::IoEngine io(reactor);
EXPECT_DEATH({ corey::IoEngine io2(reactor); }, ".*");
}

0 comments on commit 26ee1f0

Please sign in to comment.