Home | History | Annotate | Download | only in Tests
      1 from Cython.TestUtils import CythonTest
      2 import Cython.Compiler.Errors as Errors
      3 from Cython.Compiler.Nodes import *
      4 from Cython.Compiler.ParseTreeTransforms import *
      5 from Cython.Compiler.Buffer import *
      6 
      7 
      8 class TestBufferParsing(CythonTest):
      9     # First, we only test the raw parser, i.e.
     10     # the number and contents of arguments are NOT checked.
     11     # However "dtype"/the first positional argument is special-cased
     12     #  to parse a type argument rather than an expression
     13 
     14     def parse(self, s):
     15         return self.should_not_fail(lambda: self.fragment(s)).root
     16 
     17     def not_parseable(self, expected_error, s):
     18         e = self.should_fail(lambda: self.fragment(s),  Errors.CompileError)
     19         self.assertEqual(expected_error, e.message_only)
     20 
     21     def test_basic(self):
     22         t = self.parse(u"cdef object[float, 4, ndim=2, foo=foo] x")
     23         bufnode = t.stats[0].base_type
     24         self.assert_(isinstance(bufnode, TemplatedTypeNode))
     25         self.assertEqual(2, len(bufnode.positional_args))
     26 #        print bufnode.dump()
     27         # should put more here...
     28 
     29     def test_type_pos(self):
     30         self.parse(u"cdef object[short unsigned int, 3] x")
     31 
     32     def test_type_keyword(self):
     33         self.parse(u"cdef object[foo=foo, dtype=short unsigned int] x")
     34 
     35     def test_pos_after_key(self):
     36         self.not_parseable("Non-keyword arg following keyword arg",
     37                            u"cdef object[foo=1, 2] x")
     38 
     39 
     40 # See also tests/error/e_bufaccess.pyx and tets/run/bufaccess.pyx
     41 # THESE TESTS ARE NOW DISABLED, the code they test was pretty much
     42 # refactored away
     43 class TestBufferOptions(CythonTest):
     44     # Tests the full parsing of the options within the brackets
     45 
     46     def nonfatal_error(self, error):
     47         # We're passing self as context to transform to trap this
     48         self.error = error
     49         self.assert_(self.expect_error)
     50 
     51     def parse_opts(self, opts, expect_error=False):
     52         assert opts != ""
     53         s = u"def f():\n  cdef object[%s] x" % opts
     54         self.expect_error = expect_error
     55         root = self.fragment(s, pipeline=[NormalizeTree(self), PostParse(self)]).root
     56         if not expect_error:
     57             vardef = root.stats[0].body.stats[0]
     58             assert isinstance(vardef, CVarDefNode) # use normal assert as this is to validate the test code
     59             buftype = vardef.base_type
     60             self.assert_(isinstance(buftype, TemplatedTypeNode))
     61             self.assert_(isinstance(buftype.base_type_node, CSimpleBaseTypeNode))
     62             self.assertEqual(u"object", buftype.base_type_node.name)
     63             return buftype
     64         else:
     65             self.assert_(len(root.stats[0].body.stats) == 0)
     66 
     67     def non_parse(self, expected_err, opts):
     68         self.parse_opts(opts, expect_error=True)
     69 #        e = self.should_fail(lambda: self.parse_opts(opts))
     70         self.assertEqual(expected_err, self.error.message_only)
     71 
     72     def __test_basic(self):
     73         buf = self.parse_opts(u"unsigned short int, 3")
     74         self.assert_(isinstance(buf.dtype_node, CSimpleBaseTypeNode))
     75         self.assert_(buf.dtype_node.signed == 0 and buf.dtype_node.longness == -1)
     76         self.assertEqual(3, buf.ndim)
     77 
     78     def __test_dict(self):
     79         buf = self.parse_opts(u"ndim=3, dtype=unsigned short int")
     80         self.assert_(isinstance(buf.dtype_node, CSimpleBaseTypeNode))
     81         self.assert_(buf.dtype_node.signed == 0 and buf.dtype_node.longness == -1)
     82         self.assertEqual(3, buf.ndim)
     83 
     84     def __test_ndim(self):
     85         self.parse_opts(u"int, 2")
     86         self.non_parse(ERR_BUF_NDIM, u"int, 'a'")
     87         self.non_parse(ERR_BUF_NDIM, u"int, -34")
     88 
     89     def __test_use_DEF(self):
     90         t = self.fragment(u"""
     91         DEF ndim = 3
     92         def f():
     93             cdef object[int, ndim] x
     94             cdef object[ndim=ndim, dtype=int] y
     95         """, pipeline=[NormalizeTree(self), PostParse(self)]).root
     96         stats = t.stats[0].body.stats
     97         self.assert_(stats[0].base_type.ndim == 3)
     98         self.assert_(stats[1].base_type.ndim == 3)
     99 
    100     # add exotic and impossible combinations as they come along...
    101 
    102 if __name__ == '__main__':
    103     import unittest
    104     unittest.main()
    105 
    106