new API
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 2s

This commit is contained in:
2026-04-13 23:02:57 +02:00
parent a44248bc50
commit 4d115e3de4
17 changed files with 965 additions and 275 deletions

View File

@@ -5,7 +5,7 @@ import kpmatch
class MatchTests(TestCase):
def test_match_empty(self):
pattern = kpmatch.compile("")
(pattern,) = kpmatch.compile("")
self.assertTrue(pattern.match(""))
self.assertFalse(pattern.match("index.html"))
self.assertFalse(pattern.match("about"))
@@ -13,7 +13,7 @@ class MatchTests(TestCase):
self.assertFalse(pattern.match("content/deep/deeper/file.txt"))
def test_match_any(self):
pattern = kpmatch.compile("**")
(pattern,) = kpmatch.compile("**")
self.assertTrue(pattern.match(""))
self.assertTrue(pattern.match("index.html"))
self.assertTrue(pattern.match("about"))
@@ -21,7 +21,7 @@ class MatchTests(TestCase):
self.assertTrue(pattern.match("content/deep/deeper/file.txt"))
def test_match_any_name(self):
pattern = kpmatch.compile("*.html")
(pattern,) = kpmatch.compile("*.html")
self.assertTrue(pattern.match("index.html"))
self.assertTrue(pattern.match("about.html"))
self.assertFalse(pattern.match("about"))
@@ -29,7 +29,7 @@ class MatchTests(TestCase):
self.assertFalse(pattern.match("content/deep/deeper/file.txt"))
def test_match_any_name_anywhere(self):
pattern = kpmatch.compile("**/*.html")
(pattern,) = kpmatch.compile("**/*.html")
self.assertTrue(pattern.match("index.html"))
self.assertTrue(pattern.match("about.html"))
self.assertFalse(pattern.match("about"))
@@ -38,36 +38,28 @@ class MatchTests(TestCase):
self.assertFalse(pattern.match("content/deep/deeper/file.txt"))
def test_match_any_directory_name(self):
pattern = kpmatch.compile("packages/*/package.json")
(pattern,) = kpmatch.compile("packages/*/package.json")
self.assertFalse(pattern.match("packages/package.json"))
self.assertTrue(pattern.match("packages/a/package.json"))
self.assertTrue(pattern.match("packages/b/package.json"))
self.assertFalse(pattern.match("packages/a/b/package.json"))
def test_match_one_of(self):
pattern = kpmatch.compile("*.{png,jpg,jpeg,webp}")
self.assertTrue(pattern.match("image.png"))
self.assertTrue(pattern.match("image.jpg"))
self.assertTrue(pattern.match("image.jpeg"))
self.assertTrue(pattern.match("image.webp"))
self.assertFalse(pattern.match("image.svg"))
def test_match_character_set(self):
pattern = kpmatch.compile("*.[jt]s")
(pattern,) = kpmatch.compile("*.[jt]s")
self.assertTrue(pattern.match("file.js"))
self.assertTrue(pattern.match("file.ts"))
self.assertFalse(pattern.match("file.rs"))
self.assertFalse(pattern.match("file.cs"))
def test_match_negative_character_set(self):
pattern = kpmatch.compile("*.[!jt]s")
(pattern,) = kpmatch.compile("*.[!jt]s")
self.assertFalse(pattern.match("file.js"))
self.assertFalse(pattern.match("file.ts"))
self.assertTrue(pattern.match("file.rs"))
self.assertTrue(pattern.match("file.cs"))
def test_match_any_character(self):
pattern = kpmatch.compile("*.?s")
(pattern,) = kpmatch.compile("*.?s")
self.assertTrue(pattern.match("file.js"))
self.assertTrue(pattern.match("file.ts"))
self.assertTrue(pattern.match("file.rs"))

View File

@@ -1,9 +1,9 @@
import os.path
from unittest import TestCase
from kpmatch import PatternParsingError
import kpmatch.matchers as m
import kpmatch.parse as p
from kpmatch.config import Config
def join_matchers(first: m.PathMatcher, *others: m.PathMatcher) -> m.PathMatcher:
@@ -13,10 +13,59 @@ def join_matchers(first: m.PathMatcher, *others: m.PathMatcher) -> m.PathMatcher
class ParseTests(TestCase):
def test_expand_braces(self):
cfg = Config()
self.assertEqual(
[
"ab/**/*.def",
"ab/ghi",
"a?c/**/*.def",
"a?c/ghi",
],
p._expand_braces(cfg, "a{b,?c}/{**/*.def,ghi}"),
)
def test_parse_braces_notation(self):
# braces_notation <- ( fixed_section (braced_section fixed_section)* )? EOS
cfg = Config()
self.assertEqual(p._parse_braces_notation(cfg, ""), [])
self.assertEqual(
p._parse_braces_notation(cfg, "a{b,?c}/{**/*.def,ghi}"),
[["a"], ["b", "?c"], ["/"], ["**/*.def", "ghi"]],
)
def test_parse_braced_section(self):
# braced_section = "{" braces_alternative ("," braces_alternative)* "}"
self.assertTupleEqual(
p._parse_braced_section("{abc,def}", 0), (["abc", "def"], 9)
)
self.assertTupleEqual(p._parse_braced_section("{abc}", 0), (["abc"], 5))
self.assertTupleEqual(
p._parse_braced_section("{a,b,c,}", 0), (["a", "b", "c", ""], 8)
)
self.assertRaises(p.PatternParsingError, p._parse_braced_section, "abc,def}", 0)
self.assertRaises(p.PatternParsingError, p._parse_braced_section, "{abc,def", 0)
self.assertRaises(
p.PatternParsingError, p._parse_braced_section, "{abc{def}", 0
)
def test_parse_braces_alternative(self):
# braces_alternative <- (!("{" / "," / "}") .)+
self.assertTupleEqual(p._parse_braces_alternative("ab{c,d}", 0), ("ab", 2))
self.assertTupleEqual(p._parse_braces_alternative("ab{c,d}", 3), ("c", 4))
self.assertTupleEqual(p._parse_braces_alternative("ab{c,d}", 5), ("d", 6))
def test_parse_fixed_section(self):
# fixed_section <- (!("{" / "}") .)+
self.assertTupleEqual(p._parse_fixed_section("", 0), ("", 0))
self.assertTupleEqual(p._parse_fixed_section("ab{c,d}", 0), ("ab", 2))
self.assertRaises(p.PatternParsingError, p._parse_fixed_section, "c,d}", 0)
def test_parse_pattern(self):
cfg = Config()
# pattern <- (any_segments / fixed_segment)*
self.assertEqual(
p._parse_pattern("?a[bc]/**/*.{def,ghi}"),
p._parse_pattern(cfg, "?a[bc]/**/*.def"),
join_matchers(
m.AnyCharacter(),
m.FixedName("a"),
@@ -25,7 +74,7 @@ class ParseTests(TestCase):
m.AnySegments(),
m.AnyName(),
m.FixedName("."),
m.OneOf((m.FixedName("def"), m.FixedName("ghi"))),
m.FixedName("def"),
m.EndOfSegment(),
m.EndOfPath(),
),
@@ -40,7 +89,7 @@ class ParseTests(TestCase):
def test_parse_fixed_segment(self):
# fixed_segment <- name_part+ (SEP / EOS)
self.assertTupleEqual(
p._parse_fixed_segment("?a[bc]/*.{def,ghi}", 0),
p._parse_fixed_segment("?a[bc]/*.def", 0),
(
join_matchers(
m.AnyCharacter(),
@@ -52,15 +101,14 @@ class ParseTests(TestCase):
),
)
self.assertTupleEqual(
p._parse_fixed_segment("?a[bc]/*.{def,ghi}", 7),
p._parse_fixed_segment("?a[bc]/*.def", 7),
(
join_matchers(
m.AnyName(),
m.FixedName("."),
m.OneOf((m.FixedName("def"), m.FixedName("ghi"))),
m.FixedName(".def"),
m.EndOfSegment(),
),
18,
12,
),
)
@@ -82,8 +130,8 @@ class ParseTests(TestCase):
p._parse_name_part("?a[bc],*{def,ghi}", 7), (m.AnyName(), 8)
)
self.assertTupleEqual(
p._parse_name_part("?a[bc],*{def,ghi}", 8),
(m.OneOf((m.FixedName("def"), m.FixedName("ghi"))), 17),
p._parse_name_part("?a[bc],*def", 8),
(m.FixedName("def"), 17),
)
def test_parse_any_name(self):
@@ -92,56 +140,6 @@ class ParseTests(TestCase):
self.assertTupleEqual(p._parse_any_name("a*", 1), (m.AnyName(), 2))
self.assertTupleEqual(p._parse_any_name("a***a", 1), (m.AnyName(), 4))
def test_parse_one_of(self):
# one_of = "{" simple_name_part+ ("," simple_name_part+)* "}"
self.assertTupleEqual(p._parse_one_of("", 0), (None, 0))
self.assertTupleEqual(p._parse_one_of("abc,def}", 0), (None, 0))
self.assertTupleEqual(p._parse_one_of("{abc,def", 0), (None, 0))
self.assertTupleEqual(
p._parse_one_of("{abc,def}", 0),
(m.OneOf((m.FixedName("abc"), m.FixedName("def"))), 9),
)
self.assertTupleEqual(
p._parse_one_of("{a[bc],d?f,ghi}", 0),
(
m.OneOf(
(
join_matchers(m.FixedName("a"), m.CharacterSet("bc", False)),
join_matchers(
m.FixedName("d"), m.AnyCharacter(), m.FixedName("f")
),
m.FixedName("ghi"),
)
),
15,
),
)
self.assertRaises(PatternParsingError, p._parse_one_of, "{abc{def}", 0)
self.assertTupleEqual(
p._parse_one_of("{abc{def}", 4), (m.OneOf((m.FixedName("def"),)), 9)
)
self.assertTupleEqual(
p._parse_one_of("{abc}def}", 0), (m.OneOf((m.FixedName("abc"),)), 5)
)
self.assertTupleEqual(
p._parse_one_of(os.path.join("{abc", "def}"), 0),
(None, 0),
)
self.assertTupleEqual(p._parse_one_of("{,def", 0), (None, 0))
self.assertRaises(PatternParsingError, p._parse_one_of, "{,def}", 0)
def test_parse_one_of_choice(self):
# one_of_choice = simple_name_part+
self.assertTupleEqual(
p._parse_one_of_choice("?a[bc],def", 0),
(
join_matchers(
m.AnyCharacter(), m.FixedName("a"), m.CharacterSet("bc", False)
),
6,
),
)
def test_parse_simple_name_part(self):
# simple_name_part = any_character / character_set / fixed_name
self.assertTupleEqual(
@@ -169,11 +167,11 @@ class ParseTests(TestCase):
p._parse_character_set("[!abcdef]", 0), (m.CharacterSet("abcdef", True), 9)
)
self.assertTupleEqual(p._parse_character_set("[abcdef]", 3), (None, 3))
self.assertRaises(PatternParsingError, p._parse_character_set, "[abc[def]", 0)
self.assertRaises(p.PatternParsingError, p._parse_character_set, "[abc[def]", 0)
self.assertTupleEqual(
p._parse_character_set("[abc[def]", 4), (m.CharacterSet("def", False), 9)
)
self.assertRaises(PatternParsingError, p._parse_character_set, "[abc!def]", 0)
self.assertRaises(p.PatternParsingError, p._parse_character_set, "[abc!def]", 0)
self.assertTupleEqual(
p._parse_character_set("[abc]def]", 0), (m.CharacterSet("abc", False), 5)
)

View File

@@ -9,25 +9,20 @@ from kpmatch import PatternParsingError
class ParseTests(TestCase):
def test_compile(self):
pattern = kpmatch.compile("*.txt")
(pattern,) = kpmatch.compile("*.txt")
self.assertIsInstance(pattern, kpmatch.Pattern)
self.assertTrue(pattern.match("file.txt"))
self.assertEqual(pattern.specificity, 12)
patterns = kpmatch.compile("*.{png,jpg,jpeg,webp}")
self.assertEqual(4, len(patterns))
self.assertTrue(any([pattern.match("file.jpeg") for pattern in patterns]))
self.assertTrue(any([pattern.match("file.png") for pattern in patterns]))
self.assertFalse(any([pattern.match("file.txt") for pattern in patterns]))
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)
@@ -49,12 +44,5 @@ class ParseTests(TestCase):
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}"
'Character "{" is not allowed inside a braced section', 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}")

View File

@@ -1,6 +1,10 @@
from unittest import TestCase
from kpmatch import specificity
from kpmatch import compile
def specificity(pattern: str) -> int:
return compile(pattern)[0].specificity
class SpecificityTests(TestCase):
@@ -10,7 +14,6 @@ class SpecificityTests(TestCase):
self.assertGreater(specificity("**/a/file.txt"), specificity("**/a/*.txt"))
self.assertGreater(specificity("file.txt"), specificity("*.txt"))
self.assertGreater(specificity("*.txt"), specificity("*"))
self.assertGreater(specificity("file.{txt,html}"), specificity("*.txt"))
self.assertGreater(specificity("image_???.png"), specificity("image_*.png"))
self.assertGreater(specificity("image.png"), specificity("image_???.png"))