mirror of https://github.com/JAJames/jessilib.git
Jessica James
6 years ago
5 changed files with 255 additions and 4 deletions
@ -0,0 +1,136 @@ |
|||||
|
/**
|
||||
|
* Copyright (C) 2018 Jessica James. |
||||
|
* |
||||
|
* Permission to use, copy, modify, and/or 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. |
||||
|
* |
||||
|
* Written by Jessica James <jessica.aj@outlook.com> |
||||
|
*/ |
||||
|
|
||||
|
#include <chrono> |
||||
|
#include <filesystem> |
||||
|
#include <fstream> |
||||
|
#include "test.hpp" |
||||
|
#include "config.hpp" |
||||
|
#include "serialize.hpp" // format_not_available |
||||
|
|
||||
|
using namespace jessilib; |
||||
|
using namespace std::literals; |
||||
|
|
||||
|
// NOTE: see test/parser.cpp for test_parser implementation and details for format "test"
|
||||
|
|
||||
|
std::filesystem::path make_tmp_file(std::filesystem::path in_filename, const std::string& in_data) { |
||||
|
// Build path
|
||||
|
std::filesystem::path path = std::filesystem::temp_directory_path(); |
||||
|
path /= in_filename; |
||||
|
|
||||
|
// Delete any previously existing file
|
||||
|
std::filesystem::remove(path); |
||||
|
|
||||
|
// Populate file
|
||||
|
{ |
||||
|
std::ofstream file{ path, std::ios::binary | std::ios::out }; |
||||
|
file << in_data << std::flush; |
||||
|
} |
||||
|
|
||||
|
// Return fstream to file
|
||||
|
return path; |
||||
|
//return std::fstream{ path, std::ios::binary | std::ios::out | std::ios::in };
|
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, get_format) { |
||||
|
EXPECT_EQ(config::get_format("example.json"), "json"); |
||||
|
EXPECT_EQ(config::get_format("example.json", "xml"), "xml"); |
||||
|
EXPECT_EQ(config::get_format("example", "json"), "json"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, read_object) { |
||||
|
std::filesystem::path file_path = make_tmp_file("read_object.test", "some_data"); |
||||
|
EXPECT_EQ(config::read_object(file_path).get<std::string>(), "some_data"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, write_object) { |
||||
|
std::filesystem::path file_path = make_tmp_file("write_object.test", "some_data"); |
||||
|
config::write_object(object{}, file_path); |
||||
|
|
||||
|
EXPECT_EQ(config::read_object(file_path).get<std::string>(), "serialize_result"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, load) { |
||||
|
config l_config; |
||||
|
|
||||
|
// Write temp file out
|
||||
|
std::filesystem::path file_path = make_tmp_file("load.test", "some_data"); |
||||
|
|
||||
|
// Load data from disk
|
||||
|
l_config.load(file_path); |
||||
|
|
||||
|
// Verify
|
||||
|
EXPECT_EQ(l_config.data().get<std::string>(), "some_data"); |
||||
|
EXPECT_EQ(l_config.filename(), file_path); |
||||
|
EXPECT_EQ(l_config.format(), "test"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, reload) { |
||||
|
config l_config; |
||||
|
|
||||
|
// Write temp file out
|
||||
|
std::filesystem::path file_path = make_tmp_file("reload.test", "some_data"); |
||||
|
|
||||
|
// Load data from disk
|
||||
|
l_config.load(file_path); |
||||
|
|
||||
|
// Write new data to disk
|
||||
|
make_tmp_file("reload.test", "some_other_data"); |
||||
|
|
||||
|
// Reload data from disk and compare
|
||||
|
l_config.reload(); |
||||
|
EXPECT_EQ(l_config.data().get<std::string>(), "some_other_data"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, set_data) { |
||||
|
config l_config; |
||||
|
|
||||
|
l_config.set_data("some_data"); |
||||
|
EXPECT_EQ(l_config.data().get<std::string>(), "some_data"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, write) { |
||||
|
config l_config; |
||||
|
std::filesystem::path file_path = make_tmp_file("write.test", ""); |
||||
|
|
||||
|
l_config.set_data("some_data"); |
||||
|
l_config.write(file_path); |
||||
|
|
||||
|
l_config.reload(); |
||||
|
EXPECT_EQ(l_config.data().get<std::string>(), "some_data"); |
||||
|
} |
||||
|
|
||||
|
TEST(ConfigTest, rewrite) { |
||||
|
config l_config; |
||||
|
|
||||
|
// Write temp file out
|
||||
|
std::filesystem::path file_path = make_tmp_file("rewrite.test", "some_data"); |
||||
|
|
||||
|
// Load data from disk
|
||||
|
l_config.load(file_path); |
||||
|
|
||||
|
// Set some other data
|
||||
|
l_config.set_data("some_other_data"); |
||||
|
|
||||
|
// Write data to disk
|
||||
|
l_config.write(); |
||||
|
|
||||
|
// Reload from disk and verify
|
||||
|
l_config.reload(); |
||||
|
EXPECT_EQ(l_config.data().get<std::string>(), "some_other_data"); |
||||
|
} |
@ -0,0 +1,113 @@ |
|||||
|
/**
|
||||
|
* Copyright (C) 2018 Jessica James. |
||||
|
* |
||||
|
* Permission to use, copy, modify, and/or 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. |
||||
|
* |
||||
|
* Written by Jessica James <jessica.aj@outlook.com> |
||||
|
*/ |
||||
|
|
||||
|
#include <chrono> |
||||
|
#include <functional> |
||||
|
#include "test.hpp" |
||||
|
#include "parser.hpp" |
||||
|
#include "serialize.hpp" |
||||
|
|
||||
|
using namespace jessilib; |
||||
|
using namespace std::literals; |
||||
|
|
||||
|
/** test_parser */ |
||||
|
|
||||
|
class test_parser : public parser { |
||||
|
public: |
||||
|
/** deserialize/serialize overrides */ |
||||
|
virtual object deserialize(std::string_view in_data) override { |
||||
|
return deserialize_impl(in_data); |
||||
|
} |
||||
|
|
||||
|
virtual std::string serialize(const object& in_object) override { |
||||
|
return serialize_impl(in_object); |
||||
|
} |
||||
|
|
||||
|
/** helpers */ |
||||
|
static void reset() { |
||||
|
serialize_impl = &serialize_default; |
||||
|
deserialize_impl = &deserialize_default; |
||||
|
} |
||||
|
|
||||
|
/** default serialize/deserialize implementations */ |
||||
|
static std::string serialize_default(const object& in_object) { |
||||
|
if (in_object.has<std::string>()) { |
||||
|
return in_object.get<std::string>(); |
||||
|
} |
||||
|
|
||||
|
return DEFAULT_SERIALIZE_RESULT; |
||||
|
} |
||||
|
|
||||
|
static object deserialize_default(std::string_view in_data) { |
||||
|
return object{ in_data }; |
||||
|
} |
||||
|
|
||||
|
/** static members */ |
||||
|
static constexpr const char DEFAULT_SERIALIZE_RESULT[]{ "serialize_result" }; |
||||
|
static std::function<std::string(const object&)> serialize_impl; |
||||
|
static std::function<object(std::string_view)> deserialize_impl; |
||||
|
}; |
||||
|
|
||||
|
std::function<std::string(const object&)> test_parser::serialize_impl{ &serialize_default }; |
||||
|
std::function<object(std::string_view)> test_parser::deserialize_impl{ &deserialize_default }; |
||||
|
|
||||
|
parser_registration<test_parser> test_parser_registration{ "test" }; |
||||
|
|
||||
|
/** ParserTest */ |
||||
|
|
||||
|
class ParserTest : public base_test { |
||||
|
virtual void SetUp() override { |
||||
|
test_parser::reset(); |
||||
|
} |
||||
|
|
||||
|
virtual void TearDown() override { |
||||
|
test_parser::reset(); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
/** Parser tests */ |
||||
|
|
||||
|
TEST_F(ParserTest, find_parser) { |
||||
|
EXPECT_NO_THROW(serialize_object("test_data", "test")); |
||||
|
EXPECT_NO_THROW(deserialize_object("test_data"sv, "test")); |
||||
|
|
||||
|
EXPECT_THROW(serialize_object("test_data", "invalid_format_test"), format_not_available); |
||||
|
EXPECT_THROW(deserialize_object("test_data"sv, "invalid_format_test"), format_not_available); |
||||
|
} |
||||
|
|
||||
|
TEST_F(ParserTest, temp_parser) { |
||||
|
EXPECT_THROW(serialize_object("test_data", "test_tmp"), format_not_available); |
||||
|
EXPECT_THROW(deserialize_object("test_data"sv, "test_tmp"), format_not_available); |
||||
|
|
||||
|
{ |
||||
|
parser_registration<test_parser> test_tmp_registration{ "test_tmp" }; |
||||
|
EXPECT_NO_THROW(serialize_object("test_data", "test_tmp")); |
||||
|
EXPECT_NO_THROW(deserialize_object("test_data"sv, "test_tmp")); |
||||
|
} |
||||
|
|
||||
|
EXPECT_THROW(serialize_object("test_data", "test_tmp"), format_not_available); |
||||
|
EXPECT_THROW(deserialize_object("test_data"sv, "test_tmp"), format_not_available); |
||||
|
} |
||||
|
|
||||
|
TEST_F(ParserTest, serialize) { |
||||
|
EXPECT_EQ(serialize_object("test_data", "test"), "test_data"); |
||||
|
} |
||||
|
|
||||
|
TEST_F(ParserTest, deserialize) { |
||||
|
EXPECT_EQ(deserialize_object("test_data"sv, "test").get<std::string>(), "test_data"); |
||||
|
} |
Loading…
Reference in new issue