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

Conversation

hthuz
Copy link
Contributor

@hthuz hthuz commented Dec 17, 2024

Motivation

Add two edge case tests for extract_value in https://github.com/alloy-rs/alloy/blob/main/crates/node-bindings/src/utils.rs#L26, which extract values given keys from the line of text.
When there is no comma (like extract value from string "key: value"), the function will fail the test.
Also add doc to better explain this function.

Solution

The code uses different patterns for finding values for keys ending with '=' and ': ', as shown below. By changing them into the same pattern, the issue is fixed. 10 tests in utils.rs are all passed.

    // Try to find the key with '='
    if let Some(pos) = line.find(key_equal.as_ref()) {
        let start = pos + key_equal.len();
        let end = line[start..].find(' ').map(|i| start + i).unwrap_or(line.len());
        if start <= line.len() && end <= line.len() {
            return Some(line[start..end].trim());
        }
    }

    // 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();
       // NOTE that there's no map here. If end of line is met, start + end will definited > line.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());
        }
    }

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty

@mattsse mattsse merged commit e551eb7 into alloy-rs:main Dec 17, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants