Back to index

python3.2  3.2.2
Public Member Functions
test.test_keywordonlyarg.KeywordOnlyArgTestCase Class Reference

List of all members.

Public Member Functions

def assertRaisesSyntaxError
def testSyntaxErrorForFunctionDefinition
def testSyntaxForManyArguments
def testTooManyPositionalErrorMessage
def testSyntaxErrorForFunctionCall
def testRaiseErrorFuncallWithUnexpectedKeywordArgument
def testFunctionCall
def testKwDefaults
def test_kwonly_methods

Detailed Description

Definition at line 40 of file test_keywordonlyarg.py.


Member Function Documentation

Definition at line 41 of file test_keywordonlyarg.py.

00041 
00042     def assertRaisesSyntaxError(self, codestr):
00043         def shouldRaiseSyntaxError(s):
00044             compile(s, "<test>", "single")
00045         self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 156 of file test_keywordonlyarg.py.

00156 
00157     def test_kwonly_methods(self):
00158         class Example:
00159             def f(self, *, k1=1, k2=2):
00160                 return k1, k2
00161 
00162         self.assertEqual(Example().f(k1=1, k2=2), (1, 2))
00163         self.assertEqual(Example.f(Example(), k1=1, k2=2), (1, 2))
00164         self.assertRaises(TypeError, Example.f, k1=1, k2=2)

Here is the call graph for this function:

Definition at line 103 of file test_keywordonlyarg.py.

00103 
00104     def testFunctionCall(self):
00105         self.assertEqual(1, posonly_sum(1))
00106         self.assertEqual(1+2, posonly_sum(1,**{"2":2}))
00107         self.assertEqual(1+2+3, posonly_sum(1,*(2,3)))
00108         self.assertEqual(1+2+3+4, posonly_sum(1,*(2,3),**{"4":4}))
00109 
00110         self.assertEqual(1, keywordonly_sum(k2=1))
00111         self.assertEqual(1+2, keywordonly_sum(k1=1, k2=2))
00112 
00113         self.assertEqual(1+2, keywordonly_and_kwarg_sum(k1=1, k2=2))
00114         self.assertEqual(1+2+3, keywordonly_and_kwarg_sum(k1=1, k2=2, k3=3))
00115         self.assertEqual(1+2+3+4,
00116                          keywordonly_and_kwarg_sum(k1=1, k2=2,
00117                                                     **{"a":3,"b":4}))
00118 
00119         self.assertEqual(1+2, mixedargs_sum(1, k1=2))
00120         self.assertEqual(1+2+3, mixedargs_sum(1, 2, k1=3))
00121         self.assertEqual(1+2+3+4, mixedargs_sum(1, 2, k1=3, k2=4))
00122         self.assertEqual(1+2+3+4+5, mixedargs_sum(1, 2, 3, k1=4, k2=5))
00123 
00124         self.assertEqual(1+2, mixedargs_sum2(1, k1=2))
00125         self.assertEqual(1+2+3, mixedargs_sum2(1, 2, k1=3))
00126         self.assertEqual(1+2+3+4, mixedargs_sum2(1, 2, k1=3, k2=4))
00127         self.assertEqual(1+2+3+4+5, mixedargs_sum2(1, 2, 3, k1=4, k2=5))
00128         self.assertEqual(1+2+3+4+5+6,
00129                          mixedargs_sum2(1, 2, 3, k1=4, k2=5, k3=6))
00130         self.assertEqual(1+2+3+4+5+6,
00131                          mixedargs_sum2(1, 2, 3, k1=4, **{'k2':5, 'k3':6}))
00132 
00133         self.assertEqual(1, Foo(k1=1).sum())
00134         self.assertEqual(1+2, Foo(k1=1,k2=2).sum())
00135 
00136         self.assertEqual([1,2,3], sortnum(3,2,1))
00137         self.assertEqual([3,2,1], sortnum(1,2,3, reverse=True))
00138 
00139         self.assertEqual(['a','b','c'], sortwords('a','c','b'))
00140         self.assertEqual(['c','b','a'], sortwords('a','c','b', reverse=True))
00141         self.assertEqual(['c','b','a'],
00142                          sortwords('a','c','b', reverse=True, ignore='ignore'))

Here is the call graph for this function:

Definition at line 143 of file test_keywordonlyarg.py.

00143 
00144     def testKwDefaults(self):
00145         def foo(p1,p2=0, *, k1, k2=0):
00146             return p1 + p2 + k1 + k2
00147 
00148         self.assertEqual(2, foo.__code__.co_kwonlyargcount)
00149         self.assertEqual({"k2":0}, foo.__kwdefaults__)
00150         foo.__kwdefaults__ = {"k1":0}
00151         try:
00152             foo(1,k1=10)
00153             self.fail("__kwdefaults__ is not properly changed")
00154         except TypeError:
00155             pass

Here is the call graph for this function:

Definition at line 88 of file test_keywordonlyarg.py.

00088 
00089     def testRaiseErrorFuncallWithUnexpectedKeywordArgument(self):
00090         self.assertRaises(TypeError, keywordonly_sum, ())
00091         self.assertRaises(TypeError, keywordonly_nodefaults_sum, ())
00092         self.assertRaises(TypeError, Foo, ())
00093         try:
00094             keywordonly_sum(k2=100, non_existing_arg=200)
00095             self.fail("should raise TypeError")
00096         except TypeError:
00097             pass
00098         try:
00099             keywordonly_nodefaults_sum(k2=2)
00100             self.fail("should raise TypeError")
00101         except TypeError:
00102             pass

Here is the call graph for this function:

Definition at line 84 of file test_keywordonlyarg.py.

00084 
00085     def testSyntaxErrorForFunctionCall(self):
00086         self.assertRaisesSyntaxError("f(p, k=1, p2)")
00087         self.assertRaisesSyntaxError("f(p, k1=50, *(1,2), k1=100)")

Here is the call graph for this function:

Definition at line 46 of file test_keywordonlyarg.py.

00046 
00047     def testSyntaxErrorForFunctionDefinition(self):
00048         self.assertRaisesSyntaxError("def f(p, *):\n  pass\n")
00049         self.assertRaisesSyntaxError("def f(p1, *, p1=100):\n  pass\n")
00050         self.assertRaisesSyntaxError("def f(p1, *k1, k1=100):\n  pass\n")
00051         self.assertRaisesSyntaxError("def f(p1, *, k1, k1=100):\n  pass\n")
00052         self.assertRaisesSyntaxError("def f(p1, *, **k1):\n  pass\n")
00053         self.assertRaisesSyntaxError("def f(p1, *, k1, **k1):\n  pass\n")
00054         self.assertRaisesSyntaxError("def f(p1, *, None, **k1):\n  pass\n")
00055         self.assertRaisesSyntaxError("def f(p, *, (k1, k2), **kw):\n  pass\n")

Here is the call graph for this function:

Definition at line 56 of file test_keywordonlyarg.py.

00056 
00057     def testSyntaxForManyArguments(self):
00058         fundef = "def f("
00059         for i in range(255):
00060             fundef += "i%d, "%i
00061         fundef += "*, key=100):\n pass\n"
00062         self.assertRaisesSyntaxError(fundef)
00063 
00064         fundef2 = "def foo(i,*,"
00065         for i in range(255):
00066             fundef2 += "i%d, "%i
00067         fundef2 += "lastarg):\n  pass\n"
00068         self.assertRaisesSyntaxError(fundef2)
00069 
00070         # exactly 255 arguments, should compile ok
00071         fundef3 = "def f(i,*,"
00072         for i in range(253):
00073             fundef3 += "i%d, "%i
00074         fundef3 += "lastarg):\n  pass\n"
00075         compile(fundef3, "<test>", "single")

Here is the call graph for this function:

Definition at line 76 of file test_keywordonlyarg.py.

00076 
00077     def testTooManyPositionalErrorMessage(self):
00078         def f(a, b=None, *, c=None):
00079             pass
00080         with self.assertRaises(TypeError) as exc:
00081             f(1, 2, 3)
00082         expected = "f() takes at most 2 positional arguments (3 given)"
00083         self.assertEqual(str(exc.exception), expected)


The documentation for this class was generated from the following file: