tbx/test/tests/sequencer.cpp

124 lines
5.2 KiB
C++

/*!
* \file sequencer.cpp
*
* \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
*
* <dl class=\"section copyright\"><dt>License</dt><dd>
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* </dd></dl>
*
*/
#include <utils/sequencer.h>
#include <gtest/gtest.h>
namespace test_sequencer {
using namespace tbx;
// Sequencer implementer mock
class Seq : public sequencer_t<Seq, char, 128> {
const char *msg_[10] = {
"", "", "", "\r\nOK\r\n",
"", "", "+CCLK = \"21/08/26-12:16:30+12\"\r\nOK\r\n"
"", "", "\r\nERROR\r\n"
};
public:
size_t get(char* data) {
static int msg =0;
size_t len = strlen(msg_[msg]);
strcpy(data, msg_[msg++]);
if (msg >= 10) msg =0;
return len;
}
size_t put (const char* data, size_t n) {
(void)*data;
return n;
}
clock_t clock() { static clock_t t=0; return ++t; }
static status_t my_handler (const char* data, size_t size) {
(void)*data;
(void)size;
return Seq::status_t::OK;
}
};
/*
* Test sequencer.run()
*/
TEST(Tsequencer, run_delay) {
Seq s;
const std::array<Seq::record_t, 1> script = {{
/* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
}};
EXPECT_EQ ((int)Seq::status_t::OK, (int)s.run(script));
}
TEST(Tsequencer, run_dummy_output) {
Seq s;
const std::array<Seq::record_t, 2> script = {{
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
/* 1 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
}};
EXPECT_EQ ((int)Seq::status_t::OK, (int)s.run(script));
}
TEST(Tsequencer, run_exits) {
Seq s;
const std::array<Seq::record_t, 1> script1 = {{
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000},
}};
EXPECT_EQ ((int)Seq::status_t::OK, (int)s.run(script1));
const std::array<Seq::record_t, 1> script2 = {{
/* 0 */{Seq::control_t::SEND, {"", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_ERROR, 0}, 1000},
}};
EXPECT_EQ ((int)Seq::status_t::ERROR, (int)s.run(script2));
}
TEST(Tsequencer, run_sequence) {
Seq s;
const std::array<Seq::record_t, 8> script = {{
/* 0 */{Seq::control_t::NOP, {"", Seq::match_t::NO, nullptr, Seq::action_t::GOTO, 1}, 1000},
/* 1 */{Seq::control_t::SEND, {"ATE0\r\n", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
/* 2 */{Seq::control_t::EXPECT, {{
{"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
1000
},
/* 3 */{Seq::control_t::SEND, {"AT+CCLK?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
/* 4 */{Seq::control_t::EXPECT, {{
{"OK\r\n", Seq::match_t::ENDS_WITH, Seq::my_handler, Seq::action_t::NEXT, 0},
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
1000
},
/* 5 */{Seq::control_t::SEND, {"AT+CT?", Seq::match_t::NO, nullptr, Seq::action_t::NEXT, 0}, 1000},
/* 6 */{Seq::control_t::EXPECT, {{
{"OK\r\n", Seq::match_t::ENDS_WITH, nullptr, Seq::action_t::NEXT, 0},
{"ERROR", Seq::match_t::CONTAINS, nullptr, Seq::action_t::EXIT_ERROR, 0} }},
1000
},
/* 7 */{Seq::control_t::SEND, {"AT+POWD=0", Seq::match_t::NO, nullptr, Seq::action_t::EXIT_OK, 0}, 1000}
}};
EXPECT_EQ ((int)Seq::status_t::ERROR, (int)s.run(script));
}
}