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

Improve BleUuid API and tests #62

Merged
merged 5 commits into from
Jul 15, 2024
Merged

Improve BleUuid API and tests #62

merged 5 commits into from
Jul 15, 2024

Conversation

fotiDim
Copy link
Contributor

@fotiDim fotiDim commented Jul 13, 2024

PR Type

Enhancement, Tests


Description

  • Refactored BleUuid to BleUuidParser for better clarity and functionality.
  • Updated UUID parsing methods and improved their logic.
  • Replaced old UUID parsing method calls with new ones across the codebase.
  • Added comprehensive tests for the new BleUuidParser methods.
  • Removed outdated tests for the old BleUuid class.
  • Added a GitHub Actions workflow to run tests on the new BleUuidParser.
  • Updated documentation and changelog to reflect the changes.

Changes walkthrough 📝

Relevant files
Enhancement
6 files
ble_service.dart
Update UUID parsing method in BleService and BleCharacteristic

lib/src/models/ble_service.dart

  • Changed BleUuid.parse to BleUuidParser.string for UUID parsing.
  • +2/-2     
    ble_uuid_parser.dart
    Refactor and enhance UUID parsing utility                               

    lib/src/models/ble_uuid_parser.dart

  • Renamed BleUuid to BleUuidParser.
  • Updated method names for UUID parsing and comparison.
  • Improved UUID parsing logic.
  • +24/-13 
    model_exports.dart
    Update export statement for BleUuidParser                               

    lib/src/models/model_exports.dart

  • Updated export statement to reflect the new BleUuidParser class name.
  • +1/-1     
    universal_ble.dart
    Update UUID parsing method in UniversalBle class                 

    lib/src/universal_ble.dart

  • Changed BleUuid.parse to BleUuidParser.string for UUID parsing in
    multiple methods.
  • +6/-6     
    universal_ble_pigeon_channel.dart
    Update UUID parsing method in UniversalBleScanResult extension

    lib/src/universal_ble_pigeon/universal_ble_pigeon_channel.dart

  • Changed BleUuid.parse to BleUuidParser.string for UUID parsing in
    extension method.
  • +1/-1     
    universal_ble_platform_interface.dart
    Update UUID parsing method in UniversalBlePlatform interface

    lib/src/universal_ble_platform_interface.dart

  • Changed BleUuid.parse to BleUuidParser.string for UUID parsing in
    updateCharacteristicValue method.
  • +2/-1     
    Tests
    3 files
    ble_uuid_parser_test.dart
    Add tests for BleUuidParser methods                                           

    test/ble_uuid_parser_test.dart

    • Added comprehensive tests for BleUuidParser methods.
    +226/-0 
    universal_ble_test.dart
    Remove outdated tests for BleUuid class                                   

    test/universal_ble_test.dart

    • Removed old tests for BleUuid class.
    +0/-74   
    text_ble_uuid.yml
    Add GitHub Actions workflow for BleUuidParser tests           

    .github/workflows/text_ble_uuid.yml

  • Added GitHub Actions workflow for running tests on BleUuidParser.
  • +36/-0   
    Documentation
    2 files
    CHANGELOG.md
    Update changelog for BleUuid utility methods                         

    CHANGELOG.md

    • Updated changelog to reflect changes in BleUuid utility methods.
    +1/-1     
    README.md
    Update README for BleUuidParser methods                                   

    README.md

    • Updated documentation to reflect new BleUuidParser methods.
    +8/-8     

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

    Copy link

    PR Reviewer Guide 🔍

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

    Possible Bug
    The method BleUuidParser.number should handle numbers greater than 0xFFFF and less than 0x10000, but the current implementation only allows numbers between 0x0100 and 0xFFFF. This might exclude valid 16-bit UUIDs like 0x0001.

    Error Handling
    The method BleUuidParser.string might throw a FormatException for valid UUIDs that are exactly 32 characters long without dashes. This scenario is not handled correctly in the current implementation.

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Add a check for hexadecimal validity in UUID strings

    Consider adding a check for hexadecimal validity in the string method to ensure that
    the UUID string contains only hexadecimal characters before processing it. This can
    prevent runtime errors or unexpected behavior due to invalid input.

    lib/src/models/ble_uuid_parser.dart [6]

     static String string(String uuid) {
       if (uuid.length < 4) {
         throw const FormatException('Invalid UUID');
       }
    +  final hexRegex = RegExp(r'^[0-9a-fA-F]+$');
    +  if (!hexRegex.hasMatch(uuid.replaceAll('-', ''))) {
    +    throw const FormatException('UUID must contain only hexadecimal characters');
    +  }
       ...
     }
     
    Suggestion importance[1-10]: 9

    Why: This suggestion adds a crucial validation step to ensure that the UUID string contains only hexadecimal characters, which can prevent runtime errors and unexpected behavior due to invalid input.

    9
    Adjust the range check in the number method to support 32-bit UUIDs

    Refactor the number method to handle the full range of valid 16-bit and 32-bit UUIDs
    by adjusting the conditional check to allow values up to 0xFFFFFFFF.

    lib/src/models/ble_uuid_parser.dart [52-56]

     static String number(int short) {
    -  if (short <= 0xFF || short > 0xFFFF) {
    +  if (short < 0 || short > 0xFFFFFFFF) {
         throw const FormatException('Invalid UUID');
       }
    -  return string(short.toRadixString(16).padLeft(4, '0'));
    +  return string(short.toRadixString(16).padLeft(8, '0'));
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion correctly expands the range check to include the full range of valid 32-bit UUIDs, enhancing the method's functionality and ensuring it handles a broader range of inputs.

    8
    Bug fix
    Ensure correct formatting of UUIDs starting with '0x' in the string method

    In the string method, ensure that UUIDs starting with '0x' are properly formatted
    after removing the prefix to avoid incorrect UUID formats.

    lib/src/models/ble_uuid_parser.dart [11-12]

     if (uuid.startsWith('0x')) {
    -  uuid = uuid.substring(2);
    +  uuid = uuid.substring(2).padLeft(8, '0');
     }
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: This suggestion addresses a potential formatting issue with UUIDs starting with '0x', ensuring they are correctly formatted after removing the prefix. However, the existing code already handles this well, so the improvement is minor.

    7

    @fotiDim fotiDim force-pushed the Improve-tests branch 2 times, most recently from a1169ef to 5bb0783 Compare July 14, 2024 05:32
    @fotiDim fotiDim merged commit efaa72a into main Jul 15, 2024
    1 check passed
    @fotiDim fotiDim deleted the Improve-tests branch August 11, 2024 21:31
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants