#!/usr/bin/env python3 """Compare the authored and parser-free runners on real document operations.""" import argparse import hashlib import json from pathlib import Path import struct import subprocess import tempfile HERE = Path(__file__).resolve().parent def run(binary, store, program, *args): command = [str(binary)] if program is not None: command += ['--program', str(program)] command += ['--store', str(store), *map(str, args)] return subprocess.run(command, capture_output=True, timeout=15) def same_call(source, fixed, source_store, fixed_store, program, *args): original = run(source, source_store, program, *args) prepared = run(fixed, fixed_store, None, *args) assert (original.returncode, original.stdout, original.stderr) == ( prepared.returncode, prepared.stdout, prepared.stderr), ( args, original, prepared) return prepared def check(build, selection_build, excerpt_build): source = (build / 'pure-edit').resolve() fixed = (build / 'pure-edit-fixed').resolve() selected = (selection_build / 'pure-edit-fixed').resolve() excerpt_fixed = (excerpt_build / 'pure-edit-fixed').resolve() compiler = (build / 'pure-compile').resolve() with tempfile.TemporaryDirectory(prefix='pure-fixed-program-') as name: root = Path(name) source_store, fixed_store = root / 'source', root / 'fixed' editor = HERE / 'examples/editor.pure' small = root / 'small' small.write_bytes(b'alpha') first = same_call(source, fixed, source_store, fixed_store, editor, 'edit', 'note', '0', '0', '0', small) assert first.returncode == 0 assert same_call(source, fixed, source_store, fixed_store, editor, 'show', 'note').stdout == b'alpha' small.write_bytes(b'XY') assert same_call(source, fixed, source_store, fixed_store, editor, 'edit', 'note', '1', '2', '2', small).returncode == 0 assert same_call(source, fixed, source_store, fixed_store, editor, 'edit', 'note', '1', '0', '0', small).returncode == 3 assert same_call(source, fixed, source_store, fixed_store, editor, 'edit', 'note', '2', '500', '0', small).returncode != 0 assert same_call(source, fixed, source_store, fixed_store, editor, 'show', 'note').stdout == b'alXYa' one_mib = root / 'one-mib' one_mib.write_bytes(b'A' * 1_048_576) assert same_call(source, fixed, source_store, fixed_store, editor, 'edit', 'large', '0', '0', '0', one_mib).returncode == 0 small.write_bytes(b'Z') assert same_call(source, fixed, source_store, fixed_store, editor, 'edit', 'large', '1', '1048575', '1', small).returncode == 0 assert same_call(source, fixed, source_store, fixed_store, editor, 'show', 'large').stdout == b'A' * 1_048_575 + b'Z' selection = HERE / 'examples/selection.pure' input_file = root / 'selection-input' input_file.write_bytes(struct.pack('