# -*- coding: utf-8 -*- """545-1 측점 표기 거울 — D04 `station_label` · `common_util_station.py`(py) ↔ `common_util_station.ts`. 566-1 넓힘: 공용 파이썬 짝 `station_label(…, decimals 0 · 1)` · `infer_station_interval` 도 TS 와 같음.""" import json import subprocess import sys from pathlib import Path PROJECT_ROOT = Path(__file__).resolve().parents[2] if str(PROJECT_ROOT) not in sys.path: sys.path.insert(0, str(PROJECT_ROOT)) from common_util.common_util_station import infer_station_interval # noqa: E402 from common_util.common_util_station import station_label as common_label # noqa: E402 from D04_PlanConfirm.D04_PlanConfirm_Export import station_label # noqa: E402 TS = PROJECT_ROOT / "common_util" / "common_util_station.ts" TYPESCRIPT = PROJECT_ROOT / "config" / "node_modules" / "typescript" VALUES = [ 0, 19.96, 20, 39.99, 100, 1234.5, 1234.56, 1234.44, 1234.05, 1234.95, 61 * 20 + 14.5, 812.345, ] SCRIPT = r""" const ts = require(process.argv[1]); const src = require("fs").readFileSync(process.argv[2], "utf8"); const js = ts.transpileModule(src, { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText; const box = { exports: {} }; new Function("exports", "module", js)(box.exports, box); process.stdout.write(JSON.stringify(JSON.parse(process.argv[3]).map((v) => box.exports.stationLabel(v, 20, 1)))); """ COMMON = r""" const ts = require(process.argv[1]); const src = require("fs").readFileSync(process.argv[2], "utf8"); const js = ts.transpileModule(src, { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText; const box = { exports: {} }; new Function("exports", "module", js)(box.exports, box); const m = box.exports; const { values, intervals, lists } = JSON.parse(process.argv[3]); process.stdout.write(JSON.stringify({ labels: intervals.flatMap((i) => values.flatMap((v) => [m.stationLabel(v, i, 0), m.stationLabel(v, i, 1)])), inferred: lists.map((l) => m.inferStationInterval(l.map((c) => ({ chainage_m: c })))), })); """ INTERVALS = [20, 10, 25.5, 0] LISTS = [ [], [0], [0, 20, 40, 47.3, 60, 80], [0, 10, 20, 30, 50, 70], [0, 5, 5, 3, 13], [0, 19.96, 40], ] def _node(script: str, payload) -> str: proc = subprocess.run( ["node", "-e", script, str(TYPESCRIPT), str(TS), json.dumps(payload)], capture_output=True, text=True, encoding="utf-8", timeout=60, ) assert proc.returncode == 0, proc.stderr return proc.stdout def test_파이썬과_TS_측점_표기_같음(): assert json.loads(_node(SCRIPT, VALUES)) == [station_label(v) for v in VALUES] def test_공용_파이썬_짝_TS_와_같음(): got = json.loads(_node(COMMON, {"values": VALUES, "intervals": INTERVALS, "lists": LISTS})) assert got["labels"] == [ common_label(v, i, d) for i in INTERVALS for v in VALUES for d in (0, 1) ] assert got["inferred"] == [ infer_station_interval([{"chainage_m": c} for c in lst]) for lst in LISTS ] assert common_label(1234.5, 20) == "61+14.5" and common_label(1234.5, 20, 0) == "61+15" assert got["inferred"][2:4] == [20, 10] # 최빈 · 10 m 셋 > 20 m 둘 def test_표기_꼴(): assert station_label(1234.5) == "61+14.5" assert station_label(0) == "0+0" assert station_label(39.99) == "2+0"