forked from Mirror/wren
More file IO!
Can now create, delete, and write to files.
This commit is contained in:
22
test/io/file/create.wren
Normal file
22
test/io/file/create.wren
Normal file
@ -0,0 +1,22 @@
|
||||
import "io" for File
|
||||
|
||||
// Create a new file.
|
||||
var file = File.create("file.temp")
|
||||
System.print(file.isOpen) // expect: true
|
||||
System.print(file.size) // expect: 0
|
||||
|
||||
file.writeBytes("stuff")
|
||||
file.close()
|
||||
|
||||
System.print(File.size("file.temp")) // expect: 5
|
||||
|
||||
// Overwrite a file.
|
||||
file = File.create("file.temp")
|
||||
|
||||
// Truncates.
|
||||
System.print(file.size) // expect: 0
|
||||
file.close()
|
||||
|
||||
File.delete("file.temp")
|
||||
|
||||
// TODO: Test it cannot be read from.
|
||||
25
test/io/file/create_block.wren
Normal file
25
test/io/file/create_block.wren
Normal file
@ -0,0 +1,25 @@
|
||||
import "io" for File
|
||||
|
||||
// Create a new file.
|
||||
var f
|
||||
File.create("file.temp") {|file|
|
||||
f = file
|
||||
System.print(file.isOpen) // expect: true
|
||||
System.print(file.size) // expect: 0
|
||||
|
||||
file.writeBytes("stuff")
|
||||
}
|
||||
|
||||
// Closed on block exit.
|
||||
System.print(f.isOpen) // expect: false
|
||||
|
||||
System.print(File.size("file.temp")) // expect: 5
|
||||
|
||||
// Overwrite a file.
|
||||
File.create("file.temp") {|file|
|
||||
// Truncates.
|
||||
System.print(file.size) // expect: 0
|
||||
}
|
||||
|
||||
File.delete("file.temp")
|
||||
// TODO: Test return value.
|
||||
13
test/io/file/delete.wren
Normal file
13
test/io/file/delete.wren
Normal file
@ -0,0 +1,13 @@
|
||||
import "io" for File, Stat
|
||||
|
||||
// Create a file.
|
||||
var file = File.create("file.temp")
|
||||
file.close()
|
||||
|
||||
File.delete("file.temp")
|
||||
|
||||
// TODO: More graceful API to tell if a file exists.
|
||||
var error = Fiber.new {
|
||||
Stat.path("file.temp")
|
||||
}.try()
|
||||
System.print(error) // expect: no such file or directory
|
||||
3
test/io/file/delete_nonexistent.wren
Normal file
3
test/io/file/delete_nonexistent.wren
Normal file
@ -0,0 +1,3 @@
|
||||
import "io" for File
|
||||
|
||||
File.delete("nonexistent") // expect runtime error: no such file or directory
|
||||
14
test/io/file/delete_open.wren
Normal file
14
test/io/file/delete_open.wren
Normal file
@ -0,0 +1,14 @@
|
||||
import "io" for File, Stat
|
||||
|
||||
// Create a file and don't close it.
|
||||
var file = File.create("file.temp")
|
||||
|
||||
File.delete("file.temp")
|
||||
|
||||
file.close()
|
||||
|
||||
// TODO: More graceful API to tell if a file exists.
|
||||
var error = Fiber.new {
|
||||
Stat.path("file.temp")
|
||||
}.try()
|
||||
System.print(error) // expect: no such file or directory
|
||||
@ -14,3 +14,4 @@ System.print(stash.isOpen) // expect: false
|
||||
System.print(File.open("test/io/file/open_block.wren") {|file|}) // expect: null
|
||||
|
||||
// TODO: Test a fiber aborting inside the block.
|
||||
// TODO: Test return value.
|
||||
|
||||
12
test/io/file/write_bytes.wren
Normal file
12
test/io/file/write_bytes.wren
Normal file
@ -0,0 +1,12 @@
|
||||
import "io" for File
|
||||
|
||||
File.create("file.temp") {|file|
|
||||
// Appends.
|
||||
file.writeBytes("one")
|
||||
file.writeBytes("two")
|
||||
file.writeBytes("three")
|
||||
}
|
||||
|
||||
System.print(File.read("file.temp")) // expect: onetwothree
|
||||
|
||||
File.delete("file.temp")
|
||||
10
test/io/file/write_bytes_after_close.wren
Normal file
10
test/io/file/write_bytes_after_close.wren
Normal file
@ -0,0 +1,10 @@
|
||||
import "io" for File
|
||||
|
||||
var file = File.create("file.temp")
|
||||
file.close()
|
||||
|
||||
System.print(Fiber.new {
|
||||
file.writeBytes("one")
|
||||
}.try()) // expect: File is not open.
|
||||
|
||||
File.delete("file.temp")
|
||||
9
test/io/file/write_bytes_from_bytes_not_string.wren
Normal file
9
test/io/file/write_bytes_from_bytes_not_string.wren
Normal file
@ -0,0 +1,9 @@
|
||||
import "io" for File
|
||||
|
||||
System.print(Fiber.new {
|
||||
File.create("file.temp") {|file|
|
||||
file.writeBytes(123, 0)
|
||||
}
|
||||
}.try()) // expect: Bytes must be a string.
|
||||
|
||||
File.delete("file.temp")
|
||||
11
test/io/file/write_bytes_from_offset.wren
Normal file
11
test/io/file/write_bytes_from_offset.wren
Normal file
@ -0,0 +1,11 @@
|
||||
import "io" for File
|
||||
|
||||
File.create("file.temp") {|file|
|
||||
file.writeBytes("prettyshort", 0)
|
||||
file.writeBytes("agoodbitlonger", 2)
|
||||
file.writeBytes("short", 3)
|
||||
}
|
||||
|
||||
System.print(File.read("file.temp")) // expect: prashortitlonger
|
||||
|
||||
File.delete("file.temp")
|
||||
9
test/io/file/write_bytes_from_offset_negative.wren
Normal file
9
test/io/file/write_bytes_from_offset_negative.wren
Normal file
@ -0,0 +1,9 @@
|
||||
import "io" for File
|
||||
|
||||
System.print(Fiber.new {
|
||||
File.create("file.temp") {|file|
|
||||
file.writeBytes("", -1)
|
||||
}
|
||||
}.try()) // expect: Offset cannot be negative.
|
||||
|
||||
File.delete("file.temp")
|
||||
9
test/io/file/write_bytes_from_offset_not_integer.wren
Normal file
9
test/io/file/write_bytes_from_offset_not_integer.wren
Normal file
@ -0,0 +1,9 @@
|
||||
import "io" for File
|
||||
|
||||
System.print(Fiber.new {
|
||||
File.create("file.temp") {|file|
|
||||
file.writeBytes("", 1.2)
|
||||
}
|
||||
}.try()) // expect: Offset must be an integer.
|
||||
|
||||
File.delete("file.temp")
|
||||
9
test/io/file/write_bytes_from_offset_not_num.wren
Normal file
9
test/io/file/write_bytes_from_offset_not_num.wren
Normal file
@ -0,0 +1,9 @@
|
||||
import "io" for File
|
||||
|
||||
System.print(Fiber.new {
|
||||
File.create("file.temp") {|file|
|
||||
file.writeBytes("", "string")
|
||||
}
|
||||
}.try()) // expect: Offset must be an integer.
|
||||
|
||||
File.delete("file.temp")
|
||||
9
test/io/file/write_bytes_not_string.wren
Normal file
9
test/io/file/write_bytes_not_string.wren
Normal file
@ -0,0 +1,9 @@
|
||||
import "io" for File
|
||||
|
||||
System.print(Fiber.new {
|
||||
File.create("file.temp") {|file|
|
||||
file.writeBytes(123)
|
||||
}
|
||||
}.try()) // expect: Bytes must be a string.
|
||||
|
||||
File.delete("file.temp")
|
||||
Reference in New Issue
Block a user