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_text(xpath): assert xpath.endswith('.exp') if '@' in xpath: [ipath, language] = xpath[:-4].rsplit('@') else: language = 'en-US' ipath = xpath[:-4] ipath += '.txt' text = _get_output(ipath, language) with open(xpath, 'rt', encoding='UTF-8') as file: expected = file.read() if expected != text: altxpath = xpath[:-4] + '.alt' try: file = open(altxpath, 'rt', encoding='UTF-8') except IOError as exc: if exc.errno == errno.ENOENT: pass else: raise else: with file: alt_expected = file.read() if alt_expected == text: expected = alt_expected assert_multi_line_equal(expected, text)
def _test_good(self, filename): base_name, _ = os.path.splitext(filename) input_filename = base_name + '.input' if not os.path.exists(input_filename): input_filename = os.devnull output_filename = base_name + '.output' rc, stderr = self._compile(filename) stderr = stderr.decode() assert_multi_line_equal(stderr, '') assert_equal(rc, 0) with open(input_filename, 'rb') as input_file: child = ipc.Popen(self.runner + [self.executable], stdin=input_file, stdout=ipc.PIPE, stderr=ipc.PIPE ) stdout = child.stdout.read() stderr = child.stderr.read() rc = child.wait() stderr = stderr.decode() assert_multi_line_equal(stderr, '') assert_equal(rc, 0) with open(output_filename, 'rb') as output_file: expected_stdout = output_file.read() assert_equal(expected_stdout, stdout)
def _test(format): pandoc_output = call_pandoc(format) ref_file = 'tests/spec.{ext}'.format(ext=format) with open(ref_file) as f: nt.assert_multi_line_equal(pandoc_output, f.read())
def test_semanticizer_nlwiki(): tempfile = NamedTemporaryFile() db = create_model(join(dirname(__file__), 'nlwiki-20140927-pages-articles-sample.xml'), tempfile.name) sem = Semanticizer(tempfile.name) dirs = {d: join(dirname(__file__), 'nlwiki', d) for d in "in expected actual".split()} input_test_cases = glob(join(dirs['in'], '*')) assert_equal(len(input_test_cases), 20, msg=("number of input test cases in %r should be 20" % dirs['in'])) for doc in input_test_cases: fname = basename(doc) with open(doc) as f: with open(join(dirs['actual'], fname), 'w') as out: tokens = f.read().split() out.write("\n".join(str(cand) for cand in sem.all_candidates(tokens))) with open(join(dirs['expected'], fname)) as f: expected = f.read() with open(join(dirs['actual'], fname)) as f: actual = f.read() assert_multi_line_equal(expected, actual)
def test_notedown(): """Integration test the whole thing.""" from difflib import ndiff notebook = create_json_notebook(sample_markdown) diff = ndiff(sample_notebook.splitlines(1), notebook.splitlines(1)) print('\n'.join(diff)) nt.assert_multi_line_equal(create_json_notebook(sample_markdown), sample_notebook)
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_notedown(): """Integration test the whole thing.""" from difflib import ndiff notebook = create_json_notebook(sample_markdown) diff = ndiff(sample_notebook.splitlines(1), notebook.splitlines(1)) print '\n'.join(diff) nt.assert_multi_line_equal(create_json_notebook(sample_markdown), sample_notebook)
def test_group_parentheses(): tokens = [ Token(T.Keyword, 'CREATE'), Token(T.Whitespace, ' '), Token(T.Keyword, 'TABLE'), Token(T.Whitespace, ' '), Token(T.Name, 'table_name'), Token(T.Whitespace, ' '), Token(T.Punctuation, '('), Token(T.Name, 'id'), Token(T.Whitespace, ' '), Token(T.Keyword, 'SERIAL'), Token(T.Whitespace, ' '), Token(T.Keyword, 'CHECK'), Token(T.Punctuation, '('), Token(T.Name, 'id'), Token(T.Operator, '='), Token(T.Number, '0'), Token(T.Punctuation, ')'), Token(T.Punctuation, ')'), Token(T.Punctuation, ';'), ] expected_tokens = TokenList([ Token(T.Keyword, 'CREATE'), Token(T.Keyword, 'TABLE'), Token(T.Name, 'table_name'), Parenthesis([ Token(T.Punctuation, '('), Token(T.Name, 'id'), Token(T.Keyword, 'SERIAL'), Token(T.Keyword, 'CHECK'), Parenthesis([ Token(T.Punctuation, '('), Token(T.Name, 'id'), Token(T.Operator, '='), Token(T.Number, '0'), Token(T.Punctuation, ')'), ]), Token(T.Punctuation, ')'), ]), Token(T.Punctuation, ';'), ]) grouped_tokens = group_parentheses(tokens) stdout = sys.stdout try: sys.stdout = StringIO() expected_tokens._pprint_tree() a = sys.stdout.getvalue() sys.stdout = StringIO() grouped_tokens._pprint_tree() b = sys.stdout.getvalue() finally: sys.stdout = stdout assert_multi_line_equal(a, b)
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 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 test_write_table2(self): import os TestObsTable.obstable.add_records_to_table([TestObsTable.obsrecord,TestObsTable.obsrecord]) TestObsTable.obstable.filename = 'testtable2.dat' TestObsTable.obstable.write_table() expected_result = open(TestObsTable.filename, 'r').read() result = open('testtable2.dat', 'r').read() os.remove('testtable2.dat') assert_multi_line_equal(result, expected_result)
def _test_bad(self, filename): base_name, _ = os.path.splitext(filename) error_filename = base_name + '.error' rc, stderr = self._compile(filename, output_filename=os.devnull) stderr = stderr.decode() assert_not_equal(rc, 0) with open(error_filename, 'r') as error_file: expected_stderr = error_file.read() assert_multi_line_equal(expected_stderr, stderr)
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_write_table2(self): import os TestObsTable.obstable.add_records_to_table( [TestObsTable.obsrecord, TestObsTable.obsrecord]) TestObsTable.obstable.filename = 'testtable2.dat' TestObsTable.obstable.write_table() expected_result = open(TestObsTable.filename, 'r').read() result = open('testtable2.dat', 'r').read() os.remove('testtable2.dat') assert_multi_line_equal(result, expected_result)
def _test_text(ipath, xpath): assert xpath.endswith('.exp') if '@' in xpath: language = xpath[:-4].rsplit('@')[1] else: language = 'en-US' text = _get_output(ipath, language) with open(xpath, 'rt', encoding='utf-8') as file: expected = file.read() assert_multi_line_equal(text, expected)
def _test(format): pandoc_cmd = ('pandoc', 'spec.md', '--lua-filter', './pandocBeamerFilter.lua', '--to', format, '-o', 'temp.txt') call_pandoc(pandoc_cmd) with open('temp.txt') as f: pandoc_output = f.read() ref_file = 'tests/spec.{ext}'.format(ext=format) with open(ref_file) as f: nt.assert_multi_line_equal(pandoc_output, f.read()) os.remove('temp.txt')
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_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 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_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_text(xpath): assert xpath.endswith('.exp') if '@' in xpath: [ipath, language] = xpath[:-4].rsplit('@') else: language = 'en-US' ipath = xpath[:-4] ipath += '.txt' text = _get_output(ipath, language) with open(xpath, 'rt', encoding='UTF-8') as file: expected = file.read() assert_multi_line_equal(expected, text)
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_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_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 assert_(self, stdout='', stderr='', rc=0): if stderr is None: pass elif isinstance(stderr, re.type): assert_grep(self.stderr, stderr) else: assert_multi_line_equal(self.stderr, stderr) if rc is not None: assert_equal(self.rc, rc) if stdout is None: pass elif isinstance(stdout, re.type): assert_grep(self.stdout, stdout) else: assert_multi_line_equal(self.stdout, stdout)
def assert_(self, stdout='', stderr='', rc=0): if stderr is None: pass elif isinstance(stderr, re_type): assert_regex(self.stderr, stderr) else: assert_multi_line_equal(self.stderr, stderr) if rc is not None: assert_equal(_ipc_rc(self.rc), _ipc_rc(rc)) if stdout is None: pass elif isinstance(stdout, re_type): assert_regex(self.stdout, stdout) else: assert_multi_line_equal(self.stdout, stdout)
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_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_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_roundtrip(): """Run nbconvert using our custom markdown template to recover original markdown from a notebook. """ # create a notebook from the markdown mr = notedown.MarkdownReader() roundtrip_notebook = mr.to_notebook(roundtrip_markdown) # write the notebook into json notebook_json = nbformat.writes(roundtrip_notebook) # write the json back into notebook notebook = nbformat.reads(notebook_json, as_version=4) # convert notebook to markdown mw = notedown.MarkdownWriter(template_file='notedown/templates/markdown.tpl', strip_outputs=True) markdown = mw.writes(notebook) nt.assert_multi_line_equal(roundtrip_markdown, markdown)
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(format): pandoc_cmd = ('pandoc', 'spec.md', '--lua-filter', './pandocCommentFilter.lua', '--mathjax', '-M', 'comment=draft', '-M', 'fixme=draft', '-M', 'margin=draft', '-M', 'highlight=draft', '--to', format, '-o', 'temp.txt') call_pandoc(pandoc_cmd) with open('temp.txt') as f: pandoc_output = f.read() ref_file = 'tests/spec-draft.{ext}'.format(ext=format) with open(ref_file) as f: nt.assert_multi_line_equal(pandoc_output, f.read()) pandoc_cmd = ('pandoc', 'spec.md', '--lua-filter', './pandocCommentFilter.lua', '--mathjax', '-M', 'comment=print', '-M', 'fixme=print', '-M', 'margin=print', '-M', 'highlight=print', '--to', format, '-o', 'temp.txt') call_pandoc(pandoc_cmd) with open('temp.txt') as f: pandoc_output = f.read() ref_file = 'tests/spec-print.{ext}'.format(ext=format) with open(ref_file) as f: nt.assert_multi_line_equal(pandoc_output, f.read()) pandoc_cmd = ('pandoc', 'spec.md', '--lua-filter', './pandocCommentFilter.lua', '--mathjax', '-M', 'comment=hide', '-M', 'fixme=hide', '-M', 'margin=hide', '-M', 'highlight=hide', '--to', format, '-o', 'temp.txt') call_pandoc(pandoc_cmd) with open('temp.txt') as f: pandoc_output = f.read() ref_file = 'tests/spec-hide.{ext}'.format(ext=format) with open(ref_file) as f: nt.assert_multi_line_equal(pandoc_output, f.read()) os.remove('temp.txt')
def _test_text(xpath): assert xpath.endswith('.exp') if '@' in xpath: [ipath, language] = xpath[:-4].rsplit('@') else: language = 'en-US' ipath = xpath[:-4] ipath += '.txt' text = _get_output(ipath, language) with open(xpath, 'rt', encoding='UTF-8') as file: expected = file.read() if expected != text: altxpath = xpath[:-4] + '.alt' try: file = open(altxpath, 'rt', encoding='UTF-8') except FileNotFoundError: pass else: with file: alt_expected = file.read() if alt_expected == text: expected = alt_expected assert_multi_line_equal(expected, text)
def test_R(): """Check that the R notebook generated from Rmd looks the same as the reference (without output cells). """ knitr = notedown.Knitr() with open('r-examples/r-example.Rmd') as rmd: knitted_markdown_file = knitr.knit(rmd) reader = notedown.MarkdownReader(precode=r"%load_ext rpy2.ipython", magic=True) notebook = reader.read(knitted_markdown_file) with open('r-examples/r-example.ipynb') as f: reference_notebook = nbformat.read(f, as_version=4) notedown.main.strip(notebook) notedown.main.strip(reference_notebook) writer = nbformat nbjson = writer.writes(notebook) reference_nbjson = writer.writes(reference_notebook) nt.assert_multi_line_equal(nbjson, reference_nbjson)
def test_indent_block(): block = lines("a", "", "b") indented_block = indent_block(block, 1) assert_multi_line_equal(indented_block, lines(" a", "", " b"))