Back to Repositories

Testing WHOIS Command Correction Logic in TheFuck

This test suite validates the WHOIS command correction functionality in TheFuck, ensuring proper handling of URL-to-domain conversions for WHOIS queries. It verifies the pattern matching and command transformation logic for various URL formats.

Test Coverage Overview

The test suite provides comprehensive coverage of the WHOIS command correction feature.

Key areas tested include:
  • URL pattern matching for different domain formats
  • Domain extraction from full URLs
  • Multiple command suggestions for subdomain queries
  • Edge case handling for basic WHOIS commands

Implementation Analysis

The implementation uses pytest’s parametrize decorator to efficiently test multiple input scenarios. The testing approach separates matching logic from command generation, using discrete test functions for each concern.

Technical patterns include:
  • Parametrized test cases for input validation
  • Command object usage for input/output modeling
  • Multiple assertion paths for command suggestions

Technical Details

Testing infrastructure includes:
  • Pytest framework for test organization
  • Custom Command type for input handling
  • Parametrize decorator for test case management
  • Assert statements for validation
  • Modular test function organization

Best Practices Demonstrated

The test suite exemplifies several testing best practices including separation of concerns, comprehensive edge case coverage, and efficient test case organization. Notable practices include:
  • Clear test function naming
  • Isolated test scenarios
  • Comprehensive input validation
  • Explicit test case documentation

nvbn/thefuck

tests/rules/test_whois.py

            
import pytest
from thefuck.rules.whois import match, get_new_command
from thefuck.types import Command


@pytest.mark.parametrize('command', [
    Command('whois https://en.wikipedia.org/wiki/Main_Page', ''),
    Command('whois https://en.wikipedia.org/', ''),
    Command('whois meta.unix.stackexchange.com', '')])
def test_match(command):
    assert match(command)


def test_not_match():
    assert not match(Command('whois', ''))


# `whois com` actually makes sense
@pytest.mark.parametrize('command, new_command', [
    (Command('whois https://en.wikipedia.org/wiki/Main_Page', ''),
     'whois en.wikipedia.org'),
    (Command('whois https://en.wikipedia.org/', ''),
     'whois en.wikipedia.org'),
    (Command('whois meta.unix.stackexchange.com', ''),
     ['whois unix.stackexchange.com',
      'whois stackexchange.com',
      'whois com'])])
def test_get_new_command(command, new_command):
    assert get_new_command(command) == new_command