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

离线校验 Razor C# for/foreach/while 循环与 string[] 数组索引语义

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。explore-razor-cs-loops.py
explore-razor-cs-loops.py115 行
"""Offline ASP.NET Razor C# loops & arrays verification lab for Chapter 21 (razor-cs-loops)."""
import html
import json

VERSION = "CPython 3.12.14 offline ASP.NET Razor C# loops semantic verifier (IIS/ASP.NET host absent)"


def case_cs_for_loop_range_render():
    items = [f"<p>Line {i}</p>" for i in range(10, 21)]
    expected = {
        "count": 11,
        "first": "<p>Line 10</p>",
        "last": "<p>Line 20</p>",
        "contains_21": False,
    }
    actual = {
        "count": len(items),
        "first": items[0],
        "last": items[-1],
        "contains_21": "<p>Line 21</p>" in items,
    }
    return {
        "name": "cs_for_loop_range_render",
        "summary": "验证 C# @for(var i = 10; i < 21; i++) 半开区间循环生成 11 行段落且不含 21",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_cs_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": "cs_foreach_server_variables",
        "summary": "模拟 @foreach (var x in Request.ServerVariables) 遍历服务器变量键集合并渲染 <ul><li> 列表",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_cs_while_counter_loop():
    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": "cs_while_counter_loop",
        "summary": "验证 @{ var i = 0; while (i < 5) { i += 1; <p>Line @i</p> } } 先自增后输出的 1..5 序列",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_cs_array_indexing_and_indexof():
    members = ["Jani", "Hege", "Kai", "Jim"]
    pos_kai = members.index("Kai") + 1
    length = len(members)
    second_person = members[2 - 1]
    not_found = members.index("Unknown") if "Unknown" in members else -1
    expected = {
        "length": 4,
        "person_at_position_2": "Hege",
        "kai_1based_position": 3,
        "not_found_index": -1,
        "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,
        "not_found_index": not_found,
        "paragraphs": [f"<p>{p}</p>" for p in members],
    }
    return {
        "name": "cs_array_indexing_and_indexof",
        "summary": "验证 string[] 数组声明、0 基索引 members[2-1]、Length 属性与 Array.IndexOf 查找语义",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_cs_for_loop_range_render(),
        case_cs_foreach_server_variables(),
        case_cs_while_counter_loop(),
        case_cs_array_indexing_and_indexof(),
    ]
    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-cs-loops-lab/explore-razor-cs-loops.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-razor-cs-loops.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Web Forms

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

下载课程关联工程包:Web Forms

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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