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

test: unit test for address file #1612

Closed
wants to merge 1 commit into from
Closed

test: unit test for address file #1612

wants to merge 1 commit into from

Conversation

milton-cw
Copy link

@milton-cw milton-cw commented Aug 6, 2024

PR Type

Tests


Description

  • Added multiple unit tests for the Address struct and related functionalities in src/eth/primitives/address.rs.
  • Tests include:
    • Dereferencing and display of Address.
    • Valid and invalid conversions from byte arrays and strings.
    • Checks for special addresses like coinbase and zero address.
    • Conversion between Address and other types like H160, Token, and RevmAddress.
    • Serialization and deserialization using Serde.

Changes walkthrough 📝

Relevant files
Tests
address.rs
Add unit tests for `Address` struct and related functionalities

src/eth/primitives/address.rs

  • Added multiple unit tests for the Address struct and related
    functionalities.
  • Tests cover creation, conversion, display, and error handling.
  • Ensured comprehensive coverage for Address methods and traits.
  • +167/-0 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @milton-cw milton-cw self-assigned this Aug 6, 2024
    Copy link

    github-actions bot commented Aug 6, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Unwrap Usage
    The unwrap method is used in multiple test cases. It is recommended to use expect with a meaningful error message to provide better context in case of failure.

    Unwrap Usage
    The unwrap method is used in multiple test cases. It is recommended to use expect with a meaningful error message to provide better context in case of failure.

    Unwrap Usage
    The unwrap method is used in multiple test cases. It is recommended to use expect with a meaningful error message to provide better context in case of failure.

    Copy link

    github-actions bot commented Aug 6, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add a test case to check the Address::try_from method with an invalid vector length

    In the test_try_from_vec_u8_valid test, consider adding a case to check the
    Address::try_from method with an invalid vector length to ensure it handles errors
    correctly.

    src/eth/primitives/address.rs [197-201]

     #[test]
     fn test_try_from_vec_u8_valid() {
         let bytes = vec![0u8; 20];
         let address = Address::try_from(bytes).unwrap();
         assert_eq!(address.0, H160::zero());
    +
    +    let invalid_bytes = vec![0u8; 19];
    +    let result = Address::try_from(invalid_bytes);
    +    assert!(result.is_err());
     }
     
    Suggestion importance[1-10]: 9

    Why: This suggestion adds a valuable test case to ensure that the Address::try_from method handles errors correctly when given an invalid vector length, improving the robustness of the code.

    9
    Add a test case to check the deserialization of an invalid JSON string

    In the test_serde_deserialize test, consider adding a case to check the
    deserialization of an invalid JSON string to ensure it handles errors correctly.

    src/eth/primitives/address.rs [342-346]

     #[test]
     fn test_serde_deserialize() {
         let json = "\"0x0101010101010101010101010101010101010101\"";
         let address: Address = serde_json::from_str(json).unwrap();
         let expected_address = Address::new([1u8; 20]);
         assert_eq!(address, expected_address);
    +
    +    let invalid_json = "\"invalid_address\"";
    +    let result: Result<Address, _> = serde_json::from_str(invalid_json);
    +    assert!(result.is_err());
     }
     
    Suggestion importance[1-10]: 9

    Why: This suggestion adds a crucial test case to ensure that deserialization handles errors correctly when given an invalid JSON string, improving the robustness and reliability of the code.

    9
    Add a check for the error message in the test_from_str_invalid_address test

    In the test_from_str_invalid_address test, consider adding a case to check the error
    message to ensure it provides meaningful feedback.

    src/eth/primitives/address.rs [294-298]

     #[test]
     fn test_from_str_invalid_address() {
         let address_str = "invalid_address";
         let result: Result<Address, _> = address_str.parse();
         assert!(result.is_err());
    +    assert_eq!(result.unwrap_err().to_string(), "Invalid address format");
     }
     
    Suggestion importance[1-10]: 8

    Why: Checking the error message provides meaningful feedback and ensures that the error handling is informative and correct, which is important for debugging and user experience.

    8
    Enhancement
    Add a test case to check the display of a non-zero address

    In the test_display_address test, consider adding a case to check the display of a
    non-zero address to ensure the formatting is correct.

    src/eth/primitives/address.rs [190-194]

     #[test]
     fn test_display_address() {
         let address = Address::new([0u8; 20]);
         assert_eq!(format!("{}", address), "0x0000000000000000000000000000000000000000");
    +
    +    let non_zero_address = Address::new([1u8; 20]);
    +    assert_eq!(format!("{}", non_zero_address), "0x0101010101010101010101010101010101010101");
     }
     
    Suggestion importance[1-10]: 7

    Why: Adding a test case for displaying a non-zero address enhances the test coverage and ensures that the formatting is correct for different address values.

    7



    #[test]
    fn test_deref_address() {
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    These types of tests would be better implemented as macros see https://github.com/cloudwalk/stratus/blob/main/src/ext.rs#L304

    @dinhani-cw dinhani-cw closed this Aug 12, 2024
    @mayconamaroCW mayconamaroCW deleted the test/qe-tb-codium branch August 14, 2024 18:42
    @mayconamaroCW mayconamaroCW restored the test/qe-tb-codium branch August 14, 2024 18:43
    @mayconamaroCW mayconamaroCW deleted the test/qe-tb-codium branch August 14, 2024 18:50
    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.

    3 participants