feat: add async/await keywords, Num/String extensions, and Jinja markdown example
Add `async` and `await` token support to the Wren compiler with scheduler resolution logic, extend `Num` with `e` constant, hyperbolic trig, base conversion, and new methods (`isZero`, `gcd`, `lcm`, `digits`, etc.), extend `String` with `lower`, `upper`, `capitalize`, `title`, and regenerate `wren_core.wren.inc`. Include `Makefile` for multi-platform builds, rewrite `README.md` with updated build instructions, and add `example/await_demo.wren` and `example/jinja_markdown.wren` demonstrating new features.
This commit is contained in:
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-l", {"long": "--level", "choices": ["debug", "info", "warn", "error"]})
|
||||
|
||||
var args = parser.parseArgs(["-l", "info"])
|
||||
System.print(args["level"]) // expect: info
|
||||
|
||||
var args2 = parser.parseArgs(["--level", "error"])
|
||||
System.print(args2["level"]) // expect: error
|
||||
|
||||
var args3 = parser.parseArgs(["--level", "debug"])
|
||||
System.print(args3["level"]) // expect: debug
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-l", {"choices": ["debug", "info", "warn", "error"]})
|
||||
|
||||
var args = parser.parseArgs(["-l", "trace"]) // expect runtime error: Invalid choice 'trace' for -l
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-o", {"required": true})
|
||||
|
||||
var args = parser.parseArgs([]) // expect runtime error: Missing required option: -o
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("filename")
|
||||
|
||||
var args = parser.parseArgs([]) // expect runtime error: Missing required argument: filename
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-f", {"nargs": "+"})
|
||||
|
||||
var args = parser.parseArgs(["-f"]) // expect runtime error: Option -f requires at least one value
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
|
||||
var args = parser.parseArgs(["extra"]) // expect runtime error: Unexpected argument: extra
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
|
||||
var args = parser.parseArgs(["--unknown"]) // expect runtime error: Unknown option: --unknown
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new("A test program")
|
||||
parser.prog = "myapp"
|
||||
parser.addArgument("input", {"help": "Input file path"})
|
||||
parser.addArgument("-o", {"long": "--output", "help": "Output file path"})
|
||||
parser.addArgument("-v", {"long": "--verbose", "action": "storeTrue", "help": "Enable verbose mode"})
|
||||
|
||||
parser.printHelp()
|
||||
// expect: usage: myapp [options] input
|
||||
// expect:
|
||||
// expect: A test program
|
||||
// expect:
|
||||
// expect: positional arguments:
|
||||
// expect: input Input file path
|
||||
// expect:
|
||||
// expect: optional arguments:
|
||||
// expect: -o, --output Output file path
|
||||
// expect: -v, --verbose Enable verbose mode
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("input")
|
||||
parser.addArgument("-o", {"long": "--output", "default": "out.txt"})
|
||||
parser.addArgument("-v", {"action": "storeTrue"})
|
||||
parser.addArgument("-n", {"type": "int", "default": 1})
|
||||
|
||||
var args = parser.parseArgs(["data.csv", "-o", "result.json", "-v", "-n", "10"])
|
||||
System.print(args["input"]) // expect: data.csv
|
||||
System.print(args["output"]) // expect: result.json
|
||||
System.print(args["v"]) // expect: true
|
||||
System.print(args["n"]) // expect: 10
|
||||
|
||||
var args2 = parser.parseArgs(["data.csv"])
|
||||
System.print(args2["input"]) // expect: data.csv
|
||||
System.print(args2["output"]) // expect: out.txt
|
||||
System.print(args2["v"]) // expect: false
|
||||
System.print(args2["n"]) // expect: 1
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-p", {"long": "--point", "nargs": 2, "type": "int"})
|
||||
|
||||
var args = parser.parseArgs(["-p", "10", "20"])
|
||||
System.print(args["point"].count) // expect: 2
|
||||
System.print(args["point"][0]) // expect: 10
|
||||
System.print(args["point"][1]) // expect: 20
|
||||
|
||||
var args2 = parser.parseArgs(["--point", "5", "15"])
|
||||
System.print(args2["point"].count) // expect: 2
|
||||
System.print(args2["point"][0]) // expect: 5
|
||||
System.print(args2["point"][1]) // expect: 15
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-f", {"long": "--files", "nargs": "+"})
|
||||
|
||||
var args = parser.parseArgs(["-f", "a.txt", "b.txt"])
|
||||
System.print(args["files"].count) // expect: 2
|
||||
System.print(args["files"][0]) // expect: a.txt
|
||||
System.print(args["files"][1]) // expect: b.txt
|
||||
|
||||
var args2 = parser.parseArgs(["--files", "single.txt"])
|
||||
System.print(args2["files"].count) // expect: 1
|
||||
System.print(args2["files"][0]) // expect: single.txt
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("files", {"nargs": "*", "required": false})
|
||||
|
||||
var args = parser.parseArgs(["a.txt", "b.txt", "c.txt"])
|
||||
System.print(args["files"].count) // expect: 3
|
||||
System.print(args["files"][0]) // expect: a.txt
|
||||
System.print(args["files"][1]) // expect: b.txt
|
||||
System.print(args["files"][2]) // expect: c.txt
|
||||
|
||||
var parser2 = ArgumentParser.new()
|
||||
parser2.addArgument("files", {"nargs": "+"})
|
||||
|
||||
var args2 = parser2.parseArgs(["x.txt", "y.txt"])
|
||||
System.print(args2["files"].count) // expect: 2
|
||||
System.print(args2["files"][0]) // expect: x.txt
|
||||
System.print(args2["files"][1]) // expect: y.txt
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "argparse" for ArgumentParser
|
||||
|
||||
var parser = ArgumentParser.new()
|
||||
parser.addArgument("-f", {"long": "--files", "nargs": "*"})
|
||||
|
||||
var args = parser.parseArgs(["-f", "a.txt", "b.txt", "c.txt"])
|
||||
System.print(args["files"].count) // expect: 3
|
||||
System.print(args["files"][0]) // expect: a.txt
|
||||
System.print(args["files"][1]) // expect: b.txt
|
||||
System.print(args["files"][2]) // expect: c.txt
|
||||
|
||||
var args2 = parser.parseArgs(["--files"])
|
||||
System.print(args2["files"].count) // expect: 0
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import "scheduler" for Scheduler, Future
|
||||
import "timer" for Timer
|
||||
|
||||
var task = async { "ready" }
|
||||
|
||||
await Timer.sleep(10)
|
||||
System.print(task.isDone) // expect: true
|
||||
System.print(await task) // expect: ready
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
import "scheduler" for Scheduler, Future
|
||||
import "timer" for Timer
|
||||
|
||||
var task = async { 42 }
|
||||
|
||||
var result = await task
|
||||
System.print(result) // expect: 42
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import "scheduler" for Scheduler, Future
|
||||
import "timer" for Timer
|
||||
|
||||
var order = []
|
||||
|
||||
var task1 = async {
|
||||
await Timer.sleep(2)
|
||||
order.add("task1")
|
||||
}
|
||||
|
||||
var task2 = async {
|
||||
await Timer.sleep(1)
|
||||
order.add("task2")
|
||||
}
|
||||
|
||||
await task1
|
||||
await task2
|
||||
System.print(order) // expect: [task2, task1]
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
import "scheduler" for Scheduler
|
||||
import "timer" for Timer
|
||||
|
||||
await Timer.sleep(1)
|
||||
System.print("done") // expect: done
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
import "scheduler" for Scheduler
|
||||
import "timer" for Timer
|
||||
|
||||
class Helper {
|
||||
static run() {
|
||||
await Timer.sleep(1)
|
||||
return "completed"
|
||||
}
|
||||
}
|
||||
|
||||
System.print(Helper.run()) // expect: completed
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import "scheduler" for Scheduler
|
||||
import "timer" for Timer
|
||||
|
||||
var result = await Timer.sleep(1)
|
||||
System.print(result == null) // expect: true
|
||||
System.print("done") // expect: done
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print("apple" < "banana") // expect: true
|
||||
System.print("banana" > "apple") // expect: true
|
||||
System.print("abc" <= "abc") // expect: true
|
||||
System.print("abc" >= "abc") // expect: true
|
||||
|
||||
System.print("abc" < "abd") // expect: true
|
||||
System.print("abd" > "abc") // expect: true
|
||||
System.print("abd" < "abc") // expect: false
|
||||
System.print("abc" > "abd") // expect: false
|
||||
|
||||
System.print("abc" < "abcd") // expect: true
|
||||
System.print("abcd" > "abc") // expect: true
|
||||
System.print("abcd" < "abc") // expect: false
|
||||
System.print("abc" > "abcd") // expect: false
|
||||
|
||||
System.print("a" <= "b") // expect: true
|
||||
System.print("b" >= "a") // expect: true
|
||||
System.print("b" <= "a") // expect: false
|
||||
System.print("a" >= "b") // expect: false
|
||||
|
||||
System.print("b" < "a") // expect: false
|
||||
System.print("a" > "b") // expect: false
|
||||
|
||||
System.print("" < "a") // expect: true
|
||||
System.print("a" > "") // expect: true
|
||||
System.print("" <= "") // expect: true
|
||||
System.print("" >= "") // expect: true
|
||||
System.print("" < "") // expect: false
|
||||
System.print("" > "") // expect: false
|
||||
|
||||
System.print("a" < "a") // expect: false
|
||||
System.print("a" > "a") // expect: false
|
||||
System.print("a" <= "a") // expect: true
|
||||
System.print("a" >= "a") // expect: true
|
||||
|
||||
System.print("A" < "a") // expect: true
|
||||
System.print("a" > "A") // expect: true
|
||||
System.print("Z" < "a") // expect: true
|
||||
System.print("a" > "Z") // expect: true
|
||||
|
||||
System.print("abc" < "abc") // expect: false
|
||||
System.print("abc" > "abc") // expect: false
|
||||
System.print("abc" <= "abc") // expect: true
|
||||
System.print("abc" >= "abc") // expect: true
|
||||
|
||||
System.print("xyz" < "xya") // expect: false
|
||||
System.print("xyz" > "xya") // expect: true
|
||||
System.print("xyz" <= "xya") // expect: false
|
||||
System.print("xyz" >= "xya") // expect: true
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "jinja" for Environment, DictLoader
|
||||
|
||||
var env = Environment.new(DictLoader.new({
|
||||
"md_heading": "{\% markdowntohtml \%}# Hello{\% endmarkdowntohtml \%}",
|
||||
"md_bold": "{\% markdowntohtml \%}**bold**{\% endmarkdowntohtml \%}",
|
||||
"md_var": "{\% markdowntohtml \%}# {{ title }}{\% endmarkdowntohtml \%}",
|
||||
"html_to_md": "{\% markdownfromhtml \%}<h1>Hello</h1>{\% endmarkdownfromhtml \%}",
|
||||
"html_to_md_bold": "{\% markdownfromhtml \%}<p><strong>bold</strong></p>{\% endmarkdownfromhtml \%}",
|
||||
"filter_md": "{{ text|markdown }}",
|
||||
"filter_fromhtml": "{{ html|markdownfromhtml }}"
|
||||
}))
|
||||
|
||||
System.print(env.getTemplate("md_heading").render({})) // expect: <h1>Hello</h1>
|
||||
System.print(env.getTemplate("md_bold").render({})) // expect: <p><strong>bold</strong></p>
|
||||
System.print(env.getTemplate("md_var").render({"title": "World"})) // expect: <h1>World</h1>
|
||||
System.print(env.getTemplate("html_to_md").render({})) // expect: # Hello
|
||||
System.print(env.getTemplate("html_to_md_bold").render({})) // expect: **bold**
|
||||
System.print(env.getTemplate("filter_md").render({"text": "**strong**"})) // expect: <p><strong>strong</strong></p>
|
||||
System.print(env.getTemplate("filter_fromhtml").render({"html": "<h1>Title</h1>"})) // expect: # Title
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(Num.pi.toDegrees) // expect: 180
|
||||
System.print(180.toRadians == Num.pi) // expect: true
|
||||
System.print(0.toDegrees) // expect: 0
|
||||
System.print(0.toRadians) // expect: 0
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(Num.e > 2.718) // expect: true
|
||||
System.print(Num.e < 2.719) // expect: true
|
||||
System.print(1.log == 0) // expect: true
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(65.toChar) // expect: A
|
||||
System.print(97.toChar) // expect: a
|
||||
System.print(48.toChar) // expect: 0
|
||||
|
||||
System.print(255.toBase(16)) // expect: ff
|
||||
System.print(255.toBase(2)) // expect: 11111111
|
||||
System.print(255.toBase(8)) // expect: 377
|
||||
System.print(0.toBase(16)) // expect: 0
|
||||
System.print((-42).toBase(10)) // expect: -42
|
||||
System.print(35.toBase(36)) // expect: z
|
||||
|
||||
System.print(255.toHex) // expect: ff
|
||||
System.print(10.toBinary) // expect: 1010
|
||||
System.print(8.toOctal) // expect: 10
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(3.14159.format(2)) // expect: 3.14
|
||||
System.print(3.14159.format(0)) // expect: 3
|
||||
System.print(42.format(3)) // expect: 42.000
|
||||
System.print((-1.5).format(1)) // expect: -1.5
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(12.gcd(8)) // expect: 4
|
||||
System.print(54.gcd(24)) // expect: 6
|
||||
System.print(7.gcd(13)) // expect: 1
|
||||
System.print(0.gcd(5)) // expect: 5
|
||||
System.print((-12).gcd(8)) // expect: 4
|
||||
|
||||
System.print(4.lcm(6)) // expect: 12
|
||||
System.print(3.lcm(5)) // expect: 15
|
||||
System.print(0.lcm(5)) // expect: 0
|
||||
System.print(0.lcm(0)) // expect: 0
|
||||
|
||||
System.print(123.digits) // expect: [1, 2, 3]
|
||||
System.print(0.digits) // expect: [0]
|
||||
System.print((-456).digits) // expect: [4, 5, 6]
|
||||
System.print(9.digits) // expect: [9]
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(100.log10) // expect: 2
|
||||
System.print(1000.log10) // expect: 3
|
||||
System.print(1.log10) // expect: 0
|
||||
|
||||
System.print(0.sinh) // expect: 0
|
||||
System.print(0.cosh) // expect: 1
|
||||
System.print(0.tanh) // expect: 0
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print(0.isZero) // expect: true
|
||||
System.print(1.isZero) // expect: false
|
||||
System.print((-1).isZero) // expect: false
|
||||
|
||||
System.print(5.isPositive) // expect: true
|
||||
System.print(0.isPositive) // expect: false
|
||||
System.print((-3).isPositive) // expect: false
|
||||
|
||||
System.print((-7).isNegative) // expect: true
|
||||
System.print(0.isNegative) // expect: false
|
||||
System.print(3.isNegative) // expect: false
|
||||
|
||||
System.print(42.isFinite) // expect: true
|
||||
System.print(0.isFinite) // expect: true
|
||||
System.print((1/0).isFinite) // expect: false
|
||||
System.print((0/0).isFinite) // expect: false
|
||||
|
||||
System.print(4.isEven) // expect: true
|
||||
System.print(3.isEven) // expect: false
|
||||
System.print(0.isEven) // expect: true
|
||||
System.print(1.5.isEven) // expect: false
|
||||
|
||||
System.print(3.isOdd) // expect: true
|
||||
System.print(4.isOdd) // expect: false
|
||||
System.print(0.isOdd) // expect: false
|
||||
System.print(1.5.isOdd) // expect: false
|
||||
|
||||
System.print(5.isBetween(1, 10)) // expect: true
|
||||
System.print(1.isBetween(1, 10)) // expect: true
|
||||
System.print(10.isBetween(1, 10)) // expect: true
|
||||
System.print(0.isBetween(1, 10)) // expect: false
|
||||
System.print(11.isBetween(1, 10)) // expect: false
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var dir = Path.new("/tmp/wren_pathlib_test_copy")
|
||||
if (dir.exists()) dir.rmtree()
|
||||
dir.mkdir()
|
||||
|
||||
var src = dir / "original.txt"
|
||||
src.writeText("copy me")
|
||||
|
||||
var dst = dir / "copied.txt"
|
||||
src.copyfile(dst)
|
||||
|
||||
System.print(dst.exists()) // expect: true
|
||||
System.print(dst.readText()) // expect: copy me
|
||||
|
||||
dir.rmtree()
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var cwd = Path.cwd
|
||||
System.print(cwd.isAbsolute) // expect: true
|
||||
System.print(cwd.exists()) // expect: true
|
||||
System.print(cwd.isDir()) // expect: true
|
||||
|
||||
var home = Path.home
|
||||
System.print(home.isAbsolute) // expect: true
|
||||
System.print(home.exists()) // expect: true
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var p1 = Path.new("/home/user")
|
||||
var p2 = Path.new("/home/user")
|
||||
var p3 = Path.new("/home/other")
|
||||
|
||||
System.print(p1 == p2) // expect: true
|
||||
System.print(p1 != p3) // expect: true
|
||||
System.print(p1 == p3) // expect: false
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var p1 = Path.new("/tmp")
|
||||
System.print(p1.exists()) // expect: true
|
||||
System.print(p1.isDir()) // expect: true
|
||||
System.print(p1.isFile()) // expect: false
|
||||
|
||||
var p2 = Path.new("/nonexistent_path_xyz_12345")
|
||||
System.print(p2.exists()) // expect: false
|
||||
System.print(p2.isDir()) // expect: false
|
||||
System.print(p2.isFile()) // expect: false
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var home = Path.home
|
||||
var expanded = Path.new("~").expanduser()
|
||||
System.print(expanded == home) // expect: true
|
||||
|
||||
var subpath = Path.new("~/docs").expanduser()
|
||||
var expected = home / "docs"
|
||||
System.print(subpath == expected) // expect: true
|
||||
|
||||
var regular = Path.new("/usr/local").expanduser()
|
||||
System.print(regular) // expect: /usr/local
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var base = Path.new("/tmp/wren_pathlib_test_glob")
|
||||
if (base.exists()) base.rmtree()
|
||||
base.mkdir(true)
|
||||
|
||||
(base / "a.txt").writeText("a")
|
||||
(base / "b.txt").writeText("b")
|
||||
(base / "c.md").writeText("c")
|
||||
var sub = base / "sub"
|
||||
sub.mkdir()
|
||||
(sub / "d.txt").writeText("d")
|
||||
|
||||
var txtFiles = base.glob("*.txt")
|
||||
System.print(txtFiles.count) // expect: 2
|
||||
|
||||
var allTxt = base.rglob("*.txt")
|
||||
System.print(allTxt.count) // expect: 3
|
||||
|
||||
base.rmtree()
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var dir = Path.new("/tmp/wren_pathlib_test_iterdir")
|
||||
if (dir.exists()) dir.rmtree()
|
||||
dir.mkdir()
|
||||
|
||||
(dir / "a.txt").writeText("a")
|
||||
(dir / "b.txt").writeText("b")
|
||||
(dir / "sub").mkdir()
|
||||
|
||||
var entries = dir.iterdir()
|
||||
System.print(entries.count) // expect: 3
|
||||
|
||||
var names = {}
|
||||
for (e in entries) {
|
||||
names[e.name] = true
|
||||
}
|
||||
System.print(names.containsKey("a.txt")) // expect: true
|
||||
System.print(names.containsKey("b.txt")) // expect: true
|
||||
System.print(names.containsKey("sub")) // expect: true
|
||||
|
||||
dir.rmtree()
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var p1 = Path.new("/home")
|
||||
var p2 = p1.joinpath("user")
|
||||
System.print(p2) // expect: /home/user
|
||||
|
||||
var p3 = p1 / "user" / "docs"
|
||||
System.print(p3) // expect: /home/user/docs
|
||||
|
||||
var p4 = p1.joinpath("/absolute")
|
||||
System.print(p4) // expect: /absolute
|
||||
|
||||
var p5 = Path.new("").joinpath("file.txt")
|
||||
System.print(p5) // expect: file.txt
|
||||
|
||||
var p6 = Path.new("/home/").joinpath("user")
|
||||
System.print(p6) // expect: /home/user
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var p1 = Path.new("/home/user/file.txt")
|
||||
System.print(p1.match("*.txt")) // expect: true
|
||||
System.print(p1.match("*.py")) // expect: false
|
||||
System.print(p1.match("file.*")) // expect: true
|
||||
System.print(p1.match("f???.*")) // expect: true
|
||||
System.print(p1.match("*")) // expect: true
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var base = Path.new("/tmp/wren_pathlib_test_mkdir")
|
||||
if (base.exists()) base.rmtree()
|
||||
|
||||
var nested = base / "a" / "b" / "c"
|
||||
nested.mkdir(true)
|
||||
System.print(nested.exists()) // expect: true
|
||||
System.print(nested.isDir()) // expect: true
|
||||
|
||||
base.rmtree()
|
||||
System.print(base.exists()) // expect: false
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var dir = Path.new("/tmp/wren_pathlib_test_rw")
|
||||
if (!dir.exists()) dir.mkdir()
|
||||
|
||||
var f = dir / "test.txt"
|
||||
f.writeText("hello pathlib")
|
||||
System.print(f.readText()) // expect: hello pathlib
|
||||
System.print(f.exists()) // expect: true
|
||||
System.print(f.isFile()) // expect: true
|
||||
|
||||
f.unlink()
|
||||
System.print(f.exists()) // expect: false
|
||||
|
||||
dir.rmdir()
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var p1 = Path.new("/home/user/docs/file.txt")
|
||||
System.print(p1.relativeTo("/home/user")) // expect: docs/file.txt
|
||||
System.print(p1.relativeTo("/home")) // expect: user/docs/file.txt
|
||||
|
||||
var p2 = Path.new("/home/user")
|
||||
System.print(p2.relativeTo("/home/user")) // expect: .
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var dir = Path.new("/tmp/wren_pathlib_test_rename")
|
||||
if (dir.exists()) dir.rmtree()
|
||||
dir.mkdir()
|
||||
|
||||
var src = dir / "source.txt"
|
||||
src.writeText("content")
|
||||
System.print(src.exists()) // expect: true
|
||||
|
||||
var dst = dir / "dest.txt"
|
||||
src.rename(dst)
|
||||
System.print(src.exists()) // expect: false
|
||||
System.print(dst.exists()) // expect: true
|
||||
System.print(dst.readText()) // expect: content
|
||||
|
||||
dir.rmtree()
|
||||
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var base = Path.new("/tmp/wren_pathlib_test_walk")
|
||||
if (base.exists()) base.rmtree()
|
||||
base.mkdir()
|
||||
|
||||
(base / "file1.txt").writeText("a")
|
||||
var sub = base / "subdir"
|
||||
sub.mkdir()
|
||||
(sub / "file2.txt").writeText("b")
|
||||
|
||||
var entries = base.walk()
|
||||
System.print(entries.count) // expect: 2
|
||||
|
||||
var root1 = entries[0][0]
|
||||
var dirs1 = entries[0][1]
|
||||
var files1 = entries[0][2]
|
||||
System.print(dirs1) // expect: [subdir]
|
||||
System.print(files1) // expect: [file1.txt]
|
||||
|
||||
var dirs2 = entries[1][1]
|
||||
var files2 = entries[1][2]
|
||||
System.print(dirs2) // expect: []
|
||||
System.print(files2) // expect: [file2.txt]
|
||||
|
||||
base.rmtree()
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "pathlib" for Path
|
||||
|
||||
var p1 = Path.new("/home/user/file.txt")
|
||||
|
||||
var p2 = p1.withName("other.md")
|
||||
System.print(p2) // expect: /home/user/other.md
|
||||
|
||||
var p3 = p1.withStem("archive")
|
||||
System.print(p3) // expect: /home/user/archive.txt
|
||||
|
||||
var p4 = p1.withSuffix(".bak")
|
||||
System.print(p4) // expect: /home/user/file.bak
|
||||
|
||||
var p5 = Path.new("/data/file.tar.gz").withSuffix(".zip")
|
||||
System.print(p5) // expect: /data/file.tar.zip
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print("Hello World".lower) // expect: hello world
|
||||
System.print("HELLO".lower) // expect: hello
|
||||
System.print("hello".lower) // expect: hello
|
||||
System.print("".lower) // expect:
|
||||
System.print("123!@#".lower) // expect: 123!@#
|
||||
|
||||
System.print("Hello World".upper) // expect: HELLO WORLD
|
||||
System.print("hello".upper) // expect: HELLO
|
||||
System.print("HELLO".upper) // expect: HELLO
|
||||
System.print("".upper) // expect:
|
||||
System.print("123!@#".upper) // expect: 123!@#
|
||||
|
||||
System.print("hello world".capitalize) // expect: Hello world
|
||||
System.print("HELLO".capitalize) // expect: Hello
|
||||
System.print("h".capitalize) // expect: H
|
||||
System.print("".capitalize) // expect:
|
||||
System.print("already Capitalized".capitalize) // expect: Already capitalized
|
||||
|
||||
System.print("hello world".title) // expect: Hello World
|
||||
System.print("HELLO WORLD".title) // expect: Hello World
|
||||
System.print("hello".title) // expect: Hello
|
||||
System.print("".title) // expect:
|
||||
System.print("multi\tword\ntest".title) // expect: Multi Word
|
||||
// expect: Test
|
||||
|
||||
System.print("Hello World".swapCase) // expect: hELLO wORLD
|
||||
System.print("hello".swapCase) // expect: HELLO
|
||||
System.print("HELLO".swapCase) // expect: hello
|
||||
System.print("".swapCase) // expect:
|
||||
System.print("123".swapCase) // expect: 123
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print("hello".isLower) // expect: true
|
||||
System.print("Hello".isLower) // expect: false
|
||||
System.print("HELLO".isLower) // expect: false
|
||||
System.print("123".isLower) // expect: false
|
||||
System.print("".isLower) // expect: false
|
||||
|
||||
System.print("HELLO".isUpper) // expect: true
|
||||
System.print("Hello".isUpper) // expect: false
|
||||
System.print("hello".isUpper) // expect: false
|
||||
System.print("123".isUpper) // expect: false
|
||||
System.print("".isUpper) // expect: false
|
||||
|
||||
System.print("12345".isDigit) // expect: true
|
||||
System.print("123a5".isDigit) // expect: false
|
||||
System.print("".isDigit) // expect: false
|
||||
System.print("0".isDigit) // expect: true
|
||||
|
||||
System.print("hello".isAlpha) // expect: true
|
||||
System.print("Hello".isAlpha) // expect: true
|
||||
System.print("hello1".isAlpha) // expect: false
|
||||
System.print("".isAlpha) // expect: false
|
||||
|
||||
System.print("hello123".isAlphaNumeric) // expect: true
|
||||
System.print("Hello".isAlphaNumeric) // expect: true
|
||||
System.print("12345".isAlphaNumeric) // expect: true
|
||||
System.print("hello!".isAlphaNumeric) // expect: false
|
||||
System.print("".isAlphaNumeric) // expect: false
|
||||
|
||||
System.print(" ".isSpace) // expect: true
|
||||
System.print(" \t\n".isSpace) // expect: true
|
||||
System.print("hello".isSpace) // expect: false
|
||||
System.print("".isSpace) // expect: false
|
||||
|
||||
System.print("hello".isAscii) // expect: true
|
||||
System.print("123!@#".isAscii) // expect: true
|
||||
System.print("".isAscii) // expect: true
|
||||
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print("42".toNum) // expect: 42
|
||||
System.print("3.14".toNum) // expect: 3.14
|
||||
System.print("-7".toNum) // expect: -7
|
||||
System.print("abc".toNum) // expect: null
|
||||
System.print("".toNum) // expect: null
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print("hello world hello".lastIndexOf("hello")) // expect: 12
|
||||
System.print("hello".lastIndexOf("hello")) // expect: 0
|
||||
System.print("hello".lastIndexOf("x")) // expect: -1
|
||||
System.print("aaaa".lastIndexOf("aa")) // expect: 2
|
||||
System.print("".lastIndexOf("x")) // expect: -1
|
||||
|
||||
System.print("hello world hello".lastIndexOf("hello", 11)) // expect: 0
|
||||
System.print("hello world hello".lastIndexOf("hello", 12)) // expect: 12
|
||||
System.print("hello world hello".lastIndexOf("hello", 100)) // expect: 12
|
||||
System.print("hello".lastIndexOf("x", 3)) // expect: -1
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
var lines = "line1\nline2\nline3".splitLines
|
||||
System.print(lines.count) // expect: 3
|
||||
System.print(lines[0]) // expect: line1
|
||||
System.print(lines[1]) // expect: line2
|
||||
System.print(lines[2]) // expect: line3
|
||||
|
||||
var crlfLines = "a\r\nb\r\nc".splitLines
|
||||
System.print(crlfLines.count) // expect: 3
|
||||
System.print(crlfLines[0]) // expect: a
|
||||
System.print(crlfLines[1]) // expect: b
|
||||
System.print(crlfLines[2]) // expect: c
|
||||
|
||||
var chars = "abc".chars
|
||||
System.print(chars.count) // expect: 3
|
||||
System.print(chars[0]) // expect: a
|
||||
System.print(chars[1]) // expect: b
|
||||
System.print(chars[2]) // expect: c
|
||||
|
||||
System.print("".chars.count) // expect: 0
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
System.print("hello".reverse) // expect: olleh
|
||||
System.print("".reverse) // expect:
|
||||
System.print("a".reverse) // expect: a
|
||||
System.print("abcde".reverse) // expect: edcba
|
||||
|
||||
System.print("[%("hi".center(10))]") // expect: [ hi ]
|
||||
System.print("hi".center(10, "-")) // expect: ----hi----
|
||||
System.print("hello".center(3)) // expect: hello
|
||||
System.print("x".center(5, ".")) // expect: ..x..
|
||||
|
||||
System.print("42".lpad(5, "0")) // expect: 00042
|
||||
System.print("hello".lpad(3, "x")) // expect: hello
|
||||
System.print("a".lpad(5, "-")) // expect: ----a
|
||||
|
||||
System.print("hi".rpad(5, ".")) // expect: hi...
|
||||
System.print("hello".rpad(3, "x")) // expect: hello
|
||||
System.print("a".rpad(5, "-")) // expect: a----
|
||||
|
||||
System.print("42".zfill(5)) // expect: 00042
|
||||
System.print("-42".zfill(6)) // expect: -00042
|
||||
System.print("+42".zfill(6)) // expect: +00042
|
||||
System.print("12345".zfill(3)) // expect: 12345
|
||||
|
||||
System.print("HelloWorld".removePrefix("Hello")) // expect: World
|
||||
System.print("HelloWorld".removePrefix("World")) // expect: HelloWorld
|
||||
System.print("".removePrefix("x")) // expect:
|
||||
|
||||
System.print("HelloWorld".removeSuffix("World")) // expect: Hello
|
||||
System.print("HelloWorld".removeSuffix("Hello")) // expect: HelloWorld
|
||||
System.print("".removeSuffix("x")) // expect:
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TempFile
|
||||
import "io" for Directory, File
|
||||
import "pathlib" for Path
|
||||
|
||||
var baseDir = TempFile.mkdtemp()
|
||||
|
||||
var path = TempFile.mkstemp("", "tmp", baseDir)
|
||||
System.print(path.startsWith(baseDir)) // expect: true
|
||||
System.print(Path.new(path).exists()) // expect: true
|
||||
File.delete(path)
|
||||
|
||||
var dir = TempFile.mkdtemp("", "tmp", baseDir)
|
||||
System.print(dir.startsWith(baseDir)) // expect: true
|
||||
System.print(Path.new(dir).isDir()) // expect: true
|
||||
Directory.delete(dir)
|
||||
|
||||
Directory.delete(baseDir)
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TempFile
|
||||
|
||||
var dir = TempFile.gettempdir()
|
||||
System.print(dir is String) // expect: true
|
||||
System.print(dir.count > 0) // expect: true
|
||||
|
||||
System.print(TempFile.gettempprefix()) // expect: tmp
|
||||
|
||||
System.print(TempFile.tempdir == null) // expect: true
|
||||
|
||||
TempFile.tempdir = "/custom/path"
|
||||
System.print(TempFile.gettempdir()) // expect: /custom/path
|
||||
|
||||
TempFile.tempdir = null
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TempFile
|
||||
import "io" for Directory
|
||||
import "pathlib" for Path
|
||||
|
||||
var dir = TempFile.mkdtemp()
|
||||
System.print(dir is String) // expect: true
|
||||
System.print(Path.new(dir).exists()) // expect: true
|
||||
System.print(Path.new(dir).isDir()) // expect: true
|
||||
|
||||
Directory.delete(dir)
|
||||
System.print(Path.new(dir).exists()) // expect: false
|
||||
|
||||
var dir2 = TempFile.mkdtemp("_end", "mydir_")
|
||||
System.print(dir2.contains("mydir_")) // expect: true
|
||||
System.print(dir2.endsWith("_end")) // expect: true
|
||||
Directory.delete(dir2)
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TempFile
|
||||
import "io" for File
|
||||
import "pathlib" for Path
|
||||
|
||||
var path = TempFile.mkstemp()
|
||||
System.print(path is String) // expect: true
|
||||
System.print(Path.new(path).exists()) // expect: true
|
||||
|
||||
File.delete(path)
|
||||
System.print(Path.new(path).exists()) // expect: false
|
||||
|
||||
var path2 = TempFile.mkstemp(".txt")
|
||||
System.print(path2.endsWith(".txt")) // expect: true
|
||||
File.delete(path2)
|
||||
|
||||
var path3 = TempFile.mkstemp(".dat", "test_")
|
||||
System.print(path3.contains("test_")) // expect: true
|
||||
System.print(path3.endsWith(".dat")) // expect: true
|
||||
File.delete(path3)
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TempFile
|
||||
import "pathlib" for Path
|
||||
|
||||
var name = TempFile.mktemp()
|
||||
System.print(name is String) // expect: true
|
||||
System.print(name.count > 0) // expect: true
|
||||
System.print(Path.new(name).exists()) // expect: false
|
||||
|
||||
var name2 = TempFile.mktemp(".log", "app_")
|
||||
System.print(name2.contains("app_")) // expect: true
|
||||
System.print(name2.endsWith(".log")) // expect: true
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for NamedTemporaryFile
|
||||
import "pathlib" for Path
|
||||
|
||||
var tmp = NamedTemporaryFile.new()
|
||||
System.print(tmp.name is String) // expect: true
|
||||
System.print(Path.new(tmp.name).exists()) // expect: true
|
||||
System.print(tmp.closed) // expect: false
|
||||
System.print(tmp.delete) // expect: true
|
||||
|
||||
tmp.write("hello tempfile")
|
||||
var content = tmp.read()
|
||||
System.print(content) // expect: hello tempfile
|
||||
|
||||
tmp.close()
|
||||
System.print(tmp.closed) // expect: true
|
||||
System.print(Path.new(tmp.name).exists()) // expect: false
|
||||
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for NamedTemporaryFile
|
||||
import "io" for File
|
||||
import "pathlib" for Path
|
||||
|
||||
var tmp = NamedTemporaryFile.new("", "tmp", null, false)
|
||||
System.print(tmp.delete) // expect: false
|
||||
|
||||
tmp.write("persistent")
|
||||
tmp.close()
|
||||
System.print(tmp.closed) // expect: true
|
||||
System.print(Path.new(tmp.name).exists()) // expect: true
|
||||
|
||||
var content = File.read(tmp.name)
|
||||
System.print(content) // expect: persistent
|
||||
|
||||
File.delete(tmp.name)
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for NamedTemporaryFile
|
||||
import "pathlib" for Path
|
||||
|
||||
var savedName = null
|
||||
|
||||
NamedTemporaryFile.new().use {|tmp|
|
||||
tmp.write("inside use block")
|
||||
var content = tmp.read()
|
||||
System.print(content) // expect: inside use block
|
||||
savedName = tmp.name
|
||||
}
|
||||
|
||||
System.print(Path.new(savedName).exists()) // expect: false
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TemporaryDirectory
|
||||
import "pathlib" for Path
|
||||
|
||||
var tmp = TemporaryDirectory.new()
|
||||
System.print(tmp.name is String) // expect: true
|
||||
System.print(Path.new(tmp.name).exists()) // expect: true
|
||||
System.print(Path.new(tmp.name).isDir()) // expect: true
|
||||
System.print(tmp.closed) // expect: false
|
||||
System.print(tmp.delete) // expect: true
|
||||
|
||||
tmp.cleanup()
|
||||
System.print(tmp.closed) // expect: true
|
||||
System.print(Path.new(tmp.name).exists()) // expect: false
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "tempfile" for TemporaryDirectory
|
||||
import "io" for File, FileFlags
|
||||
import "pathlib" for Path
|
||||
|
||||
var savedName = null
|
||||
|
||||
TemporaryDirectory.new().use {|tmp|
|
||||
savedName = tmp.name
|
||||
System.print(Path.new(savedName).isDir()) // expect: true
|
||||
|
||||
var filePath = savedName + "/test.txt"
|
||||
File.create(filePath) {|f|
|
||||
f.writeBytes("hello", 0)
|
||||
}
|
||||
System.print(Path.new(filePath).exists()) // expect: true
|
||||
}
|
||||
|
||||
System.print(Path.new(savedName).exists()) // expect: false
|
||||
Vendored
+1
-1
@@ -64,6 +64,6 @@ Scheduler.add {
|
||||
ws.close()
|
||||
}
|
||||
|
||||
Timer.sleep(30000)
|
||||
Timer.sleep(10000)
|
||||
|
||||
System.print(result) // expect: ok
|
||||
|
||||
Reference in New Issue
Block a user