import fnmatch from configparser import ParsingError from typing import Callable, Any from unittest import TestCase import kpmatch from kpmatch import PatternParsingError class ParseTests(TestCase): def test_compile(self): pattern = kpmatch.compile("*.txt") self.assertIsInstance(pattern, kpmatch.Pattern) self.assertTrue(pattern.match("file.txt")) self.assertEqual(pattern.specificity, 12) def test_kpmatch(self): self.assertTrue(kpmatch.kpmatch("file.txt", "*.txt")) self.assertFalse(kpmatch.kpmatch("file.jpg", "*.txt")) def test_is_valid_pattern(self): self.assertTrue(kpmatch.is_valid_pattern("*.txt")) self.assertFalse(kpmatch.is_valid_pattern("[.txt")) def test_specificity(self): self.assertEqual(kpmatch.specificity(""), 0) self.assertEqual(kpmatch.specificity("**"), 1) self.assertEqual(kpmatch.specificity("*.txt"), 12) self.assertEqual(kpmatch.specificity("file.txt"), 15) def assertRaisesPPE(self, error_message: str, position: int, pattern: str): with self.assertRaises(PatternParsingError) as cm: kpmatch.compile(pattern) self.assertEqual( str(PatternParsingError(error_message, pattern, position)), str(cm.exception), ) def test_error_messages(self): self.assertRaisesPPE("Empty path segment", 4, "abc//def") self.assertRaisesPPE('Missing closing bracket "]" to match "["', 3, "abc[def") self.assertRaisesPPE( 'Character "!" is not allowed inside a character set', 6, "abc[de!f]" ) self.assertRaisesPPE( 'Character "[" is not allowed inside a character set', 6, "abc[de[f]" ) self.assertRaisesPPE('Missing closing bracket "}" to match "{"', 3, "abc{def") self.assertRaisesPPE( 'Character "{" is not allowed inside a one-of pattern', 6, "abc{de{f}" ) self.assertRaisesPPE( 'A star "*" wildcard is not allowed inside a one-of pattern', 8, "abc{def,*}", ) self.assertRaisesPPE("Empty choice in a one-of pattern", 8, "abc{def,}") self.assertRaisesPPE("Empty choice in a one-of pattern", 4, "abc{,def}")