"""Acceptance test for time-to-green task T02 (written for the 2026-10-30 bench; the protocol names it, does not ship it). T02: tally: make `stats` accept --col more than once and print one block per column separated by a blank line, in the order given. Green: python -m unittest tasks.t02_test (run from the repo root) A "block" is the existing seven-line stats output for one column. Leading/trailing newlines around the whole output are not judged; the separator between blocks must be exactly one blank line. Single --col output stays as the shipped suite pins it (frozen copy in tasks/). """ from __future__ import annotations import importlib.util import io import os import tempfile import unittest from tasks.regress_tally import HeadTests, StatsTests # noqa: F401 (collected as regression tests) ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) _spec = importlib.util.spec_from_file_location( "tally_t02", os.path.join(ROOT, "build-a-cli-tool-with-an-agent-from-a-spec.py")) tally = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(tally) SAMPLE = "name,price,qty\nbolt,1.5,10\nnut,,4\nwasher,0.25,100\nscrew,2,\n" PRICE = ("column: price\ncount: 3\nblank: 1\nmin: 0.25\n" "max: 2\nmean: 1.25\nmedian: 1.5") QTY = ("column: qty\ncount: 3\nblank: 1\nmin: 4\n" "max: 100\nmean: 38\nmedian: 10") def run(argv, text=SAMPLE): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "data.csv") with open(path, "w", encoding="utf-8", newline="") as fh: fh.write(text) argv = [path if a == "FILE" else a for a in argv] out, err = io.StringIO(), io.StringIO() code = tally.main(argv, out=out, err=err) return code, out.getvalue(), err.getvalue() class RepeatedColTests(unittest.TestCase): def test_two_cols_print_two_blocks_in_the_order_given(self): code, out, err = run(["stats", "FILE", "--col", "qty", "--col", "price"]) self.assertEqual(code, 0, err) self.assertEqual(out.strip("\n").split("\n\n"), [QTY, PRICE]) def test_order_follows_the_flags_not_the_header(self): code, out, err = run(["stats", "FILE", "--col", "price", "--col", "qty"]) self.assertEqual(code, 0, err) self.assertEqual(out.strip("\n").split("\n\n"), [PRICE, QTY]) def test_single_col_output_is_unchanged(self): code, out, _ = run(["stats", "FILE", "--col", "price"]) self.assertEqual(code, 0) self.assertEqual(out, PRICE + "\n") if __name__ == "__main__": unittest.main()