only:more LiveDataProcessor coverage

This commit is contained in:
rusefillc 2023-12-06 12:32:50 -05:00
parent 997a72e184
commit a56d7fdfb3
7 changed files with 55 additions and 60 deletions

View File

@ -15,7 +15,4 @@ public interface LazyFile extends Output {
@Override
void write(String line);
@Override
void close() throws IOException;
}

View File

@ -2,7 +2,7 @@ package com.rusefi.util;
import java.io.IOException;
public interface Output {
public interface Output extends AutoCloseable {
void write(String line);
void close() throws IOException;

View File

@ -12,6 +12,7 @@ dependencies {
api project(':inifile')
api project(':enum_to_string')
api global_libs.snakeyaml
testFixturesApi global_libs.junit
}
shadowJar {

View File

@ -21,6 +21,8 @@ public class LiveDataProcessor {
private final static String tsOutputsDestination = "console/binary/";
public static final String DATA_LOG_FILE_NAME = tsOutputsDestination + File.separator + "generated/data_logs.ini";
public static final String OUTPUTS_SECTION_FILE_NAME = tsOutputsDestination + File.separator + "generated/output_channels.ini";
public static final String DATA_FRAGMENTS_H = "console/binary/generated/live_data_fragments.h";
public static final String STATE_DICTIONARY_FACTORY_JAVA = "../java_console/io/src/main/java/com/rusefi/enums/StateDictionaryFactory.java";
private final ReaderProvider readerProvider;
private final LazyFile.LazyFileFactory fileFactory;
@ -65,7 +67,6 @@ public class LiveDataProcessor {
LiveDataProcessor liveDataProcessor = new LiveDataProcessor(yamlFileName, ReaderProvider.REAL, LazyFile.REAL);
int sensorTsPosition = liveDataProcessor.handleYaml(data);
liveDataProcessor.writeFiles();
log.info("TS_TOTAL_OUTPUT_SIZE=" + sensorTsPosition);
try (FileWriter fw = new FileWriter("console/binary/generated/total_live_data_generated.h")) {
@ -257,20 +258,22 @@ public class LiveDataProcessor {
outputValueConsumer.endFile();
GetConfigValueConsumer.writeStringToFile("../java_console/io/src/main/java/com/rusefi/enums/StateDictionaryFactory.java", stateDictionaryGenerator.getCompleteClass(), fileFactory);
GetConfigValueConsumer.writeStringToFile(STATE_DICTIONARY_FACTORY_JAVA, stateDictionaryGenerator.getCompleteClass(), fileFactory);
totalSensors.append(javaSensorsConsumer.getContent());
writeFiles();
return javaSensorsConsumer.sensorTsPosition;
}
private void writeFiles() throws IOException {
try (FileWriter fw = new FileWriter(enumContentFileName)) {
try (LazyFile fw = fileFactory.create(enumContentFileName)) {
fw.write(enumContent.toString());
fw.write(baseAddressCHeader.toString());
}
try (FileWriter fw = new FileWriter("console/binary/generated/live_data_fragments.h")) {
try (LazyFile fw = fileFactory.create(DATA_FRAGMENTS_H)) {
fw.write(fragmentsContent.toString());
}
}

View File

@ -9,52 +9,22 @@ import java.util.Map;
import static org.junit.Assert.assertEquals;
public class LiveDataProcessorTest {
@Test
public void testUnnamedSection() throws IOException {
String testYaml = "Usages:\n" +
"# output_channels always goes first at least because it has protocol version at well-known offset\n" +
" - name: output_channels\n" +
" java: TsOutputs.java\n" +
" folder: console/binary\n" +
" cppFileName: status_loop\n" +
" constexpr: \"engine->outputChannels\"\n";
Map<String, Object> data = LiveDataProcessor.getStringObjectMap(new StringReader(testYaml));
TestFileCaptor captor = new TestFileCaptor();
LiveDataProcessor liveDataProcessor = new LiveDataProcessor("test", new ReaderProvider() {
@Override
public Reader read(String fileName) {
System.out.println("read " + fileName);
return new StringReader("struct_no_prefix wideband_state_s\n" +
"\tuint16_t tempC;WBO: Temperature;\"C\", 1, 0, 500, 1000, 0\n" +
"\tuint16_t esr;WBO: ESR;\"ohm\", 1, 0, 0, 10000, 0\n" +
"end_struct");
}
}, captor);
liveDataProcessor.handleYaml(data);
assertEquals(7, captor.fileCapture.size());
assertEquals("tempC = scalar, U16, 0, \"C\", 1, 0\n" +
"esr = scalar, U16, 2, \"ohm\", 1, 0\n" +
"; total TS size = 4\n", captor.fileCapture.get(LiveDataProcessor.OUTPUTS_SECTION_FILE_NAME).sb.toString());
assertEquals("entry = tempC, \"WBO: Temperature\", int, \"%d\"\n" +
"entry = esr, \"WBO: ESR\", int, \"%d\"\n", captor.fileCapture.get(LiveDataProcessor.DATA_LOG_FILE_NAME).sb.toString());
}
@Test
public void testTwoSections() throws IOException {
String testYaml = "Usages:\n" +
"# output_channels always goes first at least because it has protocol version at well-known offset\n" +
" - name: output_channels\n" +
" - name: wbo_channels\n" +
" java: TsOutputs.java\n" +
" folder: console/binary\n" +
" cppFileName: status_loop\n" +
" output_name: [ \"wb1\", \"wb2\" ]\n" +
" constexpr: \"engine->outputChannels\"\n";
" constexpr: \"engine->outputChannels\"\n" +
"# output_channels always goes first at least because it has protocol version at well-known offset\n" +
" - name: output_channels\n" +
" java: TsOutputs.java\n" +
" folder: console/binary\n" +
" cppFileName: status_loop\n" +
" constexpr: \"engine->outputChannels\"\n"
;
Map<String, Object> data = LiveDataProcessor.getStringObjectMap(new StringReader(testYaml));
@ -64,21 +34,39 @@ public class LiveDataProcessorTest {
@Override
public Reader read(String fileName) {
System.out.println("read " + fileName);
return new StringReader("struct_no_prefix wideband_state_s\n" +
"\tuint16_t tempC;WBO: Temperature;\"C\", 1, 0, 500, 1000, 0\n" +
"\tuint16_t esr;WBO: ESR;\"ohm\", 1, 0, 0, 10000, 0\n" +
"end_struct");
if (fileName.contains("output_channels")) {
return new StringReader("struct_no_prefix output_state_s\n" +
"\tuint16_t oootempC;Temperature;\"C\", 1, 0, 500, 1000, 0\n" +
"\tuint16_t oooesr;ESR;\"ohm\", 1, 0, 0, 10000, 0\n" +
"end_struct");
} else {
return new StringReader("struct_no_prefix wideband_state_s\n" +
"\tuint16_t tempC;WBO: Temperature;\"C\", 1, 0, 500, 1000, 0\n" +
"\tuint16_t esr;WBO: ESR;\"ohm\", 1, 0, 0, 10000, 0\n" +
"end_struct");
}
}
}, captor);
liveDataProcessor.handleYaml(data);
assertEquals(7, captor.fileCapture.size());
assertEquals(10, captor.fileCapture.size());
assertEquals("tempC = scalar, U16, 0, \"C\", 1, 0\n" +
captor.assertOutput("tempC = scalar, U16, 0, \"C\", 1, 0\n" +
"esr = scalar, U16, 2, \"ohm\", 1, 0\n" +
"; total TS size = 4\n", captor.fileCapture.get(LiveDataProcessor.OUTPUTS_SECTION_FILE_NAME).sb.toString());
"; total TS size = 4\n" +
"oootempC = scalar, U16, 4, \"C\", 1, 0\n" +
"oooesr = scalar, U16, 6, \"ohm\", 1, 0\n" +
"; total TS size = 8\n", LiveDataProcessor.OUTPUTS_SECTION_FILE_NAME);
assertEquals("entry = tempC, \"WBO: Temperature\", int, \"%d\"\n" +
"entry = esr, \"WBO: ESR\", int, \"%d\"\n", captor.fileCapture.get(LiveDataProcessor.DATA_LOG_FILE_NAME).sb.toString());
captor.assertOutput("entry = tempC, \"WBO: Temperature\", int, \"%d\"\n" +
"entry = esr, \"WBO: ESR\", int, \"%d\"\n" +
"entry = oootempC, \"Temperature\", int, \"%d\"\n" +
"entry = oooesr, \"ESR\", int, \"%d\"\n", LiveDataProcessor.DATA_LOG_FILE_NAME);
captor.assertOutput("// generated by gen_live_documentation.sh / LiveDataProcessor.java\n" +
"decl_frag<wbo_channels_s, 0>{},\t// wb1\n" +
"// decl_frag<wbo_channels_s, 1>{},\t// wb2\n" +
"decl_frag<output_channels_s>{},\n", LiveDataProcessor.DATA_FRAGMENTS_H);
}
}

View File

@ -1,11 +1,9 @@
package com.rusefi.ldmp;
import com.rusefi.util.LazyFile;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@ -34,8 +32,7 @@ public class StateDictionaryGeneratorTest {
TestFileCaptor captor = new TestFileCaptor();
LiveDataProcessor liveDataProcessor = new LiveDataProcessor("test", fileName -> new StringReader(""), captor);
liveDataProcessor.handleYaml(data);
assertEquals(7, captor.fileCapture.size());
assertEquals(9, captor.fileCapture.size());
assertEquals(" stateDictionary.register(live_data_e.LDS_output_channels, TsOutputs.VALUES, \"status_loop\");\n" +
" stateDictionary.register(live_data_e.LDS_fuel_computer, FuelComputer.VALUES, \"fuel_computer\");\n", liveDataProcessor.stateDictionaryGenerator.content.toString());

View File

@ -5,9 +5,18 @@ import com.rusefi.util.LazyFile;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class TestFileCaptor implements LazyFile.LazyFileFactory {
Map<String, StringBufferLazyFile> fileCapture = new HashMap<>();
void assertOutput(String expected, String key) {
StringBufferLazyFile stringBufferLazyFile = fileCapture.get(key);
assertNotNull("Not found " + key, stringBufferLazyFile);
assertEquals(expected, stringBufferLazyFile.sb.toString());
}
@Override
public LazyFile create(String fileName) {
StringBufferLazyFile file = new StringBufferLazyFile();