-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lack of unittests for yanet-cli is bad. Started with unittests for Call helper function
- Loading branch information
1 parent
2de0ccd
commit 30eadb5
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
|
||
#include "cli/helper.h" | ||
|
||
namespace | ||
{ | ||
|
||
class CallableClass | ||
{ | ||
public: | ||
void operator()(int a, std::optional<double> b, const std::string& c) const | ||
{ | ||
EXPECT_EQ(a, 70); | ||
if (b.has_value()) | ||
{ | ||
EXPECT_DOUBLE_EQ(b.value(), 45.67); | ||
} | ||
EXPECT_EQ(c, "test"); | ||
} | ||
}; | ||
|
||
void CheckIntegerAndString(int a, const std::string& str) | ||
{ | ||
EXPECT_EQ(a, 42); | ||
EXPECT_EQ(str, "hello"); | ||
} | ||
|
||
void CheckOptionalIntegerAndStringPresent(int a, std::optional<int> b, const std::string& str) | ||
{ | ||
EXPECT_EQ(a, 42); | ||
EXPECT_TRUE(b.has_value()); | ||
EXPECT_EQ(b.value(), 100); | ||
EXPECT_EQ(str, "world"); | ||
} | ||
|
||
void CheckOptionalIntegerMissing(int a, std::optional<int> b, const std::string& str) | ||
{ | ||
EXPECT_EQ(a, 42); | ||
EXPECT_FALSE(b.has_value()); | ||
EXPECT_EQ(str, "world"); | ||
} | ||
|
||
void CheckBooleanTrueAndOptionalString(bool flag, std::optional<std::string> text) | ||
{ | ||
EXPECT_TRUE(flag); | ||
EXPECT_TRUE(text.has_value()); | ||
EXPECT_EQ(text.value(), "some_text"); | ||
} | ||
|
||
void CheckBooleanFalseAndMissingOptionalString(bool flag, std::optional<std::string> text) | ||
{ | ||
EXPECT_FALSE(flag); | ||
EXPECT_FALSE(text.has_value()); | ||
} | ||
|
||
void CheckCallableClassWithOptional(int a, std::optional<double> b, const std::string& c) | ||
{ | ||
EXPECT_EQ(a, 70); | ||
EXPECT_TRUE(b.has_value()); | ||
EXPECT_DOUBLE_EQ(b.value(), 45.67); | ||
EXPECT_EQ(c, "test"); | ||
} | ||
|
||
void CheckCallableClassWithMissingOptional(int a, std::optional<double> b, const std::string& c) | ||
{ | ||
EXPECT_EQ(a, 70); | ||
EXPECT_FALSE(b.has_value()); | ||
EXPECT_EQ(c, "test"); | ||
} | ||
|
||
TEST(CallFunctionTests, IntegerAndString) | ||
{ | ||
std::vector<std::string> args = {"42", "hello"}; | ||
EXPECT_NO_THROW(Call(CheckIntegerAndString, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, OptionalIntegerPresent) | ||
{ | ||
std::vector<std::string> args = {"42", "100", "world"}; | ||
EXPECT_NO_THROW(Call(CheckOptionalIntegerAndStringPresent, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, OptionalIntegerMissing) | ||
{ | ||
std::vector<std::string> args = {"42", "", "world"}; | ||
EXPECT_NO_THROW(Call(CheckOptionalIntegerMissing, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, BooleanTrueAndOptionalStringPresent) | ||
{ | ||
std::vector<std::string> args = {"true", "some_text"}; | ||
EXPECT_NO_THROW(Call(CheckBooleanTrueAndOptionalString, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, BooleanFalseAndMissingOptionalString) | ||
{ | ||
std::vector<std::string> args = {"false", ""}; | ||
EXPECT_NO_THROW(Call(CheckBooleanFalseAndMissingOptionalString, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, CallableClassWithOptional) | ||
{ | ||
std::vector<std::string> args = {"70", "45.67", "test"}; | ||
EXPECT_NO_THROW(Call(CheckCallableClassWithOptional, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, CallableClassWithMissingOptional) | ||
{ | ||
std::vector<std::string> args = {"70", "", "test"}; | ||
EXPECT_NO_THROW(Call(CheckCallableClassWithMissingOptional, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, LambdaFunction_WithValidArguments) | ||
{ | ||
auto lambda = [](int x, std::optional<std::string> opt, bool flag) { | ||
EXPECT_EQ(x, 50); | ||
if (opt.has_value()) | ||
{ | ||
EXPECT_EQ(opt.value(), "optional_text"); | ||
} | ||
EXPECT_TRUE(flag); | ||
}; | ||
|
||
std::vector<std::string> args = {"50", "optional_text", "true"}; | ||
EXPECT_NO_THROW(Call(lambda, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, LambdaFunction_WithMissingOptional) | ||
{ | ||
auto lambda = [](int x, std::optional<std::string> opt, bool flag) { | ||
EXPECT_EQ(x, 50); | ||
EXPECT_FALSE(opt.has_value()); | ||
EXPECT_FALSE(flag); | ||
}; | ||
|
||
std::vector<std::string> args = {"50", "", "false"}; | ||
EXPECT_NO_THROW(Call(lambda, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, CallableClass_WithValidArguments) | ||
{ | ||
CallableClass callable_object; | ||
std::vector<std::string> args = {"70", "45.67", "test"}; | ||
EXPECT_NO_THROW(Call(callable_object, args)); | ||
} | ||
|
||
TEST(CallFunctionTests, CallableClass_WithMissingOptional) | ||
{ | ||
CallableClass callable_object; | ||
std::vector<std::string> args = {"70", "", "test"}; | ||
EXPECT_NO_THROW(Call(callable_object, args)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
dependencies = [] | ||
dependencies += libyanet_protobuf_dep | ||
dependencies += libjson.get_variable('nlohmann_json_dep') | ||
dependencies += dependency('threads') | ||
dependencies += dependency('gtest') | ||
dependencies += dependency('gtest_main') | ||
|
||
cli_sources = files() | ||
|
||
sources = files('unittest.cpp', | ||
'call.cpp', | ||
) | ||
|
||
unittest = executable('yanet-cli-unittest', | ||
[cli_sources, sources], | ||
include_directories: yanet_rootdir, | ||
dependencies: dependencies, | ||
cpp_args: cpp_args_append, | ||
override_options: 'b_lto=false') | ||
|
||
test('cli', | ||
unittest, | ||
protocol: 'gtest', | ||
timeout: 300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "common/define.h" | ||
|
||
inline common::log::LogPriority common::log::logPriority = common::log::TLOG_DEBUG; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters