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

Ensure response.parsed_body support for pattern matching #49003

Merged

Commits on Aug 23, 2023

  1. Ensure response.parsed_body support for pattern matching

    Both `Nokogiri` and `Minitest` have merged the PRs mentioned to
    integrate support for Ruby's Pattern matching
    (sparklemotion/nokogiri#2523 and
    minitest/minitest#936, respectively).
    
    This commit adds coverage for those new assertions, and incorporates
    examples into the documentation for the `response.parsed_body` method.
    
    In order to incorporate pattern-matching support for JSON responses,
    this commit changes the response parser to call `JSON.parse` with
    [object_class: ActiveSupport::HashWithIndifferentAccess][object_class],
    since String instances for `Hash` keys are incompatible with Ruby's
    syntactically pattern matching.
    
    For example:
    
    ```ruby
    irb(main):001:0> json = {"key" => "value"}
    => {"key"=>"value"}
    irb(main):002:0> json in {key: /value/}
    => false
    
    irb(main):001:0> json = {"key" => "value"}
    => {"key"=>"value"}
    irb(main):002:0> json in {"key" => /value/}
    .../3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/lib/irb/workspace.rb:113:in `eval': (irb):2: syntax error, unexpected terminator, expecting literal content or tSTRING_DBEG or tSTRING_DVAR or tLABEL_END (SyntaxError)
    json in {"key" => /value/}
                 ^
    
            .../ruby/3.2.0/lib/ruby/gems/3.2.0/gems/irb-1.7.4/exe/irb:9:in `<top (required)>'
            .../ruby/3.2.0/bin/irb:25:in `load'
            .../ruby/3.2.0/bin/irb:25:in `<main>'
    ```
    
    When the Hash maps String keys to Symbol keys, it's able to be pattern
    matched:
    
    ```ruby
    irb(main):005:0> json = {"key" => "value"}.with_indifferent_access
    => {"key"=>"value"}
    irb(main):006:0> json in {key: /value/}
    => true
    ```
    
    [object_class]: https://docs.ruby-lang.org/en/3.2/JSON.html#module-JSON-label-Parsing+Options
    seanpdoyle committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    0f4ab82 View commit details
    Browse the repository at this point in the history