Skip to content

Commit

Permalink
Enhanced unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mauer committed Dec 20, 2023
1 parent e930769 commit 541b12f
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 34 deletions.
6 changes: 3 additions & 3 deletions lib/env/tests/command_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
// If not, see <https://www.gnu.org/licenses/>.
//---------------------------------------------------------------------------------------------------------------------

#ifndef XMC_CMD_TESTS_H
#define XMC_CMD_TESTS_H
#ifndef XMC_COMMAND_TESTS_H
#define XMC_COMMAND_TESTS_H

// Standard
#include <map>
Expand Down Expand Up @@ -49,4 +49,4 @@ class command_tests : public commands {

} // Namespace xmidictrl

#endif // XMC_CMD_TESTS_H
#endif // XMC_COMMAND_TESTS_H
24 changes: 20 additions & 4 deletions lib/env/tests/data_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ namespace xmidictrl {
/**
* Check if a dataref is valid
*/
bool data_tests::check(std::string_view)
bool data_tests::check(std::string_view in_name)
{
return true;
if (m_data_string.contains(in_name.data()))
return true;

if (m_data_float.contains(in_name.data()))
return true;

return false;
}


Expand All @@ -39,7 +45,12 @@ bool data_tests::read(text_logger& in_log, std::string_view in_name, std::string
{
out_value = std::string();

return true;
if (m_data_string.contains(in_name.data())) {
out_value = m_data_string.at(in_name.data());
return true;
}

return false;
}


Expand All @@ -50,7 +61,12 @@ bool data_tests::read(text_logger& in_log, std::string_view in_name, float& out_
{
out_value = 0.0f;

return true;
if (m_data_float.contains(in_name.data())) {
out_value = m_data_float.at(in_name.data());
return true;
}

return false;
}


Expand Down
5 changes: 4 additions & 1 deletion lib/env/tests/data_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ class data_tests : public data {
std::string_view in_value_on,
std::string_view in_value_off) override;

private:
std::map<std::string, std::string> m_data_string {};
std::map<std::string, float> m_data_float {};
};

} // Namespace xmidictrl

#endif // DATA_STANDALONE_H
#endif // XMC_DATA_TESTS_H
7 changes: 2 additions & 5 deletions lib/map/map_init/map_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,10 @@ std::shared_ptr<outbound_task> map_init::execute()
break;
}

task->channel = channel();
task->data_1 = data_1();
task->channel = static_cast<char>(channel());
task->data_1 = static_cast<char>(data_1());
task->data_2 = static_cast<char>(m_data_2);

// TODO: add mapping to task
//task->mapping = this;

return task;
}

Expand Down
7 changes: 2 additions & 5 deletions lib/map/map_out/map_out_con.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,10 @@ std::shared_ptr<outbound_task> map_out_con::execute(text_logger&, outbound_send_
break;
}

task->channel = channel();
task->data_1 = data_1();
task->channel = static_cast<char>(channel());
task->data_1 = static_cast<char>(data_1());
task->data_2 = static_cast<char>(m_data_2);

// TODO: add mapping to task
//task->mapping = this;

return task;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/map/map_out/map_out_drf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ std::shared_ptr<outbound_task> map_out_drf::execute(text_logger& in_log,
break;
}

task->channel = channel();
task->data_1 = data_1();
task->channel = static_cast<char>(channel());
task->data_1 = static_cast<char>(data_1());

if ((m_send_on == send_mode::all && send_on_cnt == m_datarefs.size())
|| (m_send_on == send_mode::one && send_on_cnt > 0))
task->data_2 = m_data_2_on;
task->data_2 = static_cast<char>(m_data_2_on);
else
task->data_2 = m_data_2_off;
task->data_2 = static_cast<char>(m_data_2_off);

return task;
}
Expand Down
9 changes: 3 additions & 6 deletions lib/map/map_out/map_out_sld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,9 @@ std::shared_ptr<outbound_task> map_out_sld::execute(text_logger& in_log,
break;
}

task->channel = channel();
task->data_1 = data_1();
task->data_2 = static_cast<unsigned char>(data_2);

// TODO add mapping to task
//task->mapping = this;
task->channel = static_cast<char>(channel());
task->data_1 = static_cast<char>(data_1());
task->data_2 = static_cast<char>(data_2);

return task;
}
Expand Down
3 changes: 3 additions & 0 deletions src/profile/midi_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ void midi_device::process_init_mappings()
if (task == nullptr)
continue;

// add mapping
task->mapping = mapping;

if (m_midi_out != nullptr && m_midi_out->isPortOpen())
add_outbound_task(task);
}
Expand Down
5 changes: 0 additions & 5 deletions src/profile/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,14 @@ std::string profile::get_filename_profiles_path(filename_prefix in_prefix)
*/
void profile::process(text_logger& in_log)
{
in_log.debug("Process outbound messages");

if (!m_init_send) {
in_log.debug("Process outbound init messages");
// process init mappings
m_device_list->process_init_mappings();
m_init_send = true;
}

// process midi outbound mappings
in_log.debug("Process outbound messages 2");
m_device_list->process_outbound_mappings(in_log);
in_log.debug("Process outbound messages 3");
}


Expand Down
43 changes: 42 additions & 1 deletion tests/test_map_in_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ TEST_CASE("Test Inbound Mapping for commands with default data 2")
}


TEST_CASE("Test Inbound Mapping for commands with sublayer")
{
auto* log = new text_logger();
auto* env = new env_tests(*log);

using namespace toml::literals::toml_literals;
toml::value cfg = u8R"(
ch = 16
cc = 20
sl = "1"
command = "sim/autopilot/enable"
)"_toml;

auto map = new map_in_cmd(*env);
map->read_config(*log, cfg, cfg);

auto msg = new midi_message(*log, midi_direction::in);
msg->set_data_2(MIDI_DATA_2_MAX);

CHECK(map->check(*log));
CHECK(map->execute(*msg, "2"));
CHECK(env->cmd_tests().current_command() == "");

CHECK(map->check(*log));
CHECK(map->execute(*msg, "1"));
CHECK(env->cmd_tests().current_command() == "sim/autopilot/enable");

msg->set_data_2(MIDI_DATA_2_MIN);

CHECK(map->execute(*msg, "2"));
CHECK(env->cmd_tests().current_command() == "sim/autopilot/enable");

CHECK(map->execute(*msg, "1"));
CHECK(env->cmd_tests().current_command() == "");
CHECK(env->cmd_tests().last_command() == "sim/autopilot/enable");
}


TEST_CASE("Test Inbound Mapping for commands with custom data 2")
{
auto* log = new text_logger();
Expand Down Expand Up @@ -90,4 +128,7 @@ TEST_CASE("Test Inbound Mapping for commands with custom data 2")
CHECK(map->execute(*msg, ""));
CHECK(env->cmd_tests().current_command() == "");
CHECK(env->cmd_tests().last_command() == "sim/autopilot/enable");
}
}


// TODO: Add unit tests with sublayer

0 comments on commit 541b12f

Please sign in to comment.