212 lines
4.7 KiB
C++
Raw Normal View History

2021-06-02 20:57:20 +02:00
#include "CppUnitTest.h"
#include "helper.h"
#include "../Hazelnupp/Hazelnupp.h"
2021-06-02 22:48:54 +02:00
#include "../Hazelnupp/HazelnuppException.h"
2021-06-02 20:57:20 +02:00
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace TestHazelnupp
{
TEST_CLASS(_Basics)
{
public:
// Tests the application path gets exported correctly
TEST_METHOD(ApplicationPathWorks)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out"
2021-06-02 22:48:54 +02:00
});
2021-06-02 20:57:20 +02:00
// Exercise
Hazelnupp nupp(C_Ify(args));
2021-06-02 22:48:54 +02:00
nupp.SetCrashOnFail(false);
2021-06-02 20:57:20 +02:00
// Verify
Assert::AreEqual(std::string("/my/fake/path/wahoo.out"), nupp.GetExecutableName());
return;
}
2021-06-02 22:48:54 +02:00
// Edgecase test: We only have one param.
TEST_METHOD(Only_One_Param)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--dummy"
});
// Exercise
Hazelnupp nupp(C_Ify(args));
nupp.SetCrashOnFail(false);
// Verify
Assert::IsTrue(nupp.HasParam("--dummy"));
return;
}
2021-06-02 20:57:20 +02:00
// Tests keys exist after parsing
2021-06-02 22:48:54 +02:00
TEST_METHOD(KeysExist)
2021-06-02 20:57:20 +02:00
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--my_string",
"billybob",
"--my_void",
"--my_float",
"-23.199",
"--my_int",
"199",
"--my_num_list",
"1",
"2",
"3",
"4",
"--my_str_list",
"apple",
"banana",
"pumpkin",
2021-06-02 22:48:54 +02:00
});
2021-06-02 20:57:20 +02:00
// Exercise
Hazelnupp nupp(C_Ify(args));
2021-06-02 22:48:54 +02:00
nupp.SetCrashOnFail(false);
2021-06-02 20:57:20 +02:00
// Verify
Assert::IsTrue(nupp.HasParam("--my_string"));
Assert::IsTrue(nupp.HasParam("--my_void"));
Assert::IsTrue(nupp.HasParam("--my_float"));
Assert::IsTrue(nupp.HasParam("--my_int"));
Assert::IsTrue(nupp.HasParam("--my_num_list"));
Assert::IsTrue(nupp.HasParam("--my_str_list"));
return;
}
// Tests keys are of the correct type after parsing
2021-06-02 22:48:54 +02:00
TEST_METHOD(CorrectType)
2021-06-02 20:57:20 +02:00
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--my_string",
"billybob",
"--my_void",
"--my_float",
"-23.199",
"--my_int",
"199",
"--my_num_list",
"1",
"2",
"3",
"4",
"--my_str_list",
"apple",
"banana",
"pumpkin",
2021-06-02 22:48:54 +02:00
});
2021-06-02 20:57:20 +02:00
// Exercise
Hazelnupp nupp(C_Ify(args));
2021-06-02 22:48:54 +02:00
nupp.SetCrashOnFail(false);
2021-06-02 20:57:20 +02:00
// Verify
Assert::IsTrue(nupp["--my_string"].GetDataType() == DATA_TYPE::STRING);
Assert::IsTrue(nupp["--my_void"].GetDataType() == DATA_TYPE::VOID);
Assert::IsTrue(nupp["--my_float"].GetDataType() == DATA_TYPE::FLOAT);
Assert::IsTrue(nupp["--my_int"].GetDataType() == DATA_TYPE::INT);
Assert::IsTrue(nupp["--my_num_list"].GetDataType() == DATA_TYPE::LIST);
Assert::IsTrue(nupp["--my_str_list"].GetDataType() == DATA_TYPE::LIST);
return;
}
// Tests keys have the correct value after parsing
2021-06-02 22:48:54 +02:00
TEST_METHOD(CorrectValues)
2021-06-02 20:57:20 +02:00
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--my_string",
"billybob",
"--my_void",
"--my_float",
"-23.199",
"--my_int",
"199",
"--my_num_list",
"1",
"2",
"3",
"4",
"--my_str_list",
"apple",
"banana",
"pumpkin",
2021-06-02 22:48:54 +02:00
});
2021-06-02 20:57:20 +02:00
// Exercise
Hazelnupp nupp(C_Ify(args));
2021-06-02 22:48:54 +02:00
nupp.SetCrashOnFail(false);
2021-06-02 20:57:20 +02:00
// Verify
Assert::AreEqual(nupp["--my_string"].GetString(), std::string("billybob"));
Assert::AreEqual(nupp["--my_float"].GetFloat32(), -23.199);
Assert::AreEqual(nupp["--my_int"].GetInt32(), 199);
Assert::AreEqual(nupp["--my_num_list"].GetList()[0]->GetInt32(), 1);
Assert::AreEqual(nupp["--my_num_list"].GetList()[1]->GetInt32(), 2);
Assert::AreEqual(nupp["--my_num_list"].GetList()[2]->GetInt32(), 3);
Assert::AreEqual(nupp["--my_num_list"].GetList()[3]->GetInt32(), 4);
Assert::AreEqual(nupp["--my_str_list"].GetList()[0]->GetString(), std::string("apple"));
Assert::AreEqual(nupp["--my_str_list"].GetList()[1]->GetString(), std::string("banana"));
Assert::AreEqual(nupp["--my_str_list"].GetList()[2]->GetString(), std::string("pumpkin"));
return;
}
2021-06-02 22:48:54 +02:00
// Tests that an HazelnuppInvalidKeyException gets raised, if an invalid gey is tried to access
TEST_METHOD(Exception_On_Invalid_Key)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--my_string",
"billybob",
"--my_void",
"--my_float",
"-23.199",
"--my_int",
"199",
"--my_num_list",
"1",
"2",
"3",
"4",
"--my_str_list",
"apple",
"banana",
"pumpkin",
});
Hazelnupp nupp(C_Ify(args));
nupp.SetCrashOnFail(false);
// Exercise, Verify
Assert::ExpectException<HazelnuppInvalidKeyException>(
[args]
{
Hazelnupp nupp(C_Ify(args));
nupp["--borrnana"];
}
);
return;
}
2021-06-02 20:57:20 +02:00
};
}