蓝色天空网 · mbsky.com 全部实例

命令:每次执行的快照与撤销分支

把请求交给调用者管理,并为真实执行保存独立历史。

本页展示最近一次实际本地Python命名测试结果,不是在线Python解释器。外部服务、真实硬件及完整来源语义仍分别验收。

Python 3.12.14 · 24 项实际检查 · 当前入口SHA256 7e8cfbdaa958c5c5667f4e06afcaa2e2882dca7b07622ed9515497db21c52c81

检查项实际结果预期结果
undo on already-on restores true not falsetruetrue
reused command captures separate executions7070
undo second step6060
undo first step5050
redo actual stored result6060
new action clears redo branchfalsefalse
failed command"ValueError""ValueError"
failed command no state mutation{"power": true, "volume": 60}{"power": true, "volume": 60}
failed command no history22
can_execute explicit contractfalsefalse
guard enforced"RuntimeError""RuntimeError"
macro after{"power": false, "volume": 80}{"power": false, "volume": 80}
macro undo prior state{"power": true, "volume": 60}{"power": true, "volume": 60}
macro intermediate failure"ValueError""ValueError"
model macro failure leaves receiver{"power": true, "volume": 60}{"power": true, "volume": 60}
outside change detected by value"RuntimeError""RuntimeError"
delete0is no-op not empty"蓝色天空网""蓝色天空网"
delete actual suffix"蓝色天空""蓝色天空"
undo restores deleted text"蓝色天空网""蓝色天空网"
invalid deletion preserves text"ValueError""ValueError"
after failed deletion"蓝色天空网""蓝色天空网"
enqueue no execution"蓝色天空网""蓝色天空网"
FIFO actual deferred execution"蓝色天空网 · mbsky.com""蓝色天空网 · mbsky.com"
queue exhausted00

本章源码与生成文件

下载独立入口 共享实现 补充实例实现 本章结果JSON

查看当前核心代码
from pathlib import Path
import json,sys
from pattern_cases import *
from pattern_extensions import *

def command(c,out):
    receiver=ModelReceiver({"power":True,"volume":50});history=CommandHistory(receiver)
    power_on=ActionCommand(receiver,lambda state:dict(state,power=True),"on")
    history.execute(power_on);history.undo();c.eq("undo on already-on restores true not false",receiver.state["power"],True)
    volume_up=ActionCommand(receiver,lambda state:dict(state,volume=min(100,state["volume"]+10)),"up")
    history.execute(volume_up);history.execute(volume_up);c.eq("reused command captures separate executions",receiver.state["volume"],70)
    history.undo();c.eq("undo second step",receiver.state["volume"],60);history.undo();c.eq("undo first step",receiver.state["volume"],50)
    history.redo();c.eq("redo actual stored result",receiver.state["volume"],60)
    history.execute(power_on);c.eq("new action clears redo branch",history.redo(),False)
    before=deepcopy(receiver.state);count=len(history.done)
    def fail(state):state["volume"]=99;raise ValueError("controlled command failure")
    c.raises("failed command",ValueError,lambda:history.execute(ActionCommand(receiver,fail,"bad")))
    c.eq("failed command no state mutation",receiver.state,before);c.eq("failed command no history",len(history.done),count)
    guarded=ActionCommand(receiver,lambda state:state,"guarded",guard=lambda state:False)
    c.eq("can_execute explicit contract",guarded.can_execute(),False);c.raises("guard enforced",RuntimeError,lambda:history.execute(guarded))
    def macro(*actions):
        def perform(state):
            for action in actions:state=action(state)
            return state
        return perform
    movie=ActionCommand(receiver,macro(lambda state:dict(state,power=False),lambda state:dict(state,volume=80)),"macro")
    before=deepcopy(receiver.state);history.execute(movie);c.eq("macro after",receiver.state,{"power":False,"volume":80});history.undo();c.eq("macro undo prior state",receiver.state,before)
    c.raises("macro intermediate failure",ValueError,lambda:history.execute(ActionCommand(receiver,macro(lambda state:dict(state,volume=1),fail),"bad macro")))
    c.eq("model macro failure leaves receiver",receiver.state,before)
    receiver.state["volume"]=22;c.raises("outside change detected by value",RuntimeError,history.undo)
    doc=ModelReceiver({"content":""});edits=CommandHistory(doc)
    def insert(text):return ActionCommand(doc,lambda state:{"content":state["content"]+text},"insert")
    def delete(length):
        def operation(state):
            if type(length)is not int or length<0 or length>len(state["content"]):raise ValueError("valid deletion length")
            return {"content":state["content"]if length==0 else state["content"][:-length]}
        return ActionCommand(doc,operation,"delete")
    edits.execute(insert("蓝色天空"));edits.execute(insert("网"));edits.execute(delete(0));c.eq("delete0is no-op not empty",doc.state["content"],"蓝色天空网")
    edits.execute(delete(1));c.eq("delete actual suffix",doc.state["content"],"蓝色天空");edits.undo();c.eq("undo restores deleted text",doc.state["content"],"蓝色天空网")
    c.raises("invalid deletion preserves text",ValueError,lambda:edits.execute(delete(99)));c.eq("after failed deletion",doc.state["content"],"蓝色天空网")
    from collections import deque
    queue=deque([insert(" · "),insert("mbsky.com")]);before=doc.state["content"];c.eq("enqueue no execution",before,"蓝色天空网")
    while queue:edits.execute(queue.popleft())
    c.eq("FIFO actual deferred execution",doc.state["content"],"蓝色天空网 · mbsky.com")
    c.eq("queue exhausted",len(queue),0)



def run_case(out):
    c=Checks()
    command(c,out)
    return {"chapter":'python-command',"checks":c.rows,"passed":True,"scope":"named owned all24chapter named cases only; full source coverage pending"}

if __name__ == "__main__":
    output=Path(sys.argv[1]);output.mkdir(parents=True,exist_ok=False)
    print(json.dumps(run_case(output),ensure_ascii=False,default=repr))

上图是本站运行生成文件,不能当作来源原图已采集。