diff --git a/tools/push3-transpiler/inject.sh b/tools/push3-transpiler/inject.sh index de9a5c9..14cd262 100755 --- a/tools/push3-transpiler/inject.sh +++ b/tools/push3-transpiler/inject.sh @@ -38,10 +38,14 @@ body_start = push3.index("\n", brace_start) + 1 lines = push3[body_start:].split("\n") body_lines = [] +depth = 1 for line in lines: - if line.strip() == "}" and (line.startswith(" }") or line == "}"): + depth += line.count("{") - line.count("}") + if depth <= 0: break body_lines.append(line) +else: + sys.exit("error: calculateParams body: closing brace never found (EOF)") body = "\n".join(body_lines) diff --git a/tools/push3-transpiler/package.json b/tools/push3-transpiler/package.json index 9fd7f02..a229184 100644 --- a/tools/push3-transpiler/package.json +++ b/tools/push3-transpiler/package.json @@ -5,7 +5,8 @@ "main": "dist/index.js", "scripts": { "build": "tsc", - "transpile": "ts-node src/index.ts" + "transpile": "ts-node src/index.ts", + "test": "bash test_inject_extraction.sh" }, "dependencies": {}, "devDependencies": { diff --git a/tools/push3-transpiler/test_inject_extraction.sh b/tools/push3-transpiler/test_inject_extraction.sh new file mode 100755 index 0000000..fb826b9 --- /dev/null +++ b/tools/push3-transpiler/test_inject_extraction.sh @@ -0,0 +1,161 @@ +#!/usr/bin/env bash +# test_inject_extraction.sh — Tests the calculateParams body extraction logic in inject.sh. +# Verifies that the brace-depth counter correctly handles simple bodies and bodies +# containing nested if/else blocks. +# Exit: 0 if all tests pass, 1 if any fail. +set -euo pipefail + +PASS=0 +FAIL=0 + +run_extraction() { + local sol_content="$1" + python3 - < 0) {" +assert_contains "nested: includes true branch" "$OUT" "ci = 1;" +assert_contains "nested: includes else clause" "$OUT" "} else {" +assert_contains "nested: includes false branch" "$OUT" "ci = 0;" +assert_contains "nested: includes closing if brace" "$OUT" " }" +assert_not_line "nested: excludes function closing }" "$OUT" " \}" + +# ── Test 3: Multiple levels of nesting ─────────────────────────────────────── +echo "Test 3: Multiple nesting levels" +DEEP_SOL='contract C { + function calculateParams() public pure returns (uint256 ci) { + if (a) { + if (b) { + ci = 2; + } else { + ci = 1; + } + } else { + ci = 0; + } + } +}' + +OUT=$(run_extraction "$DEEP_SOL") +assert_contains "deep: outer if" "$OUT" "if (a) {" +assert_contains "deep: inner if" "$OUT" "if (b) {" +assert_contains "deep: inner value" "$OUT" "ci = 2;" +assert_contains "deep: outer else" "$OUT" "ci = 0;" + +# ── Test 4: EOF without closing brace triggers non-zero exit ───────────────── +echo "Test 4: EOF without closing brace" +BAD_SOL='contract C { + function calculateParams() public pure returns (uint256 ci) { + uint256 x = 1;' + +if python3 - </dev/null; then +import sys +push3 = """$BAD_SOL""" +fn_start = push3.find("function calculateParams") +brace_start = push3.find("{", fn_start) +body_start = push3.index("\n", brace_start) + 1 +lines = push3[body_start:].split("\n") +body_lines = [] +depth = 1 +for line in lines: + depth += line.count("{") - line.count("}") + if depth <= 0: + break + body_lines.append(line) +else: + sys.exit("error: calculateParams body: closing brace never found (EOF)") +PYEOF + echo " FAIL: EOF case should have exited non-zero" + FAIL=$((FAIL + 1)) +else + echo " PASS: EOF case exits non-zero" + PASS=$((PASS + 1)) +fi + +# ── Summary ────────────────────────────────────────────────────────────────── +echo "" +echo "Results: $PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ]