Skip to content

Commit

Permalink
Add unit tests for the cd command
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryKogan committed Dec 11, 2023
1 parent a2c924c commit b572046
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/cd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "../src/FileSystem/FileSystem.hpp"
#include <filesystem>
#include <gtest/gtest.h>

class CdTest : public testing::Test {
protected:
// NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes)
std::string const PATH = "test.fs";
std::uint64_t const SIZE = 1024;
std::uint64_t const CLUSTER_SIZE = 64;

FileSystem file_system_;
// NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes)

auto SetUp() -> void override {
FileSystem::make(PATH, {SIZE, CLUSTER_SIZE});
file_system_ = FileSystem(PATH);
}

auto TearDown() -> void override { std::filesystem::remove(PATH); }
};

TEST_F(CdTest, Empty) {
file_system_.mkdir("/dir1");
file_system_.mkdir("/dir2");
file_system_.mkdir("/dir3");

file_system_.cd("/dir1");
EXPECT_EQ(file_system_.pwd(), "/dir1");
file_system_.cd("/dir2");
EXPECT_EQ(file_system_.pwd(), "/dir2");
file_system_.cd("/dir3");
EXPECT_EQ(file_system_.pwd(), "/dir3");
}

TEST_F(CdTest, NonExistent) { EXPECT_THROW(file_system_.cd("/non_existent"), std::invalid_argument); }

TEST_F(CdTest, NonExistentNested) {
file_system_.mkdir("/dir1");
file_system_.mkdir("/dir2");
file_system_.mkdir("/dir1/dir3");

EXPECT_THROW(file_system_.cd("/dir1/non_existent"), std::invalid_argument);
}

TEST_F(CdTest, MultipleFilesNested) {
file_system_.mkdir("dir1");
file_system_.mkdir("dir2");
file_system_.mkdir("dir1/dir3");

file_system_.cd("dir1");
EXPECT_EQ(file_system_.pwd(), "/dir1");
file_system_.cd("dir3");
EXPECT_EQ(file_system_.pwd(), "/dir1/dir3");
file_system_.cd("/dir2");
EXPECT_EQ(file_system_.pwd(), "/dir2");
}

0 comments on commit b572046

Please sign in to comment.