aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/world/file.lux
blob: 361cf99ba657d4b838639a170b16259b863176b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(.module:
  [lux #*
   ["." io (#+ IO)]
   [control
    [monad (#+ do)]]
   [concurrency
    ["." promise]]
   [data
    ["." error (#+ Error)]
    ["." number]
    ["." text
     format]
    [collection
     ["." list]]]
   [time
    ["." instant]
    ["." duration]]
   [world
    ["@" file (#+ Path File)]
    ["." binary (#+ Binary)]]
   [math
    ["r" random ("#/." monad)]]]
  lux/test
  [//
   ["_." binary]])

(def: truncate-millis
  (|>> (i// +1_000) (i/* +1_000)))

(def: (creation-and-deletion number)
  (-> Nat Test)
  (r/wrap (do promise.monad
            [#let [path (format "temp_file_" (%n number))]
             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!
                        file (:: @.system 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
            [#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 [])]
                       (wrap (:: binary.equivalence = data 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)
     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))))
        )))