1#************************************************************** 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20#************************************************************** 21 22# usage: roundtrip.py <port> <file> <checks.json> [--capture <out.json>] 23# checks.json: [{"name": ..., "expr": "<python expression over doc>", "expect": <value>}] 24# With --capture, evaluates each expr and writes {"name": value} instead of comparing. 25import sys, json, uno 26from unohelp import connect, desktop, load 27 28 29def plain(v): 30 if isinstance(v, (bool, int, float, str)) or v is None: 31 return v 32 if isinstance(v, (tuple, list)): 33 return [plain(x) for x in v] 34 if isinstance(v, uno.Enum): 35 return v.value 36 if hasattr(v, "typeName"): 37 return {k: plain(getattr(v, k)) for k in dir(v) if not k.startswith("_") and k not in ("typeName", "value")} 38 return repr(v) 39 40 41port, path, checks = int(sys.argv[1]), sys.argv[2], json.load(open(sys.argv[3])) 42doc = load(desktop(connect(port)), path) 43got = {} 44for c in checks: 45 try: 46 got[c["name"]] = plain(eval(c["expr"], {"doc": doc, "uno": uno})) 47 except Exception as e: 48 got[c["name"]] = "ERROR " + repr(e) 49doc.close(True) 50if "--capture" in sys.argv: 51 json.dump(got, open(sys.argv[sys.argv.index("--capture") + 1], "w"), indent=1) 52 print(json.dumps(got, indent=1)); sys.exit(0) 53fails = [(c["name"], c["expect"], got[c["name"]]) for c in checks if got[c["name"]] != c["expect"]] 54for n, e, g in fails: 55 print(f"MISMATCH {n}: expected {e!r} got {g!r}") 56print(f"roundtrip: {len(checks) - len(fails)}/{len(checks)} match") 57sys.exit(1 if fails else 0) 58