"""Offline ASP.NET Razor C# logic verification lab for Chapter 22 (razor-cs-logic)."""
import json
VERSION = "CPython 3.12.14 offline ASP.NET Razor C# logic semantic verifier (IIS/ASP.NET host absent)"
def case_cs_if_single_branch():
def render_if(price):
return "<p>The price is too high.</p>" if price > 30 else ""
expected = {
"price_50": "<p>The price is too high.</p>",
"price_30": "",
"price_20": "",
}
actual = {
"price_50": render_if(50),
"price_30": render_if(30),
"price_20": render_if(20),
}
return {
"name": "cs_if_single_branch",
"summary": "验证 @if (price > 30) 单分支条件在大于、等于与小于阈值时的输出与空渲染行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cs_if_else_branch():
def render_if_else(price):
if price > 30:
return "<p>The price is too high.</p>"
return "<p>The price is OK.</p>"
expected = {
"price_20": "<p>The price is OK.</p>",
"price_30": "<p>The price is OK.</p>",
"price_45": "<p>The price is too high.</p>",
}
actual = {
"price_20": render_if_else(20),
"price_30": render_if_else(30),
"price_45": render_if_else(45),
}
return {
"name": "cs_if_else_branch",
"summary": "验证 @if ... else 二选一互斥分支对 price=20/30/45 的精确落入结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cs_else_if_range_classification():
def classify(price):
if price >= 30:
return "<p>The price is high.</p>"
elif price > 20 and price < 30:
return "<p>The price is OK.</p>"
else:
return "<p>The price is low.</p>"
expected = {
"price_30": "<p>The price is high.</p>",
"price_25": "<p>The price is OK.</p>",
"price_20": "<p>The price is low.</p>",
"price_10": "<p>The price is low.</p>",
}
actual = {
"price_30": classify(30),
"price_25": classify(25),
"price_20": classify(20),
"price_10": classify(10),
}
return {
"name": "cs_else_if_range_classification",
"summary": "验证 @if / else if (price>20 && price<30) / else 多级区间划分与边界点 20/30 判定",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cs_switch_weekday_dispatch():
def dispatch(day):
if day == "Monday":
msg = "This is the first weekday."
elif day == "Thursday":
msg = "Only one day before weekend."
elif day == "Friday":
msg = "Tomorrow is weekend!"
else:
msg = "Today is " + day
return f"<p>{msg}</p>"
expected = {
"Monday": "<p>This is the first weekday.</p>",
"Thursday": "<p>Only one day before weekend.</p>",
"Friday": "<p>Tomorrow is weekend!</p>",
"Wednesday": "<p>Today is Wednesday</p>",
}
actual = {d: dispatch(d) for d in ["Monday", "Thursday", "Friday", "Wednesday"]}
return {
"name": "cs_switch_weekday_dispatch",
"summary": "验证 @switch(day) 精确匹配各 case 标签(冒号结尾 + break 终止)及 default 兜底分支",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_cs_if_single_branch(),
case_cs_if_else_branch(),
case_cs_else_if_range_classification(),
case_cs_switch_weekday_dispatch(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。
入口文件:projects/web-pages-razor-cs-logic-lab/explore-razor-cs-logic.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-razor-cs-logic.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a3037.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3049.zip