Skip to content

Commit

Permalink
Share more code between examples/chip-tool and examples/placeholder (#…
Browse files Browse the repository at this point in the history
…13452)

* [chip-tool] Create ValueChecker.h and ConstraintsChecker.h from methods signature living into TestCommand.h

Use CharSpan instead of Span<const char> and add a few const prefix

* Move ValueChecker.h and ConstraintsChecker.h to src/app/tests/suites/include

* [chip-tool] Create LogCommands class and move it under src/app/tests/suites/commands/log

* Use LogCommands from src/app/tests/suites/commands/log into examples/placeholder instead of a custom implementation

* [chip-tool] Add src/app/tests/suites/include/PICSChecker.h and use it into examples/chip-tool and examples/placeholder

* Update generated test content
  • Loading branch information
vivien-apple authored Jan 11, 2022
1 parent 9f3493d commit ed95611
Show file tree
Hide file tree
Showing 13 changed files with 1,674 additions and 650 deletions.
4 changes: 4 additions & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ config("config") {

static_library("chip-tool-utils") {
sources = [
"${chip_root}/src/app/tests/suites/include/ConstraintsChecker.h",
"${chip_root}/src/app/tests/suites/include/ValueChecker.h",
"commands/clusters/ModelCommand.cpp",
"commands/common/CHIPCommand.cpp",
"commands/common/CHIPCommand.h",
Expand All @@ -63,6 +65,7 @@ static_library("chip-tool-utils") {

deps = [
"${chip_root}/src/app/server",
"${chip_root}/src/app/tests/suites/commands/log",
"${chip_root}/src/app/tests/suites/pics",
"${chip_root}/src/controller/data_model",
"${chip_root}/src/lib",
Expand All @@ -87,6 +90,7 @@ executable("chip-tool") {
deps = [
":chip-tool-utils",
"${chip_root}/src/app/server",
"${chip_root}/src/app/tests/suites/commands/log",
"${chip_root}/src/app/tests/suites/pics",
"${chip_root}/src/controller/data_model",
"${chip_root}/src/lib",
Expand Down
114 changes: 0 additions & 114 deletions examples/chip-tool/commands/tests/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,6 @@ CHIP_ERROR TestCommand::Wait(chip::System::Clock::Timeout duration)
return chip::DeviceLayer::SystemLayer().StartTimer(duration, OnWaitForMsFn, this);
}

CHIP_ERROR TestCommand::Log(const char * message)
{
ChipLogDetail(chipTool, "%s", message);
ReturnErrorOnFailure(ContinueOnChipMainThread());
return CHIP_NO_ERROR;
}

CHIP_ERROR TestCommand::UserPrompt(const char * message)
{
ChipLogDetail(chipTool, "USER_PROMPT: %s", message);
ReturnErrorOnFailure(ContinueOnChipMainThread());
return CHIP_NO_ERROR;
}

void TestCommand::Exit(std::string message)
{
ChipLogError(chipTool, " ***** Test Failure: %s\n", message.c_str());
Expand All @@ -94,103 +80,3 @@ void TestCommand::ThrowSuccessResponse()
{
Exit("Expecting failure response but got a success response");
}

bool TestCommand::CheckConstraintType(const char * itemName, const char * current, const char * expected)
{
ChipLogError(chipTool, "Warning: %s type checking is not implemented yet. Expected type: '%s'", itemName, expected);
return true;
}

bool TestCommand::CheckConstraintFormat(const char * itemName, const char * current, const char * expected)
{
ChipLogError(chipTool, "Warning: %s format checking is not implemented yet. Expected format: '%s'", itemName, expected);
return true;
}

bool TestCommand::CheckConstraintStartsWith(const char * itemName, const chip::Span<const char> current, const char * expected)
{
std::string value(current.data(), current.size());
if (value.rfind(expected, 0) != 0)
{
Exit(std::string(itemName) + " (\"" + value + "\") does not starts with: \"" + std::string(expected) + "\"");
return false;
}

return true;
}

bool TestCommand::CheckConstraintEndsWith(const char * itemName, const chip::Span<const char> current, const char * expected)
{
std::string value(current.data(), current.size());
if (value.find(expected, value.size() - strlen(expected)) == std::string::npos)
{
Exit(std::string(itemName) + " (\"" + value + "\") does not ends with: \"" + std::string(expected) + "\"");
return false;
}

return true;
}

bool TestCommand::CheckConstraintMinLength(const char * itemName, uint64_t current, uint64_t expected)
{
if (current < expected)
{
Exit(std::string(itemName) + " length < minLength: " + std::to_string(current) + " < " + std::to_string(expected));
return false;
}

return true;
}

bool TestCommand::CheckConstraintMaxLength(const char * itemName, uint64_t current, uint64_t expected)
{
if (current > expected)
{
Exit(std::string(itemName) + " length > minLength: " + std::to_string(current) + " > " + std::to_string(expected));
return false;
}

return true;
}

bool TestCommand::CheckValueAsString(const char * itemName, chip::ByteSpan current, chip::ByteSpan expected)
{
if (!current.data_equal(expected))
{
Exit(std::string(itemName) + " value mismatch, expecting " +
std::string(chip::Uint8::to_const_char(expected.data()), expected.size()));
return false;
}

return true;
}

bool TestCommand::CheckValueAsString(const char * itemName, chip::CharSpan current, chip::CharSpan expected)
{
if (!current.data_equal(expected))
{
Exit(std::string(itemName) + " value mismatch, expected '" + std::string(expected.data(), expected.size()) + "' but got '" +
std::string(current.data(), current.size()) + "'");
return false;
}

return true;
}

bool TestCommand::ShouldSkip(const char * expression)
{
// If there is no PICS configuration file, considers that nothing should be skipped.
if (!PICS.HasValue())
{
return false;
}

std::map<std::string, bool> pics(PICS.Value());
bool shouldSkip = !PICSBooleanExpressionParser::Eval(expression, pics);
if (shouldSkip)
{
ChipLogProgress(chipTool, " **** Skipping: %s == false\n", expression);
ContinueOnChipMainThread();
}
return shouldSkip;
}
Loading

0 comments on commit ed95611

Please sign in to comment.