Files
kpmatch/test/match.py
Louis DEVIE 4d115e3de4
All checks were successful
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 2s
new API
2026-04-13 23:03:11 +02:00

67 lines
2.8 KiB
Python

from unittest import TestCase
import kpmatch
class MatchTests(TestCase):
def test_match_empty(self):
(pattern,) = kpmatch.compile("")
self.assertTrue(pattern.match(""))
self.assertFalse(pattern.match("index.html"))
self.assertFalse(pattern.match("about"))
self.assertFalse(pattern.match("about/about.html"))
self.assertFalse(pattern.match("content/deep/deeper/file.txt"))
def test_match_any(self):
(pattern,) = kpmatch.compile("**")
self.assertTrue(pattern.match(""))
self.assertTrue(pattern.match("index.html"))
self.assertTrue(pattern.match("about"))
self.assertTrue(pattern.match("about/about.html"))
self.assertTrue(pattern.match("content/deep/deeper/file.txt"))
def test_match_any_name(self):
(pattern,) = kpmatch.compile("*.html")
self.assertTrue(pattern.match("index.html"))
self.assertTrue(pattern.match("about.html"))
self.assertFalse(pattern.match("about"))
self.assertFalse(pattern.match("about/about.html"))
self.assertFalse(pattern.match("content/deep/deeper/file.txt"))
def test_match_any_name_anywhere(self):
(pattern,) = kpmatch.compile("**/*.html")
self.assertTrue(pattern.match("index.html"))
self.assertTrue(pattern.match("about.html"))
self.assertFalse(pattern.match("about"))
self.assertTrue(pattern.match("about/about.html"))
self.assertTrue(pattern.match("docs/getting-started/installation.html"))
self.assertFalse(pattern.match("content/deep/deeper/file.txt"))
def test_match_any_directory_name(self):
(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_character_set(self):
(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")
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")
self.assertTrue(pattern.match("file.js"))
self.assertTrue(pattern.match("file.ts"))
self.assertTrue(pattern.match("file.rs"))
self.assertTrue(pattern.match("file.cs"))