test_wildcard.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """Some tests for the wildcard utilities."""
  2. #-----------------------------------------------------------------------------
  3. # Library imports
  4. #-----------------------------------------------------------------------------
  5. # Stdlib
  6. import unittest
  7. # Our own
  8. from IPython.utils import wildcard
  9. #-----------------------------------------------------------------------------
  10. # Globals for test
  11. #-----------------------------------------------------------------------------
  12. class obj_t(object):
  13. pass
  14. root = obj_t()
  15. l = ["arna","abel","ABEL","active","bob","bark","abbot"]
  16. q = ["kate","loop","arne","vito","lucifer","koppel"]
  17. for x in l:
  18. o = obj_t()
  19. setattr(root,x,o)
  20. for y in q:
  21. p = obj_t()
  22. setattr(o,y,p)
  23. root._apan = obj_t()
  24. root._apan.a = 10
  25. root._apan._a = 20
  26. root._apan.__a = 20
  27. root.__anka = obj_t()
  28. root.__anka.a = 10
  29. root.__anka._a = 20
  30. root.__anka.__a = 20
  31. root._APAN = obj_t()
  32. root._APAN.a = 10
  33. root._APAN._a = 20
  34. root._APAN.__a = 20
  35. root.__ANKA = obj_t()
  36. root.__ANKA.a = 10
  37. root.__ANKA._a = 20
  38. root.__ANKA.__a = 20
  39. #-----------------------------------------------------------------------------
  40. # Test cases
  41. #-----------------------------------------------------------------------------
  42. class Tests (unittest.TestCase):
  43. def test_case(self):
  44. ns=root.__dict__
  45. tests=[
  46. ("a*", ["abbot","abel","active","arna",]),
  47. ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",]),
  48. ("_a*", []),
  49. ("_*anka", ["__anka",]),
  50. ("_*a*", ["__anka",]),
  51. ]
  52. for pat,res in tests:
  53. res.sort()
  54. a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=False,
  55. show_all=False).keys())
  56. self.assertEqual(a,res)
  57. def test_case_showall(self):
  58. ns=root.__dict__
  59. tests=[
  60. ("a*", ["abbot","abel","active","arna",]),
  61. ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",]),
  62. ("_a*", ["_apan"]),
  63. ("_*anka", ["__anka",]),
  64. ("_*a*", ["__anka","_apan",]),
  65. ]
  66. for pat,res in tests:
  67. res.sort()
  68. a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=False,
  69. show_all=True).keys())
  70. self.assertEqual(a,res)
  71. def test_nocase(self):
  72. ns=root.__dict__
  73. tests=[
  74. ("a*", ["abbot","abel","ABEL","active","arna",]),
  75. ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",
  76. "ABEL.koppel","ABEL.loop",]),
  77. ("_a*", []),
  78. ("_*anka", ["__anka","__ANKA",]),
  79. ("_*a*", ["__anka","__ANKA",]),
  80. ]
  81. for pat,res in tests:
  82. res.sort()
  83. a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=True,
  84. show_all=False).keys())
  85. self.assertEqual(a,res)
  86. def test_nocase_showall(self):
  87. ns=root.__dict__
  88. tests=[
  89. ("a*", ["abbot","abel","ABEL","active","arna",]),
  90. ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",
  91. "ABEL.koppel","ABEL.loop",]),
  92. ("_a*", ["_apan","_APAN"]),
  93. ("_*anka", ["__anka","__ANKA",]),
  94. ("_*a*", ["__anka","__ANKA","_apan","_APAN"]),
  95. ]
  96. for pat,res in tests:
  97. res.sort()
  98. a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=True,
  99. show_all=True).keys())
  100. a.sort()
  101. self.assertEqual(a,res)
  102. def test_dict_attributes(self):
  103. """Dictionaries should be indexed by attributes, not by keys. This was
  104. causing Github issue 129."""
  105. ns = {"az":{"king":55}, "pq":{1:0}}
  106. tests = [
  107. ("a*", ["az"]),
  108. ("az.k*", ["az.keys"]),
  109. ("pq.k*", ["pq.keys"])
  110. ]
  111. for pat, res in tests:
  112. res.sort()
  113. a = sorted(wildcard.list_namespace(ns, "all", pat, ignore_case=False,
  114. show_all=True).keys())
  115. self.assertEqual(a, res)
  116. def test_dict_dir(self):
  117. class A(object):
  118. def __init__(self):
  119. self.a = 1
  120. self.b = 2
  121. def __getattribute__(self, name):
  122. if name=="a":
  123. raise AttributeError
  124. return object.__getattribute__(self, name)
  125. a = A()
  126. adict = wildcard.dict_dir(a)
  127. assert "a" not in adict # change to assertNotIn method in >= 2.7
  128. self.assertEqual(adict["b"], 2)