Back to Repositories

Testing Punctuation Processing Operations in Coqui-AI TTS

This test suite validates the punctuation handling functionality in the Coqui-AI TTS system, focusing on stripping and restoring punctuation marks in text processing. It ensures reliable text manipulation for speech synthesis.

Test Coverage Overview

The test suite provides comprehensive coverage of punctuation manipulation operations including:
  • Punctuation stripping from various text inputs
  • Setting and getting punctuation configurations
  • Punctuation restoration after processing
Key edge cases include empty strings, multiple consecutive punctuation marks, and mixed punctuation patterns.

Implementation Analysis

The testing approach uses unittest framework with a setUp method to initialize test data and the Punctuation class instance. Each test method focuses on specific functionality:
  • test_get_set_puncs: Validates punctuation configuration
  • test_strip_punc: Tests punctuation removal
  • test_strip_restore: Verifies reversible punctuation operations

Technical Details

Testing components include:
  • unittest framework for test organization
  • Punctuation class from TTS.tts.utils.text.punctuation
  • _DEF_PUNCS constant for default punctuation marks
  • setUp method for test data initialization
  • Assert methods for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Comprehensive test data covering various scenarios
  • Clear test method naming conventions
  • Proper test setup and initialization
  • Isolated test cases for specific functionality
  • Verification of both normal operations and edge cases

coqui-ai/tts

tests/text_tests/test_punctuation.py

            
import unittest

from TTS.tts.utils.text.punctuation import _DEF_PUNCS, Punctuation


class PunctuationTest(unittest.TestCase):
    def setUp(self):
        self.punctuation = Punctuation()
        self.test_texts = [
            ("This, is my text ... to be striped !! from text?", "This is my text to be striped from text"),
            ("This, is my text ... to be striped !! from text", "This is my text to be striped from text"),
            ("This, is my text ... to be striped  from text?", "This is my text to be striped  from text"),
            ("This, is my text to be striped from text", "This is my text to be striped from text"),
            (".", ""),
            (" . ", ""),
            ("!!! Attention !!!", "Attention"),
            ("!!! Attention !!! This is just a ... test.", "Attention This is just a test"),
            ("!!! Attention! This is just a ... test.", "Attention This is just a test"),
        ]

    def test_get_set_puncs(self):
        self.punctuation.puncs = "-="
        self.assertEqual(self.punctuation.puncs, "-=")

        self.punctuation.puncs = _DEF_PUNCS
        self.assertEqual(self.punctuation.puncs, _DEF_PUNCS)

    def test_strip_punc(self):
        for text, gt in self.test_texts:
            text_striped = self.punctuation.strip(text)
            self.assertEqual(text_striped, gt)

    def test_strip_restore(self):
        for text, gt in self.test_texts:
            text_striped, puncs_map = self.punctuation.strip_to_restore(text)
            text_restored = self.punctuation.restore(text_striped, puncs_map)
            self.assertEqual(" ".join(text_striped), gt)
            self.assertEqual(text_restored[0], text)