findSensorTypeByName

This commit is contained in:
Andrey 2021-10-21 14:33:59 -04:00
parent 37594d5646
commit b750755369
2 changed files with 33 additions and 7 deletions

View File

@ -59,12 +59,19 @@ static int lua_getAuxAnalog(lua_State* l) {
return getSensor(l, type);
}
static int lua_getSensor(lua_State* l) {
static int lua_getSensorByIndex(lua_State* l) {
auto sensorIndex = luaL_checkinteger(l, 1);
return getSensor(l, static_cast<SensorType>(sensorIndex));
}
static int lua_getSensorByName(lua_State* l) {
auto sensorName = luaL_checklstring(l, 1, nullptr);
SensorType type = findSensorTypeByName(sensorName);
return getSensor(l, type);
}
static int lua_getSensorRaw(lua_State* l) {
auto sensorIndex = luaL_checkinteger(l, 1);
@ -344,7 +351,8 @@ void configureRusefiLuaHooks(lua_State* l) {
lua_register(l, "print", lua_efi_print);
lua_register(l, "readPin", lua_readpin);
lua_register(l, "getAuxAnalog", lua_getAuxAnalog);
lua_register(l, "getSensor", lua_getSensor);
lua_register(l, "getSensorByIndex", lua_getSensorByIndex);
lua_register(l, "getSensor", lua_getSensorByName);
lua_register(l, "getSensorRaw", lua_getSensorRaw);
lua_register(l, "hasSensor", lua_hasSensor);
lua_register(l, "table3d", lua_table3d);

View File

@ -2,22 +2,40 @@
#include "rusefi_lua.h"
static const char* getSensorTest = R"(
static const char* getSensorTestByIndex = R"(
function testFunc()
return getSensor(10)
return getSensorByIndex(10)
end
)";
TEST(LuaHooks, TestGetSensor) {
TEST(LuaHooks, TestGetSensorByIndex) {
// Test failed sensor, returns nil
Sensor::resetMockValue(static_cast<SensorType>(10));
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTest), unexpected);
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByIndex), unexpected);
// Now test with a value, returns value
Sensor::setMockValue(10, 33);
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTest).value_or(0), 33);
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByIndex).value_or(0), 33);
}
static const char* getSensorTestByName = R"(
function testFunc()
return getSensor("CLT")
end
)";
TEST(LuaHooks, TestGetSensorByName) {
// Test failed sensor, returns nil
Sensor::resetMockValue(SensorType::Clt);
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByName), unexpected);
// Now test with a value, returns value
Sensor::setMockValue((int)SensorType::Clt, 33);
EXPECT_EQ(testLuaReturnsNumberOrNil(getSensorTestByName).value_or(0), 33);
}
static const char* tableTest = R"(