"""Offline ASP.NET Razor VB loops & arrays verification lab for Chapter 24 (razor-vb-loops)."""
import html
import json
VERSION = "CPython 3.12.14 offline ASP.NET Razor VB loops semantic verifier (IIS/ASP.NET host absent)"
def case_vb_for_to_inclusive_loop():
# VB "For i = 10 To 21 ... Next i" is inclusive on both bounds (12 iterations)
items = [f"<p>Line #{i}</p>" for i in range(10, 21 + 1)]
expected = {
"count": 12,
"first": "<p>Line #10</p>",
"last": "<p>Line #21</p>",
"inclusive_upper_bound": True,
}
actual = {
"count": len(items),
"first": items[0],
"last": items[-1],
"inclusive_upper_bound": items[-1] == "<p>Line #21</p>",
}
return {
"name": "vb_for_to_inclusive_loop",
"summary": "验证 VB @For i = 10 To 21 闭区间循环共执行 12 次(含 21),与 C# i < 21 的 11 次形成对比",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vb_foreach_server_variables():
server_vars = ["HTTP_HOST", "REQUEST_METHOD", "SERVER_PORT", "URL"]
rendered = "<ul>" + "".join(f"<li>{html.escape(k)}</li>" for k in server_vars) + "</ul>"
expected = {
"item_count": 4,
"rendered": "<ul><li>HTTP_HOST</li><li>REQUEST_METHOD</li><li>SERVER_PORT</li><li>URL</li></ul>",
}
actual = {
"item_count": len(server_vars),
"rendered": rendered,
}
return {
"name": "vb_foreach_server_variables",
"summary": "模拟 VB @For Each x In Request.ServerVariables ... Next x 遍历并渲染无序列表",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vb_do_while_loop_render():
i = 0
lines = []
while i < 5:
i += 1
lines.append(f"<p>Line #{i}</p>")
expected = {
"final_i": 5,
"lines": ["<p>Line #1</p>", "<p>Line #2</p>", "<p>Line #3</p>", "<p>Line #4</p>", "<p>Line #5</p>"],
}
actual = {
"final_i": i,
"lines": lines,
}
return {
"name": "vb_do_while_loop_render",
"summary": "验证 VB Do While i < 5 ... Loop 配合 @<p> 内联标记逐次递增输出 1..5",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_vb_array_parenthesis_indexing():
members = ["Jani", "Hege", "Kai", "Jim"]
pos_kai = members.index("Kai") + 1
length = len(members)
second_person = members[2 - 1] # VB members(2-1)
expected = {
"length": 4,
"person_at_position_2": "Hege",
"kai_1based_position": 3,
"paragraphs": ["<p>Jani</p>", "<p>Hege</p>", "<p>Kai</p>", "<p>Jim</p>"],
}
actual = {
"length": length,
"person_at_position_2": second_person,
"kai_1based_position": pos_kai,
"paragraphs": [f"<p>{p}</p>" for p in members],
}
return {
"name": "vb_array_parenthesis_indexing",
"summary": "验证 VB String() 数组声明、圆括号下标 members(2-1)、Length 属性与 Array.IndexOf 定位",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_vb_for_to_inclusive_loop(),
case_vb_foreach_server_variables(),
case_vb_do_while_loop_render(),
case_vb_array_parenthesis_indexing(),
]
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-forms-razor-vb-loops-lab/explore-razor-vb-loops.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-razor-vb-loops.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2981.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2999.zip