def test_abjadextract_1(self):
     input_file_contents = [
         '',
         '\\begin{comment}',
         '<abjadextract abjad.tools.abjadbooktools:example_function \>',
         '\\end{comment}',
         '',
     ]
     document_handler = abjadbooktools.LaTeXDocumentHandler()
     input_blocks = document_handler.collect_input_blocks(
         input_file_contents)
     code_block = tuple(input_blocks.values())[0]
     assert code_block.executed_lines == (
         'from abjad.tools.abjadbooktools import example_function', )
     assert code_block.input_file_contents == (
         'def example_function(expr):',
         "    r'''This is a multiline docstring.",
         '',
         '    This is the third line of the docstring.',
         "    '''",
         '    # This is a comment.',
         "    print('Entering example function.')",
         '    try:',
         '        expr = expr + 1',
         '    except TypeError:',
         "        print('Wrong type!')",
         '    print(expr)',
         "    print('Leaving example function.')",
     )
예제 #2
0
    def test_syntax_error_2(self):
        input_file_contents = [
            '<abjad>[allow_exceptions=true]',
            'foo bar baz',
            '</abjad>',
        ]
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            input_file_contents=input_file_contents, )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == stringtools.normalize(
            '''
            <abjad>[allow_exceptions=true]
            foo bar baz
            </abjad>

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> foo bar baz
              File "<stdin>", line 1
                foo bar baz
                      ^
            SyntaxError: invalid syntax
            \\end{lstlisting}
            %%% ABJADBOOK END %%%
            ''', )
 def test_hide_1(self):
     input_file_contents = [
         '\\begin{comment}',
         '<abjad>[hide=true]',
         'def do_something(expr):',
         "    print('before')",
         '    print(expr + 1)',
         "    print('after')",
         '',
         '</abjad>',
         '\\end{comment}',
         '',
         '\\begin{comment}',
         '<abjad>',
         'do_something(23)',
         '</abjad>',
         '\\end{comment}',
     ]
     document_handler = abjadbooktools.LaTeXDocumentHandler()
     input_blocks = document_handler.collect_input_blocks(
         input_file_contents)
     input_blocks = tuple(input_blocks.values())
     assert input_blocks[0].code_block_specifier is not None
     assert input_blocks[0].code_block_specifier.hide
     assert input_blocks[1].code_block_specifier is None
예제 #4
0
    def test_strip_prompt_2(self):
        input_file_contents = [
            '\\begin{comment}',
            '<abjad>[strip_prompt=true]',
            'def do_something(expr):',
            "    print('before')",
            '    print(expr + 1)',
            "    print('after')",
            '',
            '</abjad>',
            '\\end{comment}',
            '',
            '\\begin{comment}',
            '<abjad>',
            'do_something(23)',
            '</abjad>',
            '\\end{comment}',
            ]
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            input_file_contents=input_file_contents,
            )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == stringtools.normalize(
            """
            \\begin{comment}
            <abjad>[strip_prompt=true]
            def do_something(expr):
                print('before')
                print(expr + 1)
                print('after')

            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            def do_something(expr):
                print('before')
                print(expr + 1)
                print('after')
            \\end{lstlisting}
            %%% ABJADBOOK END %%%

            \\begin{comment}
            <abjad>
            do_something(23)
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> do_something(23)
            before
            24
            after
            \\end{lstlisting}
            %%% ABJADBOOK END %%%
            """,
            )
    def test_abjadextract_3(self):
        input_file_contents = [
            '\\begin{comment}',
            '<abjadextract abjad.tools.abjadbooktools:example_function \>[hide=true]',
            '\\end{comment}',
            '',
            '\\begin{comment}',
            '<abjad>[allow_exceptions=true]',
            "example_function('foo')",
            '</abjad>',
            '\\end{comment}',
            '',
            '\\begin{comment}',
            '<abjad>[allow_exceptions=true]',
            "example_function(23)",
            '</abjad>',
            '\\end{comment}',
        ]
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            input_file_contents=input_file_contents, )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == stringtools.normalize(
            """
            \\begin{comment}
            <abjadextract abjad.tools.abjadbooktools:example_function \\>[hide=true]
            \\end{comment}

            \\begin{comment}
            <abjad>[allow_exceptions=true]
            example_function('foo')
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> example_function('foo')
            Entering example function.
            Wrong type!
            foo
            Leaving example function.
            \\end{lstlisting}
            %%% ABJADBOOK END %%%

            \\begin{comment}
            <abjad>[allow_exceptions=true]
            example_function(23)
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> example_function(23)
            Entering example function.
            24
            Leaving example function.
            \\end{lstlisting}
            %%% ABJADBOOK END %%%
            """, )
예제 #6
0
 def test_invalid_source_8(self):
     input_file_contents = [
         ''
         '%%% ABJADBOOK END %%%',
     ]
     document_handler = abjadbooktools.LaTeXDocumentHandler(
         input_file_contents=input_file_contents, )
     self.assertRaises(
         ValueError,
         document_handler.__call__,
     )
예제 #7
0
 def test_invalid_source_1(self):
     input_file_contents = [
         '<abjad>',
         '<abjad>',
     ]
     document_handler = abjadbooktools.LaTeXDocumentHandler(
         input_file_contents=input_file_contents, )
     self.assertRaises(
         ValueError,
         document_handler.__call__,
     )
예제 #8
0
 def test_syntax_error_1(self):
     input_file_contents = [
         '<abjad>',
         'foo bar baz',
         '</abjad>',
     ]
     document_handler = abjadbooktools.LaTeXDocumentHandler(
         input_file_contents=input_file_contents, )
     self.assertRaises(
         abjadbooktools.AbjadBookError,
         document_handler.__call__,
     )
예제 #9
0
 def test_exception_1(self):
     input_file_contents = [
         '<abjad>',
         "'foo' / 19",
         '</abjad>',
     ]
     document_handler = abjadbooktools.LaTeXDocumentHandler(
         input_file_contents=input_file_contents, )
     self.assertRaises(
         abjadbooktools.AbjadBookError,
         document_handler.__call__,
     )
    def test_latex_root_directory_1(self):
        input_file_contents = [
            '\\begin{comment}',
            '<abjad>',
            'note = Note(0, (1, 4))',
            'show(note)',
            '</abjad>',
            '\\end{comment}',
            ]
        assets_directory = 'ExamplePaper/assets'
        input_file_path = 'ExamplePaper/chapters/chapter-1/section-2.tex'
        latex_root_directory = 'ExamplePaper'
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            assets_directory=assets_directory,
            input_file_contents=input_file_contents,
            input_file_path=input_file_path,
            latex_root_directory=latex_root_directory,
            )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == systemtools.TestManager.clean_string(
            '''
            \\begin{comment}
            <abjad>
            note = Note(0, (1, 4))
            show(note)
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> note = Note(0, (1, 4))
            >>> show(note)
            \\end{lstlisting}
            \\noindent\\includegraphics{assets/lilypond-65d03e56d1fdd997411f2f04c401fe16.pdf}
            %%% ABJADBOOK END %%%
            ''',
            )
    def test_latex_root_directory_1(self):
        input_file_contents = [
            '\\begin{comment}',
            '<abjad>',
            'note = Note(0, (1, 4))',
            'show(note)',
            '</abjad>',
            '\\end{comment}',
        ]
        assets_directory = 'ExamplePaper/assets'
        input_file_path = 'ExamplePaper/chapters/chapter-1/section-2.tex'
        latex_root_directory = 'ExamplePaper'
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            assets_directory=assets_directory,
            input_file_contents=input_file_contents,
            input_file_path=input_file_path,
            latex_root_directory=latex_root_directory,
        )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == stringtools.normalize(
            '''
            \\begin{comment}
            <abjad>
            note = Note(0, (1, 4))
            show(note)
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> note = Note(0, (1, 4))
            >>> show(note)
            \\end{lstlisting}
            \\noindent\\includegraphics{assets/lilypond-fe5d1d78512d19b7f51b96c2ce9180f9.pdf}
            %%% ABJADBOOK END %%%
            ''', )
예제 #12
0
    def test_exception_2(self):
        input_file_contents = [
            '<abjad>[allow_exceptions=true]',
            "'foo' / 19",
            '</abjad>',
        ]
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            input_file_contents=input_file_contents, )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == stringtools.normalize(
            '''
            <abjad>[allow_exceptions=true]
            'foo' / 19
            </abjad>

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> 'foo' / 19
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            TypeError: unsupported operand type(s) for /: 'str' and 'int'
            \\end{lstlisting}
            %%% ABJADBOOK END %%%
            ''', )
    def test_abjadextract_2(self):
        input_file_contents = [
            '\\begin{comment}',
            '<abjadextract abjad.tools.abjadbooktools:example_function \>',
            '\\end{comment}',
            '',
            '\\begin{comment}',
            '<abjad>[allow_exceptions=true]',
            "example_function('foo')",
            '</abjad>',
            '\\end{comment}',
            '',
            '\\begin{comment}',
            '<abjad>[allow_exceptions=true]',
            "example_function(23)",
            '</abjad>',
            '\\end{comment}',
        ]
        document_handler = abjadbooktools.LaTeXDocumentHandler(
            input_file_contents=input_file_contents, )
        rebuilt_source = document_handler(return_source=True)
        assert rebuilt_source == stringtools.normalize(
            """
            \\begin{comment}
            <abjadextract abjad.tools.abjadbooktools:example_function \\>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            def example_function(expr):
                r'''This is a multiline docstring.

                This is the third line of the docstring.
                '''
                # This is a comment.
                print('Entering example function.')
                try:
                    expr = expr + 1
                except TypeError:
                    print('Wrong type!')
                print(expr)
                print('Leaving example function.')
            \\end{lstlisting}
            %%% ABJADBOOK END %%%

            \\begin{comment}
            <abjad>[allow_exceptions=true]
            example_function('foo')
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> example_function('foo')
            Entering example function.
            Wrong type!
            foo
            Leaving example function.
            \\end{lstlisting}
            %%% ABJADBOOK END %%%

            \\begin{comment}
            <abjad>[allow_exceptions=true]
            example_function(23)
            </abjad>
            \\end{comment}

            %%% ABJADBOOK START %%%
            \\begin{lstlisting}
            >>> example_function(23)
            Entering example function.
            24
            Leaving example function.
            \\end{lstlisting}
            %%% ABJADBOOK END %%%
            """, )