More buildins and vscode exstention <CO-AUthored, ai>

This commit is contained in:
allexanderbergmns
2026-08-25 16:20:11 +02:00
parent 5daadce7a9
commit 7ed774089e
27 changed files with 2080 additions and 122 deletions
+102
View File
@@ -0,0 +1,102 @@
// stdlib.vr — tests for the extended stdlib modules (regex, crypto, csv,
// path/process helpers, date arithmetic). Run with `vr test examples/stdlib.vr`.
import regex
import crypto
import csv
import os
import time
import http
fn test_regex() {
assert regex.is_match(r"^[a-z]+$", "hello") == 1
assert regex.is_match(r"^[a-z]+$", "Hello1") == 0
let nums = regex.find_all(r"\d+", "a1b22c333")
assert len(nums) == 3
assert nums[0] == "1"
assert nums[2] == "333"
assert regex.replace(r"\d+", "x1y22z", "#") == "x#y#z"
let parts = regex.split(r"[,;]", "a,b;c")
assert len(parts) == 3
assert parts[1] == "b"
// replacing a match in context (no group refs — use find_all for captures)
assert regex.replace(r"\d+", "id:42 name:bob", "#") == "id:# name:bob"
// invalid pattern throws (catchable)
try {
regex.is_match("([", "x")
assert false
} catch e {
assert len(e) > 0
}
}
fn test_crypto() {
assert crypto.sha256("hello") == "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
assert crypto.md5("hello") == "5d41402abc4b2a76b9719d911017c592"
let enc = crypto.base64_encode("hello")
assert enc == "aGVsbG8="
assert crypto.base64_decode(enc) == "hello"
// hashes differ across inputs
assert crypto.sha256("a") != crypto.sha256("b")
}
fn test_csv() {
let rows = csv.parse("name,age\n\"amy, smith\",30\nbob,41")
assert len(rows) == 3
assert rows[0][0] == "name"
assert rows[1][0] == "amy, smith"
assert rows[2][1] == "41"
// empty input yields no rows
assert len(csv.parse("")) == 0
}
fn test_path_helpers() {
assert os.ext("a/b/file.txt") == ".txt"
assert os.ext("noext") == ""
assert os.base("a/b/c.vr") == "c.vr"
assert os.dir("a/b/c.vr") == "a/b"
assert os.rel("a/b/c.txt", "a") == "b/c.txt"
assert os.rel("a/b/c.txt", "a/b/c.txt") == "."
let abs_p = os.abs("main.v")
assert abs_p != "main.v"
assert len(abs_p) > 0
}
fn test_exec_full() {
let res = os.exec_full("echo hello-out; echo hello-err 1>&2")
assert res.code == 0
assert res.stdout == "hello-out\n"
assert res.stderr == "hello-err\n"
let bad = os.exec_full("exit 3")
assert bad.code == 3
}
fn test_time_arithmetic() {
let t = time.now()
assert time.add_days(t, 1) - t == 86400
assert time.add_hours(t, 2) - t == 7200
assert time.add_minutes(t, 5) - t == 300
assert time.add_seconds(t, 10) - t == 10
// 2026-08-25 was a Tuesday
let fixed = time.parse("2026-08-25 12:00:00")
assert time.weekday(fixed) == "Tue"
let later = time.add_days(fixed, 1)
assert time.date(later) == "2026-08-26"
}
fn test_http_headers_timeout() {
// offline-safe: connecting to a closed local port fails fast
try {
let h = { "X-Test": "1" }
http.request("GET", "http://127.0.0.1:1/nope", "", h, 500)
assert false
} catch e {
assert len(e) > 0
}
try {
http.get_timeout("http://127.0.0.1:1/nope", 500)
assert false
} catch e {
assert len(e) > 0
}
}
+74 -1
View File
@@ -319,7 +319,7 @@ fn test_module_imports() {
}
assert os.base("a/b/c.vr") == "c.vr"
assert os.dir("a/b/c.vr") == "a/b"
assert len(os.glob("lib/*.vr")) == 6
assert len(os.glob("lib/*.vr")) == 9
let tmp = os.join(os.cwd(), "tmp_test.txt")
os.write_lines(tmp, ["x", "y"])
assert len(os.read_lines(tmp)) == 2
@@ -455,6 +455,79 @@ fn test_time() {
}
}
fn test_closure_capture() {
// closures capture enclosing locals by value
let factor = 3
let dbl = fn(x) {
return x * factor
}
assert dbl(7) == 21
let name = "alice"
let age = 30
let greet = fn() {
return "hi " + name + " age=" + str(age)
}
assert greet() == "hi alice age=30"
// capture by value: later changes to the outer var don't affect the closure
let counter = 10
let read = fn() {
return counter
}
assert read() == 10
counter = 99
assert read() == 10
// nested closures capture through both levels
let base = 100
let outer = fn() {
let mid = 5
let inner = fn(x) {
return base + mid + x
}
return inner(1)
}
assert outer() == 106
// assignment to a captured variable writes the closure's own copy
let n = 1
let bump = fn() {
n = n + 1
return n
}
assert bump() == 2
assert n == 1
// closure stored in a struct, called dynamically
let handler = fn() {
return "handled"
}
let rec = { run: handler }
assert rec.run() == "handled"
}
fn test_dynamic_map_keys() {
// map keys may be computed expressions, not just string literals
let m = {}
for i in 0..3 {
m["key" + str(i)] = i * 10
}
assert m["key0"] == 0
assert m["key2"] == 20
let k = "key1"
assert m[k] == 10
assert has(m, "key1") == 1
assert len(m) == 3
delete(m, "key1")
assert has(m, "key1") == 0
assert len(m) == 2
// nested computed access
let cfg = { "db": { "host": "localhost", "port": 5432 } }
let which = "db"
assert cfg[which]["host"] == "localhost"
}
fn test_failing() {
// this one is meant to fail — shows up in `vr test` output
assert 1 == 2