CodeToRest_test.py - Unit testing

This test bench exercises the CodeToRest module. First, set up for development (see To package). To run, execute py.test from the command line. Note the period in this command – pytest does NOT work (it is a completely different program).

Imports

These are listed in the order prescribed by PEP 8.

Library imports

from io import StringIO
import re
import shutil
from textwrap import dedent
 

Third-party imports

Used to run docutils.

from docutils import core
from pygments.token import Token
from pygments.lexers import get_lexer_by_name
import pygments.util
import pytest
 

Local application imports

from CodeChat.RestToCode import rest_to_code_string, remove_codechat_style
from CodeChat.CodeToRest import code_to_rest_string, code_to_html_file, _generate_rest
from CodeChat.SourceClassifier import (
    _remove_comment_delim,
    _group_lexer_tokens,
    _gather_groups_on_newlines,
    _is_rest_comment,
    _classify_groups,
    _is_space_indented_line,
    _is_delim_indented_line,
    _GROUP,
    _pygments_lexer,
)
from CodeChat.CommentDelimiterInfo import COMMENT_DELIMITER_INFO
 

See if we have the PIC24 lexer installed.

try:
    PIC24 = get_lexer_by_name("PIC24")
except pygments.util.ClassNotFound:
    PIC24 = None
 

Define some commonly-used strings to make testing less verbose. Per the Summary and implementation, “Code blocks must be preceded and followed by a removed marker (fences).” These two functions (begin fence == bf, end fence == ef) contain the fence strings _generate_rest produces.

bf = "\n" ".. fenced-code::\n" "\n" " Beginning fence\n"
ef = " Ending fence\n" "\n" "..\n" "\n"
 
 

_generate_rest inserts a <div> to format indented comments followed by a set-line directive to show line numbers of the comments correctly. This function generates the same string.

def div(

The size of the indent, in em. Each space = 0.5 em, so a 3-space indent would be size=1.5.

    size,

The line number passed to sl below.

    line,
):

    return (
        "\n"
        ".. raw:: html\n"
        '\n <div class="CodeChat-indent" style="margin-left:{}em;">\n'
        "\n"
    ).format(size) + sl(line)
 
 

After a <div>, _generate_rest inserts a set-line directive. This function provides that directive as a string.

def sl(

The line number for the set-line directive, which is comment_line - 4. For example, for a comment in the first line of the file, implying comment_line == 1, use sl(-3).

    line,
):
    return ("\n" ".. set-line:: {}\n" "\n" "..\n" "\n").format(line)
 
 

The standard string which marks the end of a <div>.

div_end = "\n" ".. raw:: html\n" "\n" " </div>\n" "\n" "..\n" "\n"
 
 

Commonly-used comment delimiters.

c_comment_delimiter_info = COMMENT_DELIMITER_INFO["C"]
py_comment_delimiter_info = COMMENT_DELIMITER_INFO["Python"]
 

Commonly-used lexers.

c_lexer = get_lexer_by_name("c")
 
 

This actually tests using code_to_rest_string, since that makes code_to_rest easy to call.

class TestCodeToRest:

C-like language tests

multi-test: Check that the given code’s output is correct over the provided sequence of languages.