Back to Repositories

Testing CSS Selector Splitting Implementation in Capybara

This test suite validates the CSS selector splitting functionality in Capybara, focusing on handling complex CSS selectors with various edge cases. The tests ensure proper parsing and splitting of CSS selectors while maintaining string integrity and pseudo-selector handling.

Test Coverage Overview

The test suite provides comprehensive coverage for the Capybara::Selector::CSS::Splitter class, examining both scenarios where splitting is needed and not needed.

  • Tests basic CSS selector handling
  • Validates comma handling within strings
  • Verifies pseudo-selector processing
  • Ensures proper splitting of root-level selectors
  • Handles complex cases with quotes and escape characters

Implementation Analysis

The implementation uses RSpec’s context-based organization to clearly separate different testing scenarios. It leverages let blocks for setup and employs expectation matching to verify selector splitting behavior.

  • Uses RSpec’s described_class for better maintainability
  • Implements focused test cases for specific selector patterns
  • Employs string escape sequence testing

Technical Details

  • Framework: RSpec
  • Test Type: Unit Tests
  • Primary Class: Capybara::Selector::CSS::Splitter
  • Key Methods: split(css)
  • Test Environment: Frozen String Literals enabled

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases and comprehensive edge case coverage.

  • Clear context separation
  • Descriptive test naming
  • Focused test scenarios
  • Complex edge case handling
  • Consistent assertion patterns

teamcapybara/capybara

spec/css_splitter_spec.rb

            
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Capybara::Selector::CSS::Splitter do
  let :splitter do
    described_class.new
  end

  context 'split not needed' do
    it 'normal CSS selector' do
      css = 'div[id="abc"]'
      expect(splitter.split(css)).to eq [css]
    end

    it 'comma in strings' do
      css = 'div[id="a,bc"]'
      expect(splitter.split(css)).to eq [css]
    end

    it 'comma in pseudo-selector' do
      css = 'div.class1:not(.class1, .class2)'
      expect(splitter.split(css)).to eq [css]
    end
  end

  context 'split needed' do
    it 'root level comma' do
      css = 'div.class1, span, p.class2'
      expect(splitter.split(css)).to eq ['div.class1', 'span', 'p.class2']
    end

    it 'root level comma when quotes and pseudo selectors' do
      css = 'div.class1[id="abc\\"def,ghi"]:not(.class3, .class4), span[id=\'a"c\\\'de\'], section, #abc\\,def'
      expect(splitter.split(css)).to eq ['div.class1[id="abc\\"def,ghi"]:not(.class3, .class4)', 'span[id=\'a"c\\\'de\']', 'section', '#abc\\,def']
    end
  end
end