|
1 | | -; |
2 | | -; Copyright (C) 2026 The Goldfish Scheme Authors |
3 | | -; |
4 | | -; Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -; you may not use this file except in compliance with the License. |
6 | | -; You may obtain a copy of the License at |
7 | | -; |
8 | | -; http://www.apache.org/licenses/LICENSE-2.0 |
9 | | -; |
10 | | -; Unless required by applicable law or agreed to in writing, software |
11 | | -; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | -; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | -; License for the specific language governing permissions and limitations |
14 | | -; under the License. |
15 | | -; |
16 | | - |
17 | | -(import (liii check)) |
18 | | - |
19 | | -(check-set-mode! 'report-failed) |
20 | | - |
21 | | -(define test-filename "file-test-中文.txt") |
22 | | -(define test-filenames |
23 | | - '("中文.txt" |
24 | | - "測試.txt" |
25 | | - "日本語.txt" |
26 | | - "한글.txt" |
27 | | - " ελληνικά.txt" |
28 | | - "ملف.txt") |
29 | | -) ;define |
30 | | - |
31 | | -(define (clean-filename filename) |
32 | | - (lambda () (delete-file filename)) |
33 | | -) ;define |
34 | | -(define clean-test-filename (clean-filename test-filename)) |
35 | | - |
36 | | -;; with-output-to-file |
37 | | - |
38 | | -; 中文文件名,中文内容 |
39 | | -(check |
40 | | - (dynamic-wind |
41 | | - #f ; before |
42 | | - (lambda () |
43 | | - (with-output-to-file test-filename |
44 | | - (lambda () (display "测试内容")) |
45 | | - ) ;with-output-to-file |
46 | | - |
47 | | - (call-with-input-file test-filename |
48 | | - (lambda (port) |
49 | | - (read-line port) |
50 | | - ) ;lambda |
51 | | - ) ;call-with-input-file |
52 | | - ) ;lambda |
53 | | - clean-test-filename ; after |
54 | | - ) ;dynamic-wind |
55 | | - => "测试内容" |
56 | | -) ;check |
57 | | - |
58 | | -; 中文文件名,英文内容 |
59 | | -(check |
60 | | - (dynamic-wind |
61 | | - #f ; before |
62 | | - (lambda () |
63 | | - (with-output-to-file test-filename |
64 | | - (lambda () (display "ok")) |
65 | | - ) ;with-output-to-file |
66 | | - |
67 | | - (call-with-input-file test-filename |
68 | | - (lambda (port) |
69 | | - (read-line port) |
70 | | - ) ;lambda |
71 | | - ) ;call-with-input-file |
72 | | - ) ;lambda |
73 | | - clean-test-filename ; after |
74 | | - ) ;dynamic-wind |
75 | | - => "ok" |
76 | | -) ;check |
77 | | - |
78 | | -; 中文文件名,多行中文内容 |
79 | | -(check |
80 | | - (dynamic-wind |
81 | | - #f ; before |
82 | | - (lambda () |
83 | | - (with-output-to-file test-filename |
84 | | - (lambda () |
85 | | - (display "第一行\n") |
86 | | - (display "第二行") |
87 | | - ) ;lambda |
88 | | - ) ;with-output-to-file |
89 | | - |
90 | | - (call-with-input-file test-filename |
91 | | - (lambda (port) |
92 | | - (list (read-line port) |
93 | | - (read-line port) |
94 | | - ) ;list |
95 | | - ) ;lambda |
96 | | - ) ;call-with-input-file |
97 | | - ) ;lambda |
98 | | - clean-test-filename ; after |
99 | | - ) ;dynamic-wind |
100 | | - => '("第一行" "第二行") |
101 | | -) ;check |
102 | | - |
103 | | -; 测试文件是否确实被创建 |
104 | | -(for-each |
105 | | - (lambda (filename) |
106 | | - (dynamic-wind |
107 | | - #f ; before |
108 | | - (lambda () |
109 | | - ; 确保测试文件还不存在 |
110 | | - (check (file-exists? filename) => #f) |
111 | | - |
112 | | - ; 测试文件创建 |
113 | | - (with-output-to-file filename |
114 | | - (lambda () (display "test")) |
115 | | - ) ;with-output-to-file |
116 | | - |
117 | | - ; 验证文件存在 |
118 | | - ; NOTE: 若写入文件名时编码不对应,file-exists? 会返回 #f |
119 | | - ; 如 `中文` 被直接写作文件名,由 Windows 解释为 GBK,会显示为 `涓枃` |
120 | | - (check-true (file-exists? filename)) |
121 | | - ) ;lambda |
122 | | - (clean-filename filename) ; after |
123 | | - ) ;dynamic-wind |
124 | | - ) ;lambda |
125 | | - test-filenames |
126 | | -) ;for-each |
127 | | - |
128 | | -;; load |
129 | | - |
130 | | -(define test-content |
131 | | - '(begin |
132 | | - (define 测试变量 "你好,世界!") |
133 | | - (define (测试函数 x) (+ x 1)) |
134 | | - #t) |
135 | | -) ;define |
136 | | - |
137 | | -(dynamic-wind |
138 | | - (lambda () ; before |
139 | | - (with-output-to-file test-filename |
140 | | - (lambda () (display "(+ 21 21)")) |
141 | | - ) ;with-output-to-file |
142 | | - ) ;lambda |
143 | | - (lambda () |
144 | | - (check (load test-filename) => 42) |
145 | | - ) ;lambda |
146 | | - clean-test-filename ; after |
147 | | -) ;dynamic-wind |
148 | | - |
149 | | -; 测试文件是否确实被创建 |
150 | | -(for-each |
151 | | - (lambda (filename) |
152 | | - (dynamic-wind |
153 | | - #f ; before |
154 | | - (lambda () |
155 | | - ; 确保测试文件还不存在 |
156 | | - (check (file-exists? filename) => #f) |
157 | | - |
158 | | - ; 测试文件创建 |
159 | | - (with-output-to-file filename |
160 | | - (lambda () (display "(+ 21 21)")) |
161 | | - ) ;with-output-to-file |
162 | | - |
163 | | - ; 验证能够正常 load |
164 | | - (check (load filename) => 42) |
165 | | - ) ;lambda |
166 | | - (clean-filename filename) ; after |
167 | | - ) ;dynamic-wind |
168 | | - ) ;lambda |
169 | | - test-filenames |
170 | | -) ;for-each |
171 | | - |
172 | | -(check-report) |
| 1 | +;; # (scheme file) 库测试和文档 |
| 2 | +;; |
| 3 | +;; (scheme file) 是 R7RS 标准库,提供文件操作相关的函数。 |
| 4 | +;; |
| 5 | +;; ## 导出的函数 |
| 6 | +;; |
| 7 | +;; | 函数名 | 说明 | 测试文件 | |
| 8 | +;; |--------|------|----------| |
| 9 | +;; | call-with-input-file | 以输入端口打开文件并调用过程 | call-with-input-file-test.scm | |
| 10 | +;; | call-with-output-file | 以输出端口打开文件并调用过程 | call-with-output-file-test.scm | |
| 11 | +;; | delete-file | 删除文件 | delete-file-test.scm | |
| 12 | +;; | file-exists? | 检查文件是否存在 | file-exists-p-test.scm | |
| 13 | +;; | open-binary-input-file | 以二进制输入模式打开文件 | open-binary-input-file-test.scm | |
| 14 | +;; | open-binary-output-file | 以二进制输出模式打开文件 | open-binary-output-file-test.scm | |
| 15 | +;; | open-input-file | 以输入模式打开文件 | open-input-file-test.scm | |
| 16 | +;; | open-output-file | 以输出模式打开文件 | open-output-file-test.scm | |
| 17 | +;; | with-input-from-file | 将当前输入端口重定向到文件 | with-input-from-file-test.scm | |
| 18 | +;; | with-output-to-file | 将当前输出端口重定向到文件 | with-output-to-file-test.scm | |
| 19 | +;; |
| 20 | +;; ## 测试说明 |
| 21 | +;; |
| 22 | +;; 每个函数都有独立的测试文件,位于 tests/scheme/file/ 目录下。 |
| 23 | +;; 测试内容包括: |
| 24 | +;; - 基本功能测试 |
| 25 | +;; - 中文文件名测试 |
| 26 | +;; - 错误处理测试(参数类型错误等) |
0 commit comments