Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding load and safe config functions to Expander. Unit tests #11

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Expander.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "Expander.h"
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>

using namespace std;
using namespace PatternExpander;
Expand All @@ -9,6 +12,7 @@ Expander::Expander(wchar_t esc, wchar_t range, wchar_t grpBegin, wchar_t grpEnd)
escapeSymbol(esc), rangeSymbol(range), groupBegin(grpBegin), groupEnd(
grpEnd)
{
loadConfig();
}

void Expander::append(vector<wstring> &results, const wstring &newData)
Expand Down Expand Up @@ -458,3 +462,47 @@ std::vector<std::wstring> Expander::getData()
{
return data;
}

void Expander::loadConfig(const std::string& filePath)
{

wifstream in(filePath, ios::in);
while (in)
{
wstring key;
wstring val;
in >> key >> val;

std::transform(key.begin(), key.end(), key.begin(), ::tolower);

if (key == L"range")
{
setRangeChar(val[0]);
}
else if (key == L"escape")
{
setEscChar(val[0]);
}
else if (key == L"group.begin")
{
setGroupBegin(val[0]);
}
else if (key == L"group.end")
{
setGroupEnd(val[0]);
}
}

}

void Expander::saveConfig(const std::string& filePath)
{
wofstream out(filePath, ios::out);

out << L"range " << getRangeChar() << std::endl;
out << L"escape " << getEscChar() << endl;

out << L"group.begin " << getGroupBegin() << endl;
out << L"group.end " << getGroupEnd() << endl;

}
4 changes: 4 additions & 0 deletions src/Expander.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class Expander

std::wstringstream output;

void loadConfig(const std::string& filePath="~/.patexpconfig");

void saveConfig(const std::string& filePath="~/.patexpconfig");

private:
///used to escape other special symbols
wchar_t escapeSymbol;
Expand Down
49 changes: 49 additions & 0 deletions test/TestExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,55 @@ TEST_F(TestExpander, testGenerate_VariableBlock_EscQuote_OutsideGroup)

}

TEST_F(TestExpander, testLoadConfig)
{
//If the file doesn't exist the settings don't change
//Assuming that no ~/.patexpconfig exists, those will be the default values
underTest.loadConfig("./nofile.txt");

EXPECT_EQ(underTest.getEscChar(), PatternExpander::DEFAULT_ESC_SYM);
EXPECT_EQ(underTest.getGroupBegin(), PatternExpander::GROUP_SYM_BEGIN);
EXPECT_EQ(underTest.getGroupEnd(), PatternExpander::GROUP_SYM_END);
EXPECT_EQ(underTest.getRangeChar(), PatternExpander::DEFAULT_RANGE_SYM);

altExpander.loadConfig("./nofile.txt");
EXPECT_EQ(altExpander.getEscChar(), L'#');
EXPECT_EQ(altExpander.getGroupBegin(), L'{');
EXPECT_EQ(altExpander.getGroupEnd(), L'}');
EXPECT_EQ(altExpander.getRangeChar(), L'>');

}

TEST_F(TestExpander, testLoadConfig_altSettings)
{
//Load the alt config file. All settings will change
underTest.loadConfig("test_data/altConfig.txt");

EXPECT_EQ(underTest.getEscChar(), L'#');
EXPECT_EQ(underTest.getGroupBegin(), L'{');
EXPECT_EQ(underTest.getGroupEnd(), L'}');
EXPECT_EQ(underTest.getRangeChar(), L'>');

}

TEST_F(TestExpander, testSaveConfig)
{
//Save the alt config to file, then load it in the default expander
string configFileName = std::tmpnam(nullptr);

altExpander.saveConfig(configFileName);
underTest.loadConfig(configFileName);

EXPECT_EQ(underTest.getEscChar(), L'#');
EXPECT_EQ(underTest.getGroupBegin(), L'{');
EXPECT_EQ(underTest.getGroupEnd(), L'}');
EXPECT_EQ(underTest.getRangeChar(), L'>');


}



int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down
4 changes: 4 additions & 0 deletions test_data/altConfig.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
range >
escape #
group.begin {
group.end }