@@ -673,6 +673,138 @@ def test_file_transliteration_preserve_accents_true(self):
673673 self .assertIn ('ì e tuk' , result )
674674
675675
676+ class TestCLI (unittest .TestCase ):
677+ ''' Test command-line interface functionality. '''
678+
679+ def test_invalid_language_code (self ):
680+ ''' Test that invalid language code produces error. '''
681+ import subprocess
682+ import sys
683+
684+ # Try to use an invalid language code
685+ result = subprocess .run (
686+ [sys .executable , '-m' , 'cyrtranslit.cyrtranslit' , '-l' , 'xx' , '-i' , 'tests/sr.txt' ],
687+ capture_output = True ,
688+ text = True
689+ )
690+
691+ # Should fail
692+ self .assertNotEqual (result .returncode , 0 )
693+
694+ # Should show error message
695+ self .assertIn ('not supported' , result .stderr )
696+
697+ def test_output_file_creation (self ):
698+ ''' Test that output file is created correctly. '''
699+ import subprocess
700+ import sys
701+ import os
702+ import tempfile
703+
704+ # Create a temporary output file path
705+ with tempfile .NamedTemporaryFile (mode = 'w' , delete = False , suffix = '.txt' ) as tmp :
706+ output_file = tmp .name
707+
708+ try :
709+ # Run CLI with output file
710+ result = subprocess .run (
711+ [sys .executable , '-m' , 'cyrtranslit.cyrtranslit' , '-l' , 'sr' , '-i' , 'tests/sr.txt' , '-o' , output_file ],
712+ capture_output = True ,
713+ text = True
714+ )
715+
716+ # Should succeed
717+ self .assertEqual (result .returncode , 0 , f"Command failed with: { result .stderr } " )
718+
719+ # Output file should exist
720+ self .assertTrue (os .path .exists (output_file ))
721+
722+ # Read and verify content
723+ with open (output_file , 'r' , encoding = 'utf-8' ) as f :
724+ content = f .read ()
725+ # Should contain transliterated Serbian text
726+ self .assertIn ('a' , content .lower ())
727+
728+ finally :
729+ # Clean up
730+ if os .path .exists (output_file ):
731+ os .remove (output_file )
732+
733+ def test_reverse_transliteration_flag (self ):
734+ ''' Test -c flag for Latin to Cyrillic transliteration. '''
735+ import subprocess
736+ import sys
737+
738+ # Run CLI with -c flag on Latin text
739+ result = subprocess .run (
740+ [sys .executable , '-m' , 'cyrtranslit.cyrtranslit' , '-l' , 'sr' , '-c' , '-i' , 'tests/sr_latinica.txt' ],
741+ capture_output = True ,
742+ text = True ,
743+ encoding = 'utf-8'
744+ )
745+
746+ # Should succeed
747+ self .assertEqual (result .returncode , 0 , f"Command failed with: { result .stderr } " )
748+
749+ # Output should contain Cyrillic characters
750+ # Check for common Serbian Cyrillic letters
751+ self .assertTrue (any (ord (c ) >= 0x0400 and ord (c ) <= 0x04FF for c in result .stdout ),
752+ "Output should contain Cyrillic characters" )
753+
754+ def test_preserve_accents_flag (self ):
755+ ''' Test -p flag for preserving accents. '''
756+ import subprocess
757+ import sys
758+
759+ # Run CLI with -p flag on Macedonian text with accents
760+ result = subprocess .run (
761+ [sys .executable , '-m' , 'cyrtranslit.cyrtranslit' , '-l' , 'mk' , '-p' , '-i' , 'tests/mk_accented.txt' ],
762+ capture_output = True ,
763+ text = True ,
764+ encoding = 'utf-8'
765+ )
766+
767+ # Should succeed
768+ self .assertEqual (result .returncode , 0 , f"Command failed with: { result .stderr } " )
769+
770+ # Output should contain Latin letters with grave accents
771+ self .assertIn ('ì' , result .stdout )
772+
773+ def test_combined_flags (self ):
774+ ''' Test combining -c and -p flags. '''
775+ import subprocess
776+ import sys
777+
778+ # Run CLI with both -c and -p flags
779+ result = subprocess .run (
780+ [sys .executable , '-m' , 'cyrtranslit.cyrtranslit' , '-l' , 'mk' , '-c' , '-p' , '-i' , 'tests/mk_accented_latin.txt' ],
781+ capture_output = True ,
782+ text = True ,
783+ encoding = 'utf-8'
784+ )
785+
786+ # Should succeed
787+ self .assertEqual (result .returncode , 0 , f"Command failed with: { result .stderr } " )
788+
789+ # Output should contain Cyrillic with accents
790+ self .assertIn ('ѝ' , result .stdout )
791+
792+ def test_file_not_found (self ):
793+ ''' Test error handling when input file doesn't exist. '''
794+ import subprocess
795+ import sys
796+
797+ # Try to read non-existent file
798+ result = subprocess .run (
799+ [sys .executable , '-m' , 'cyrtranslit.cyrtranslit' , '-l' , 'sr' , '-i' , 'nonexistent_file.txt' ],
800+ capture_output = True ,
801+ text = True
802+ )
803+
804+ # Should fail
805+ self .assertNotEqual (result .returncode , 0 )
806+
807+
676808if __name__ == '__main__' :
677809 # Run all tests.
678810 unittest .main ()
0 commit comments