"""Offline Classic ASP TextStream object verification lab (IIS ASP absent)."""
import json
from pathlib import Path
import tempfile
class MockTextStream:
"""Offline model of Scripting.TextStream tracking Line, Column, AtEndOfLine, AtEndOfStream."""
def __init__(self, path: Path, mode: str) -> None:
self._path = path
self._mode = mode
self._closed = False
self._line = 1
self._col = 1
if mode == "w":
self._buf: list[str] = []
self._data = ""
self._pos = 0
elif mode == "r":
self._buf = []
self._data = path.read_bytes().decode("utf-8")
self._pos = 0
else:
raise ValueError(mode)
def _ensure_open(self) -> None:
if self._closed:
raise ValueError("TextStream is closed")
@property
def Line(self) -> int:
self._ensure_open()
return self._line
@property
def Column(self) -> int:
self._ensure_open()
return self._col
@property
def AtEndOfStream(self) -> bool:
self._ensure_open()
return self._pos >= len(self._data)
@property
def AtEndOfLine(self) -> bool:
self._ensure_open()
if self._pos >= len(self._data):
return True
return self._data[self._pos] in ("\r", "\n")
def Write(self, text: str) -> None:
self._ensure_open()
self._buf.append(text)
for ch in text:
if ch == "\n":
self._line += 1
self._col = 1
else:
self._col += 1
def WriteLine(self, text: str = "") -> None:
self.Write(text + "\r\n")
def WriteBlankLines(self, count: int) -> None:
for _ in range(count):
self.Write("\r\n")
def Read(self, count: int) -> str:
self._ensure_open()
out = []
while len(out) < count and self._pos < len(self._data):
ch = self._data[self._pos]
self._pos += 1
out.append(ch)
if ch == "\n":
self._line += 1
self._col = 1
elif ch != "\r":
self._col += 1
return "".join(out)
def ReadLine(self) -> str:
self._ensure_open()
out = []
while self._pos < len(self._data):
ch = self._data[self._pos]
self._pos += 1
if ch == "\r":
if self._pos < len(self._data) and self._data[self._pos] == "\n":
self._pos += 1
self._line += 1
self._col = 1
break
if ch == "\n":
self._line += 1
self._col = 1
break
out.append(ch)
self._col += 1
return "".join(out)
def ReadAll(self) -> str:
self._ensure_open()
return self.Read(len(self._data) - self._pos)
def Skip(self, count: int) -> None:
self.Read(count)
def SkipLine(self) -> None:
self.ReadLine()
def Close(self) -> None:
if not self._closed:
if self._mode == "w":
self._path.write_bytes("".join(self._buf).encode("utf-8"))
self._closed = True
def main() -> None:
cases = []
with tempfile.TemporaryDirectory(prefix="asp_textstream_") as tmp:
p = Path(tmp) / "test.txt"
# Case 1: Write, WriteLine, WriteBlankLines, Close, ReadAll
w = MockTextStream(p, "w")
w.WriteLine("Hello World!")
w.Write("ASP ")
w.Write("TextStream")
w.WriteBlankLines(2)
w.WriteLine("Last Line")
w.Close()
r = MockTextStream(p, "r")
full = r.ReadAll()
eos_after_all = r.AtEndOfStream
r.Close()
expected1 = {
"content": "Hello World!\r\nASP TextStream\r\n\r\nLast Line\r\n",
"eos_after_readall": True,
}
actual1 = {"content": full, "eos_after_readall": eos_after_all}
cases.append({
"name": "textstream_write_and_readall",
"summary": "Verify Write, WriteLine, WriteBlankLines, Close, ReadAll, and AtEndOfStream.",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: ReadLine loop + Line property
r2 = MockTextStream(p, "r")
collected = []
while not r2.AtEndOfStream:
ln = r2.Line
collected.append((ln, r2.ReadLine()))
r2.Close()
expected2 = [
(1, "Hello World!"),
(2, "ASP TextStream"),
(3, ""),
(4, "Last Line"),
]
cases.append({
"name": "textstream_readline_and_line_counter",
"summary": "Verify ReadLine strips line endings and increments Line counter until AtEndOfStream.",
"expected": expected2,
"actual": collected,
"passed": collected == expected2,
})
# Case 3: Read(n), Skip(n), SkipLine, Column, AtEndOfLine
r3 = MockTextStream(p, "r")
head = r3.Read(5)
col_after_head = r3.Column
r3.Skip(1)
word = r3.Read(6)
at_eol = r3.AtEndOfLine
r3.SkipLine()
r3.SkipLine()
r3.SkipLine()
fourth_line = r3.ReadLine()
r3.Close()
expected3 = {
"head": "Hello",
"col_after_head": 6,
"word": "World!",
"at_eol_before_crlf": True,
"fourth_line": "Last Line",
}
actual3 = {
"head": head,
"col_after_head": col_after_head,
"word": word,
"at_eol_before_crlf": at_eol,
"fourth_line": fourth_line,
}
cases.append({
"name": "textstream_read_skip_column_eol",
"summary": "Verify Read(n), Skip(n), SkipLine, Column position, and AtEndOfLine detection.",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: Empty file EOS & closed stream guard
empty_p = Path(tmp) / "empty.txt"
empty_p.write_text("", encoding="utf-8")
re_empty = MockTextStream(empty_p, "r")
empty_eos = re_empty.AtEndOfStream
re_empty.Close()
closed_error = False
try:
re_empty.ReadLine()
except ValueError:
closed_error = True
expected4 = {"empty_file_eos": True, "closed_stream_raises": True}
actual4 = {"empty_file_eos": empty_eos, "closed_stream_raises": closed_error}
cases.append({
"name": "textstream_empty_and_closed_guard",
"summary": "Verify empty file starts at AtEndOfStream=True and reading after Close raises error.",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
print(json.dumps({
"version": "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/asp-ref-textstream-lab/explore-ref-textstream.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ref-textstream.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1449.zip
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a1425.zip