Skip to content

Commit

Permalink
🚧 #70 #78 ExtractChipIsFromStringMapper test
Browse files Browse the repository at this point in the history
  • Loading branch information
rucko24 committed Nov 9, 2024
1 parent 8511492 commit f79f5a1
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ private ExtractChipIsFromStringMapper() {}
* @return A {@link String}
*/
public String getChipIsFromThisString(String input) {
String chipRegex = "(?s)Chip is (\\S+).*?Features:";
Pattern pattern = Pattern.compile(chipRegex);
Matcher matcher = pattern.matcher(input);
final String notNullInput = Objects.isNull(input) ? StringUtils.EMPTY : input;
final String chipRegex = "(?s)Chip is (\\S+).*?Features:";
final Pattern pattern = Pattern.compile(chipRegex);
final Matcher matcher = pattern.matcher(notNullInput);
String chipIs = StringUtils.EMPTY;
if (matcher.find()) {
chipIs = matcher.group(1);
}
return Objects.equals(chipIs, StringUtils.EMPTY) ? "Can`t parsed this Chip" :
chipIs.split("Features:")[0];
return Objects.equals(chipIs, StringUtils.EMPTY)
? "Can`t parsed this Chip"
: chipIs.split("Features:")[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.esp.espflow.mappers;

import com.esp.espflow.mappers.provider.ExtractChipIsFromStringMapperFailureProvider;
import com.esp.espflow.mappers.provider.ExtractChipIsFromStringMapperSuccessProvider;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("When extracting the 'Chip is ESP82XX' from the text processed in the Xterm, we check for possible " +
"errors and then send the event correctly in the notification panel.")
class ExtractChipIsFromStringMapperTest {

@ParameterizedTest
@ArgumentsSource(ExtractChipIsFromStringMapperSuccessProvider.class)
@DisplayName("The chip is obtained from the input String")
void getChipIsFromThisString_Success(String inputLines) {

final String chipIs = ExtractChipIsFromStringMapper.INSTANCE.getChipIsFromThisString(inputLines);

assertThat(chipIs).isEqualTo("ESP8266EX");

}

@ParameterizedTest
@ArgumentsSource(ExtractChipIsFromStringMapperFailureProvider.class)
@DisplayName("When the Chip cannot be parsed, cannot be matched because of lowercase f")
void attemptToParseTheChip_Failure(final String inputLine) {

final String chipIs = ExtractChipIsFromStringMapper.INSTANCE.getChipIsFromThisString(inputLine);

assertThat(chipIs).isEqualTo("Can`t parsed this Chip");

}

@Test
@DisplayName("Input String is null, something happens when the text arrives at the Xterm and its text is null")
void attemptToParseTheChip_Failure2() {

final String chipIs = ExtractChipIsFromStringMapper.INSTANCE.getChipIsFromThisString(null);

assertThat(chipIs).isEqualTo("Can`t parsed this Chip");

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.esp.espflow.mappers.provider;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import java.util.stream.Stream;

public class ExtractChipIsFromStringMapperFailureProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {

String parseMePlease = "esptool.py --port COM5 --baud 115200 " +
"flash_id esptool.py v4.7.0 " +
"Serial port COM5 " +
"Connecting.... " +
"Detecting chip type... " +
"Unsupported detection protocol, switching and trying again... " +
"Connecting.... " +
"Detecting chip type... ESP8266 " +
"Chip is ESP8266EX " +
"features: WiFi " +
"Crystal is 26MHz " +
"MAC: 2c:f4:32:10:1d:bf " +
"Uploading stub... " +
"Running stub... " +
"Stub running... " +
"Manufacturer: 85 " +
"Device: 6014 " +
"Detected flash size: 1MB " +
"Hard resetting via RTS pin...";

return Stream.of(Arguments.of(parseMePlease));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.esp.espflow.mappers.provider;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import java.util.stream.Stream;

public class ExtractChipIsFromStringMapperSuccessProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {

String parseMePlease = "esptool.py --port COM5 --baud 115200 " +
"flash_id esptool.py v4.7.0 " +
"Serial port COM5 " +
"Connecting.... " +
"Detecting chip type... " +
"Unsupported detection protocol, switching and trying again... " +
"Connecting.... " +
"Detecting chip type... ESP8266 " +
"Chip is ESP8266EX " +
"Features: WiFi " +
"Crystal is 26MHz " +
"MAC: 2c:f4:32:10:1d:bf " +
"Uploading stub... " +
"Running stub... " +
"Stub running... " +
"Manufacturer: 85 " +
"Device: 6014 " +
"Detected flash size: 1MB " +
"Hard resetting via RTS pin...";
return Stream.of(Arguments.of(parseMePlease));
}
}

0 comments on commit f79f5a1

Please sign in to comment.