コード例 #1
0
def test_write(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()

    assert proc.process(r'\write[filename="write_macro.out"]{test}') == ""
    with open("write_macro.out") as f:
        assert f.read() == "test"
コード例 #2
0
def test_parser(tmp_path):
    os.chdir(tmp_path)

    def macro(self, args, opts):
        return "PROCESSED"

    def numargs(self, args, opts):
        return str(len(args))

    def countopts(self, args, opts):
        return str(len(self.parse_options_str(opts)))

    proc = macro_expander.MacroProcessor()
    proc.addMacro('macro', macro)
    proc.addMacro('numargs', numargs)
    proc.addMacro('numopts', countopts)
    assert proc.process(
        r' \macro[a="A"]{argument1}{argument2} ') == r''' PROCESSED '''
    assert proc.process(
        r' \macro{argument1}{argument2} ') == r''' PROCESSED '''
    assert proc.process(r' \macro{argument1} ') == r''' PROCESSED '''
    assert proc.process(r' \macro{} ') == r''' PROCESSED '''

    assert proc.process(r' \numargs{a1} ') == r''' 1 '''
    assert proc.process(r' \numargs{a1}{a2} ') == r''' 2 '''
    assert proc.process(r' \numargs{a1}{a2}{a3} ') == r''' 3 '''

    assert proc.process(r' \numopts[a="A"]{a1} ') == r''' 1 '''
    assert proc.process(r' \numopts[a="A",b="B"]{a1} ') == r''' 2 '''
    assert proc.process(r' \numopts[a="A",b="B",c="C"]{a1} ') == r''' 3 '''
    assert proc.process(r' \numopts[a="A", b="B"]{a1} ') == r''' 2 '''
    assert proc.process(r' \numopts[a="A" , b="B"]{a1} ') == r''' 2 '''
    assert proc.process(r' \numopts[a ="A", b= "B"]{a1} ') == r''' 2 '''

    assert proc.process('\tpre:\macro{}:post') == '''\tpre:PROCESSED:post'''
コード例 #3
0
def test_large_multi_macro_strings(tmp_path):
    os.chdir(tmp_path)
    print()

    def process(self, args, opts):
        return "PROCESSED"

    def numargs(self, args, opts):
        return str(len(args))

    def countopts(self, args, opts):
        return str(len(self.parse_options_str(opts)))

    proc = macro_expander.MacroProcessor(use_cache=True)
    proc.addMacro('process', process)
    proc.addMacro('numargs', numargs)
    proc.addMacro('numopts', countopts)

    text = 1000 * r'pre-text \process{} middle text \numargs{one}{two} middle text 2 \numopts[a="1",b="2",c="3"]{} post text\n'

    def run():
        return proc.process(text)

    # assert  run() == r'''PROCESSED'''

    N = 2
    time = timeit.timeit(run, number=N)
    print("1000 line text with 3 macros each:", time / 2, "s")
コード例 #4
0
def test_small_single_macro_strings(tmp_path):
    os.chdir(tmp_path)
    print()

    def process(self, args, opts):
        return "PROCESSED"

    def numargs(self, args, opts):
        return str(len(args))

    def countopts(self, args, opts):
        return str(len(self.parse_options_str(opts)))

    proc = macro_expander.MacroProcessor()
    proc.addMacro('process', process)
    proc.addMacro('numargs', numargs)
    proc.addMacro('numopts', countopts)

    def run():
        return proc.process(r'\process{}')

    # assert  run() == r'''PROCESSED'''

    N = 1000
    time = timeit.timeit(run, number=N)
    print("single macro only:", time, "ms")

    def run():
        return proc.process(r'preamble text: \process{} : postamble text')

    # assert  run() == r'''preamble text: PROCESSED : postamble text'''

    N = 1000
    time = timeit.timeit(run, number=N)
    print("single macro with pre/post amble:", time, "ms")
コード例 #5
0
def test_img(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()

    assert macro_expander.our_macros._img(
        "file.png", output="markdown") == "![](./file.png)"
    assert macro_expander.our_macros._img(
        "file.png", output="latex") == r"\includegraphics{./file.png}"
コード例 #6
0
def test_shell(tmp_path):
    os.chdir(tmp_path)

    proc = macro_expander.MacroProcessor()

    assert proc.process(r"\shell{echo Hello World}") == "Hello World\n"
    assert proc.process(r"\shell{echo;echo Hello World}") == "\nHello World\n"
    assert proc.process(
        r"\shell[lstrip]{echo;echo Hello World}") == "Hello World\n"
    assert proc.process(
        r"\shell[rstrip]{echo;echo Hello World}") == "\nHello World"
    assert proc.process(
        r"\shell[strip]{echo;echo Hello World}") == "Hello World"
コード例 #7
0
    def __init__(self, fh=None, use_macro_expander_cache=True):
        super().__init__(fh)
        self.config.default_relative_numerical_uncertainty = 0.01
        self.config.minimum_relative_numerical_uncertainty = 0.01
        self.macro_expander_cache_path = pathlib.Path(
            "pyAssignment-BlackboardQuiz-Writer-Cache.bin")
        if have_macro_expander:
            self.macro_processor = macro_expander.MacroProcessor(
                use_cache=use_macro_expander_cache)
        else:
            self.macro_processor = DummyMacroProcessor()

        if self.macro_expander_cache_path.is_file():
            self.macro_processor.readCache(str(self.macro_expander_cache_path))

        self.figure_text = "</br>Consider the figure above. "
コード例 #8
0
def test_caching(tmp_path):
    os.chdir(tmp_path)

    def macro(self, args, opts):
        return "PROCESSED"

    def numargs(self, args, opts):
        return str(len(args))

    def countopts(self, args, opts):
        return str(len(self.parse_options_str(opts)))

    proc = macro_expander.MacroProcessor()
    proc.addMacro('macro', macro)
    proc.addMacro('numargs', numargs)
    proc.addMacro('numopts', countopts)

    assert len(proc.cache) == 0
    proc.process(r' \macro{} ')
    assert len(proc.cache) == 0

    proc.use_cache = True

    proc.process(r' \macro{} ')
    assert len(proc.cache) == 1
    assert list(proc.cache.keys())[0] == r'\macro{}'
    proc.clearCache()
    assert len(proc.cache) == 0
    proc.process(r' \macro{} ')
    assert len(proc.cache) == 1

    proc.writeCache("tmp.cache")
    proc.clearCache()
    assert len(proc.cache) == 0

    assert pathlib.Path("tmp.cache").is_file()

    proc.readCache("tmp.cache")

    assert len(proc.cache) == 1
    assert list(proc.cache.keys())[0] == r'\macro{}'
コード例 #9
0
def test_file(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()

    with open("file1.txt", "w") as f:
        f.write("line one\n")
        f.write("line two\n")
        f.write("line three\n")
        f.write("line four\n")

    assert proc.process(
        r"\file{file1.txt}") == "line one\nline two\nline three\nline four\n"
    assert proc.process(r"\file[b=1,e=1]{file1.txt}") == "line one\n"
    assert proc.process(
        r"\file[b=2,e=3]{file1.txt}") == "line two\nline three\n"
    assert proc.process(r"\file[b=2,n=1]{file1.txt}") == "line two\n"
    assert proc.process(
        r"\file[b=2,n=3]{file1.txt}") == "line two\nline three\nline four\n"
    assert proc.process(
        r"\file[b=/two/,n=2]{file1.txt}") == "line two\nline three\n"
    assert proc.process(r"\file[b=/two/,e=/two/]{file1.txt}") == "line two\n"
    assert proc.process(r"\file[b=/two/,e=/line/]{file1.txt}") == "line two\n"
    assert proc.process(
        r"\file[b=/two/,e=/line/+1]{file1.txt}") == "line two\nline three\n"
    assert proc.process(
        r"\file[b=/two/,e=/three/]{file1.txt}") == "line two\nline three\n"
    assert proc.process(r"\file[filter=/two/]{file1.txt}") == "line two\n"
    assert proc.process(
        r"\file[filter=/(two|four)/]{file1.txt}") == "line two\nline four\n"
    assert proc.process(r"\file[transform=/two/three/]{file1.txt}"
                        ) == "line one\nline three\nline three\nline four\n"
    assert proc.process(r"\file[b=2,e=3,transform=/two/three/]{file1.txt}"
                        ) == "line three\nline three\n"
    assert proc.process(
        r"\file[transform=/two/three/,transform=/line/text/]{file1.txt}"
    ) == "text one\ntext three\ntext three\ntext four\n"
    assert proc.process(r"\file[transform=/line//]{file1.txt}"
                        ) == " one\n two\n three\n four\n"
コード例 #10
0
def test_macros(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()

    assert proc.process(r"\shell{echo ' Hello World '}") == " Hello World \n"
    assert proc.process(
        r"\shell[strip]{echo ' Hello World '}") == "Hello World"
    assert proc.process(
        r"q1.1 \shell[strip]{echo ' Hello World '}") == "q1.1 Hello World"
    proc.addMacro("shell", lambda s, a, o: "PROCESSED")
    assert proc.process(r"\shell{echo Hello World}").strip() == "PROCESSED"
    assert proc.process(
        r"\example{}").strip() == "Processed by user-defined macro."
    assert proc.process(
        r"\exampletwo{}").strip() == "Processed by user-defined macro 2."
    assert proc.process(
        r"\otherexample{}").strip() == "Processed by other user-defined macro."
    tmp = sys.modules['user_macros']
    del sys.modules['user_macros']
    assert proc.process(
        r"\example{trash}").strip() == "Processed by example handler."
    assert proc.process(
        r"\otherexample{trash}").strip() == r"\otherexample{trash}"
    sys.modules['user_macros'] = tmp
コード例 #11
0
def test_latex(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()

    assert proc.process(r"\frac{1}{2}") == r"\frac{1}{2}"
    assert proc.process(r"\frac{{1}}{{2}}") == r"\frac{{1}}{{2}}"
コード例 #12
0
def test_macro_in_middle(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()
    assert proc.process(r"Preamble| \example{} |Postamble").strip(
    ) == "Preamble| Processed by user-defined macro. |Postamble"
コード例 #13
0
def test_shell_macro(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()
    proc.process(r"\shell{ls}")
コード例 #14
0
def test_mathimg_macro(tmp_path):
    os.chdir(tmp_path)
    proc = macro_expander.MacroProcessor()
    proc.process(r"\mathimg{\nabla \rho = 0}")