diff --git a/doxec/__init__.py b/doxec/__init__.py index 7ef09e5bc9cf945559b1f04fb8ab0f4945934fc5..60b09d4836ff2cf63260fa3a2d33cb4f01b0bde9 100644 --- a/doxec/__init__.py +++ b/doxec/__init__.py @@ -271,16 +271,33 @@ class Document: operations within the object are executed. """ - def __init__(self, document_path): + def __init__(self, document_path, syntax=Markdown): """ Creates a new document object and parses the given document. """ self.operations = [] - pass + with open(document_path) as f: + self.parse(f.read(), syntax) - def parse(self, string): + def parse(self, string, syntax): """ Parses the content string and appends all operations defined in the string to the internal storage. """ - pass + lines = re.split("\r?\n", string) + while len(lines) > 0: + op_tuple = syntax.parse(lines) + if op_tuple is None: + continue + op_obj = Operation.factory(*op_tuple) + if op_obj is None: + continue + self.operations.append(op_obj) + + + def run(self): + """ + Runs all operations stored attached to this object. + """ + for op in self.operations: + op.execute() diff --git a/doxec/tests/__init__.py b/doxec/tests/__init__.py index 4d4992fc96b31c2684b3f95beb13afdf7ca442b3..a640a6b415bf80ac7df7a37027635b8699b3814b 100644 --- a/doxec/tests/__init__.py +++ b/doxec/tests/__init__.py @@ -5,3 +5,4 @@ from .operation import OpWriteTestCase from .operation import OpAppendTestCase from .operation import OpConsoleTestCase from .operation import OpConsoleOutputTestCase +from .document import DocumentTestCase diff --git a/doxec/tests/document.py b/doxec/tests/document.py new file mode 100644 index 0000000000000000000000000000000000000000..e0ac317e2be352e0d8d7175a168952d778ff9d3c --- /dev/null +++ b/doxec/tests/document.py @@ -0,0 +1,81 @@ + +import os +import unittest +from tempfile import NamedTemporaryFile + +from doxec import Document, OpWrite, OpConsoleOutput + + +toy_doc = """ +# Installation + +In order to run the code examples in this repository you need `python3` and +the +python packages `numpy`, `scipy` and `matplotlib`. On ubuntu 16.04 you can +setup +you system by running the following two commands as super-user (prefix +`sudo`). + +```bash +apt-get update +apt-get install python3 python3-numpy python3-scipy python3-matplotlib +``` + +# First Steps +As a first example we can compute the square root of 2. Create a +file named `example.py` with the following content. + +<!-- write example.py --> +```python +import math + +print("Example 1:") +print("Square root of 2 = %g" % math.sqrt(2)) +``` + +The file can be run with the `python3` interpreter. + +<!-- console_output --> +```bash +$ python3 example.py +Example 1: +Square root of 2 = 1.41421 +``` +""" + +class DocumentTestCase(unittest.TestCase): + """ + This class tests the methods provided by the document class. + """ + + def test_parse(self): + """ + Test whether the parse() correctly parses all operations in the toy + file. + """ + tmp = NamedTemporaryFile(delete=False) + tmp.write(toy_doc.encode('utf8')) + tmp.close() + + doc = Document(tmp.name) + + self.assertEqual(len(doc.operations), 2) + self.assertIsInstance(doc.operations[0], OpWrite) + self.assertIsInstance(doc.operations[1], OpConsoleOutput) + + os.remove(tmp.name) + + def test_run(self): + """ + Check that run() does no raise an exception on the toy file. + """ + tmp = NamedTemporaryFile(delete=False) + tmp.write(toy_doc.encode('utf8')) + tmp.close() + + doc = Document(tmp.name) + + doc.run() + + os.remove(tmp.name) +