Hazelnupp/Test_Hazelnupp/Conversion.cpp

251 lines
5.6 KiB
C++
Raw Normal View History

2021-06-02 22:48:54 +02:00
#include "CppUnitTest.h"
#include "helper.h"
#include "../Hazelnupp/CmdArgsInterface.h"
2021-06-02 22:48:54 +02:00
#include "../Hazelnupp/HazelnuppException.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
2021-06-03 13:42:40 +02:00
using namespace Hazelnp;
2021-06-02 22:48:54 +02:00
namespace TestHazelnupp
{
TEST_CLASS(_Conversion)
{
public:
// Tests that string conversion methods work
TEST_METHOD(Convert_String)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--pud",
"hello"
});
// Exercise
CmdArgsInterface cmdArgsI(C_Ify(args));
cmdArgsI.SetCrashOnFail(false);
2021-06-02 22:48:54 +02:00
// Verify
const CmdArgsInterface* ptcmdArgsI = &cmdArgsI;
2021-06-02 22:48:54 +02:00
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetInt64();
2021-06-02 22:48:54 +02:00
}
, L"Int64");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetInt32();
2021-06-02 22:48:54 +02:00
}
, L"Int32");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetFloat64();
2021-06-02 22:48:54 +02:00
}
, L"Float64");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetFloat32();
2021-06-02 22:48:54 +02:00
}
, L"Float32");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetList();
2021-06-02 22:48:54 +02:00
}
, L"List");
return;
}
// Tests that void conversion methods work
TEST_METHOD(Convert_Void)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--pud"
});
// Exercise
CmdArgsInterface cmdArgsI(C_Ify(args));
cmdArgsI.SetCrashOnFail(false);
2021-06-02 22:48:54 +02:00
// Verify
const CmdArgsInterface* ptcmdArgsI = &cmdArgsI;
2021-06-02 22:48:54 +02:00
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetInt64();
2021-06-02 22:48:54 +02:00
}
, L"Int64");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetInt32();
2021-06-02 22:48:54 +02:00
}
, L"Int32");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetFloat64();
2021-06-02 22:48:54 +02:00
}
, L"Float64");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetFloat32();
2021-06-02 22:48:54 +02:00
}
, L"Float32");
2021-06-05 12:29:59 +02:00
// Expect empty string
Assert::AreEqual(std::string(), cmdArgsI["--pud"].GetString(), L"String");
2021-06-02 22:48:54 +02:00
2021-06-05 12:29:59 +02:00
// Expect empty list
Assert::AreEqual(std::size_t(0), cmdArgsI["--pud"].GetList().size(), L"List");
2021-06-02 22:48:54 +02:00
return;
}
// Tests that int conversion methods work
TEST_METHOD(Convert_Int)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--pud",
"39"
2021-06-05 12:20:32 +02:00
});
2021-06-02 22:48:54 +02:00
// Exercise
CmdArgsInterface cmdArgsI(C_Ify(args));
cmdArgsI.SetCrashOnFail(false);
2021-06-02 22:48:54 +02:00
// Verify
const CmdArgsInterface* ptcmdArgsI = &cmdArgsI;
2021-06-02 22:48:54 +02:00
Assert::AreEqual(39ll, cmdArgsI["--pud"].GetInt64(), L"Int64");
Assert::AreEqual(39, cmdArgsI["--pud"].GetInt32(), L"Int32");
Assert::IsTrue(39.0l == cmdArgsI["--pud"].GetFloat64(), L"Float64");
Assert::AreEqual(39.0, cmdArgsI["--pud"].GetFloat32(), L"Float32");
Assert::AreEqual(std::string("39"), cmdArgsI["--pud"].GetString(), L"String");
2021-06-02 22:48:54 +02:00
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetList();
2021-06-02 22:48:54 +02:00
}
, L"List");
return;
}
// Tests that float conversion methods work
TEST_METHOD(Convert_Float)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--pud",
"39.5"
2021-06-05 12:20:32 +02:00
});
2021-06-02 22:48:54 +02:00
// Exercise
CmdArgsInterface cmdArgsI(C_Ify(args));
cmdArgsI.SetCrashOnFail(false);
2021-06-02 22:48:54 +02:00
// Verify
const CmdArgsInterface* ptcmdArgsI = &cmdArgsI;
2021-06-02 22:48:54 +02:00
Assert::AreEqual(39ll, cmdArgsI["--pud"].GetInt64(), L"Int64");
Assert::AreEqual(39, cmdArgsI["--pud"].GetInt32(), L"Int32");
Assert::IsTrue(39.5l == cmdArgsI["--pud"].GetFloat64(), L"Float64");
Assert::AreEqual(39.5, cmdArgsI["--pud"].GetFloat32(), L"Float32");
Assert::AreEqual(std::string("39.5"), cmdArgsI["--pud"].GetString(), L"String");
2021-06-02 22:48:54 +02:00
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetList();
2021-06-02 22:48:54 +02:00
}
, L"List");
return;
}
// Tests that list conversion methods work
TEST_METHOD(Convert_List)
{
// Setup
ArgList args({
"/my/fake/path/wahoo.out",
"--pud",
"apple",
"1",
"2",
"3"
2021-06-05 12:20:32 +02:00
});
2021-06-02 22:48:54 +02:00
// Exercise
CmdArgsInterface cmdArgsI(C_Ify(args));
cmdArgsI.SetCrashOnFail(false);
2021-06-02 22:48:54 +02:00
// Verify
const CmdArgsInterface* ptcmdArgsI = &cmdArgsI;
2021-06-02 22:48:54 +02:00
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetInt64();
2021-06-02 22:48:54 +02:00
}
, L"Int64");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetInt32();
2021-06-02 22:48:54 +02:00
}
, L"Int32");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetFloat64();
2021-06-02 22:48:54 +02:00
}
, L"Float64");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetFloat32();
2021-06-02 22:48:54 +02:00
}
, L"Float32");
Assert::ExpectException<HazelnuppValueNotConvertibleException>(
[ptcmdArgsI]
2021-06-02 22:48:54 +02:00
{
(*ptcmdArgsI)["--pud"].GetString();
2021-06-02 22:48:54 +02:00
}
, L"String");
return;
}
};
}