#include "gtest/gtest.h"
#include "Buffer.h"
#include "TestSupport.h"
#include "Path.h"

TEST(BufferTest, writes_an_identical_file_to_what_is_loaded_if_no_changes_were_performed)
{
    const char * files_to_test[] = {
        "test/files/empty.txt",
        "test/files/line_endings/cr_format.txt",
        "test/files/line_endings/crlf_format.txt",
        "test/files/line_endings/lf_format.txt",
        "test/files/no_eol_at_eof.txt",
    };
    for (auto e : files_to_test)
    {
        Buffer b(e);
        ASSERT_TRUE(b.write_to_file("test/tmp/f"));
        ASSERT_TRUE(Path::is_file("test/tmp/f"));
        TestSupport::compare_files(e, "test/tmp/f");
    }
}

TEST(BufferTest, writes_an_empty_file_for_an_empty_buffer)
{
    Buffer b;
    ASSERT_TRUE(b.write_to_file("test/tmp/f"));
    TestSupport::compare_files("test/files/empty.txt", "test/tmp/f");
}

TEST(BufferTest, allows_navigating_using_cursors)
{
    Buffer b("test/files/line_endings/lf_format.txt");
    auto cursor = b.add_cursor();

    EXPECT_EQ(0u, cursor->column());
    EXPECT_EQ(0u, cursor->line());
    ASSERT_EQ((uint32_t)'H', **cursor);

    EXPECT_FALSE(cursor->go_up(cursor->column()));
    EXPECT_EQ(0u, cursor->column());
    EXPECT_EQ(0u, cursor->line());
    ASSERT_EQ((uint32_t)'H', **cursor);

    EXPECT_TRUE(cursor->go_right(false));
    EXPECT_EQ(1u, cursor->column());
    EXPECT_EQ(0u, cursor->line());
    ASSERT_EQ((uint32_t)'e', **cursor);

    EXPECT_TRUE(cursor->go_down(cursor->column()));
    EXPECT_EQ(1u, cursor->column());
    EXPECT_EQ(1u, cursor->line());
    ASSERT_EQ((uint32_t)'h', **cursor);

    EXPECT_TRUE(cursor->go_left());
    EXPECT_EQ(0u, cursor->column());
    EXPECT_EQ(1u, cursor->line());
    ASSERT_EQ((uint32_t)'T', **cursor);

    EXPECT_FALSE(cursor->go_left());
    EXPECT_EQ(0u, cursor->column());
    EXPECT_EQ(1u, cursor->line());
    ASSERT_EQ((uint32_t)'T', **cursor);

    EXPECT_FALSE(cursor->go_down(cursor->column()));
    EXPECT_EQ(0u, cursor->column());
    EXPECT_EQ(1u, cursor->line());
    ASSERT_EQ((uint32_t)'T', **cursor);
}
