args.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright David Abrahams 2004. Distributed under the Boost
  2. # Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. from __future__ import print_function
  5. """
  6. >>> from args_ext import *
  7. >>> raw(3, 4, foo = 'bar', baz = 42)
  8. ((3, 4), {'foo': 'bar', 'baz': 42})
  9. Prove that we can handle empty keywords and non-keywords
  10. >>> raw(3, 4)
  11. ((3, 4), {})
  12. >>> raw(foo = 'bar')
  13. ((), {'foo': 'bar'})
  14. >>> f(x= 1, y = 3, z = 'hello')
  15. (1, 3.0, 'hello')
  16. >>> f(z = 'hello', x = 3, y = 2.5)
  17. (3, 2.5, 'hello')
  18. >>> f(1, z = 'hi', y = 3)
  19. (1, 3.0, 'hi')
  20. >>> try: f(1, 2, 'hello', bar = 'baz')
  21. ... except TypeError: pass
  22. ... else: print('expected an exception: unknown keyword')
  23. Exercise the functions using default stubs
  24. >>> f1(z = 'nix', y = .125, x = 2)
  25. (2, 0.125, 'nix')
  26. >>> f1(y = .125, x = 2)
  27. (2, 0.125, 'wow')
  28. >>> f1(x = 2)
  29. (2, 4.25, 'wow')
  30. >>> f1()
  31. (1, 4.25, 'wow')
  32. >>> f2(z = 'nix', y = .125, x = 2)
  33. (2, 0.125, 'nix')
  34. >>> f2(y = .125, x = 2)
  35. (2, 0.125, 'wow')
  36. >>> f2(x = 2)
  37. (2, 4.25, 'wow')
  38. >>> f2()
  39. (1, 4.25, 'wow')
  40. >>> f3(z = 'nix', y = .125, x = 2)
  41. (2, 0.125, 'nix')
  42. >>> f3(y = .125, x = 2)
  43. (2, 0.125, 'wow')
  44. >>> f3(x = 2)
  45. (2, 4.25, 'wow')
  46. >>> f3()
  47. (1, 4.25, 'wow')
  48. Member function tests
  49. >>> q = X()
  50. >>> q.f(x= 1, y = 3, z = 'hello')
  51. (1, 3.0, 'hello')
  52. >>> q.f(z = 'hello', x = 3, y = 2.5)
  53. (3, 2.5, 'hello')
  54. >>> q.f(1, z = 'hi', y = 3)
  55. (1, 3.0, 'hi')
  56. >>> try: q.f(1, 2, 'hello', bar = 'baz')
  57. ... except TypeError: pass
  58. ... else: print('expected an exception: unknown keyword')
  59. Exercise member functions using default stubs
  60. >>> q.f1(z = 'nix', y = .125, x = 2)
  61. (2, 0.125, 'nix')
  62. >>> q.f1(y = .125, x = 2)
  63. (2, 0.125, 'wow')
  64. >>> q.f1(x = 2)
  65. (2, 4.25, 'wow')
  66. >>> q.f1()
  67. (1, 4.25, 'wow')
  68. >>> q.f2.__doc__.splitlines()[1]
  69. 'f2( (X)self [, (int)x [, (float)y [, (str)z]]]) -> tuple :'
  70. >>> q.f2.__doc__.splitlines()[2]
  71. " f2's docstring"
  72. >>> X.f.__doc__.splitlines()[1:5]
  73. ['f( (X)self, (int)x, (float)y, (str)z) -> tuple :', " This is X.f's docstring", '', ' C++ signature :']
  74. >>> xfuncs = (X.inner0, X.inner1, X.inner2, X.inner3, X.inner4, X.inner5)
  75. >>> for f in xfuncs:
  76. ... print(f(q,1).value(), end=' ')
  77. ... print(f(q, n = 1).value(), end=' ')
  78. ... print(f(q, n = 0).value(), end=' ')
  79. ... print(f.__doc__.splitlines()[1:5])
  80. 1 1 0 ['inner0( (X)self, (bool)n) -> Y :', ' docstring', '', ' C++ signature :']
  81. 1 1 0 ['inner1( (X)self, (bool)n) -> Y :', ' docstring', '', ' C++ signature :']
  82. 1 1 0 ['inner2( (X)self, (bool)n) -> Y :', ' docstring', '', ' C++ signature :']
  83. 1 1 0 ['inner3( (X)self, (bool)n) -> Y :', ' docstring', '', ' C++ signature :']
  84. 1 1 0 ['inner4( (X)self, (bool)n) -> Y :', ' docstring', '', ' C++ signature :']
  85. 1 1 0 ['inner5( (X)self, (bool)n) -> Y :', ' docstring', '', ' C++ signature :']
  86. >>> x = X(a1 = 44, a0 = 22)
  87. >>> x.inner0(0).value()
  88. 22
  89. >>> x.inner0(1).value()
  90. 44
  91. >>> x = X(a0 = 7)
  92. >>> x.inner0(0).value()
  93. 7
  94. >>> x.inner0(1).value()
  95. 1
  96. >>> inner(n = 1, self = q).value()
  97. 1
  98. >>> y = Y(value = 33)
  99. >>> y.raw(this = 1, that = 'the other')[1]
  100. {'this': 1, 'that': 'the other'}
  101. """
  102. def run(args = None):
  103. import sys
  104. import doctest
  105. if args is not None:
  106. sys.argv = args
  107. return doctest.testmod(sys.modules.get(__name__))
  108. if __name__ == '__main__':
  109. print("running...")
  110. import sys
  111. status = run()[0]
  112. if (status == 0): print("Done.")
  113. import args_ext
  114. help(args_ext)
  115. sys.exit(status)