test_helpers.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from pluggy.hooks import varnames
  2. from pluggy.manager import _formatdef
  3. import sys
  4. import pytest
  5. def test_varnames():
  6. def f(x):
  7. i = 3 # noqa
  8. class A(object):
  9. def f(self, y):
  10. pass
  11. class B(object):
  12. def __call__(self, z):
  13. pass
  14. assert varnames(f) == (("x",), ())
  15. assert varnames(A().f) == (("y",), ())
  16. assert varnames(B()) == (("z",), ())
  17. def test_varnames_default():
  18. def f(x, y=3):
  19. pass
  20. assert varnames(f) == (("x",), ("y",))
  21. def test_varnames_class():
  22. class C(object):
  23. def __init__(self, x):
  24. pass
  25. class D(object):
  26. pass
  27. class E(object):
  28. def __init__(self, x):
  29. pass
  30. class F(object):
  31. pass
  32. assert varnames(C) == (("x",), ())
  33. assert varnames(D) == ((), ())
  34. assert varnames(E) == (("x",), ())
  35. assert varnames(F) == ((), ())
  36. @pytest.mark.skipif(
  37. sys.version_info < (3,), reason="Keyword only arguments are Python 3 only"
  38. )
  39. def test_varnames_keyword_only():
  40. # SyntaxError on Python 2, so we exec
  41. ns = {}
  42. exec(
  43. "def f1(x, *, y): pass\n"
  44. "def f2(x, *, y=3): pass\n"
  45. "def f3(x=1, *, y=3): pass\n",
  46. ns,
  47. )
  48. assert varnames(ns["f1"]) == (("x",), ())
  49. assert varnames(ns["f2"]) == (("x",), ())
  50. assert varnames(ns["f3"]) == ((), ("x",))
  51. def test_formatdef():
  52. def function1():
  53. pass
  54. assert _formatdef(function1) == "function1()"
  55. def function2(arg1):
  56. pass
  57. assert _formatdef(function2) == "function2(arg1)"
  58. def function3(arg1, arg2="qwe"):
  59. pass
  60. assert _formatdef(function3) == "function3(arg1, arg2='qwe')"
  61. def function4(arg1, *args, **kwargs):
  62. pass
  63. assert _formatdef(function4) == "function4(arg1, *args, **kwargs)"