2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
#include <libaegisub/mru.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
class lagi_mru : public libagi {
|
|
|
|
protected:
|
|
|
|
std::string default_mru;
|
|
|
|
std::string conf_ok;
|
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
void SetUp() override {
|
2011-12-22 22:10:50 +01:00
|
|
|
default_mru = "{\"Valid\" : []}";
|
2010-05-21 03:13:36 +02:00
|
|
|
conf_ok = "./data/mru_ok.json";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, load_from_file) {
|
2011-12-22 22:10:50 +01:00
|
|
|
ASSERT_NO_THROW(agi::MRUManager mru(conf_ok, default_mru));
|
|
|
|
agi::MRUManager mru(conf_ok, default_mru);
|
2013-01-04 16:01:50 +01:00
|
|
|
ASSERT_NO_THROW(mru.Get("Valid"));
|
|
|
|
ASSERT_EQ(2u, mru.Get("Valid")->size());
|
|
|
|
auto entry = mru.Get("Valid")->begin();
|
|
|
|
EXPECT_STREQ("Entry One", (*entry++).string().c_str());
|
|
|
|
EXPECT_STREQ("Entry Two", (*entry++).string().c_str());
|
2011-12-22 22:10:50 +01:00
|
|
|
EXPECT_TRUE(mru.Get("Valid")->end() == entry);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, load_from_default_string) {
|
|
|
|
agi::fs::Remove("data/mru_tmp");
|
2011-12-22 22:10:50 +01:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, load_from_invalid_file) {
|
|
|
|
agi::fs::Copy("data/mru_invalid.json", "data/mru_tmp");
|
2011-12-22 22:10:50 +01:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
|
|
|
EXPECT_TRUE(mru.Get("Invalid")->empty());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, add_entry) {
|
|
|
|
agi::fs::Copy("data/mru_ok.json", "data/mru_tmp");
|
2010-05-21 03:13:36 +02:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/path/to/file"));
|
2013-01-04 16:01:50 +01:00
|
|
|
EXPECT_STREQ("/path/to/file", mru.Get("Valid")->front().string().c_str());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, remove_entry) {
|
|
|
|
agi::fs::Copy("data/mru_ok.json", "data/mru_tmp");
|
2010-05-21 03:13:36 +02:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/path/to/file"));
|
|
|
|
EXPECT_NO_THROW(mru.Remove("Valid", "/path/to/file"));
|
2013-01-04 16:01:50 +01:00
|
|
|
EXPECT_STRNE("/path/to/file", mru.Get("Valid")->front().string().c_str());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, invalid_mru_key_throws) {
|
|
|
|
agi::fs::Copy("data/mru_ok.json", "data/mru_tmp");
|
2010-05-21 03:13:36 +02:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
|
|
|
EXPECT_THROW(mru.Add("Invalid", "/path/to/file"), agi::MRUErrorInvalidKey);
|
2011-12-22 22:10:50 +01:00
|
|
|
EXPECT_THROW(mru.Get("Invalid"), agi::MRUErrorInvalidKey);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, valid_mru_key_doesnt_throw) {
|
|
|
|
agi::fs::Copy("data/mru_ok.json", "data/mru_tmp");
|
2010-05-21 03:13:36 +02:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/path/to/file"));
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
TEST_F(lagi_mru, adding_existing_moves_to_front) {
|
|
|
|
agi::fs::Remove("data/mru_tmp");
|
2011-07-26 21:51:56 +02:00
|
|
|
agi::MRUManager mru("data/mru_tmp", default_mru);
|
|
|
|
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/file/1"));
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/file/2"));
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/file/3"));
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/file/1"));
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/file/1"));
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/file/3"));
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
EXPECT_STREQ("/file/3", mru.GetEntry("Valid", 0).string().c_str());
|
|
|
|
EXPECT_STREQ("/file/1", mru.GetEntry("Valid", 1).string().c_str());
|
|
|
|
EXPECT_STREQ("/file/2", mru.GetEntry("Valid", 2).string().c_str());
|
2011-12-22 22:10:50 +01:00
|
|
|
EXPECT_THROW(mru.GetEntry("Valid", 3), agi::MRUErrorIndexOutOfRange);
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
|
|
|
|
2010-05-24 07:58:46 +02:00
|
|
|
// Check to make sure an entry is really removed. This was fixed in
|
|
|
|
// r4347, the entry was being removed from a copy of the map internally.
|
|
|
|
TEST_F(lagi_mru, MRUEntryRemove_r4347) {
|
|
|
|
|
|
|
|
agi::MRUManager mru(conf_ok, default_mru);
|
|
|
|
EXPECT_NO_THROW(mru.Add("Valid", "/path/to/file"));
|
|
|
|
EXPECT_NO_THROW(mru.Remove("Valid", "/path/to/file"));
|
|
|
|
|
|
|
|
const agi::MRUManager::MRUListMap *map_list = mru.Get("Valid");
|
2013-11-21 18:13:36 +01:00
|
|
|
auto i_lst = map_list->begin();
|
2010-05-24 07:58:46 +02:00
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
if ((i_lst != map_list->end()) && (*i_lst == "/path/to/file"))
|
2010-05-24 07:58:46 +02:00
|
|
|
FAIL() << "r4347 regression, Entry exists after remove";
|
|
|
|
}
|