Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
add unit test for a magic command
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Nov 7, 2015
1 parent 9bbde42 commit d3a0596
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
46 changes: 46 additions & 0 deletions _unittests/ut_ipythonhelper/test_ipython_magic_command.py
@@ -0,0 +1,46 @@
"""
@brief test log(time=2s)
"""

import sys
import os
import unittest
import re


try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

from src.pyquickhelper import fLOG
from src.pyquickhelper.ipythonhelper.magic_class_example import MagicClassExample


class TestMagicCommands (unittest.TestCase):

def test_magic_commands(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

mg = MagicClassExample()
mg.add_context({"MagicClassExample": MagicClassExample})
cmd = "MagicClassExample -f text"
fLOG("**", cmd)
res = mg.htmlhelp(cmd)
fLOG(res)
assert "@NB(example of a magic command)"


if __name__ == "__main__":
unittest.main()
16 changes: 13 additions & 3 deletions src/pyquickhelper/ipythonhelper/magic_class_example.py
Expand Up @@ -8,21 +8,31 @@
"""
from __future__ import print_function

from IPython.core.magic import Magics, magics_class
from .magic_class import MagicClassWithHelpers
from .magic_parser import MagicCommandParser
from ..helpgen import docstring2html
from IPython.core.magic import magics_class, line_magic, cell_magic


@magics_class
class MagicClassExample(Magics):
class MagicClassExample(MagicClassWithHelpers):

"""
@NB(example of a magic commands)
@NB(example of a magic command)
This class is an example of how a magic commands can be defined
with parameters as if it was a regular command in a terminal.
The class @see cl MagicClassExample defines magic
command ``htmlhelp`` and the associated parser.
Function @see fn load_ipython_extension
register the magic command through ``%load_ext pyquickhelper``.
The magic command can be unit tested with::
mg = MagicClassExample()
mg.add_context(context={"MagicClassExample": MagicClassExample})
cmd = "MagicClassExample -f text"
res = mg.htmlhelp(cmd)
assert "@NB(example of a magic command)"
@endNB
Expand Down

0 comments on commit d3a0596

Please sign in to comment.