scenario read-from-fake-file [
local-scope
assume-filesystem [
[a] <- [
|xyz|
]
]
contents:&:source:char <- start-reading filesystem:&:filesystem, [a]
1:char/raw <- read contents
2:char/raw <- read contents
3:char/raw <- read contents
4:char/raw <- read contents
_, 5:bool/raw <- read contents
memory-should-contain [
1 <- 120
2 <- 121
3 <- 122
4 <- 10
5 <- 1
]
]
scenario write-to-fake-file [
local-scope
assume-filesystem [
]
sink:&:sink:char, writer:num/routine <- start-writing filesystem:&:filesystem, [a]
sink <- write sink, 120/x
sink <- write sink, 121/y
close sink
wait-for-routine writer
contents-read-back:text <- slurp filesystem, [a]
10:bool/raw <- equal contents-read-back, [xy]
memory-should-contain [
10 <- 1
]
]
scenario write-to-fake-file-that-exists [
local-scope
assume-filesystem [
[a] <- []
]
sink:&:sink:char, writer:num/routine <- start-writing filesystem:&:filesystem, [a]
sink <- write sink, 120/x
sink <- write sink, 121/y
close sink
wait-for-routine writer
contents-read-back:text <- slurp filesystem, [a]
10:bool/raw <- equal contents-read-back, [xy]
memory-should-contain [
10 <- 1
]
]
scenario write-to-existing-file-preserves-other-files [
local-scope
assume-filesystem [
[a] <- []
[b] <- [
|bcd|
]
]
sink:&:sink:char, writer:num/routine <- start-writing filesystem:&:filesystem, [a]
sink <- write sink, 120/x
sink <- write sink, 121/y
close sink
wait-for-routine writer
contents-read-back:text <- slurp filesystem, [a]
10:bool/raw <- equal contents-read-back, [xy]
other-file-contents:text <- slurp filesystem, [b]
11:bool/raw <- equal other-file-contents, [bcd
]
memory-should-contain [
10 <- 1
11 <- 1
]
]
def slurp fs:&:filesystem, filename:text -> contents:text [
local-scope
load-ingredients
source:&:source:char <- start-reading fs, filename
buf:&:buffer <- new-buffer 30/capacity
{
c:char, done?:bool, source <- read source
break-if done?
buf <- append buf, c
loop
}
contents <- buffer-to-array buf
]