|
| 1 | +"""Tests for `extract_helpers` and helper-aware MBPP prompt construction.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from verifiable_rl_coder.benchmarks.mbpp_train import ( |
| 6 | + build_mbpp_prompt, |
| 7 | + extract_helpers, |
| 8 | + infer_entry_point, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +# --- entry-point inference ------------------------------------------------- |
| 13 | + |
| 14 | + |
| 15 | +def test_infer_entry_point_simple() -> None: |
| 16 | + assert infer_entry_point(["assert add(1, 2) == 3"]) == "add" |
| 17 | + |
| 18 | + |
| 19 | +def test_infer_entry_point_picks_first() -> None: |
| 20 | + assert infer_entry_point(["assert max_chain([Pair(1,2)]) == 1"]) == "max_chain" |
| 21 | + |
| 22 | + |
| 23 | +def test_infer_entry_point_returns_none_when_no_assert() -> None: |
| 24 | + assert infer_entry_point(["# no assertion here"]) is None |
| 25 | + |
| 26 | + |
| 27 | +# --- helper extraction ----------------------------------------------------- |
| 28 | + |
| 29 | + |
| 30 | +def test_extract_helpers_returns_class_definition() -> None: |
| 31 | + canonical = ( |
| 32 | + "class Pair:\n" |
| 33 | + " def __init__(self, a, b):\n" |
| 34 | + " self.a = a\n" |
| 35 | + " self.b = b\n" |
| 36 | + "\n" |
| 37 | + "def max_chain_length(arr, n):\n" |
| 38 | + " return n\n" |
| 39 | + ) |
| 40 | + helpers = extract_helpers(canonical, "max_chain_length") |
| 41 | + assert "class Pair" in helpers |
| 42 | + assert "def max_chain_length" not in helpers |
| 43 | + assert helpers.endswith("self.b = b") |
| 44 | + |
| 45 | + |
| 46 | +def test_extract_helpers_returns_imports() -> None: |
| 47 | + canonical = ( |
| 48 | + "from heapq import nlargest\n" |
| 49 | + "import math\n" |
| 50 | + "\n" |
| 51 | + "def largest_n(arr, n):\n" |
| 52 | + " return nlargest(n, arr)\n" |
| 53 | + ) |
| 54 | + helpers = extract_helpers(canonical, "largest_n") |
| 55 | + assert "from heapq import nlargest" in helpers |
| 56 | + assert "import math" in helpers |
| 57 | + assert "def largest_n" not in helpers |
| 58 | + |
| 59 | + |
| 60 | +def test_extract_helpers_returns_empty_when_no_preamble() -> None: |
| 61 | + canonical = "def add(a, b):\n return a + b\n" |
| 62 | + assert extract_helpers(canonical, "add") == "" |
| 63 | + |
| 64 | + |
| 65 | +def test_extract_helpers_returns_empty_when_def_not_found() -> None: |
| 66 | + canonical = " return a + b\n" # body only, no def line — like HumanEval+ |
| 67 | + assert extract_helpers(canonical, "add") == "" |
| 68 | + |
| 69 | + |
| 70 | +# --- prompt construction --------------------------------------------------- |
| 71 | + |
| 72 | + |
| 73 | +def test_prompt_without_helpers_omits_supporting_section() -> None: |
| 74 | + prompt = build_mbpp_prompt("Add two numbers.", ["assert add(1, 2) == 3"]) |
| 75 | + assert "Supporting definitions" not in prompt |
| 76 | + assert "Task: Add two numbers." in prompt |
| 77 | + assert "assert add(1, 2) == 3" in prompt |
| 78 | + |
| 79 | + |
| 80 | +def test_prompt_with_helpers_includes_supporting_section() -> None: |
| 81 | + helpers = "class Pair:\n pass" |
| 82 | + prompt = build_mbpp_prompt( |
| 83 | + "Find longest chain.", |
| 84 | + ["assert max_chain([Pair(1, 2)]) == 1"], |
| 85 | + helpers=helpers, |
| 86 | + ) |
| 87 | + assert "Supporting definitions" in prompt |
| 88 | + assert "class Pair" in prompt |
| 89 | + assert "do not redefine" in prompt |
0 commit comments