network-experiment/bmrshared/tst/ToTupleTest.cpp

86 lines
3.1 KiB
C++

// GNU Lesser General Public License v3.0
// Copyright (c) 2023 Bart Beumer <bart@4beumer.nl>
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License v3.0 as published by
// the Free Software Foundation.
//
#include "MockDataSource.hpp"
#include "bmrshared/IDataSource.hpp"
#include "bmrshared/flexible_value.hpp"
#include "bmrshared/to_tuple.hpp"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <tuple>
using ::bmrshared::MockDataSource;
using ::testing::An;
using ::testing::ElementsAre;
using ::testing::Return;
using ::testing::SetArgReferee;
using ::testing::Throw;
TEST(ToTupleTest, Ok)
{
MockDataSource mockSrc;
EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector<std::string>{"a", "b", "c"}));
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
EXPECT_CALL(mockSrc, Get("b", An<double&>())).WillRepeatedly(SetArgReferee<1>(4.2));
EXPECT_CALL(mockSrc, Get("c", An<std::string&>())).WillRepeatedly(SetArgReferee<1>("test"));
using TupleType = std::tuple<int64_t, double, std::string>;
const std::array<std::string, 3> names{"a", "b", "c"};
const TupleType expected{42, 4.2, "test"};
TupleType result;
bmrshared::to_tuple(mockSrc, names, result);
EXPECT_EQ(expected, result);
}
TEST(ToTupleTest, MissingParameter)
{
MockDataSource mockSrc;
EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector<std::string>{"a", "c"}));
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
EXPECT_CALL(mockSrc, Get("b", An<double&>())).WillRepeatedly(Throw(std::runtime_error("")));
EXPECT_CALL(mockSrc, Get("c", An<std::string&>())).WillRepeatedly(SetArgReferee<1>("test"));
using TupleType = std::tuple<int64_t, double, std::string>;
const std::array<std::string, 3> names{"a", "b", "c"};
try
{
TupleType result;
bmrshared::to_tuple(mockSrc, names, result);
EXPECT_TRUE(false);
}
catch (const bmrshared::ToTupleException& e)
{
EXPECT_THAT(e.GetKeysMissing(), ElementsAre("b"));
EXPECT_THAT(e.GetKeysNotUsed(), ElementsAre());
}
}
TEST(ToTupleTest, NotUsedParameter)
{
MockDataSource mockSrc;
EXPECT_CALL(mockSrc, GetKeys()).WillRepeatedly(Return(std::vector<std::string>{"a", "b", "c", "d"}));
EXPECT_CALL(mockSrc, Get("a", An<int64_t&>())).WillRepeatedly(SetArgReferee<1>(42));
EXPECT_CALL(mockSrc, Get("b", An<double&>())).WillRepeatedly(SetArgReferee<1>(4.2));
EXPECT_CALL(mockSrc, Get("c", An<std::string&>())).WillRepeatedly(SetArgReferee<1>("test"));
using TupleType = std::tuple<int64_t, double, std::string>;
const std::array<std::string, 3> names{"a", "b", "c"};
try
{
TupleType result;
bmrshared::to_tuple(mockSrc, names, result);
EXPECT_TRUE(false);
}
catch (const bmrshared::ToTupleException& e)
{
EXPECT_THAT(e.GetKeysMissing(), ElementsAre());
EXPECT_THAT(e.GetKeysNotUsed(), ElementsAre("d"));
}
}