Back to Repositories

Testing Adapter Pattern Implementation in python-patterns

This test suite validates the Adapter pattern implementation in Python, focusing on adapting different noise-making behaviors across various objects like animals, humans, and vehicles. The tests ensure proper adaptation of different interfaces into a common make_noise interface.

Test Coverage Overview

The test suite provides comprehensive coverage of both base class behaviors and adapter functionality.

  • Tests direct class methods for Dog, Cat, Human and Car classes
  • Validates adapter pattern implementation for each object type
  • Covers edge cases like variable noise levels for Car class
  • Ensures consistent interface adaptation across different object types

Implementation Analysis

The testing approach uses unittest framework to validate both original object behaviors and their adapted interfaces.

  • Implements setUp method for efficient test object initialization
  • Uses assertEqual for verification of expected outputs
  • Tests adapter pattern with different object types and noise methods
  • Validates parameter passing through adapter interface

Technical Details

  • Framework: Python unittest
  • Test Scope: Unit tests
  • Pattern Tested: Structural Adapter Pattern
  • Test Structure: Two test classes – ClassTest and AdapterTest
  • Setup Requirements: Python environment with unittest module

Best Practices Demonstrated

The test suite exemplifies several testing best practices and clean code principles.

  • Clear test method naming convention
  • Separate test classes for direct and adapter functionality
  • Consistent test structure and assertions
  • Proper test isolation and setup
  • Comprehensive coverage of both normal and edge cases

faif/python-patterns

tests/structural/test_adapter.py

            
import unittest

from patterns.structural.adapter import Adapter, Car, Cat, Dog, Human


class ClassTest(unittest.TestCase):
    def setUp(self):
        self.dog = Dog()
        self.cat = Cat()
        self.human = Human()
        self.car = Car()

    def test_dog_shall_bark(self):
        noise = self.dog.bark()
        expected_noise = "woof!"
        self.assertEqual(noise, expected_noise)

    def test_cat_shall_meow(self):
        noise = self.cat.meow()
        expected_noise = "meow!"
        self.assertEqual(noise, expected_noise)

    def test_human_shall_speak(self):
        noise = self.human.speak()
        expected_noise = "'hello'"
        self.assertEqual(noise, expected_noise)

    def test_car_shall_make_loud_noise(self):
        noise = self.car.make_noise(1)
        expected_noise = "vroom!"
        self.assertEqual(noise, expected_noise)

    def test_car_shall_make_very_loud_noise(self):
        noise = self.car.make_noise(10)
        expected_noise = "vroom!!!!!!!!!!"
        self.assertEqual(noise, expected_noise)


class AdapterTest(unittest.TestCase):
    def test_dog_adapter_shall_make_noise(self):
        dog = Dog()
        dog_adapter = Adapter(dog, make_noise=dog.bark)
        noise = dog_adapter.make_noise()
        expected_noise = "woof!"
        self.assertEqual(noise, expected_noise)

    def test_cat_adapter_shall_make_noise(self):
        cat = Cat()
        cat_adapter = Adapter(cat, make_noise=cat.meow)
        noise = cat_adapter.make_noise()
        expected_noise = "meow!"
        self.assertEqual(noise, expected_noise)

    def test_human_adapter_shall_make_noise(self):
        human = Human()
        human_adapter = Adapter(human, make_noise=human.speak)
        noise = human_adapter.make_noise()
        expected_noise = "'hello'"
        self.assertEqual(noise, expected_noise)

    def test_car_adapter_shall_make_loud_noise(self):
        car = Car()
        car_adapter = Adapter(car, make_noise=car.make_noise)
        noise = car_adapter.make_noise(1)
        expected_noise = "vroom!"
        self.assertEqual(noise, expected_noise)

    def test_car_adapter_shall_make_very_loud_noise(self):
        car = Car()
        car_adapter = Adapter(car, make_noise=car.make_noise)
        noise = car_adapter.make_noise(10)
        expected_noise = "vroom!!!!!!!!!!"

        self.assertEqual(noise, expected_noise)