def test_function_string_with_return(): f = Function("test.hpp", "", "my_fun", "double") assert_multi_line_equal( str(f), lines( "Function 'my_fun'", " Returns (double)" ) )
def test_convert_to_docstring(): assert_equal(convert_to_docstring(None), "") assert_multi_line_equal( convert_to_docstring(lines( "/**", " * This is a brief comment.", " */")), "This is a brief comment." ) assert_multi_line_equal( convert_to_docstring("/// This is a brief comment."), "This is a brief comment." ) assert_multi_line_equal( convert_to_docstring(lines( "/**", " * This is a brief comment.", " *", " * This is a detailed comment.", " */") ), lines( "This is a brief comment.", "", "This is a detailed comment.") ) assert_multi_line_equal( convert_to_docstring(lines( "/**", " * This is a brief comment.", " * This is a detailed comment.", " */") ), lines( "This is a brief comment.", "", "This is a detailed comment.") ) assert_multi_line_equal( convert_to_docstring(lines( "/**", " * This is a brief comment.", " * ", " * This is a detailed comment.", " * It contains mathematical equations, like 2 * 3 + 5 = 11.", " * It is important that it includes '*'. 5x * 3x*y = y*z!" " */") ), lines( "This is a brief comment.", "", "This is a detailed comment.", "It contains mathematical equations, like 2 * 3 + 5 = 11.", "It is important that it includes '*'. 5x * 3x*y = y*z!") )
def test_simple_function_def(): method = MethodDefinition( "Testclass", "", "testfun", [], Includes(), "void", TypeInfo({}), Config()).make() assert_multi_line_equal( method, lines("cpdef testfun(Testclass self):", " self.thisptr.testfun()") )
def python_to_cpp(self): return lines( "cdef int %(python_argname)s_length = %(python_argname)s.shape[0]", "cdef cpp.VectorXd %(cpp_argname)s = cpp.VectorXd(%(python_argname)s_length)", "cdef int %(python_argname)s_idx", "for %(python_argname)s_idx in range(%(python_argname)s_length):", " %(cpp_argname)s.data()[%(python_argname)s_idx] = %(python_argname)s[%(python_argname)s_idx]" ) % {"python_argname": self.python_argname, "cpp_argname": self.cpp_argname}
def test_make_header(): assert_multi_line_equal( make_header("a b c d"), lines( "+" + "=" * 78 + "+", "| a b c d" + " " * 70 + "|", "+" + "=" * 78 + "+" ) )
def test_template_class_string(): c = TemplateClass("test.hpp", "", "MyTemplateClass") c.template_types.append("T") assert_multi_line_equal( str(c), lines( "TemplateClass 'MyTemplateClass' ('MyTemplateClass')", " Template type 'T'" ) )
def return_output(self, copy=True): return lines( "cdef int size = result.rows()", "cdef int res_idx", "cdef np.ndarray[double, ndim=1] res = np.ndarray(shape=(size,))", "for res_idx in range(size):", " res[res_idx] = result.get(res_idx)", "return res" )
def test_function_def_with_another_cppname(): fun = FunctionDefinition("myFunInt", "", [], Includes(), "void", TypeInfo(), Config(), cppname="myFun").make() assert_multi_line_equal( fun, lines( "cpdef my_fun_int():", " cpp.myFun()" ) )
def test_function_def(): fun = FunctionDefinition("myFun", "", [], Includes(), "void", TypeInfo(), Config()).make() assert_multi_line_equal( fun, lines( "cpdef my_fun():", " cpp.myFun()" ) )
def test_default_ctor_def(): ctor = ConstructorDefinition("MyClass", "", [], Includes(), TypeInfo(), Config(), "MyClass").make() assert_multi_line_equal( ctor, lines( "def __init__(MyClass self):", " self.thisptr = new cpp.MyClass()" ) )
def test_array_arg_function_def(): method = MethodDefinition( "Testclass", "", "testfun", [Param("a", "double *"), Param("aSize", "unsigned int")], Includes(), "void", TypeInfo({}), Config()).make() assert_multi_line_equal( method, lines("cpdef testfun(Testclass self, np.ndarray[double, ndim=1] a):", " self.thisptr.testfun(&a[0], a.shape[0])") )
def test_comments(): with cython_extension_from("comments.hpp"): from comments import MyClass, MyEnum assert_multi_line_equal( lines("This is a brief class description.", "", " And this is a detailed description.", " "), MyClass.__doc__) assert_equal("Brief.", MyClass.method.__doc__.strip()) assert_equal("Brief description of enum.", MyEnum.__doc__.strip())
def test_comments(): with cython_extension_from("comments.hpp"): from comments import MyClass, MyEnum assert_multi_line_equal( lines("This is a brief class description.", " ", " And this is a detailed description.", " "), MyClass.__doc__) assert_equal("Brief.", MyClass.method.__doc__.strip()) assert_equal("Brief description of enum.", MyEnum.__doc__.strip())
def test_template_function_string(): m = TemplateFunction("test.hpp", "", "my_template_fun", "void") m.nodes.append(Param("t", "T")) m.template_types.append("T") assert_multi_line_equal( str(m), lines( "TemplateFunction 'my_template_fun'", " Parameter (T) t", " Template type 'T'" ) )
def python_to_cpp(self): return lines( "cdef int %(python_argname)s_length = %(python_argname)s.shape[0]", "cdef cpp.VectorXd %(cpp_argname)s = cpp.VectorXd(%(python_argname)s_length)", "cdef int %(python_argname)s_idx", "for %(python_argname)s_idx in range(%(python_argname)s_length):", " %(cpp_argname)s.data()[%(python_argname)s_idx] = %(python_argname)s[%(python_argname)s_idx]" ) % { "python_argname": self.python_argname, "cpp_argname": self.cpp_argname }
def test_template_method_string(): m = TemplateMethod("my_template_method", "void", "MyClass") m.nodes.append(Param("t", "T")) m.template_types.append("T") assert_multi_line_equal( str(m), lines( "TemplateMethod 'my_template_method'", " Parameter (T) t", " Template type 'T'" ) )
def test_setter_definition(): field = Field("myField", "double", "MyClass") setter = SetterDefinition( "MyClass", field, Includes(), TypeInfo(), Config()).make() assert_multi_line_equal( setter, lines( "cpdef set_my_field(MyClass self, double myField):", " cdef double cpp_myField = myField", " self.thisptr.myField = cpp_myField" ) )
def test_getter_definition(): field = Field("myField", "double", "MyClass") getter = GetterDefinition( "MyClass", field, Includes(), TypeInfo(), Config()).make() assert_multi_line_equal( getter, lines( "cpdef get_my_field(MyClass self):", " cdef double result = self.thisptr.myField", " return result" ) )
def test_setter_definition(): field = Field("myField", "double", "MyClass") setter = SetterDefinition( "MyClass", field, Includes(), TypeInfo(), Config()).make() assert_multi_line_equal( setter, lines( "cpdef __set_my_field(MyClass self, double myField):", " cdef double cpp_myField = myField", " self.thisptr.myField = cpp_myField" ) )
def test_getter_definition(): field = Field("myField", "double", "MyClass") getter = GetterDefinition( "MyClass", field, Includes(), TypeInfo(), Config()).make() assert_multi_line_equal( getter, lines( "cpdef __get_my_field(MyClass self):", " cdef double result = self.thisptr.myField", " return result", "" ) )
def test_typedef_decl(): typedef = Typedef("test.hpp", "", "MyType", "double") exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_typedef(typedef) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " ctypedef double MyType" ) )
def test_class_decl(): clazz = Clazz("test.hpp", "", "MyClass") exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_clazz(clazz) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " cdef cppclass MyClass:", " pass" ) )
def test_function_decl(): fun = Function("test.hpp", "", "myFun", "void") ignored_fun = Function("test.hpp", "", "myFun", "void") ignored_fun.ignored = True exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_function(fun) exporter.visit_function(ignored_fun) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " void myFun() except +" ) )
def test_enum_decl(): enum = Enum("test.hpp", "", "MyEnum") enum.constants.append("one") enum.constants.append("two") exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_enum(enum) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " cdef enum MyEnum:", " one", " two" ) )
def test_field_decl(): clazz = Clazz("test.hpp", "", "MyClass") field = Field("myField", "double", "MyClass") ignored_field = Field("myField", "double", "MyClass") ignored_field.ignored = True exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_field(field) exporter.visit_field(ignored_field) exporter.visit_clazz(clazz) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " cdef cppclass MyClass:", " double myField" ) )
def test_ctor_decl(): clazz = Clazz("test.hpp", "", "MyClass") ctor = Constructor("MyClass") ignored_ctor = Constructor("MyClass") ignored_ctor.ignored = True exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_constructor(ctor) exporter.visit_constructor(ignored_ctor) exporter.visit_clazz(clazz) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " cdef cppclass MyClass:", " MyClass()" ) )
def test_method_decl(): clazz = Clazz("test.hpp", "", "MyClass") method = Method("myMethod", "void", "MyClass") ignored_method = Method("", "", "") ignored_method.ignored = True exporter = CythonDeclarationExporter(Includes(), Config()) exporter.visit_param(Param("myParam", "double")) exporter.visit_method(method) exporter.visit_method(ignored_method) exporter.visit_clazz(clazz) exporter.visit_ast(None) decl = exporter.export() assert_multi_line_equal( decl.strip(), lines( "cdef extern from \"test.hpp\" namespace \"\":", " cdef cppclass MyClass:", " void myMethod(double myParam) except +" ) )
def test_convert_to_docstring(): assert_equal(convert_to_docstring(None), "") assert_multi_line_equal( convert_to_docstring(lines("/** This is a brief comment */")), "This is a brief comment.") assert_multi_line_equal( convert_to_docstring(lines("/**", " * This is a brief comment.", " */")), "This is a brief comment.") assert_multi_line_equal( convert_to_docstring("/// This is a brief comment."), "This is a brief comment.") assert_multi_line_equal( convert_to_docstring( lines("/**", " * This is a brief comment.", " *", " * This is a detailed comment.", " */")), lines("This is a brief comment.", "", "This is a detailed comment.")) assert_multi_line_equal( convert_to_docstring( lines("/**", " * This is a brief comment.", " * This is a detailed comment.", " */")), lines("This is a brief comment.", "", "This is a detailed comment.")) assert_multi_line_equal( convert_to_docstring( lines( "/**", " * This is a brief comment.", " * ", " * This is a detailed comment.", " * It contains mathematical equations, like 2 * 3 + 5 = 11.", " * It is important that it includes '*'. 5x * 3x*y = y*z!" " */")), lines("This is a brief comment.", "", "This is a detailed comment.", "It contains mathematical equations, like 2 * 3 + 5 = 11.", "It is important that it includes '*'. 5x * 3x*y = y*z!"))
def test_class_string_with_members(): c = Clazz("test.hpp", "", "MyClass") ctor = Constructor("MyClass") ctor.nodes.append(Param("a", "double")) c.nodes.append(ctor) method = Method("my_method", "int", "MyClass") method.nodes.append(Param("b", "int")) c.nodes.append(method) field = Field("my_field", "bool", "MyClass") c.nodes.append(field) assert_multi_line_equal( str(c), lines( "Class 'MyClass' ('MyClass')", " Constructor '__init__'", " Parameter (double) a", " Method 'my_method'", " Parameter (int) b", " Returns (int)", " Field (bool) my_field" ))
def test_ast_string_with_function(): ast = Ast() ast.nodes.append(Function("test.hpp", "", "fun", "void")) assert_multi_line_equal(str(ast), lines("AST", " Function 'fun'"))
def return_output(self, copy=True): return lines( "cdef int size = result.rows()", "cdef int res_idx", "cdef np.ndarray[double, ndim=1] res = np.ndarray(shape=(size,))", "for res_idx in range(size):", " res[res_idx] = result.get(res_idx)", "return res")
def test_indent_block(): block = lines("a", "", "b") indented_block = indent_block(block, 1) assert_multi_line_equal(indented_block, lines(" a", "", " b"))
def test_make_header(): assert_multi_line_equal( make_header("a b c d"), lines("+" + "=" * 78 + "+", "| a b c d" + " " * 70 + "|", "+" + "=" * 78 + "+"))
def test_ast_string_with_enum(): ast = Ast() ast.nodes.append(Enum("test.hpp", "", "MyEnum")) assert_multi_line_equal(str(ast), lines("AST", " Enum 'MyEnum'"))
def test_ast_string_with_typedef(): ast = Ast() ast.nodes.append(Typedef("test.hpp", "", "MyTypedef", "double")) assert_multi_line_equal( str(ast), lines("AST", " Typedef (double) MyTypedef"))
def test_ast_string_with_class(): ast = Ast() ast.nodes.append(Clazz("test.hpp", "bla", "MyClass")) assert_multi_line_equal( str(ast), lines( "AST", " Class 'MyClass' ('MyClass') (namespace: 'bla')"))