diff --git a/java_console/io/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java b/java_console/io/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java index e2faf59aa6..e930be7a98 100644 --- a/java_console/io/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java +++ b/java_console/io/src/test/java/com/rusefi/tune/LoadOlderTuneTest.java @@ -3,6 +3,7 @@ package com.rusefi.tune; import com.opensr5.ini.DialogModel; import com.opensr5.ini.IniFileModel; import com.rusefi.core.preferences.storage.Node; +import com.rusefi.tools.tune.TuneTools; import com.rusefi.tune.xml.Constant; import com.rusefi.tune.xml.Msq; import com.rusefi.tune.xml.Page; @@ -63,4 +64,19 @@ public class LoadOlderTuneTest { return value; return value.replaceAll("\\s+", " ").trim(); } + + @Test(expected = IllegalStateException.class) + public void testLegacyCustomEnumOrdinal() { + String tsCustomLine = "bits, U08, @OFFSET@, [0:1], \"Single Coil\", \"Individual Coils\", \"Wasted Spark\", \"Two Distributors\""; + + assertEquals(0, TuneTools.resolveEnumByName(tsCustomLine, "One coil")); + } + + @Test + public void testCustomEnumOrdinal() { + String tsCustomLine = "bits, U08, @OFFSET@, [0:1], \"Single Coil\", \"Individual Coils\", \"Wasted Spark\", \"Two Distributors\""; + + assertEquals(0, TuneTools.resolveEnumByName(tsCustomLine, "Single coil")); + assertEquals(3, TuneTools.resolveEnumByName(tsCustomLine, "Two Distributors")); + } } diff --git a/java_console/models/src/main/java/com/rusefi/tools/tune/TuneTools.java b/java_console/models/src/main/java/com/rusefi/tools/tune/TuneTools.java new file mode 100644 index 0000000000..71174f52a0 --- /dev/null +++ b/java_console/models/src/main/java/com/rusefi/tools/tune/TuneTools.java @@ -0,0 +1,19 @@ +package com.rusefi.tools.tune; + +public class TuneTools { + private static String quote(String string) { + return "\"" + string + "\""; + } + + public static int resolveEnumByName(String tsCustomLine, String key) { + String quotedKey = quote(key); + String[] tokens = tsCustomLine.split(","); + int magicOffset = 4; // skipping meta info header + for (int i = 0; i < tokens.length - magicOffset; i++) { + String token = tokens[i + magicOffset].trim(); + if (token.equalsIgnoreCase(quotedKey)) + return i; + } + throw new IllegalStateException("Enum name [" + key + "] not found in " + tsCustomLine); + } +}