Skip to content

Commit

Permalink
Tests - Better Aura Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Dec 12, 2023
1 parent 82a6b18 commit a924532
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
10 changes: 9 additions & 1 deletion include/configurationbase.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CONFIGURATIONBASE_H
#define CONFIGURATIONBASE_H

#include <filesystem>
#include <string>
#include <json/json.h>
#include "events/event.h"
Expand All @@ -18,6 +19,9 @@ namespace Nickvision::Aura
* @param key The key of the config file
*/
ConfigurationBase(const std::string& key);
/**
* @brief Deconstructs a ConfigurationBase.
*/
virtual ~ConfigurationBase() = default;
/**
* Gets the key of the config file.
Expand All @@ -31,13 +35,17 @@ namespace Nickvision::Aura
Events::Event<Events::EventArgs>& saved();
/**
* @brief Saves the config file to disk.
* @return True if saved to disk, else false
*/
void save();
bool save();

protected:
Json::Value m_json;
std::string m_key;
Events::Event<Events::EventArgs> m_saved;

private:
std::filesystem::path m_path;
};
}

Expand Down
15 changes: 8 additions & 7 deletions src/configurationbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
namespace Nickvision::Aura
{
ConfigurationBase::ConfigurationBase(const std::string& key)
: m_key{ key }
: m_key{ key },
m_path{ UserDirectories::getApplicationConfig() / (key + ".json") }
{
std::filesystem::path path{ UserDirectories::getApplicationConfig() / (key + ".json") };
if (std::filesystem::exists(path))
if (std::filesystem::exists(m_path))
{
std::ifstream in{ path };
std::ifstream in{ m_path };
in >> m_json;
}
}
Expand All @@ -26,14 +26,15 @@ namespace Nickvision::Aura
return m_saved;
}

void ConfigurationBase::save()
bool ConfigurationBase::save()
{
if (m_key.empty())
{
throw std::invalid_argument("Key must not be empty.");
return false;
}
std::ofstream out{ UserDirectories::getApplicationConfig() / (m_key + ".json") };
std::ofstream out{ m_path };
out << m_json;
m_saved.invoke({});
return true;
}
}
27 changes: 24 additions & 3 deletions tests/auratests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <gtest/gtest.h>
#include "aura.h"
#include "dependencylocator.h"
#include "userdirectories.h"

using namespace Nickvision::Aura;

Expand All @@ -15,7 +17,7 @@ class AppConfig : public ConfigurationBase
public:
AppConfig(const std::string& key) : ConfigurationBase(key)
{

}

Theme getTheme() const
Expand Down Expand Up @@ -53,12 +55,31 @@ TEST_F(AuraTest, ChangeAppConfig)
{
AppConfig& config{ Aura::getActive().getConfig<AppConfig>("config") };
config.setTheme(Theme::Light);
ASSERT_TRUE(config.save());
ASSERT_EQ(config.getTheme(), Theme::Light);
ASSERT_NO_THROW(config.save());
}

TEST_F(AuraTest, EnsureChangeInAppConfig)
{
AppConfig& config{ Aura::getActive().getConfig<AppConfig>("config") };
ASSERT_EQ(config.getTheme(), Theme::Light);
}
}

TEST_F(AuraTest, ResetAppConfig)
{
ASSERT_TRUE(std::filesystem::remove(UserDirectories::getApplicationConfig() / ("config.json")));
}

#ifdef _WIN32
TEST_F(AuraTest, DependencyCheckCmd)
{
ASSERT_FALSE(DependencyLocator::find("cmd").empty());
}
#endif

#ifndef _WIN32
TEST_F(AuraTest, DependencyCheckLs)
{
ASSERT_FALSE(DependencyLocator::find("ls").empty());
}
#endif

0 comments on commit a924532

Please sign in to comment.