migrate: own the io/console/stream/format modules and keep the official print alias target #2
No files matched your search
@@ -1,14 +1,39 @@
|
|||||||
# marqdo-std-io
|
# marqdo-std-io
|
||||||
|
|
||||||
本仓负责Console、Stream、格式化和官方 print 别名。当前状态为 `planned`。
|
本仓负责Console、Stream、格式化和官方 print 别名。当前处于**标准库源码归位迁移**阶段:源码与断言已迁入本仓,等待父仓候选组合验收(父仓工单 [marqdo#9](https://git.cflmy.cn/cflmy/marqdo/issues/9),本仓工单 [#1](https://git.cflmy.cn/cflmy/marqdo-std-io/issues/1))。
|
||||||
|
|
||||||
## 首项任务
|
## 唯一公开入口(Bundle 根相对路径)
|
||||||
|
|
||||||
迁入 Console、Stream 和格式化,负责官方 `print` 与 `打印` 别名。
|
| 发布路径 | 公开导入 | 职责 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `format.mq.md` | `std:format` | 值显示、列表显示与转义 |
|
||||||
|
| `io.mq.md` | `std:io` | 官方 `print`/`打印` 别名目标 `写值` |
|
||||||
|
| `io/console.mq.md` | `std:io/console` | stdin 读取行与 stdout 打印 |
|
||||||
|
| `io/stream.mq.md` | `std:io/stream` | 短写检测与关闭语义 |
|
||||||
|
|
||||||
|
`modules/` 保存将来 Bundle 根下的相对路径;Bundle 与 `stdlib.json` 由父仓唯一生产者生成,本仓不维护第二份官方 manifest。
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [modules/](modules/README.md) 存放对外发布的模块源码。
|
- `modules/`:对外发布的模块源码(Bundle 根相对路径)。
|
||||||
- [tests/](tests/README.md) 存放独立验收用的输入和断言。
|
- `tests/`:迁入的 `.mq.md` 自测源与 `tests/cases/` 期望(observation 终值、stdout、参数、环境)。
|
||||||
|
- `scripts/`:独立验收入口。
|
||||||
|
|
||||||
|
## 独立验收
|
||||||
|
|
||||||
|
只接受显式传入的已验证制品,不读取兄弟仓物理路径或浮动分支:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
MARQDO_BIN=/path/to/verified/marqdo \
|
||||||
|
MARQDO_STDLIB_DIR=/path/to/installed/bundle \
|
||||||
|
scripts/test.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
- `MARQDO_BIN`:真实 CLI 二进制;`MARQDO_STDLIB_DIR`:已安装且通过 manifest digest 校验、包含本仓模块的 Bundle。
|
||||||
|
- 期望值来自迁移前基线的真实执行结果;修改期望值等于声明公开行为变化,必须单独评审。
|
||||||
|
|
||||||
|
## 边界
|
||||||
|
|
||||||
|
不实现语言内核、Host 权限或资源原语;`io/console` 与 `io/stream` 只包装现行 resource 原语,stdout/stderr 不混合;`io.mq.md#写值` 保持官方 `print`/`打印` 别名目标、签名与 effects 不变,不接管别名元数据。
|
||||||
|
|
||||||
[生态迁移台账](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/MIGRATION.md) · [开发流程](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/DEVELOPMENT.md)
|
[生态迁移台账](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/MIGRATION.md) · [开发流程](https://git.cflmy.cn/cflmy/marqdo/src/branch/main/docs/DEVELOPMENT.md)
|
||||||
@@ -0,0 +1,334 @@
|
|||||||
|
|
||||||
|
# @exec format
|
||||||
|
## 字节长度
|
||||||
|
|
||||||
|
`params(文本)`
|
||||||
|
|
||||||
|
**点 = text.codepoints(文本)**
|
||||||
|
|
||||||
|
**总字节 = 0**
|
||||||
|
|
||||||
|
**下标 = 0**
|
||||||
|
|
||||||
|
`while 下标 < 长度(点)`
|
||||||
|
|
||||||
|
**码点 = 点[下标]**
|
||||||
|
|
||||||
|
`if 码点 < 128`
|
||||||
|
|
||||||
|
**总字节 = 总字节 + 1**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 码点 < 2048`
|
||||||
|
|
||||||
|
**总字节 = 总字节 + 2**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 码点 < 65536`
|
||||||
|
|
||||||
|
**总字节 = 总字节 + 3**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**总字节 = 总字节 + 4**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**下标 = 下标 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`return 总字节`
|
||||||
|
|
||||||
|
## 显示
|
||||||
|
|
||||||
|
`params(值)`
|
||||||
|
|
||||||
|
按语言值语义格式化,用于与内置 打印 的输出对照。
|
||||||
|
覆盖 Unit/None/Bool/Int/Float/Text/List/Map/Table/Formula/Code/
|
||||||
|
FunctionRef/PluginHandle/Memory/Resource。
|
||||||
|
|
||||||
|
**类别 = 类型(值)**
|
||||||
|
|
||||||
|
`if 类别 == "Unit"`
|
||||||
|
|
||||||
|
`return "Unit"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "None"`
|
||||||
|
|
||||||
|
`return "None"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Text"`
|
||||||
|
|
||||||
|
`return 值`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "List"`
|
||||||
|
|
||||||
|
`return 列表显示(值)`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Map"`
|
||||||
|
|
||||||
|
`return 映射显示(值)`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Table"`
|
||||||
|
|
||||||
|
`return 表格显示(值)`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Formula"`
|
||||||
|
|
||||||
|
`return "Formula(" + 转义文本(值.latex) + ")"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Code"`
|
||||||
|
|
||||||
|
`return "Code(" + 转义文本(值.language) + ", " + 文本(字节长度(值.source)) + " bytes)"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "FunctionRef"`
|
||||||
|
|
||||||
|
`return "FunctionRef(" + 值.module + "::" + 值.name + ")"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "PluginHandle"`
|
||||||
|
|
||||||
|
`return "PluginHandle(" + 值.plugin + "::" + 值.export + ")"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Memory"`
|
||||||
|
|
||||||
|
`return "Memory"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 类别 == "Resource"`
|
||||||
|
|
||||||
|
`return "Resource"`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return 文本(值)`
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
## 列表显示
|
||||||
|
|
||||||
|
`params(列表)`
|
||||||
|
|
||||||
|
**结果 = "["**
|
||||||
|
|
||||||
|
**下标 = 0**
|
||||||
|
|
||||||
|
`while 下标 < 长度(列表)`
|
||||||
|
|
||||||
|
`if 下标 > 0`
|
||||||
|
|
||||||
|
**结果 = 结果 + ", "**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**忽略 = 0**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**结果 = 结果 + 值显示(列表[下标])**
|
||||||
|
|
||||||
|
**下标 = 下标 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`return 结果 + "]"`
|
||||||
|
|
||||||
|
## 映射显示
|
||||||
|
|
||||||
|
`params(映射)`
|
||||||
|
|
||||||
|
**键 = map.keys(映射)**
|
||||||
|
|
||||||
|
**结果 = "{"**
|
||||||
|
|
||||||
|
**下标 = 0**
|
||||||
|
|
||||||
|
`while 下标 < 长度(键)`
|
||||||
|
|
||||||
|
`if 下标 > 0`
|
||||||
|
|
||||||
|
**结果 = 结果 + ", "**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**忽略 = 0**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**当前键 = 键[下标]**
|
||||||
|
|
||||||
|
**结果 = 结果 + 当前键 + ": " + 值显示(map.get(映射, 当前键))**
|
||||||
|
|
||||||
|
**下标 = 下标 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`return 结果 + "}"`
|
||||||
|
|
||||||
|
## 表格显示
|
||||||
|
|
||||||
|
`params(表格)`
|
||||||
|
|
||||||
|
**列 = table.columns(表格)**
|
||||||
|
|
||||||
|
**结果 = "Table[" + 连接文本(列, " | ") + "]{"**
|
||||||
|
|
||||||
|
**行号 = 0**
|
||||||
|
|
||||||
|
`while 行号 < 长度(表格)`
|
||||||
|
|
||||||
|
`if 行号 > 0`
|
||||||
|
|
||||||
|
**结果 = 结果 + "; "**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**忽略 = 0**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**行 = table.row(表格, 行号)**
|
||||||
|
|
||||||
|
**单元格 = []**
|
||||||
|
|
||||||
|
**列号 = 0**
|
||||||
|
|
||||||
|
`while 列号 < 长度(行)`
|
||||||
|
|
||||||
|
**单元格 = list.append(单元格, 显示(行[列号]))**
|
||||||
|
|
||||||
|
**列号 = 列号 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
**结果 = 结果 + 连接文本(单元格, " | ")**
|
||||||
|
|
||||||
|
**行号 = 行号 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`return 结果 + "}"`
|
||||||
|
|
||||||
|
## 连接文本
|
||||||
|
|
||||||
|
`params(列表, 分隔)`
|
||||||
|
|
||||||
|
**结果 = ""**
|
||||||
|
|
||||||
|
**下标 = 0**
|
||||||
|
|
||||||
|
`while 下标 < 长度(列表)`
|
||||||
|
|
||||||
|
`if 下标 > 0`
|
||||||
|
|
||||||
|
**结果 = 结果 + 分隔**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**忽略 = 0**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**结果 = 结果 + 列表[下标]**
|
||||||
|
|
||||||
|
**下标 = 下标 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`return 结果`
|
||||||
|
|
||||||
|
## 值显示
|
||||||
|
|
||||||
|
`params(值)`
|
||||||
|
|
||||||
|
列表/映射/表格内部使用 repr 语义:Text 带引号并转义,其它值递归显示。
|
||||||
|
|
||||||
|
**类别 = 类型(值)**
|
||||||
|
|
||||||
|
`if 类别 == "Text"`
|
||||||
|
|
||||||
|
`return 转义文本(值)`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return 显示(值)`
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
## 转义文本
|
||||||
|
|
||||||
|
`params(文本)`
|
||||||
|
|
||||||
|
**点 = text.codepoints(文本)**
|
||||||
|
|
||||||
|
**`结果 = "\""`**
|
||||||
|
|
||||||
|
**下标 = 0**
|
||||||
|
|
||||||
|
`while 下标 < 长度(点)`
|
||||||
|
|
||||||
|
**码点 = 点[下标]**
|
||||||
|
|
||||||
|
`if 码点 == 34`
|
||||||
|
|
||||||
|
**`结果 = 结果 + "\\\""`**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 码点 == 92`
|
||||||
|
|
||||||
|
**`结果 = 结果 + "\\\\"`**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 码点 == 10`
|
||||||
|
|
||||||
|
**`结果 = 结果 + "\\n"`**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 码点 == 9`
|
||||||
|
|
||||||
|
**`结果 = 结果 + "\\t"`**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 码点 == 13`
|
||||||
|
|
||||||
|
**`结果 = 结果 + "\\r"`**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**结果 = 结果 + text.from_codepoint(码点)**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**下标 = 下标 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
``return 结果 + "\""``
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
format: "./format.mq.md"
|
||||||
|
stream: "./io/stream.mq.md"
|
||||||
|
console: "./io/console.mq.md"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec io
|
||||||
|
|
||||||
|
## 写文本
|
||||||
|
|
||||||
|
`params(文本)`
|
||||||
|
|
||||||
|
`return stream.写文本(文本)`
|
||||||
|
|
||||||
|
## 写全部
|
||||||
|
|
||||||
|
`params(流, 内存, 起点, 长度)`
|
||||||
|
|
||||||
|
`return stream.写全部(流, 内存, 起点, 长度)`
|
||||||
|
|
||||||
|
## 写值
|
||||||
|
|
||||||
|
`params(值)`
|
||||||
|
|
||||||
|
按语言值语义格式化后输出,并追加换行。
|
||||||
|
|
||||||
|
`return stream.写文本(format.显示(值) + "\n")`
|
||||||
|
|
||||||
|
## 读取行
|
||||||
|
|
||||||
|
`params()`
|
||||||
|
|
||||||
|
`return console.读取行()`
|
||||||
|
|
||||||
|
## 打印
|
||||||
|
|
||||||
|
`params(值)`
|
||||||
|
|
||||||
|
`return console.打印(值)`
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
format: "../format.mq.md"
|
||||||
|
stream: "./stream.mq.md"
|
||||||
|
utf8: "../text/utf8.mq.md"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec console
|
||||||
|
|
||||||
|
## 读取行
|
||||||
|
|
||||||
|
`params()`
|
||||||
|
|
||||||
|
从已授权的 stdio/stdin 资源读取一行。按有界块读取并增量解码 UTF-8;
|
||||||
|
EOF 返回 {ok:false, code:"io.eof"},无效 UTF-8 返回可恢复错误。
|
||||||
|
|
||||||
|
**流 = resource.open("stdio", "stdin", ["read"])**
|
||||||
|
|
||||||
|
**内存 = memory.new(1, 1)**
|
||||||
|
|
||||||
|
**字节 = []**
|
||||||
|
|
||||||
|
**结束 = false**
|
||||||
|
|
||||||
|
**错误 = ""**
|
||||||
|
|
||||||
|
`while not 结束`
|
||||||
|
|
||||||
|
**读取 = resource.read(流, 内存, 0, 1)**
|
||||||
|
|
||||||
|
`if not 读取.ok`
|
||||||
|
|
||||||
|
**结束 = true**
|
||||||
|
|
||||||
|
**错误 = 读取.code**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 读取.value.eof and 读取.value.count == 0`
|
||||||
|
|
||||||
|
**结束 = true**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 内存[0] == 10`
|
||||||
|
|
||||||
|
**结束 = true**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**字节 = list.append(字节, 内存[0])**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**关闭 = resource.close(流)**
|
||||||
|
|
||||||
|
`if 错误 != ""`
|
||||||
|
|
||||||
|
`return {ok: false, code: 错误, message: "stdin read failed"}`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if not 关闭.ok`
|
||||||
|
|
||||||
|
`return 关闭`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 长度(字节) == 0`
|
||||||
|
|
||||||
|
`return {ok: false, code: "io.eof", message: "end of input"}`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**数据长度 = 长度(字节)**
|
||||||
|
|
||||||
|
`if 数据长度 > 0 and 字节[数据长度 - 1] == 13`
|
||||||
|
|
||||||
|
**数据长度 = 数据长度 - 1**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**忽略 = 0**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
**页 = (数据长度 + 65535) // 65536**
|
||||||
|
|
||||||
|
**数据 = memory.new(页, 页)**
|
||||||
|
|
||||||
|
**下标 = 0**
|
||||||
|
|
||||||
|
`while 下标 < 数据长度`
|
||||||
|
|
||||||
|
**`数据[下标] = 字节[下标]`**
|
||||||
|
|
||||||
|
**下标 = 下标 + 1**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
**解码 = utf8.解码(数据, 0, 数据长度)**
|
||||||
|
|
||||||
|
`if not 解码.ok`
|
||||||
|
|
||||||
|
`return 解码`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return {ok: true, value: 解码.value}`
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
## 打印
|
||||||
|
|
||||||
|
`params(值)`
|
||||||
|
|
||||||
|
**结果 = stream.写文本(format.显示(值) + "\n")**
|
||||||
|
|
||||||
|
`if not 结果.ok`
|
||||||
|
|
||||||
|
`return 结果`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return {ok: true, value: None}`
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
## 写文本
|
||||||
|
|
||||||
|
`params(文本)`
|
||||||
|
|
||||||
|
`return stream.写文本(文本)`
|
||||||
|
|
||||||
|
## 写全部
|
||||||
|
|
||||||
|
`params(流, 内存, 起点, 长度)`
|
||||||
|
|
||||||
|
`return stream.写全部(流, 内存, 起点, 长度)`
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
format: "../format.mq.md"
|
||||||
|
utf8: "../text/utf8.mq.md"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec stream
|
||||||
|
|
||||||
|
## 页数
|
||||||
|
|
||||||
|
`params(字节)`
|
||||||
|
|
||||||
|
**页 = (字节 + 65535) // 65536**
|
||||||
|
|
||||||
|
`if 页 == 0`
|
||||||
|
|
||||||
|
`return 1`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return 页`
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
## 写全部
|
||||||
|
|
||||||
|
`params(流, 内存, 起点, 长度)`
|
||||||
|
|
||||||
|
把 长度 字节从 内存 写到资源流。短写只返回实际数量,调用方继续;
|
||||||
|
零进度返回 io.write_zero,不静默吞掉失败。
|
||||||
|
|
||||||
|
**已写 = 0**
|
||||||
|
|
||||||
|
**失败 = false**
|
||||||
|
|
||||||
|
**本次 = {ok: true, value: {count: 0}}**
|
||||||
|
|
||||||
|
**错误结果 = {ok: false, code: "io.write_zero", message: "resource.write made no progress"}**
|
||||||
|
|
||||||
|
`while 已写 < 长度`
|
||||||
|
|
||||||
|
**剩余 = 长度 - 已写**
|
||||||
|
|
||||||
|
**本次 = resource.write(流, 内存, 起点 + 已写, 剩余)**
|
||||||
|
|
||||||
|
`if not 本次.ok`
|
||||||
|
|
||||||
|
**失败 = true**
|
||||||
|
|
||||||
|
**错误结果 = 本次**
|
||||||
|
|
||||||
|
**已写 = 长度**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 本次.value.count == 0`
|
||||||
|
|
||||||
|
**失败 = true**
|
||||||
|
|
||||||
|
**已写 = 长度**
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
**已写 = 已写 + 本次.value.count**
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`if 失败`
|
||||||
|
|
||||||
|
`return 错误结果`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if 已写 < 长度`
|
||||||
|
|
||||||
|
`return 错误结果`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return {ok: true, value: {count: 长度}}`
|
||||||
|
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
## 写文本
|
||||||
|
|
||||||
|
`params(文本)`
|
||||||
|
|
||||||
|
把文本编码为 UTF-8 并写入已授权的 stdout 资源流。内存按实际字节长度增长,
|
||||||
|
写入循环检查 {ok,count};只有全部字节写完并关闭成功才返回成功。
|
||||||
|
|
||||||
|
**字节数 = format.字节长度(文本)**
|
||||||
|
|
||||||
|
**内存 = memory.new(页数(字节数), 页数(字节数))**
|
||||||
|
|
||||||
|
**长度 = utf8.编码(文本, 内存, 0)**
|
||||||
|
|
||||||
|
**流 = resource.open("stdio", "stdout", ["write"])**
|
||||||
|
|
||||||
|
**写结果 = 写全部(流, 内存, 0, 长度)**
|
||||||
|
|
||||||
|
**关闭 = resource.close(流)**
|
||||||
|
|
||||||
|
`if not 写结果.ok`
|
||||||
|
|
||||||
|
`return 写结果`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if not 关闭.ok`
|
||||||
|
|
||||||
|
`return 关闭`
|
||||||
|
|
||||||
|
`else`
|
||||||
|
`if true`
|
||||||
|
|
||||||
|
`return {ok: true, value: {count: 长度}}`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
|
`end`
|
||||||
Executable
+207
@@ -0,0 +1,207 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Run this component's public `.mq.md` self-tests against a real CLI Bundle.
|
||||||
|
|
||||||
|
Inputs are explicit and never discovered from sibling checkouts:
|
||||||
|
|
||||||
|
* ``MARQDO_BIN`` verified ``marqdo`` executable;
|
||||||
|
* ``MARQDO_STDLIB_DIR`` Bundle that already installs this component's modules at
|
||||||
|
their frozen bundle paths (the Bundle identity is the caller's responsibility;
|
||||||
|
the CLI itself verifies the manifest digest before loading a module).
|
||||||
|
|
||||||
|
Each ``tests/cases/*.json`` file pins one self-test: the source file, stdin, CLI
|
||||||
|
flags, environment, the expected bundle module path and the expected observation
|
||||||
|
terminal value / stdout. The expected values are the baseline captured from the
|
||||||
|
pre-migration CLI Bundle; a change is a public behaviour change and must be
|
||||||
|
reviewed as one.
|
||||||
|
|
||||||
|
Usage: scripts/test.sh [CASE_NAME...]
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
REPO = Path(__file__).resolve().parents[1]
|
||||||
|
CASES = REPO / "tests" / "cases"
|
||||||
|
SCHEMA = "marqdo.stdlib-test-case"
|
||||||
|
SCHEMA_VERSION = 1
|
||||||
|
|
||||||
|
|
||||||
|
class CaseError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _substitute(value, bin_version: str):
|
||||||
|
if isinstance(value, dict):
|
||||||
|
if set(value) == {"$bin_version"}:
|
||||||
|
return bin_version
|
||||||
|
return {key: _substitute(item, bin_version) for key, item in value.items()}
|
||||||
|
if isinstance(value, list):
|
||||||
|
return [_substitute(item, bin_version) for item in value]
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _channel_text(events: list[dict], channel: str) -> str:
|
||||||
|
chunks = []
|
||||||
|
for event in events:
|
||||||
|
if event.get("kind") != channel:
|
||||||
|
continue
|
||||||
|
payload = event["payload"]
|
||||||
|
data = base64.b64decode(payload["data_base64"])
|
||||||
|
if len(data) != payload["byte_count"]:
|
||||||
|
raise CaseError(f"{channel} byte_count does not match decoded bytes")
|
||||||
|
chunks.append(data)
|
||||||
|
return b"".join(chunks).decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def _run_case(case: dict, case_path: Path, binary: str, bundle: Path) -> None:
|
||||||
|
name = case["name"]
|
||||||
|
module = case.get("require_module")
|
||||||
|
if module and not (bundle / module).is_file():
|
||||||
|
raise CaseError(f"{name}: Bundle {bundle} does not install {module}")
|
||||||
|
source = REPO / "tests" / case["file"]
|
||||||
|
if not source.is_file():
|
||||||
|
raise CaseError(f"{name}: missing test source {source}")
|
||||||
|
for relative in case.get("clean", []):
|
||||||
|
target = (REPO / relative).resolve()
|
||||||
|
if REPO not in target.parents:
|
||||||
|
raise CaseError(f"{name}: clean path escapes the repository")
|
||||||
|
if target.is_dir():
|
||||||
|
shutil.rmtree(target)
|
||||||
|
elif target.exists():
|
||||||
|
target.unlink()
|
||||||
|
env = os.environ.copy()
|
||||||
|
env["MARQDO_STDLIB_DIR"] = str(bundle)
|
||||||
|
for key, value in case.get("env", {}).items():
|
||||||
|
env[key] = str(value).replace("$MARQDO_BIN", binary)
|
||||||
|
version = subprocess.run(
|
||||||
|
[binary, "--version"], capture_output=True, text=True, check=True
|
||||||
|
).stdout
|
||||||
|
args = [str(arg).replace("$MARQDO_BIN", binary) for arg in case.get("args", [])]
|
||||||
|
command = [binary, "run", str(source), *args, "--output", "json"]
|
||||||
|
result = subprocess.run(
|
||||||
|
command,
|
||||||
|
cwd=REPO,
|
||||||
|
env=env,
|
||||||
|
input=case.get("stdin", "").encode(),
|
||||||
|
capture_output=True,
|
||||||
|
)
|
||||||
|
if result.returncode != 0:
|
||||||
|
raise CaseError(
|
||||||
|
f"{name}: CLI exited {result.returncode}\nstderr:\n"
|
||||||
|
f"{result.stderr.decode(errors='replace')}"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
report = json.loads(result.stdout)
|
||||||
|
except json.JSONDecodeError as error:
|
||||||
|
raise CaseError(f"{name}: CLI did not emit a JSON report: {error}") from error
|
||||||
|
if report.get("run", {}).get("status") != "ok":
|
||||||
|
raise CaseError(f"{name}: run status is not ok: {report.get('run')}")
|
||||||
|
events = report.get("events") or []
|
||||||
|
if not events or events[-1].get("kind") not in (
|
||||||
|
"run.completed",
|
||||||
|
"run.failed",
|
||||||
|
"run.rejected",
|
||||||
|
):
|
||||||
|
raise CaseError(f"{name}: report has no terminal event")
|
||||||
|
terminal = events[-1]["payload"]
|
||||||
|
if terminal.get("status") != "ok":
|
||||||
|
raise CaseError(f"{name}: terminal status is not ok: {terminal.get('status')}")
|
||||||
|
actual_value = terminal.get("value")
|
||||||
|
expected_value = _substitute(case["expected_terminal"], version)
|
||||||
|
if actual_value != expected_value:
|
||||||
|
raise CaseError(
|
||||||
|
f"{name}: terminal value changed\nexpected: "
|
||||||
|
f"{json.dumps(expected_value, ensure_ascii=False, sort_keys=True)}\n"
|
||||||
|
f"actual: {json.dumps(actual_value, ensure_ascii=False, sort_keys=True)}"
|
||||||
|
)
|
||||||
|
expected_stdout = case.get("expected_stdout", "")
|
||||||
|
actual_stdout = _channel_text(events, "stdout.chunk")
|
||||||
|
if actual_stdout != expected_stdout:
|
||||||
|
raise CaseError(
|
||||||
|
f"{name}: stdout changed: expected {expected_stdout!r}, got {actual_stdout!r}"
|
||||||
|
)
|
||||||
|
for check in case.get("event_checks", []):
|
||||||
|
matching = [
|
||||||
|
event
|
||||||
|
for event in events
|
||||||
|
if event.get("kind") == check["kind"]
|
||||||
|
and all(
|
||||||
|
_dig(event.get("payload"), pointer) == expected
|
||||||
|
for pointer, expected in check.get("payload", {}).items()
|
||||||
|
)
|
||||||
|
]
|
||||||
|
if not matching:
|
||||||
|
raise CaseError(
|
||||||
|
f"{name}: no {check['kind']} event matched {check.get('payload')}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _dig(value, pointer: str):
|
||||||
|
current = value
|
||||||
|
for part in pointer.split("."):
|
||||||
|
if isinstance(current, list):
|
||||||
|
current = current[int(part)]
|
||||||
|
elif isinstance(current, dict):
|
||||||
|
current = current[part]
|
||||||
|
else:
|
||||||
|
return object()
|
||||||
|
return current
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> int:
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("cases", nargs="*", help="case names to run")
|
||||||
|
args = parser.parse_args()
|
||||||
|
binary = os.environ.get("MARQDO_BIN")
|
||||||
|
bundle = os.environ.get("MARQDO_STDLIB_DIR")
|
||||||
|
if not binary or not shutil.which(binary):
|
||||||
|
print("MARQDO_BIN must point at a verified marqdo executable", file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
if not bundle or not Path(bundle).is_dir():
|
||||||
|
print("MARQDO_STDLIB_DIR must point at an installed Bundle", file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
bundle = Path(bundle).resolve()
|
||||||
|
paths = sorted(CASES.glob("*.json"))
|
||||||
|
if not paths:
|
||||||
|
print("no test cases in tests/cases", file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
selected = set(args.cases)
|
||||||
|
failures = 0
|
||||||
|
ran = 0
|
||||||
|
for path in paths:
|
||||||
|
try:
|
||||||
|
case = json.loads(path.read_text(encoding="utf-8"))
|
||||||
|
except json.JSONDecodeError as error:
|
||||||
|
print(f"FAIL {path.name}: invalid JSON: {error}", file=sys.stderr)
|
||||||
|
failures += 1
|
||||||
|
continue
|
||||||
|
if case.get("schema") != SCHEMA or case.get("schema_version") != SCHEMA_VERSION:
|
||||||
|
print(f"FAIL {path.name}: unsupported case schema", file=sys.stderr)
|
||||||
|
failures += 1
|
||||||
|
continue
|
||||||
|
if selected and case["name"] not in selected:
|
||||||
|
continue
|
||||||
|
ran += 1
|
||||||
|
try:
|
||||||
|
_run_case(case, path, binary, bundle)
|
||||||
|
print(f"PASS {case['name']}")
|
||||||
|
except CaseError as error:
|
||||||
|
print(f"FAIL {error}", file=sys.stderr)
|
||||||
|
failures += 1
|
||||||
|
if failures:
|
||||||
|
print(f"{failures} of {ran} cases failed", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
print(f"{ran} cases passed")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Executable
+7
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Explicit inputs only: a verified CLI binary and a Bundle that installs this
|
||||||
|
# component's modules at their frozen paths. Never discovers sibling checkouts.
|
||||||
|
set -eu
|
||||||
|
: "${MARQDO_BIN:?set MARQDO_BIN to the verified marqdo executable}"
|
||||||
|
: "${MARQDO_STDLIB_DIR:?set MARQDO_STDLIB_DIR to the installed Bundle}"
|
||||||
|
exec python3 "$(dirname "$0")/run_cases.py" "$@"
|
||||||
+5
-1
@@ -1,3 +1,7 @@
|
|||||||
# 验收测试
|
# 验收测试
|
||||||
|
|
||||||
首个功能附带正例和失败用例,记录所用 CLI 提交或发行版本以及实际运行命令。
|
`tests/*.mq.md` 是从迁移前 CLI 官方标准库自测迁入的真实测试源;`tests/cases/*.json` 固定每个测试的输入(stdin、CLI 参数、环境、所需 Bundle 模块)和期望结果(observation 终值、stdout、必要的资源事件)。
|
||||||
|
|
||||||
|
运行入口是 `scripts/test.sh`,它要求显式传入 `MARQDO_BIN` 与 `MARQDO_STDLIB_DIR`。缺少制品、Bundle 缺少模块或 digest 校验失败时必须失败,不得回退到其它目录。
|
||||||
|
|
||||||
|
期望值取自迁移前基线 Bundle(`sha256:5407d36...`)与已验证候选 CLI 的真实执行结果;任何期望值改动都必须作为公开行为变化单独评审并记录证据。
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"schema": "marqdo.stdlib-test-case",
|
||||||
|
"schema_version": 1,
|
||||||
|
"name": "console",
|
||||||
|
"file": "console.mq.md",
|
||||||
|
"require_module": "io/console.mq.md",
|
||||||
|
"args": [],
|
||||||
|
"stdin": "hello\n",
|
||||||
|
"env": {},
|
||||||
|
"expected_stdout": "",
|
||||||
|
"expected_terminal": {
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "bool",
|
||||||
|
"value": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"value": "hello"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"type": "list"
|
||||||
|
},
|
||||||
|
"event_checks": [
|
||||||
|
{
|
||||||
|
"kind": "effect",
|
||||||
|
"payload": {
|
||||||
|
"operation": "resource.read"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"schema": "marqdo.stdlib-test-case",
|
||||||
|
"schema_version": 1,
|
||||||
|
"name": "format",
|
||||||
|
"file": "format.mq.md",
|
||||||
|
"require_module": "format.mq.md",
|
||||||
|
"args": [],
|
||||||
|
"stdin": "",
|
||||||
|
"env": {},
|
||||||
|
"expected_stdout": "",
|
||||||
|
"expected_terminal": {
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"decimal": "3",
|
||||||
|
"type": "int"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"value": "[\"文本\", 2, 1.5]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"value": "{a: 1}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"value": "[\"a\", 2]"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"value": "None"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"type": "list"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"schema": "marqdo.stdlib-test-case",
|
||||||
|
"schema_version": 1,
|
||||||
|
"name": "io",
|
||||||
|
"file": "io.mq.md",
|
||||||
|
"require_module": "io.mq.md",
|
||||||
|
"args": [],
|
||||||
|
"stdin": "",
|
||||||
|
"env": {},
|
||||||
|
"expected_stdout": "stdlib self-test\n",
|
||||||
|
"expected_terminal": {
|
||||||
|
"type": "none"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"schema": "marqdo.stdlib-test-case",
|
||||||
|
"schema_version": 1,
|
||||||
|
"name": "stream",
|
||||||
|
"file": "stream.mq.md",
|
||||||
|
"require_module": "io/stream.mq.md",
|
||||||
|
"args": [],
|
||||||
|
"stdin": "",
|
||||||
|
"env": {},
|
||||||
|
"expected_stdout": "stream\n",
|
||||||
|
"expected_terminal": {
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"type": "bool",
|
||||||
|
"value": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"decimal": "7",
|
||||||
|
"type": "int"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"type": "list"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
console: "std:io/console"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec console
|
||||||
|
|
||||||
|
## main
|
||||||
|
|
||||||
|
**行 = console.读取行()**
|
||||||
|
|
||||||
|
`return [行.ok, 行.value]`
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
format: "std:format"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec format
|
||||||
|
|
||||||
|
## main
|
||||||
|
|
||||||
|
`return [format.字节长度("Aé"), format.显示(["文本", 2, 1.5]), format.显示({a: 1}), format.列表显示(["a", 2]), format.显示(None)]`
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
io: "std:io"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec io
|
||||||
|
|
||||||
|
## main
|
||||||
|
|
||||||
|
> **io.写值("stdlib self-test")**
|
||||||
|
|
||||||
|
`return None`
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
---
|
||||||
|
import:
|
||||||
|
stream: "std:io/stream"
|
||||||
|
---
|
||||||
|
|
||||||
|
# @exec stream
|
||||||
|
|
||||||
|
## main
|
||||||
|
|
||||||
|
**结果 = stream.写文本("stream\n")**
|
||||||
|
|
||||||
|
`return [结果.ok, 结果.value.count]`
|
||||||
Reference in new issue
Block a user