Port
Check if a value is a port
Section titled “Check if a value is a port”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (port? <expression>) 65 66))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”expression |
---|
(current-input-port) |
(current-output-port) |
Check if a value is an input port
Section titled “Check if a value is an input port”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (input-port? (current-input-port)) 65 66))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Check if a value is an output port
Section titled “Check if a value is an output port”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (if (output-port? <expression>) 65 66))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Examples
Section titled “Examples”expression |
---|
(current-output-port) |
(current-error-port) |
Check if an input port is open or not
Section titled “Check if an input port is open or not”Given a file named “main.scm” with:
(import (scheme base) (scheme file))
(define port <expression>)
(write-u8 (if (input-port-open? port) 65 66))(close-input-port port)(write-u8 (if (input-port-open? port) 65 66))
And a file named “foo.txt” with:
When I successfully run stak main.scm
Then the stdout should contain exactly “AB”.
Examples
Section titled “Examples”expression |
---|
(open-input-file “foo.txt”) |
(open-input-string “foo”) |
Check if an output port is open or not
Section titled “Check if an output port is open or not”Given a file named “main.scm” with:
(import (scheme base) (scheme file))
(define port <expression>)
(write-u8 (if (output-port-open? port) 65 66))(close-output-port port)(write-u8 (if (output-port-open? port) 65 66))
And a file named “foo.txt” with:
When I successfully run stak main.scm
Then the stdout should contain exactly “AB”.
Examples
Section titled “Examples”expression |
---|
(open-output-file “foo.txt”) |
(open-output-string) |
Read from a string port
Section titled “Read from a string port”Given a file named “main.scm” with:
(import (scheme base))
(call-with-port (open-input-string "<string>") (lambda (port) (parameterize ((current-input-port port)) (do ((x (read-u8) (read-u8))) ((eof-object? x) #f) (write-u8 x)))))
When I successfully run stak main.scm
Then the stdout should contain exactly “<string>”.
Examples
Section titled “Examples”string |
---|
ABC |
あ |
😄 |
Write to a string port
Section titled “Write to a string port”Given a file named “main.scm” with:
(import (scheme base))
(call-with-port (open-output-string) (lambda (port) (parameterize ((current-output-port port)) (for-each write-u8 '(<bytes>))) (let ((xs (get-output-string port))) (unless (= (string-length xs) <length>) (error "invalid length")) (write-string xs))))
When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”bytes | output | length |
---|---|---|
65 66 67 | ABC | 3 |
227 129 130 | あ | 1 |
240 159 152 132 | 😄 | 1 |
Read from a bytevector port
Section titled “Read from a bytevector port”Given a file named “main.scm” with:
(import (scheme base))
(call-with-port (open-input-bytevector #u8(65 66 67)) (lambda (port) (parameterize ((current-input-port port)) (do ((x (read-u8) (read-u8))) ((eof-object? x) #f) (write-u8 x)))))
When I successfully run stak main.scm
.
Write to a bytevector port
Section titled “Write to a bytevector port”Given a file named “main.scm” with:
(import (scheme base))
(call-with-port (open-output-bytevector) (lambda (port) (parameterize ((current-output-port port)) (for-each write-u8 '(<bytes>))) (let ((xs (get-output-bytevector port))) (unless (= (bytevector-length xs) <length>) (error "invalid length")) (write-bytevector xs))))
When I successfully run stak main.scm
Then the stdout should contain exactly “<output>”.
Examples
Section titled “Examples”bytes | output | length |
---|---|---|
65 | A | 1 |
65 66 | AB | 2 |
65 66 67 | ABC | 3 |