Skip to content

Commit

Permalink
!28 Updates on (liii error) (liii string) and nice ???
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Aug 8, 2024
1 parent 0652e3d commit ce81856
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| [(liii os)](goldfish/liii/os.scm) | 库类似于 Python 的 `os` 模块 | `getenv`, `mkdir` |
| [(liii uuid)](goldfish/liii/uuid.scm) | UUID 生成 | `uuid4` |
| [(liii sys)](goldfish/liii/sys.scm) | 库类似于 Python 的 `sys` 模块 | `argv` |
| [(liii error)](goldfish/liii/error.scm) | 提供类似Python的错误函数 | `error-os`函数抛出`'os-error`,类似Python的OSError |
| [(liii error)](goldfish/liii/error.scm) | 提供类似Python的错误函数 | `os-error`函数抛出`'os-error`,类似Python的OSError |
| [(liii check)](goldfish/liii/check.scm) | 基于SRFI 78的轻量级测试库加强版 | `check`, `check-catch` |

### SRFI
Expand Down
30 changes: 22 additions & 8 deletions goldfish/liii/error.scm
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,37 @@
; under the License.
;

; see https://docs.python.org/3/library/exceptions.html#exception-hierarchy

(define-library (liii error)
(export error-file-not-found error-os error-not-a-directory
error-file-exists)
(export ???
os-error file-not-found-error not-a-directory-error file-exists-error
timeout-error
type-error value-error)
(import (scheme process-context))
(begin

(define (error-file-not-found msg)
(error 'file-not-found-error msg))

(define (error-os msg)
(define (os-error msg)
(error 'os-error msg))

(define (error-not-a-directory msg)
(define (file-not-found-error msg)
(error 'file-not-found-error msg))

(define (not-a-directory-error msg)
(error 'not-a-directory-error msg))

(define (error-file-exists msg)
(define (file-exists-error msg)
(error 'file-exists-error msg))

(define (timeout-error args)
(apply (cons 'timeout-error args) error))

(define (type-error args)
(apply (cons 'type-error args) error))

; nice Scala style to throw the not-implemented-error
(define (???)
(error 'not-implemented-error))

) ; begin
) ; define-library
6 changes: 3 additions & 3 deletions goldfish/liii/os.scm
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@

(define (%check-dir-andthen path f)
(cond ((not (file-exists? path))
(error-file-not-found
(file-not-found-error
(string-append "No such file or directory: '" path "'")))
((not (isdir path))
(error-not-a-directory
(not-a-directory-error
(string-append "Not a directory: '" path "'")))
(else (f path))))

Expand All @@ -60,7 +60,7 @@

(define (mkdir path)
(if (file-exists? path)
(error-file-exists (string-append "File exists: '" path "'"))
(file-exists-error (string-append "File exists: '" path "'"))
(g_mkdir path)))

(define (rmdir path)
Expand Down
35 changes: 35 additions & 0 deletions goldfish/liii/string.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;

(define-library (liii string)
(export
string-null? string-copy string-join
string-every string-any
string-take string-take-right string-drop string-drop-right
string-pad string-pad-right
string-trim string-trim-right string-trim-both
string-prefix? string-suffix?
string-index string-index-right
string-contains string-count
string-reverse
string-tokenize
)
(import (srfi srfi-13)
(srfi srfi-1)
(liii error))
(begin
) ; end of begin
) ; end of library
22 changes: 22 additions & 0 deletions tests/liii/error-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;

(import (liii check)
(liii error))

(check-catch 'not-implemented-error (lambda () (???)))

(check-report)
23 changes: 23 additions & 0 deletions tests/liii/string-test.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;

(import (liii check)
(liii error))

(check (mk-string " " (list 1 2 3)) => "1 2 3")
(check (mk-string " " (list )) => "")

(check-report)
10 changes: 8 additions & 2 deletions tests/scheme/process-context-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
;

(import (srfi srfi-78)
(scheme process-context))
(scheme process-context)
(srfi srfi-13)
(liii os))

(check-set-mode! 'report-failed)

; (check (get-environment-variable "USER") => "da")
; (check (get-environment-variable "HOME") => "/home/da")
(when (os-linux?)
(check (string-prefix? "/home" (get-environment-variable "HOME"))
=> #t))

(check-report)

0 comments on commit ce81856

Please sign in to comment.