Commit 7aa81606 authored by Eckhart Arnold's avatar Eckhart Arnold

- syntaxtree.py: general json serialization

parent 6c6d63ce
This diff is collapsed.
This diff is collapsed.
......@@ -40,7 +40,7 @@ from typing import Dict, List, Union, cast
from DHParser.error import Error, is_error, adjust_error_locations
from DHParser.log import log_dir, logging, is_logging, clear_logs, log_parsing_history
from DHParser.parse import UnknownParserError, Parser, Lookahead
from DHParser.syntaxtree import Node, RootNode, parse_tree, flatten_sxpr, serialize, ZOMBIE_TAG
from DHParser.syntaxtree import Node, RootNode, parse_tree, flatten_sxpr, ZOMBIE_TAG
from DHParser.toolkit import GLOBALS, get_config_value, load_if_file, re
......@@ -269,10 +269,10 @@ def get_report(test_unit):
cst = tests.get('__cst__', {}).get(test_name, None)
if cst and (not ast or str(test_name).endswith('*')):
report.append('\n### CST')
report.append(indent(serialize(cst, 'cst')))
report.append(indent(cst.serialize_as('cst')))
if ast:
report.append('\n### AST')
report.append(indent(serialize(ast, 'ast')))
report.append(indent(ast.serialize_as('ast')))
for test_name, test_code in tests.get('fail', dict()).items():
heading = 'Fail-test "%s"' % test_name
report.append('\n%s\n%s\n' % (heading, '-' * len(heading)))
......@@ -434,7 +434,7 @@ def grammar_unit(test_unit, parser_factory, transformer_factory, report=True, ve
if compare:
if not compare.equals(cst):
errata.append('Concrete syntax tree test "%s" for parser "%s" failed:\n%s' %
(test_name, parser_name, serialize(cst, 'cst')))
(test_name, parser_name, cst.serialize_as('cst')))
if verbose:
infostr = ' cst-test "' + test_name + '" ... '
write(infostr + ("OK" if len(errata) == errflag else "FAIL"))
......
......@@ -55,6 +55,13 @@ def fail_on_error(src, result):
sys.exit(1)
def tree_size(tree) -> int:
"""
Recursively counts the number of nodes in the tree including the root node.
"""
return sum(tree_size(child) for child in tree.children) + 1
def tst_func():
with DHParser.log.logging(LOGGING):
files = os.listdir('testdata')
......@@ -66,7 +73,7 @@ def tst_func():
print('\n\nParsing document: "%s"' % file)
result = parser(doc)
print("Number of CST-nodes: " + str(result.tree_size()))
print("Number of CST-nodes: " + str(tree_size(result)))
# print("Number of empty nodes: " + str(count_nodes(result,
# lambda n: not bool(n.result))))
if DHParser.log.is_logging():
......@@ -80,7 +87,7 @@ def tst_func():
fail_on_error(doc, result)
transformer(result)
fail_on_error(doc, result)
print("Number of AST-nodes: " + str(result.tree_size()))
print("Number of AST-nodes: " + str(tree_size(result)))
if DHParser.log.is_logging():
print('Saving AST')
with open('LOGS/' + file[:-4] + '.ast', 'w', encoding='utf-8') as f:
......
......@@ -25,11 +25,12 @@ import json
import os
from multiprocessing import Process
import sys
import time
from typing import Tuple
sys.path.extend(['../', './'])
from DHParser.server import Server, STOP_SERVER_REQUEST, SERVER_OFFLINE
from DHParser.server import Server, STOP_SERVER_REQUEST, SERVER_OFFLINE, asyncio_run
scriptdir = os.path.dirname(os.path.realpath(__file__))
......@@ -44,12 +45,16 @@ class TestServer:
# cs = Server(compiler_dummy)
# cs.run_server()
def compiler_dummy(self, src: str, log_dir: str = '') -> str:
def compiler_dummy(self, src: str) -> str:
return src
def long_running(self, duration: str) -> str:
time.sleep(float(duration))
return(duration)
def test_server_proces(self):
"""Basic Test of server module."""
async def compile(src):
async def compile_remote(src):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(src.encode())
data = await reader.read(500)
......@@ -58,12 +63,13 @@ class TestServer:
cs = Server(self.compiler_dummy, cpu_bound=set())
try:
cs.spawn_server('127.0.0.1', 8888)
asyncio.run(compile('Test'))
asyncio_run(compile_remote('Test'))
finally:
cs.terminate_server_process()
cs.wait_for_termination()
cs.terminate_server()
def test_terminate(self):
"""Test different ways of sending a termination message to server:
http-request, plain-text and json-rpc."""
async def terminate_server(termination_request, expected_response):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(termination_request)
......@@ -75,26 +81,73 @@ class TestServer:
cs = Server(self.compiler_dummy, cpu_bound=set())
try:
cs.spawn_server('127.0.0.1', 8888)
asyncio.run(terminate_server(STOP_SERVER_REQUEST,
asyncio_run(terminate_server(STOP_SERVER_REQUEST,
b'DHParser server at 127.0.0.1:8888 stopped!'))
cs.wait_for_termination()
assert cs.stage.value == SERVER_OFFLINE
cs.spawn_server('127.0.0.1', 8888)
asyncio.run(terminate_server(b'GET ' + STOP_SERVER_REQUEST + b' HTTP',
asyncio_run(terminate_server(b'GET ' + STOP_SERVER_REQUEST + b' HTTP',
b'DHParser server at 127.0.0.1:8888 stopped!'))
cs.wait_for_termination()
assert cs.stage.value == SERVER_OFFLINE
cs.spawn_server('127.0.0.1', 8888)
jsonrpc = json.dumps({"jsonrpc": "2.0", "method": STOP_SERVER_REQUEST.decode()})
asyncio.run(terminate_server(jsonrpc.encode(),
asyncio_run(terminate_server(jsonrpc.encode(),
b'DHParser server at 127.0.0.1:8888 stopped!'))
cs.wait_for_termination()
assert cs.stage.value == SERVER_OFFLINE
finally:
cs.terminate_server_process()
cs.wait_for_termination()
cs.terminate_server()
def test_long_running_task(self):
"""Test, whether delegation of (long-running) tasks to
processes or threads works."""
sequence = []
async def call_remote(argument):
sequence.append(argument)
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(argument.encode())
sequence.append((await reader.read(500)).decode())
writer.close()
async def run_tasks():
await asyncio.gather(call_remote('0.01'),
call_remote('0.001'))
cs = Server(self.long_running,
cpu_bound=frozenset(['long_running']),
blocking=frozenset())
try:
cs.spawn_server('127.0.0.1', 8888)
asyncio_run(run_tasks())
assert sequence == ['0.01', '0.001', '0.001', '0.01']
finally:
cs.terminate_server()
cs = Server(self.long_running,
cpu_bound=frozenset(),
blocking=frozenset(['long_running']))
try:
sequence = []
cs.spawn_server('127.0.0.1', 8888)
asyncio_run(run_tasks())
assert sequence == ['0.01', '0.001', '0.001', '0.01']
finally:
cs.terminate_server()
cs = Server(self.long_running,
cpu_bound=frozenset(),
blocking=frozenset())
try:
sequence = []
cs.spawn_server('127.0.0.1', 8888)
asyncio_run(run_tasks())
assert sequence == ['0.01', '0.001', '0.01', '0.001']
finally:
cs.terminate_server()
if __name__ == "__main__":
......
......@@ -74,13 +74,14 @@ class TestParseXML:
class TestParseJSON:
def setup(self):
self.tree = parse_sxpr('(a (b ä) (d (e ö) (h ü)))')
self.tree = parse_sxpr('(a (b ä) (d (e ö) (h über)))').with_pos(0)
d = self.tree.pick('d')
d.attr['name'] = "James Bond"
d.attr['id'] = '007'
def test_json_obj_roundtrip(self):
json_obj_tree = self.tree.to_json_obj()
# print(json.dumps(json_obj_tree, ensure_ascii=False, indent=2))
tree_copy = Node.from_json_obj(json_obj_tree)
assert tree_copy.equals(self.tree)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment