Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edge case tests for extract_value and fix the newly discovered bug #1808

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions crates/node-bindings/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub(crate) fn unused_port() -> u16 {
}

/// Extracts the value for the given key from the line of text.
///
/// It supports keys that end with '=' or ': '.
/// For keys end with '=', find value until ' ' is encountered or end of line
/// For keys end with ':', find value until ',' is encountered or end of line
pub(crate) fn extract_value<'a>(key: &str, line: &'a str) -> Option<&'a str> {
let mut key_equal = Cow::from(key);
let mut key_colon = Cow::from(key);
Expand All @@ -47,9 +50,9 @@ pub(crate) fn extract_value<'a>(key: &str, line: &'a str) -> Option<&'a str> {
// If not found, try to find the key with ': '
if let Some(pos) = line.find(key_colon.as_ref()) {
let start = pos + key_colon.len();
let end = line[start..].find(',').unwrap_or(line.len()); // Assuming comma or end of line
if start <= line.len() && start + end <= line.len() {
return Some(line[start..start + end].trim());
let end = line[start..].find(',').map(|i| start + i).unwrap_or(line.len()); // Assuming comma or end of line
if start <= line.len() && end <= line.len() {
return Some(line[start..end].trim());
}
}

Expand Down Expand Up @@ -109,6 +112,18 @@ mod tests {
assert_eq!(extract_value("key", line), None);
}

#[test]
fn test_extract_value_equals_no_space() {
let line = "INFO key=";
assert_eq!(extract_value("key", line), Some(""))
}

#[test]
fn test_extract_value_colon_no_comma() {
let line = "INFO key: value";
assert_eq!(extract_value("key", line), Some("value"))
}

#[test]
fn test_extract_http_address() {
let line = "INFO [07-01|13:20:42.774] HTTP server started endpoint=127.0.0.1:8545 auth=false prefix= cors= vhosts=localhost";
Expand Down
Loading