蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行同一组测试,观察筛选修订和错误分支

PYTHONWindows,CPython 3.12.14,标准库 unittest。vibe_overview_lab.py
vibe_overview_lab.py82 行
"""Exercise a small catalogue feature from an explicit acceptance contract."""
from copy import deepcopy
import json
import sys
import unittest


def summary(payload, *, include_completed):
    if not isinstance(payload, dict) or not isinstance(payload.get("tasks"), list):
        raise ValueError("required_list")
    keys = set()
    visible = []
    for record in payload["tasks"]:
        if not isinstance(record, dict) or not isinstance(record.get("key"), str) or type(record.get("done")) is not bool:
            raise ValueError("wrong_record")
        key = record["key"]
        if key in keys:
            raise ValueError("duplicate_key")
        keys.add(key)
        if include_completed or not record["done"]:
            visible.append(key)
    return {"visible": visible, "count": len(visible)}


TASKS = {"tasks": [{"key":"paint", "done":False}, {"key":"label", "done":True}]}


class Acceptance(unittest.TestCase):
    def test_open(self):
        self.assertEqual(self.implementation(TASKS,include_completed=False),{"visible":["paint"],"count":1})

    def test_all(self):
        self.assertEqual(self.implementation(TASKS,include_completed=True),{"visible":["paint","label"],"count":2})

    def test_empty(self):
        self.assertEqual(self.implementation({"tasks":[]},include_completed=False),{"visible":[],"count":0})


def run_suite(implementation):
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(Acceptance)
    for test in suite:
        test.implementation = implementation
    result = unittest.TestResult()
    suite.run(result)
    assert not result.errors
    return result.testsRun, len(result.failures)


def main():
    cases = []

    def check(name, inputs, operation, expected, actual):
        assert actual == expected, (name, expected, actual)
        cases.append(dict(name="vibe_"+name,input=inputs,operation=operation,expected=expected,actual=actual))

    def earlier_draft(payload, *, include_completed):
        return summary(payload,include_completed=True)

    before, failed = run_suite(earlier_draft)
    after, remaining = run_suite(summary)
    assert before == after and remaining == 0
    check("flow","same three owned tests", "Run the draft, repair one filter branch, and rerun the same suite",
          ["需求","草案","测试","修正","复查",1,3],["需求","草案","测试","修正","复查",failed,after])
    sample = deepcopy(TASKS)
    output = summary(sample,include_completed=True)
    check("scope",{"tasks":TASKS,"include_completed":True},"Observe output contract and preservation of input",
          [["visible","count"],True],[list(output),sample == TASKS])
    bad = [{},{"tasks":[1]},{"tasks":[{"key":"a","done":False},{"key":"a","done":True}]}]
    errors = []
    for fixture in bad:
        try:
            summary(fixture,include_completed=False)
        except ValueError as error:
            errors.append(str(error))
    check("errors",bad,"Call three invalid inputs and record error types",
          ["required_list","wrong_record","duplicate_key"],errors)
    print(json.dumps(dict(version={"python":sys.version.split()[0]},cases=cases),ensure_ascii=False))


if __name__ == "__main__":
    main()
本例需在 Windows,CPython 3.12.14,标准库 unittest。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows,CPython 3.12.14,标准库 unittest。

入口文件:projects/vibe-coding-overview/vibe_overview_lab.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 vibe_overview_lab.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows,CPython 3.12.14,标准库 unittest。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Vibe Coding

课程关联工程包(登记关联21章) · 含当前章节或实例 · mbskycom202612a385.zip

下载课程关联工程包:Vibe Coding

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a397.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习