aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--stdlib/test/test/lux/world/file.lux228
1 files changed, 116 insertions, 112 deletions
diff --git a/stdlib/test/test/lux/world/file.lux b/stdlib/test/test/lux/world/file.lux
index 1332ebdfc..d87ea7fc5 100644
--- a/stdlib/test/test/lux/world/file.lux
+++ b/stdlib/test/test/lux/world/file.lux
@@ -11,15 +11,17 @@
["." error (#+ Error)]
["." number]
["." text
- format]]
+ format]
+ [collection
+ ["." list]]]
[time
["." instant]
["." duration]]
[world
- ["@" file (#+ File)]
+ ["@" file (#+ Path File)]
["." binary (#+ Binary)]]
[math
- ["r" random]]]
+ ["r" random ("r/." Monad<Random>)]]]
lux/test
[//
["_." binary]])
@@ -27,82 +29,93 @@
(def: truncate-millis
(|>> (i// +1_000) (i/* +1_000)))
+(def: (creation-and-deletion number)
+ (-> Nat Test)
+ (r/wrap (do promise.Monad<Promise>
+ [#let [path (format "temp_file_" (%n number))]
+ result (promise.future
+ (do (error.ErrorT io.Monad<IO>)
+ [#let [check-existence! (: (IO (Error Bit))
+ (io.from-io (@.exists? io.Monad<IO> @.System<IO> path)))]
+ pre! check-existence!
+ file (:: @.System<IO> create-file path)
+ post! check-existence!
+ _ (:: file delete [])
+ remains? check-existence!]
+ (wrap (and (not pre!)
+ post!
+ (not remains?)))))]
+ (assert "Can create/delete files."
+ (error.default #0 result)))))
+
+(def: (read-and-write number data)
+ (-> Nat Binary Test)
+ (r/wrap (do promise.Monad<Promise>
+ [#let [path (format "temp_file_" (%n number))]
+ result (promise.future
+ (do (error.ErrorT io.Monad<IO>)
+ [file (:: @.System<IO> create-file path)
+ _ (:: file over-write data)
+ content (:: file content [])
+ _ (:: file delete [])]
+ (wrap (:: binary.Equivalence<Binary> = data (taint.trust content)))))]
+ (assert "Can write/read files."
+ (error.default #0 result)))))
+
(context: "File system."
(do @
[file-size (|> r.nat (:: @ map (|>> (n/% 100) (n/max 10))))
dataL (_binary.binary file-size)
dataR (_binary.binary file-size)
- last-modified (|> r.int (:: @ map (|>> (:: number.Number<Int> abs)
- truncate-millis
- duration.from-millis
- instant.absolute)))]
+ new-modified (|> r.int (:: @ map (|>> (:: number.Number<Int> abs)
+ truncate-millis
+ duration.from-millis
+ instant.absolute)))]
($_ seq
+ (creation-and-deletion 0)
+ (read-and-write 1 dataL)
(wrap (do promise.Monad<Promise>
- [#let [file "temp_file_0"]
+ [#let [path "temp_file_2"]
result (promise.future
- (do io.Monad<Process>
- [#let [check-existence! (: (IO (Error Bit))
- (@.exists? io.Monad<IO> @.System<IO> file))]
- pre! check-existence!
- _ (:: @.System<IO> write dataL file)
- post! check-existence!
- _ (:: @.System<IO> delete file)
- remains? check-existence!]
- (wrap (and (not pre!)
- post!
- (not remains?)))))]
- (assert "Can create/delete files."
- (error.default #0 result))))
- (wrap (do promise.Monad<Promise>
- [#let [file "temp_file_1"]
- result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> write dataL file)
- output (:: @.System<IO> read file)
- _ (:: @.System<IO> delete file)]
- (wrap (:: binary.Equivalence<Binary> = dataL (taint.trust output)))))]
- (assert "Can write/read files."
- (error.default #0 result))))
- (wrap (do promise.Monad<Promise>
- [#let [file "temp_file_2"]
- result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> write dataL file)
- read-size (:: @.System<IO> size file)
- _ (:: @.System<IO> delete file)]
+ (do (error.ErrorT io.Monad<IO>)
+ [file (:: @.System<IO> create-file path)
+ _ (:: file over-write dataL)
+ read-size (:: file size [])
+ _ (:: file delete [])]
(wrap (n/= file-size read-size))))]
(assert "Can read file size."
(error.default #0 result))))
(wrap (do promise.Monad<Promise>
- [#let [file "temp_file_3"]
+ [#let [path "temp_file_3"]
result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> write dataL file)
- _ (:: @.System<IO> append dataR file)
- output (:: @.System<IO> read file)
- read-size (:: @.System<IO> size file)
- _ (:: @.System<IO> delete file)]
+ (do (error.ErrorT io.Monad<IO>)
+ [file (:: @.System<IO> create-file path)
+ _ (:: file over-write dataL)
+ _ (:: file append dataR)
+ content (:: file content [])
+ read-size (:: file size [])
+ _ (:: file delete [])]
(wrap (and (n/= (n/* 2 file-size) read-size)
(:: binary.Equivalence<Binary> =
dataL
(error.assume (binary.slice 0 (dec file-size)
- (taint.trust output))))
+ (taint.trust content))))
(:: binary.Equivalence<Binary> =
dataR
(error.assume (binary.slice file-size (dec read-size)
- (taint.trust output))))))))]
+ (taint.trust content))))))))]
(assert "Can append to files."
(error.default #0 result))))
(wrap (do promise.Monad<Promise>
- [#let [dir "temp_dir_4"]
+ [#let [path "temp_dir_4"]
result (promise.future
- (do io.Monad<Process>
+ (do (error.ErrorT io.Monad<IO>)
[#let [check-existence! (: (IO (Error Bit))
- (@.exists? io.Monad<IO> @.System<IO> dir))]
+ (io.from-io (@.exists? io.Monad<IO> @.System<IO> path)))]
pre! check-existence!
- _ (:: @.System<IO> make-directory dir)
+ dir (:: @.System<IO> create-directory path)
post! check-existence!
- _ (:: @.System<IO> delete dir)
+ _ (:: dir discard [])
remains? check-existence!]
(wrap (and (not pre!)
post!
@@ -110,79 +123,70 @@
(assert "Can create/delete directories."
(error.default #0 result))))
(wrap (do promise.Monad<Promise>
- [#let [file "temp_file_5"
- dir "temp_dir_5"]
+ [#let [file-path "temp_file_5"
+ dir-path "temp_dir_5"]
result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> write dataL file)
- file-is-file (:: @.System<IO> file? file)
- file-is-directory (:: @.System<IO> directory? file)
- _ (:: @.System<IO> delete file)
- _ (:: @.System<IO> make-directory dir)
- directory-is-file (:: @.System<IO> file? dir)
- directory-is-directory (:: @.System<IO> directory? dir)
- _ (:: @.System<IO> delete dir)]
- (wrap (and file-is-file (not file-is-directory)
- (not directory-is-file) directory-is-directory))))]
- (assert "Can differentiate files from directories."
- (error.default #0 result))))
- (wrap (do promise.Monad<Promise>
- [#let [file "temp_file_6"
- dir "temp_dir_6"]
- result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> make-directory dir)
- #let [file' (format dir "/" file)]
- _ (:: @.System<IO> write dataL file')
- read-size (:: @.System<IO> size file')
- _ (:: @.System<IO> delete file')
- _ (:: @.System<IO> delete dir)]
+ (do (error.ErrorT io.Monad<IO>)
+ [dir (:: @.System<IO> create-directory dir-path)
+ file (:: @.System<IO> create-file (format dir-path "/" file-path))
+ _ (:: file over-write dataL)
+ read-size (:: file size [])
+ _ (:: file delete [])
+ _ (:: dir discard [])]
(wrap (n/= file-size read-size))))]
(assert "Can create files inside of directories."
(error.default #0 result))))
(wrap (do promise.Monad<Promise>
- [#let [file "temp_file_7"
- dir "temp_dir_7"]
+ [#let [file-path "temp_file_6"
+ dir-path "temp_dir_6"
+ inner-dir-path "inner_temp_dir_6"]
result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> make-directory dir)
- #let [file' (format dir "/" file)]
- _ (:: @.System<IO> write dataL file')
- children (:: @.System<IO> files dir)
- _ (:: @.System<IO> delete file')
- _ (:: @.System<IO> delete dir)]
- (wrap (case children
- (^ (list child))
- (text.ends-with? file' child)
-
- _
- #0))))]
- (assert "Can list files inside a directory."
+ (do (error.ErrorT io.Monad<IO>)
+ [dir (:: @.System<IO> create-directory dir-path)
+ pre-files (:: dir files [])
+ pre-directories (:: dir directories [])
+
+ file (:: @.System<IO> create-file (format dir-path "/" file-path))
+ inner-dir (:: @.System<IO> create-directory (format dir-path "/" inner-dir-path))
+ post-files (:: dir files [])
+ post-directories (:: dir directories [])
+
+ _ (:: file delete [])
+ _ (:: inner-dir discard [])
+ _ (:: dir discard [])]
+ (wrap (and (and (n/= 0 (list.size pre-files))
+ (n/= 0 (list.size pre-directories)))
+ (and (n/= 1 (list.size post-files))
+ (n/= 1 (list.size post-directories)))))))]
+ (assert "Can list files/directories inside a directory."
(error.default #0 result))))
(wrap (do promise.Monad<Promise>
- [#let [file "temp_file_8"]
+ [#let [path "temp_file_7"]
result (promise.future
- (do io.Monad<Process>
- [_ (:: @.System<IO> write dataL file)
- _ (:: @.System<IO> modify last-modified file)
- time-read (:: @.System<IO> last-modified file)
- _ (:: @.System<IO> delete file)]
- (wrap (:: instant.Equivalence<Instant> = last-modified time-read))))]
+ (do (error.ErrorT io.Monad<IO>)
+ [file (:: @.System<IO> create-file path)
+ _ (:: file over-write dataL)
+ _ (:: file modify new-modified)
+ old-modified (:: file last-modified [])
+ _ (:: file delete [])]
+ (wrap (:: instant.Equivalence<Instant> = new-modified old-modified))))]
(assert "Can change the time of last modification."
(error.default #0 result))))
(wrap (do promise.Monad<Promise>
- [#let [file0 (format "temp_file_9+0")
- file1 (format "temp_file_9+1")]
+ [#let [path0 (format "temp_file_8+0")
+ path1 (format "temp_file_8+1")]
result (promise.future
- (do io.Monad<Process>
- [#let [check-existence! (: (-> File (IO (Error Bit)))
- (@.exists? io.Monad<IO> @.System<IO>))]
- _ (:: @.System<IO> write dataL file0)
- pre! (check-existence! file0)
- _ (:: @.System<IO> move file1 file0)
- post! (check-existence! file0)
- confirmed? (check-existence! file1)
- _ (:: @.System<IO> delete file1)]
+ (do (error.ErrorT io.Monad<IO>)
+ [#let [check-existence! (: (-> Path (IO (Error Bit)))
+ (|>> (@.exists? io.Monad<IO> @.System<IO>) io.from-io))]
+ file0 (:: @.System<IO> create-file path0)
+ _ (:: file0 over-write dataL)
+ pre! (check-existence! path0)
+ file1 (: (IO (Error (File IO))) ## TODO: Remove :
+ (:: file0 move path1))
+ post! (check-existence! path0)
+ confirmed? (check-existence! path1)
+ _ (:: file1 delete [])]
(wrap (and pre!
(not post!)
confirmed?))))]