Skip to content

Commit 9c91fdd

Browse files
committed
!1018 [216_56] 为 (scheme file) 所有函数添加单元测试
1 parent 3099f35 commit 9c91fdd

12 files changed

Lines changed: 549 additions & 174 deletions

goldfish/scheme/file.scm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
;
1616

1717
(define-library (scheme file)
18-
(export open-binary-input-file open-binary-output-file)
18+
(export call-with-input-file call-with-output-file
19+
delete-file file-exists?
20+
open-binary-input-file open-binary-output-file
21+
open-input-file open-output-file
22+
with-input-from-file with-output-to-file)
1923
(begin
20-
24+
2125
(define open-binary-input-file open-input-file)
2226

2327
(define open-binary-output-file open-output-file)

tests/scheme/file-test.scm

Lines changed: 26 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,172 +1,26 @@
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+
;; - 错误处理测试(参数类型错误等)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
(import (liii check)
2+
(scheme file)
3+
) ;import
4+
5+
(check-set-mode! 'report-failed)
6+
7+
(define test-file "tests/scheme/file/test-call-with-input-file.txt")
8+
9+
;; 创建测试文件
10+
(with-output-to-file test-file
11+
(lambda () (display "hello world"))
12+
) ;with-output-to-file
13+
14+
;; 测试 call-with-input-file 基本功能
15+
(check
16+
(call-with-input-file test-file
17+
(lambda (port) (read-string 100 port))
18+
) ;call-with-input-file
19+
=> "hello world"
20+
) ;check
21+
22+
;; 测试读取多行
23+
(with-output-to-file test-file
24+
(lambda ()
25+
(display "line1\n")
26+
(display "line2")
27+
) ;lambda
28+
) ;with-output-to-file
29+
30+
(check
31+
(call-with-input-file test-file
32+
(lambda (port)
33+
(list (read-line port) (read-line port))
34+
) ;lambda
35+
) ;call-with-input-file
36+
=> '("line1" "line2")
37+
) ;check
38+
39+
;; 测试中文文件名
40+
(define chinese-file "tests/scheme/file/中文测试.txt")
41+
(with-output-to-file chinese-file
42+
(lambda () (display "中文内容"))
43+
) ;with-output-to-file
44+
(check
45+
(call-with-input-file chinese-file
46+
(lambda (port) (read-string 100 port))
47+
) ;call-with-input-file
48+
=> "中文内容"
49+
) ;check
50+
(delete-file chinese-file)
51+
52+
;; 清理
53+
(delete-file test-file)
54+
55+
(check-report)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(import (liii check)
2+
(scheme file)
3+
) ;import
4+
5+
(check-set-mode! 'report-failed)
6+
7+
(define test-file "tests/scheme/file/test-call-with-output-file.txt")
8+
9+
;; 测试 call-with-output-file 基本功能
10+
(call-with-output-file test-file
11+
(lambda (port) (display "test content" port))
12+
) ;call-with-output-file
13+
14+
;; 验证文件内容
15+
(check
16+
(call-with-input-file test-file
17+
(lambda (port) (read-string 100 port))
18+
) ;call-with-input-file
19+
=> "test content"
20+
) ;check
21+
22+
;; 测试覆盖写入
23+
(call-with-output-file test-file
24+
(lambda (port) (display "new content" port))
25+
) ;call-with-output-file
26+
27+
(check
28+
(call-with-input-file test-file
29+
(lambda (port) (read-string 100 port))
30+
) ;call-with-input-file
31+
=> "new content"
32+
) ;check
33+
34+
;; 测试中文文件名
35+
(define chinese-file "tests/scheme/file/中文输出.txt")
36+
(call-with-output-file chinese-file
37+
(lambda (port) (display "中文写入内容" port))
38+
) ;call-with-output-file
39+
(check
40+
(call-with-input-file chinese-file
41+
(lambda (port) (read-string 100 port))
42+
) ;call-with-input-file
43+
=> "中文写入内容"
44+
) ;check
45+
(delete-file chinese-file)
46+
47+
;; 清理
48+
(delete-file test-file)
49+
50+
(check-report)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(import (liii check)
2+
(scheme file)
3+
) ;import
4+
5+
(check-set-mode! 'report-failed)
6+
7+
(define test-file "tests/scheme/file/test-delete-file.txt")
8+
9+
;; 创建测试文件
10+
(with-output-to-file test-file
11+
(lambda () (display "test"))
12+
) ;with-output-to-file
13+
14+
;; 验证文件存在
15+
(check-true (file-exists? test-file))
16+
17+
;; 测试 delete-file
18+
(check
19+
(delete-file test-file)
20+
=> #t
21+
) ;check
22+
23+
;; 验证文件已删除
24+
(check-false (file-exists? test-file))
25+
26+
;; 测试删除不存在的文件应报错
27+
(check-catch
28+
'read-error
29+
(delete-file "tests/scheme/file/non-existent-file.txt")
30+
) ;check-catch
31+
32+
;; 测试删除参数类型错误
33+
(check-catch
34+
'type-error
35+
(delete-file 123)
36+
) ;check-catch
37+
38+
;; 测试删除中文文件名
39+
(define chinese-file "tests/scheme/file/中文删除.txt")
40+
(with-output-to-file chinese-file
41+
(lambda () (display "test"))
42+
) ;with-output-to-file
43+
(check-true (file-exists? chinese-file))
44+
(check (delete-file chinese-file) => #t)
45+
(check-false (file-exists? chinese-file))
46+
47+
(check-report)

0 commit comments

Comments
 (0)