Tests.cpp 1.96 KB
Newer Older
1 2 3 4
#include "Tests.h"
#include "filesystem/IFile.h"
#include "filesystem/IDirectory.h"
#include "Utils.h"
5 6 7 8 9 10 11 12 13 14 15

class TestEnv : public ::testing::Environment
{
    public:
        virtual void SetUp()
        {
            // Always clean the DB in case a previous test crashed
            unlink("test.db");
        }
};

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
namespace mock
{
namespace defaults
{

class File : public fs::IFile
{
    std::string m_path;
    std::string m_fileName;
    std::string m_extension;

public:
    File( const std::string& file )
        : m_path( file )
        , m_fileName( utils::file::fileName( file ) )
        , m_extension( utils::file::extension( file ) )
    {
    }

    virtual const std::string& name() const
    {
        return m_fileName;
    }

    virtual const std::string& path() const
    {
        return m_path;
    }

    virtual const std::string& fullPath() const
    {
        return m_path;
    }

    virtual const std::string& extension() const
    {
        return m_extension;
    }

    virtual unsigned int lastModificationDate() const
    {
        // Ensure a non-0 value so tests can easily verify that the value
        // is initialized
        return 123;
    }
};

class FileSystemFactory : public factory::IFileSystem
{
    virtual std::unique_ptr<fs::IDirectory> createDirectory(const std::string&)
    {
        return nullptr;
    }

    virtual std::unique_ptr<fs::IFile> createFile(const std::string& fileName)
    {
        return std::unique_ptr<fs::IFile>( new File( fileName ) );
    }
};
}
}

void Tests::TearDown()
{
    ml.reset();
    unlink("test.db");
}


void Tests::Reload(std::shared_ptr<factory::IFileSystem> fs /* = nullptr */ )
{
    ml.reset( MediaLibraryFactory::create() );
    bool res = ml->initialize( "test.db", fs ? fs : defaultFs );
    ASSERT_TRUE( res );
}


void Tests::SetUp()
{
    defaultFs = std::make_shared<mock::defaults::FileSystemFactory>();
    Reload();
}

99
::testing::Environment* const env = ::testing::AddGlobalTestEnvironment(new TestEnv);