blob: 56b569772c13c7aac892eb83448e56a4fda0d5b8 [file] [log] [blame]
#include "Utils.hpp"
#include <boost/container/flat_map.hpp>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>
#include <variant>
#include "gtest/gtest.h"
using namespace std::string_literals;
TEST(TemplateCharReplace, replaceOneInt)
{
nlohmann::json j = {{"foo", "$bus"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["BUS"] = 23;
templateCharReplace(it, data, 0);
nlohmann::json expected = 23;
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, replaceOneStr)
{
nlohmann::json j = {{"foo", "$TEST"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = std::string("Test");
templateCharReplace(it, data, 0);
nlohmann::json expected = "Test";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, replaceSecondStr)
{
nlohmann::json j = {{"foo", "the $TEST"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = std::string("Test");
templateCharReplace(it, data, 0);
nlohmann::json expected = "the Test";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, replaceMiddleStr)
{
nlohmann::json j = {{"foo", "the $TEST worked"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = std::string("Test");
templateCharReplace(it, data, 0);
nlohmann::json expected = "the Test worked";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, replaceLastStr)
{
nlohmann::json j = {{"foo", "the Test $TEST"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 23;
templateCharReplace(it, data, 0);
nlohmann::json expected = "the Test 23";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, increment)
{
nlohmann::json j = {{"foo", "3 plus 1 equals $TEST + 1"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 3;
templateCharReplace(it, data, 0);
nlohmann::json expected = "3 plus 1 equals 4";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, decrement)
{
nlohmann::json j = {{"foo", "3 minus 1 equals $TEST - 1 !"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 3;
templateCharReplace(it, data, 0);
nlohmann::json expected = "3 minus 1 equals 2 !";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, modulus)
{
nlohmann::json j = {{"foo", "3 mod 2 equals $TEST % 2"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 3;
templateCharReplace(it, data, 0);
nlohmann::json expected = "3 mod 2 equals 1";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, multiply)
{
nlohmann::json j = {{"foo", "3 * 2 equals $TEST * 2"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 3;
templateCharReplace(it, data, 0);
nlohmann::json expected = "3 * 2 equals 6";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, divide)
{
nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 4;
templateCharReplace(it, data, 0);
nlohmann::json expected = "4 / 2 equals 2";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, multiMath)
{
nlohmann::json j = {{"foo", "4 * 2 % 6 equals $TEST * 2 % 6"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 4;
templateCharReplace(it, data, 0);
nlohmann::json expected = "4 * 2 % 6 equals 2";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, twoReplacements)
{
nlohmann::json j = {{"foo", "$FOO $BAR"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["FOO"] = std::string("foo");
data["BAR"] = std::string("bar");
templateCharReplace(it, data, 0);
nlohmann::json expected = "foo bar";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, twoReplacementsWithMath)
{
nlohmann::json j = {{"foo", "4 / 2 equals $TEST / 2 $BAR"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["TEST"] = 4;
data["BAR"] = std::string("bar");
templateCharReplace(it, data, 0);
nlohmann::json expected = "4 / 2 equals 2 bar";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, hexAndWrongCase)
{
nlohmann::json j = {{"Address", "0x54"},
{"Bus", 15},
{"Name", "$bus sensor 0"},
{"Type", "SomeType"}};
boost::container::flat_map<std::string, BasicVariantType> data;
data["BUS"] = 15;
for (auto it = j.begin(); it != j.end(); it++)
{
templateCharReplace(it, data, 0);
}
nlohmann::json expected = {{"Address", 84},
{"Bus", 15},
{"Name", "15 sensor 0"},
{"Type", "SomeType"}};
EXPECT_EQ(expected, j);
}
TEST(TemplateCharReplace, replaceSecondAsInt)
{
nlohmann::json j = {{"foo", "twelve is $TEST"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
data["test"] = 12;
templateCharReplace(it, data, 0);
nlohmann::json expected = "twelve is 12";
EXPECT_EQ(expected, j["foo"]);
}
TEST(TemplateCharReplace, singleHex)
{
nlohmann::json j = {{"foo", "0x54"}};
auto it = j.begin();
boost::container::flat_map<std::string, BasicVariantType> data;
templateCharReplace(it, data, 0);
nlohmann::json expected = 84;
EXPECT_EQ(expected, j["foo"]);
}
TEST(MatchProbe, stringEqString)
{
nlohmann::json j = R"("foo")"_json;
BasicVariantType v = "foo"s;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringRegexEqString)
{
nlohmann::json j = R"("foo*")"_json;
BasicVariantType v = "foobar"s;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqString)
{
nlohmann::json j = R"("foobar")"_json;
BasicVariantType v = "foo"s;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringRegexError)
{
nlohmann::json j = R"("foo[")"_json;
BasicVariantType v = "foobar"s;
EXPECT_THROW(matchProbe(j, v), std::regex_error);
}
TEST(MatchProbe, stringZeroEqFalse)
{
nlohmann::json j = R"("0")"_json;
BasicVariantType v = false;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringOneEqTrue)
{
nlohmann::json j = R"("1")"_json;
BasicVariantType v = true;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringElevenNeqTrue)
{
nlohmann::json j = R"("11")"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringFalseNeqFalse)
{
nlohmann::json j = R"("false")"_json;
BasicVariantType v = false;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringTrueNeqTrue)
{
nlohmann::json j = R"("true")"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringFalseNeqTrue)
{
nlohmann::json j = R"("false")"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringEqUint8)
{
nlohmann::json j = R"("255")"_json;
BasicVariantType v = uint8_t(255);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqUint8Overflow)
{
nlohmann::json j = R"("65535")"_json;
BasicVariantType v = uint8_t(255);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringFalseNeqUint8Zero)
{
nlohmann::json j = R"("false")"_json;
BasicVariantType v = uint8_t(0);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringTrueNeqUint8Zero)
{
nlohmann::json j = R"("true")"_json;
BasicVariantType v = uint8_t(1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringEqUint32)
{
nlohmann::json j = R"("11")"_json;
BasicVariantType v = uint32_t(11);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqUint32)
{
nlohmann::json j = R"("12")"_json;
BasicVariantType v = uint32_t(11);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringEqInt32)
{
nlohmann::json j = R"("-11")"_json;
BasicVariantType v = int32_t(-11);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqInt32)
{
nlohmann::json j = R"("-12")"_json;
BasicVariantType v = int32_t(-11);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringRegexEqInt32)
{
nlohmann::json j = R"("1*4")"_json;
BasicVariantType v = int32_t(124);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqUint64)
{
nlohmann::json j = R"("foo")"_json;
BasicVariantType v = uint64_t(65535);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringEqDouble)
{
nlohmann::json j = R"("123.4")"_json;
BasicVariantType v = double(123.4);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqDouble)
{
nlohmann::json j = R"("-123.4")"_json;
BasicVariantType v = double(123.4);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, stringNeqEmpty)
{
nlohmann::json j = R"("-123.4")"_json;
BasicVariantType v;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, boolStringError)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v = "false"s;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, trueEqTrue)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v = true;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, falseEqFalse)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v = false;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, trueNeqFalse)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v = false;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, trueNeqInt32Zero)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v = int32_t(0);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, trueNeqInt32NegativeOne)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v = int32_t(-1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, falseNeqUint32One)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v = uint32_t(1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, falseEqUint32Zero)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v = uint32_t(0);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, trueNeqDoubleNegativeOne)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v = double(-1.1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, trueEqDoubleOne)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v = double(1.0);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, falseNeqDoubleOne)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v = double(1.0);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, falseEqDoubleZero)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v = double(0.0);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, falseEmptyError)
{
nlohmann::json j = R"(false)"_json;
BasicVariantType v;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, trueEmptyError)
{
nlohmann::json j = R"(true)"_json;
BasicVariantType v;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, uintStringError)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = "11"s;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, uintEqTrue)
{
nlohmann::json j = R"(1)"_json;
BasicVariantType v = true;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, uintEqFalse)
{
nlohmann::json j = R"(0)"_json;
BasicVariantType v = false;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, uintNeqTrue)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, uintEqUint8)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = uint8_t(11);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, uintNeqUint8)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = uint8_t(12);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, uintNeqUint8Overflow)
{
nlohmann::json j = R"(65535)"_json;
BasicVariantType v = uint8_t(255);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, uintEqInt8)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = int8_t(11);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, uintEqDouble)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = double(11.0);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, uintEqDoubleRound)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v = double(11.7);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, uintEmptyError)
{
nlohmann::json j = R"(11)"_json;
BasicVariantType v;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, intStringError)
{
nlohmann::json j = R"(-11)"_json;
BasicVariantType v = "-11"s;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, intNeqTrue)
{
nlohmann::json j = R"(-1)"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, intNeqUint8)
{
nlohmann::json j = R"(-11)"_json;
BasicVariantType v = uint8_t(11);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, intEqInt8)
{
nlohmann::json j = R"(-11)"_json;
BasicVariantType v = int8_t(-11);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, intNeqDouble)
{
nlohmann::json j = R"(-124)"_json;
BasicVariantType v = double(-123.0);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, intEqDouble)
{
nlohmann::json j = R"(-11)"_json;
BasicVariantType v = double(-11.0);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, intEqDoubleRound)
{
nlohmann::json j = R"(-11)"_json;
BasicVariantType v = double(-11.7);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, intEmptyError)
{
nlohmann::json j = R"(-11)"_json;
BasicVariantType v;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, doubleStringError)
{
nlohmann::json j = R"(0.0)"_json;
BasicVariantType v = "0.0"s;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, doubleEqFalse)
{
nlohmann::json j = R"(0.0)"_json;
BasicVariantType v = false;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, doubleNeqTrue)
{
nlohmann::json j = R"(1.1)"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, doubleEqTrue)
{
nlohmann::json j = R"(1.0)"_json;
BasicVariantType v = true;
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, doubleEqInt32)
{
nlohmann::json j = R"(-124.0)"_json;
BasicVariantType v = int32_t(-124);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, doubleNeqInt32)
{
nlohmann::json j = R"(-124.0)"_json;
BasicVariantType v = int32_t(-123);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, doubleRoundNeqInt)
{
nlohmann::json j = R"(124.7)"_json;
BasicVariantType v = int32_t(124);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, doubleEqDouble)
{
nlohmann::json j = R"(-124.2)"_json;
BasicVariantType v = double(-124.2);
EXPECT_TRUE(matchProbe(j, v));
}
TEST(MatchProbe, doubleNeqDouble)
{
nlohmann::json j = R"(-124.3)"_json;
BasicVariantType v = double(-124.2);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, doubleEmptyError)
{
nlohmann::json j = R"(-11.0)"_json;
BasicVariantType v;
EXPECT_THROW(matchProbe(j, v), std::invalid_argument);
}
TEST(MatchProbe, arrayNeqString)
{
nlohmann::json j = R"([1, 2])"_json;
BasicVariantType v = "hello"s;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, arrayNeqFalse)
{
nlohmann::json j = R"([1, 2])"_json;
BasicVariantType v = false;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, arrayNeqTrue)
{
nlohmann::json j = R"([1, 2])"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, arrayNeqUint8)
{
nlohmann::json j = R"([1, 2])"_json;
BasicVariantType v = uint8_t(1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, arrayNeqInt32)
{
nlohmann::json j = R"([1, 2])"_json;
BasicVariantType v = int32_t(-1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, arrayNeqDouble)
{
nlohmann::json j = R"([1, 2])"_json;
BasicVariantType v = double(1.1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, objNeqString)
{
nlohmann::json j = R"({"foo": "bar"})"_json;
BasicVariantType v = "hello"s;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, objNeqFalse)
{
nlohmann::json j = R"({"foo": "bar"})"_json;
BasicVariantType v = false;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, objNeqTrue)
{
nlohmann::json j = R"({"foo": "bar"})"_json;
BasicVariantType v = true;
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, objNeqUint8)
{
nlohmann::json j = R"({"foo": "bar"})"_json;
BasicVariantType v = uint8_t(1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, objNeqInt32)
{
nlohmann::json j = R"({"foo": "bar"})"_json;
BasicVariantType v = int32_t(-1);
EXPECT_FALSE(matchProbe(j, v));
}
TEST(MatchProbe, objNeqDouble)
{
nlohmann::json j = R"({"foo": "bar"})"_json;
BasicVariantType v = double(1.1);
EXPECT_FALSE(matchProbe(j, v));
}