diff options
Diffstat (limited to 'stdlib/source/test/lux/world/file.lux')
-rw-r--r-- | stdlib/source/test/lux/world/file.lux | 318 |
1 files changed, 162 insertions, 156 deletions
diff --git a/stdlib/source/test/lux/world/file.lux b/stdlib/source/test/lux/world/file.lux index deed8dbd2..e27add632 100644 --- a/stdlib/source/test/lux/world/file.lux +++ b/stdlib/source/test/lux/world/file.lux @@ -1,26 +1,29 @@ (.module: [lux #* - ["." io (#+ IO)] + data/text/format + ["r" math/random (#+ Random) ("#@." monad)] + ["_" test (#+ Test)] [control - [monad (#+ do)]] - [concurrency - ["." promise]] + ["." monad (#+ do)] + [concurrency + ["." promise]] + [security + ["!" capability]]] + ["." io (#+ IO)] [data ["." error (#+ Error)] - ["." number] - ["." text - format] + ["." text] + [number + ["." int]] [collection ["." list]]] [time ["." instant] ["." duration]] [world - ["@" file (#+ Path File)] - ["." binary (#+ Binary)]] - [math - ["r" random ("#;." monad)]]] - lux/test + ["." binary (#+ Binary)]]] + {1 + ["." / (#+ Path File)]} [// ["_." binary]]) @@ -30,163 +33,166 @@ (def: (creation-and-deletion number) (-> Nat Test) - (r;wrap (do promise.monad + (r@wrap (do promise.monad [#let [path (format "temp_file_" (%n number))] result (promise.future - (do (error.ErrorT io.monad) + (do (error.with io.monad) [#let [check-existence! (: (IO (Error Bit)) - (io.from-io (@.exists? io.monad @.system path)))] + (error.lift io.monad (/.exists? io.monad /.system path)))] pre! check-existence! - file (:: @.system create-file path) + file (!.use (:: /.system create-file) path) post! check-existence! - _ (:: file delete []) + _ (!.use (:: file delete) []) remains? check-existence!] (wrap (and (not pre!) post! (not remains?)))))] - (assert "Can create/delete files." - (error.default #0 result))))) + (_.assert "Can create/delete files." + (error.default #0 result))))) (def: (read-and-write number data) (-> Nat Binary Test) - (r;wrap (do promise.monad + (r@wrap (do promise.monad [#let [path (format "temp_file_" (%n number))] result (promise.future - (do (error.ErrorT io.monad) - [file (:: @.system create-file path) - _ (:: file over-write data) - content (:: file content []) - _ (:: file delete [])] + (do (error.with io.monad) + [file (!.use (:: /.system create-file) path) + _ (!.use (:: file over-write) data) + content (!.use (:: file content) []) + _ (!.use (:: file delete) [])] (wrap (:: binary.equivalence = data content))))] - (assert "Can write/read files." - (error.default #0 result))))) + (_.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) - new-modified (|> r.int (:: @ map (|>> (:: number.number abs) - truncate-millis - duration.from-millis - instant.absolute)))] - ($_ seq - (creation-and-deletion 0) - (read-and-write 1 dataL) - (wrap (do promise.monad - [#let [path "temp_file_2"] - result (promise.future - (do (error.ErrorT io.monad) - [file (:: @.system 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 - [#let [path "temp_file_3"] - result (promise.future - (do (error.ErrorT io.monad) - [file (:: @.system 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 = - dataL - (error.assume (binary.slice 0 (dec file-size) content))) - (:: binary.equivalence = - dataR - (error.assume (binary.slice file-size (dec read-size) content)))))))] - (assert "Can append to files." - (error.default #0 result)))) - (wrap (do promise.monad - [#let [path "temp_dir_4"] - result (promise.future - (do (error.ErrorT io.monad) - [#let [check-existence! (: (IO (Error Bit)) - (io.from-io (@.exists? io.monad @.system path)))] - pre! check-existence! - dir (:: @.system create-directory path) - post! check-existence! - _ (:: dir discard []) - remains? check-existence!] - (wrap (and (not pre!) - post! - (not remains?)))))] - (assert "Can create/delete directories." - (error.default #0 result)))) - (wrap (do promise.monad - [#let [file-path "temp_file_5" - dir-path "temp_dir_5"] - result (promise.future - (do (error.ErrorT io.monad) - [dir (:: @.system create-directory dir-path) - file (:: @.system 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 - [#let [file-path "temp_file_6" - dir-path "temp_dir_6" - inner-dir-path "inner_temp_dir_6"] - result (promise.future - (do (error.ErrorT io.monad) - [dir (:: @.system create-directory dir-path) - pre-files (:: dir files []) - pre-directories (:: dir directories []) - - file (:: @.system create-file (format dir-path "/" file-path)) - inner-dir (:: @.system 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 - [#let [path "temp_file_7"] - result (promise.future - (do (error.ErrorT io.monad) - [file (:: @.system create-file path) - _ (:: file over-write dataL) - _ (:: file modify new-modified) - old-modified (:: file last-modified []) - _ (:: file delete [])] - (wrap (:: instant.equivalence = new-modified old-modified))))] - (assert "Can change the time of last modification." - (error.default #0 result)))) - (wrap (do promise.monad - [#let [path0 (format "temp_file_8+0") - path1 (format "temp_file_8+1")] - result (promise.future - (do (error.ErrorT io.monad) - [#let [check-existence! (: (-> Path (IO (Error Bit))) - (|>> (@.exists? io.monad @.system) io.from-io))] - file0 (:: @.system 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?))))] - (assert "Can move a file from one path to another." - (error.default #0 result)))) - ))) +(def: #export test + Test + (<| (_.context (%name (name-of /._))) + (do r.monad + [file-size (|> r.nat (:: @ map (|>> (n/% 100) (n/max 10)))) + dataL (_binary.binary file-size) + dataR (_binary.binary file-size) + new-modified (|> r.int (:: @ map (|>> (:: int.number abs) + truncate-millis + duration.from-millis + instant.absolute)))] + ($_ _.and + (creation-and-deletion 0) + (read-and-write 1 dataL) + (wrap (do promise.monad + [#let [path "temp_file_2"] + result (promise.future + (do (error.with io.monad) + [file (!.use (:: /.system create-file) path) + _ (!.use (:: file over-write) dataL) + read-size (!.use (:: file size) []) + _ (!.use (:: file delete) [])] + (wrap (n/= file-size read-size))))] + (_.assert "Can read file size." + (error.default #0 result)))) + (wrap (do promise.monad + [#let [path "temp_file_3"] + result (promise.future + (do (error.with io.monad) + [file (!.use (:: /.system create-file) path) + _ (!.use (:: file over-write) dataL) + _ (!.use (:: file append) dataR) + content (!.use (:: file content) []) + read-size (!.use (:: file size) []) + _ (!.use (:: file delete) [])] + (wrap (and (n/= (n/* 2 file-size) read-size) + (:: binary.equivalence = + dataL + (error.assume (binary.slice 0 (dec file-size) content))) + (:: binary.equivalence = + dataR + (error.assume (binary.slice file-size (dec read-size) content)))))))] + (_.assert "Can append to files." + (error.default #0 result)))) + (wrap (do promise.monad + [#let [path "temp_dir_4"] + result (promise.future + (do (error.with io.monad) + [#let [check-existence! (: (IO (Error Bit)) + (error.lift io.monad (/.exists? io.monad /.system path)))] + pre! check-existence! + dir (!.use (:: /.system create-directory) path) + post! check-existence! + _ (!.use (:: dir discard) []) + remains? check-existence!] + (wrap (and (not pre!) + post! + (not remains?)))))] + (_.assert "Can create/delete directories." + (error.default #0 result)))) + (wrap (do promise.monad + [#let [file-path "temp_file_5" + dir-path "temp_dir_5"] + result (promise.future + (do (error.with io.monad) + [dir (!.use (:: /.system create-directory) dir-path) + file (!.use (:: /.system create-file) (format dir-path "/" file-path)) + _ (!.use (:: file over-write) dataL) + read-size (!.use (:: file size) []) + _ (!.use (:: file delete) []) + _ (!.use (:: dir discard) [])] + (wrap (n/= file-size read-size))))] + (_.assert "Can create files inside of directories." + (error.default #0 result)))) + (wrap (do promise.monad + [#let [file-path "temp_file_6" + dir-path "temp_dir_6" + inner-dir-path "inner_temp_dir_6"] + result (promise.future + (do (error.with io.monad) + [dir (!.use (:: /.system create-directory) dir-path) + pre-files (!.use (:: dir files) []) + pre-directories (!.use (:: dir directories) []) + + file (!.use (:: /.system create-file) (format dir-path "/" file-path)) + inner-dir (!.use (:: /.system create-directory) (format dir-path "/" inner-dir-path)) + post-files (!.use (:: dir files) []) + post-directories (!.use (:: dir directories) []) + + _ (!.use (:: file delete) []) + _ (!.use (:: inner-dir discard) []) + _ (!.use (:: 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 + [#let [path "temp_file_7"] + result (promise.future + (do (error.with io.monad) + [file (!.use (:: /.system create-file) path) + _ (!.use (:: file over-write) dataL) + _ (!.use (:: file modify) new-modified) + old-modified (!.use (:: file last-modified) []) + _ (!.use (:: file delete) [])] + (wrap (:: instant.equivalence = new-modified old-modified))))] + (_.assert "Can change the time of last modification." + (error.default #0 result)))) + (wrap (do promise.monad + [#let [path0 (format "temp_file_8+0") + path1 (format "temp_file_8+1")] + result (promise.future + (do (error.with io.monad) + [#let [check-existence! (: (-> Path (IO (Error Bit))) + (|>> (/.exists? io.monad /.system) + (error.lift io.monad)))] + file0 (!.use (:: /.system create-file) path0) + _ (!.use (:: file0 over-write) dataL) + pre! (check-existence! path0) + file1 (: (IO (Error (File IO))) ## TODO: Remove : + (!.use (:: file0 move) path1)) + post! (check-existence! path0) + confirmed? (check-existence! path1) + _ (!.use (:: file1 delete) [])] + (wrap (and pre! + (not post!) + confirmed?))))] + (_.assert "Can move a file from one path to another." + (error.default #0 result)))) + )))) |