test_glob_matcher.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2021 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import unittest
  16. from glob_matcher import GlobMatcher
  17. class TestGlobMatcher(unittest.TestCase):
  18. def test_exact_match(self):
  19. self.assertTrue(GlobMatcher('').matches(''))
  20. self.assertTrue(GlobMatcher('a').matches('a'))
  21. self.assertTrue(GlobMatcher('abc').matches('abc'))
  22. self.assertFalse(GlobMatcher('').matches('a'))
  23. self.assertFalse(GlobMatcher('abc').matches(''))
  24. self.assertFalse(GlobMatcher('x').matches('y'))
  25. self.assertFalse(GlobMatcher('abcd').matches('abcz'))
  26. self.assertFalse(GlobMatcher('abcdef').matches('abczef'))
  27. self.assertFalse(GlobMatcher('abc').matches('abcd'))
  28. self.assertFalse(GlobMatcher('abc').matches('bc'))
  29. self.assertFalse(GlobMatcher('abcd').matches('abc'))
  30. def test_questionmark(self):
  31. self.assertTrue(GlobMatcher('?').matches('a'))
  32. self.assertTrue(GlobMatcher('?x?z').matches('axyz'))
  33. self.assertTrue(GlobMatcher('a??').matches('abc'))
  34. self.assertFalse(GlobMatcher('??').matches('a'))
  35. self.assertFalse(GlobMatcher('??').matches('abc'))
  36. self.assertFalse(GlobMatcher('a').matches('?'))
  37. def test_star(self):
  38. self.assertTrue(GlobMatcher('*').matches(''))
  39. self.assertTrue(GlobMatcher('*').matches('a'))
  40. self.assertTrue(GlobMatcher('*').matches('abc'))
  41. self.assertTrue(GlobMatcher('*x').matches('abcx'))
  42. self.assertTrue(GlobMatcher('*x*').matches('x'))
  43. self.assertTrue(GlobMatcher('*x*').matches('xabc'))
  44. self.assertTrue(GlobMatcher('*x*').matches('abcx'))
  45. self.assertTrue(GlobMatcher('*x*').matches('abcxdef'))
  46. self.assertTrue(GlobMatcher('a*c').matches('abc'))
  47. self.assertTrue(GlobMatcher('a*c').matches('athisislongc'))
  48. self.assertTrue(GlobMatcher('123*').matches('123abc'))
  49. self.assertTrue(GlobMatcher('123*').matches('123'))
  50. self.assertTrue(GlobMatcher(
  51. 'more*stars*here').matches('more_stars___here'))
  52. self.assertFalse(GlobMatcher('*x').matches('abc'))
  53. self.assertFalse(GlobMatcher('x*').matches('abc'))
  54. self.assertFalse(GlobMatcher(
  55. '*some*test').matches('thisissomelongertestcase'))
  56. def test_group(self):
  57. self.assertTrue(GlobMatcher('{a,b}').matches('a'))
  58. self.assertTrue(GlobMatcher('{a,b}').matches('b'))
  59. self.assertTrue(GlobMatcher('some{a,b}here').matches('someahere'))
  60. self.assertTrue(GlobMatcher('{a,b}x{c,d}').matches('axd'))
  61. self.assertTrue(GlobMatcher('{a,b}x{c,d}').matches('bxc'))
  62. self.assertFalse(GlobMatcher('{a,b}').matches(''))
  63. self.assertFalse(GlobMatcher('{a,b}').matches('c'))
  64. self.assertFalse(GlobMatcher('{a,b}').matches('ac'))
  65. self.assertFalse(GlobMatcher('{a,b}').matches('ca'))
  66. self.assertFalse(GlobMatcher('{a,b}x{c,d}').matches('axe'))
  67. self.assertFalse(GlobMatcher('{a,b}x{c,d}').matches('exd'))
  68. def test_combined(self):
  69. self.assertTrue(GlobMatcher('a{,bc}').matches('a'))
  70. self.assertTrue(GlobMatcher('a{,bc}').matches('abc'))
  71. self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abcdxz'))
  72. self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abc1234dxz'))
  73. self.assertTrue(GlobMatcher('ab{c*d,ef}xz').matches('abefxz'))
  74. self.assertFalse(GlobMatcher('a{,bc}').matches('ab'))
  75. self.assertFalse(GlobMatcher('a{,bc}').matches('ax'))
  76. self.assertFalse(GlobMatcher('a{,bc}').matches('abcd'))
  77. self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abxz'))
  78. self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abcxz'))
  79. self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abdxz'))
  80. self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abxz'))
  81. self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abexz'))
  82. self.assertFalse(GlobMatcher('ab{c*d,ef}xz').matches('abfxz'))
  83. if __name__ == '__main__':
  84. unittest.main()