Back to Repositories

Testing Keyboard Layout Switching Commands in TheFuck

This test suite validates the language switching functionality in TheFuck, focusing on command correction for keyboard layout mismatches. It verifies the system’s ability to detect and convert commands typed in wrong keyboard layouts across multiple languages and character sets.

Test Coverage Overview

The test suite provides comprehensive coverage of keyboard layout switching scenarios across multiple languages including Cyrillic, Greek, Hebrew, and Korean characters. Key functionality tested includes:

  • Command pattern matching for mistyped commands
  • Layout detection and conversion accuracy
  • Support for command arguments and flags
  • Handling of different character encodings

Implementation Analysis

The testing approach utilizes pytest’s parametrize decorator to efficiently test multiple input scenarios. The implementation follows a data-driven testing pattern, separating test cases into matching and non-matching commands, with explicit verification of command translations.

  • Parametrized test cases for different language inputs
  • Separate positive and negative test scenarios
  • Command object usage for input/output validation

Technical Details

Testing infrastructure includes:

  • pytest as the primary testing framework
  • UTF-8 encoding specification
  • Custom Command type implementation
  • Modular test structure with separate match and conversion tests

Best Practices Demonstrated

The test suite demonstrates several testing best practices including comprehensive edge case coverage and clear test organization. Notable practices include:

  • Systematic test case organization
  • Clear separation of concerns between matching and conversion
  • Extensive use of parametrized testing
  • Proper handling of Unicode and multi-byte characters

nvbn/thefuck

tests/rules/test_switch_lang.py

            
# -*- encoding: utf-8 -*-

import pytest

from thefuck.rules import switch_lang
from thefuck.types import Command


@pytest.mark.parametrize('command', [
    Command(u'фзе-пуе', 'command not found: фзе-пуе'),
    Command(u'λσ', 'command not found: λσ'),
    Command(u'שפא-עקא', 'command not found: שפא-עקא'),
    Command(u'ךד', 'command not found: ךד'),
    Command(u'녀애 ㅣㄴ', 'command not found: 녀애 ㅣㄴ')])
def test_match(command):
    assert switch_lang.match(command)


@pytest.mark.parametrize('command', [
    Command(u'pat-get', 'command not found: pat-get'),
    Command(u'ls', 'command not found: ls'),
    Command(u'агсл', 'command not found: агсл'),
    Command(u'фзе-пуе', 'some info'),
    Command(u'שפא-עקא', 'some info'),
    Command(u'녀애 ㅣㄴ', 'some info')])
def test_not_match(command):
    assert not switch_lang.match(command)


@pytest.mark.parametrize('command, new_command', [
    (Command(u'фзе-пуе штыефдд мшь', ''), 'apt-get install vim'),
    (Command(u'λσ -λα', ''), 'ls -la'),
    (Command(u'שפא-עקא ןמדאשךך הןצ', ''), 'apt-get install vim'),
    (Command(u'ךד -ךש', ''), 'ls -la'),
    (Command(u'멧-ㅎㄷㅅ ㅑㅜㄴㅅ미ㅣ 퍄ㅡ', ''), 'apt-get install vim'),
    (Command(u'ㅣㄴ -ㅣㅁ', ''), 'ls -la'),
    (Command(u'ㅔㅁㅅ촤', ''), 'patchk'), ])
def test_get_new_command(command, new_command):
    assert switch_lang.get_new_command(command) == new_command