feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules

Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
This commit is contained in:
2026-01-24 22:40:22 +00:00
parent 0a65272f80
commit 5a4054ec66
105 changed files with 23275 additions and 94 deletions
+12
View File
@@ -0,0 +1,12 @@
// retoor <retoor@molodetz.nl>
import "argparse" for ArgumentParser
var parser = ArgumentParser.new()
parser.addArgument("-i", {"long": "--include", "action": "append"})
var args = parser.parseArgs(["-i", "foo", "-i", "bar", "--include", "baz"])
System.print(args["include"].count) // expect: 3
System.print(args["include"][0]) // expect: foo
System.print(args["include"][1]) // expect: bar
System.print(args["include"][2]) // expect: baz
+15
View File
@@ -0,0 +1,15 @@
// retoor <retoor@molodetz.nl>
import "argparse" for ArgumentParser
var parser = ArgumentParser.new()
parser.addArgument("-v", {"action": "count"})
var args = parser.parseArgs([])
System.print(args["v"]) // expect: 0
var args2 = parser.parseArgs(["-v"])
System.print(args2["v"]) // expect: 1
var args3 = parser.parseArgs(["-v", "-v", "-v"])
System.print(args3["v"]) // expect: 3
+17
View File
@@ -0,0 +1,17 @@
// retoor <retoor@molodetz.nl>
import "argparse" for ArgumentParser
var parser = ArgumentParser.new()
parser.addArgument("-v", {"long": "--verbose", "action": "storeTrue"})
parser.addArgument("-q", {"action": "storeFalse", "dest": "verbose_output"})
var args = parser.parseArgs([])
System.print(args["verbose"]) // expect: false
System.print(args["verbose_output"]) // expect: true
var args2 = parser.parseArgs(["-v"])
System.print(args2["verbose"]) // expect: true
var args3 = parser.parseArgs(["-q"])
System.print(args3["verbose_output"]) // expect: false
+18
View File
@@ -0,0 +1,18 @@
// retoor <retoor@molodetz.nl>
import "argparse" for ArgumentParser
var parser = ArgumentParser.new()
parser.addArgument("-o", {"long": "--output", "default": "out.txt"})
parser.addArgument("-n", {"type": "int", "default": 1})
var args = parser.parseArgs([])
System.print(args["output"]) // expect: out.txt
System.print(args["n"]) // expect: 1
var args2 = parser.parseArgs(["-o", "result.txt", "-n", "5"])
System.print(args2["output"]) // expect: result.txt
System.print(args2["n"]) // expect: 5
var args3 = parser.parseArgs(["--output", "final.txt"])
System.print(args3["output"]) // expect: final.txt
+15
View File
@@ -0,0 +1,15 @@
// retoor <retoor@molodetz.nl>
import "argparse" for ArgumentParser
var parser = ArgumentParser.new()
parser.addArgument("filename")
parser.addArgument("output", {"required": false, "default": "out.txt"})
var args = parser.parseArgs(["input.txt"])
System.print(args["filename"]) // expect: input.txt
System.print(args["output"]) // expect: out.txt
var args2 = parser.parseArgs(["input.txt", "result.txt"])
System.print(args2["filename"]) // expect: input.txt
System.print(args2["output"]) // expect: result.txt
+31
View File
@@ -0,0 +1,31 @@
// retoor <retoor@molodetz.nl>
import "bytes" for Bytes
System.print(Bytes.length("Hello")) // expect: 5
System.print(Bytes.length("")) // expect: 0
System.print(Bytes.toList("AB")) // expect: [65, 66]
System.print(Bytes.toList("")) // expect: []
System.print(Bytes.fromList([65, 66, 67])) // expect: ABC
System.print(Bytes.fromList([])) // expect:
System.print(Bytes.concat("Hello", " World")) // expect: Hello World
System.print(Bytes.concat("", "Test")) // expect: Test
System.print(Bytes.concat("Test", "")) // expect: Test
System.print(Bytes.slice("Hello", 0, 2)) // expect: He
System.print(Bytes.slice("Hello", 2, 5)) // expect: llo
System.print(Bytes.slice("Hello", 0, 100)) // expect: Hello
System.print(Bytes.length(Bytes.slice("Hello", 10, 20))) // expect: 0
var mask = Bytes.fromList([0xFF, 0xFF, 0xFF, 0xFF])
var data = Bytes.fromList([0, 1, 2, 3])
var xored = Bytes.xorMask(data, mask)
System.print(Bytes.toList(xored)) // expect: [255, 254, 253, 252]
var mask2 = Bytes.fromList([0x12, 0x34])
var data2 = Bytes.fromList([0, 0, 0, 0])
var xored2 = Bytes.xorMask(data2, mask2)
System.print(Bytes.toList(xored2)) // expect: [18, 52, 18, 52]
+20
View File
@@ -0,0 +1,20 @@
// retoor <retoor@molodetz.nl>
import "dataset" for Dataset
var ds = Dataset.memory()
var users = ds["users"]
users.insert({"name": "Alice"})
var cols1 = users.columns
System.print(cols1.containsKey("uid")) // expect: true
System.print(cols1.containsKey("name")) // expect: true
System.print(cols1.containsKey("email")) // expect: false
users.insert({"name": "Bob", "email": "bob@example.com"})
var cols2 = users.columns
System.print(cols2.containsKey("email")) // expect: true
ds.close()
+20
View File
@@ -0,0 +1,20 @@
// retoor <retoor@molodetz.nl>
import "dataset" for Dataset
var ds = Dataset.memory()
var users = ds["users"]
var user = users.insert({"name": "Alice", "age": 30})
System.print(user["name"]) // expect: Alice
System.print(user["age"]) // expect: 30
System.print(user["uid"] != null) // expect: true
System.print(user["created_at"] != null) // expect: true
var user2 = users.insert({"name": "Bob", "age": 25, "active": true})
System.print(user2["name"]) // expect: Bob
System.print(user2["active"]) // expect: true
System.print(users.count()) // expect: 2
ds.close()
+25
View File
@@ -0,0 +1,25 @@
// retoor <retoor@molodetz.nl>
import "dataset" for Dataset
var ds = Dataset.memory()
var users = ds["users"]
users.insert({"name": "Alice", "age": 30})
users.insert({"name": "Bob", "age": 25})
users.insert({"name": "Charlie", "age": 35})
var all = users.all()
System.print(all.count) // expect: 3
var found = users.find({"age__gt": 28})
System.print(found.count) // expect: 2
var alice = users.findOne({"name": "Alice"})
System.print(alice["age"]) // expect: 30
var young = users.find({"age__lt": 30})
System.print(young.count) // expect: 1
System.print(young[0]["name"]) // expect: Bob
ds.close()
+19
View File
@@ -0,0 +1,19 @@
// retoor <retoor@molodetz.nl>
import "dataset" for Dataset
var ds = Dataset.memory()
var users = ds["users"]
var user = users.insert({"name": "Alice", "age": 30})
var uid = user["uid"]
users.update({"uid": uid, "age": 31})
var updated = users.findOne({"uid": uid})
System.print(updated["age"]) // expect: 31
System.print(users.count()) // expect: 1
System.print(users.delete(uid)) // expect: true
System.print(users.count()) // expect: 0
ds.close()
+12
View File
@@ -0,0 +1,12 @@
// retoor <retoor@molodetz.nl>
import "html" for Html
var encoded = Html.encodeParams({"name": "John Doe", "age": 30})
System.print(encoded.contains("name=John+Doe")) // expect: true
System.print(encoded.contains("age=30")) // expect: true
System.print(encoded.contains("&")) // expect: true
var decoded = Html.decodeParams("name=John+Doe&age=30")
System.print(decoded["name"]) // expect: John Doe
System.print(decoded["age"]) // expect: 30
+13
View File
@@ -0,0 +1,13 @@
// retoor <retoor@molodetz.nl>
import "html" for Html
System.print(Html.quote("<script>")) // expect: &lt;script&gt;
System.print(Html.quote("a & b")) // expect: a &amp; b
System.print(Html.quote("\"quoted\"")) // expect: &quot;quoted&quot;
System.print(Html.quote("it's")) // expect: it&#39;s
System.print(Html.quote("normal text")) // expect: normal text
System.print(Html.unquote("&lt;script&gt;")) // expect: <script>
System.print(Html.unquote("a &amp; b")) // expect: a & b
System.print(Html.unquote("&quot;quoted&quot;")) // expect: "quoted"
+10
View File
@@ -0,0 +1,10 @@
// retoor <retoor@molodetz.nl>
import "html" for Html
System.print(Html.slugify("Hello World")) // expect: hello-world
System.print(Html.slugify("This is a TEST")) // expect: this-is-a-test
System.print(Html.slugify("foo---bar")) // expect: foo-bar
System.print(Html.slugify(" spaces ")) // expect: spaces
System.print(Html.slugify("test123")) // expect: test123
System.print(Html.slugify("UPPERCASE")) // expect: uppercase
+14
View File
@@ -0,0 +1,14 @@
// retoor <retoor@molodetz.nl>
import "html" for Html
System.print(Html.urldecode("hello+world")) // expect: hello world
var encoded1 = "foo" + "\%3D" + "bar"
System.print(Html.urldecode(encoded1)) // expect: foo=bar
var encoded2 = "a" + "\%26" + "b"
System.print(Html.urldecode(encoded2)) // expect: a&b
System.print(Html.urldecode("test")) // expect: test
System.print(Html.urldecode("")) // expect:
+9
View File
@@ -0,0 +1,9 @@
// retoor <retoor@molodetz.nl>
import "html" for Html
System.print(Html.urlencode("hello world")) // expect: hello+world
System.print(Html.urlencode("foo=bar")) // expect: foo%3Dbar
System.print(Html.urlencode("a&b")) // expect: a%26b
System.print(Html.urlencode("test")) // expect: test
System.print(Html.urlencode("")) // expect:
+16
View File
@@ -0,0 +1,16 @@
// retoor <retoor@molodetz.nl>
import "markdown" for Markdown
var code = Markdown.toHtml("```\ncode here\n```")
System.print(code.contains("<pre><code>")) // expect: true
System.print(code.contains("code here")) // expect: true
System.print(code.contains("</code></pre>")) // expect: true
var quote = Markdown.toHtml("> quoted text")
System.print(quote.contains("<blockquote>")) // expect: true
System.print(quote.contains("quoted text")) // expect: true
System.print(quote.contains("</blockquote>")) // expect: true
var hr = Markdown.toHtml("---")
System.print(hr.contains("<hr>")) // expect: true
+8
View File
@@ -0,0 +1,8 @@
// retoor <retoor@molodetz.nl>
import "markdown" for Markdown
System.print(Markdown.toHtml("**bold**")) // expect: <p><strong>bold</strong></p>
System.print(Markdown.toHtml("*italic*")) // expect: <p><em>italic</em></p>
System.print(Markdown.toHtml("`code`")) // expect: <p><code>code</code></p>
System.print(Markdown.toHtml("~~deleted~~")) // expect: <p><del>deleted</del></p>
+8
View File
@@ -0,0 +1,8 @@
// retoor <retoor@molodetz.nl>
import "markdown" for Markdown
System.print(Markdown.toHtml("# Heading 1")) // expect: <h1>Heading 1</h1>
System.print(Markdown.toHtml("## Heading 2")) // expect: <h2>Heading 2</h2>
System.print(Markdown.toHtml("### Heading 3")) // expect: <h3>Heading 3</h3>
System.print(Markdown.toHtml("###### Heading 6")) // expect: <h6>Heading 6</h6>
+9
View File
@@ -0,0 +1,9 @@
// retoor <retoor@molodetz.nl>
import "markdown" for Markdown
var link = Markdown.toHtml("[Google](https://google.com)")
System.print(link.contains("<a href=\"https://google.com\">Google</a>")) // expect: true
var img = Markdown.toHtml("![Alt text](image.png)")
System.print(img.contains("<img src=\"image.png\" alt=\"Alt text\">")) // expect: true
+14
View File
@@ -0,0 +1,14 @@
// retoor <retoor@molodetz.nl>
import "markdown" for Markdown
var ul = Markdown.toHtml("- item 1\n- item 2")
System.print(ul.contains("<ul>")) // expect: true
System.print(ul.contains("<li>item 1</li>")) // expect: true
System.print(ul.contains("<li>item 2</li>")) // expect: true
System.print(ul.contains("</ul>")) // expect: true
var ol = Markdown.toHtml("1. first\n2. second")
System.print(ol.contains("<ol>")) // expect: true
System.print(ol.contains("<li>first</li>")) // expect: true
System.print(ol.contains("</ol>")) // expect: true
+27
View File
@@ -0,0 +1,27 @@
// retoor <retoor@molodetz.nl>
import "uuid" for Uuid
var u1 = Uuid.v4()
System.print(u1.count) // expect: 36
System.print(u1[8] == "-") // expect: true
System.print(u1[13] == "-") // expect: true
System.print(u1[18] == "-") // expect: true
System.print(u1[23] == "-") // expect: true
System.print(u1[14]) // expect: 4
var variant = u1[19]
System.print(variant == "8" || variant == "9" || variant == "a" || variant == "b") // expect: true
System.print(Uuid.isValid(u1)) // expect: true
System.print(Uuid.isV4(u1)) // expect: true
System.print(Uuid.isValid("550e8400-e29b-41d4-a716-446655440000")) // expect: true
System.print(Uuid.isValid("invalid-uuid")) // expect: false
System.print(Uuid.isValid(123)) // expect: false
System.print(Uuid.isValid("550e8400-e29b-41d4-a716-44665544000X")) // expect: false
var u2 = Uuid.v4()
System.print(u1 != u2) // expect: true
+36
View File
@@ -0,0 +1,36 @@
// retoor <retoor@molodetz.nl>
import "wdantic" for Schema, Field, ValidationResult
var userSchema = Schema.new({
"name": Field.string({"minLength": 1}),
"age": Field.integer({"min": 0, "max": 150}),
"email": Field.email()
})
var validData = {"name": "John", "age": 30, "email": "john@example.com"}
var result = userSchema.validate(validData)
System.print(result.isValid) // expect: true
System.print(result.data["name"]) // expect: John
System.print(result.data["age"]) // expect: 30
var invalidData = {"name": "", "age": -5, "email": "invalid"}
var result2 = userSchema.validate(invalidData)
System.print(result2.isValid) // expect: false
System.print(result2.errors.count > 0) // expect: true
var optionalSchema = Schema.new({
"name": Field.string(),
"nickname": Field.optional(Field.string())
})
var data3 = {"name": "Alice"}
var result3 = optionalSchema.validate(data3)
System.print(result3.isValid) // expect: true
var listSchema = Schema.new({
"tags": Field.list(Field.string())
})
var data4 = {"tags": ["a", "b", "c"]}
var result4 = listSchema.validate(data4)
System.print(result4.isValid) // expect: true
+36
View File
@@ -0,0 +1,36 @@
// retoor <retoor@molodetz.nl>
import "wdantic" for Validator
System.print(Validator.email("test@example.com")) // expect: true
System.print(Validator.email("invalid")) // expect: false
System.print(Validator.email("test@domain")) // expect: false
System.print(Validator.domain("example.com")) // expect: true
System.print(Validator.domain("sub.example.com")) // expect: true
System.print(Validator.domain("-invalid.com")) // expect: false
System.print(Validator.safeStr("Hello World")) // expect: true
System.print(Validator.safeStr("Line1\nLine2")) // expect: false
System.print(Validator.url("https://example.com")) // expect: true
System.print(Validator.url("http://localhost:8080/path")) // expect: true
System.print(Validator.url("ftp://invalid")) // expect: false
System.print(Validator.uuid("550e8400-e29b-41d4-a716-446655440000")) // expect: true
System.print(Validator.uuid("invalid")) // expect: false
System.print(Validator.minLength("hello", 3)) // expect: true
System.print(Validator.minLength("hi", 3)) // expect: false
System.print(Validator.maxLength("hi", 5)) // expect: true
System.print(Validator.maxLength("hello world", 5)) // expect: false
System.print(Validator.range(5, 1, 10)) // expect: true
System.print(Validator.range(15, 1, 10)) // expect: false
System.print(Validator.positive(5)) // expect: true
System.print(Validator.positive(-5)) // expect: false
System.print(Validator.integer(5)) // expect: true
System.print(Validator.integer(5.5)) // expect: false
+19
View File
@@ -0,0 +1,19 @@
// retoor <retoor@molodetz.nl>
import "web" for Response
var r1 = Response.text("Hello")
System.print(r1.body) // expect: Hello
System.print(r1.headers["Content-Type"].contains("text/plain")) // expect: true
var r2 = Response.html("<h1>Hi</h1>")
System.print(r2.body) // expect: <h1>Hi</h1>
System.print(r2.headers["Content-Type"].contains("text/html")) // expect: true
var r3 = Response.json({"name": "test"})
System.print(r3.body.contains("name")) // expect: true
System.print(r3.headers["Content-Type"].contains("application/json")) // expect: true
var r4 = Response.redirect("/login")
System.print(r4.status) // expect: 302
System.print(r4.headers["Location"]) // expect: /login
+21
View File
@@ -0,0 +1,21 @@
// retoor <retoor@molodetz.nl>
import "web" for Router
var router = Router.new()
router.get("/", Fn.new { |r| "home" })
router.get("/users/:id", Fn.new { |r| "user" })
router.post("/api/data", Fn.new { |r| "data" })
var m1 = router.match("GET", "/")
System.print(m1 != null) // expect: true
var m2 = router.match("GET", "/users/123")
System.print(m2 != null) // expect: true
System.print(m2["params"]["id"]) // expect: 123
var m3 = router.match("POST", "/api/data")
System.print(m3 != null) // expect: true
var m4 = router.match("GET", "/notfound")
System.print(m4 == null) // expect: true
+19
View File
@@ -0,0 +1,19 @@
// retoor <retoor@molodetz.nl>
import "web" for SessionStore
var store = SessionStore.new()
var session = store.create()
System.print(session.id != null) // expect: true
System.print(session.id.count) // expect: 36
session["user"] = "alice"
System.print(session["user"]) // expect: alice
store.save(session)
var retrieved = store.get(session.id)
System.print(retrieved != null) // expect: true
System.print(retrieved["user"]) // expect: alice
store.destroy(session.id)
System.print(store.get(session.id) == null) // expect: true
+69
View File
@@ -0,0 +1,69 @@
// retoor <retoor@molodetz.nl>
import "websocket" for WebSocket, WebSocketServer
import "scheduler" for Scheduler
import "timer" for Timer
var port = 19877
var payloadSize = 3 * 1024 * 1024
var result = "pending"
Scheduler.add {
var server = WebSocketServer.bind("127.0.0.1", port)
var ws = server.accept()
if (ws != null) {
var msg = ws.receive()
if (msg != null && msg.isBinary) {
ws.sendBinary(msg.bytes)
}
ws.close()
}
server.close()
}
Timer.sleep(50)
Scheduler.add {
var ws = WebSocket.connect("ws://127.0.0.1:%(port)")
var payload = []
var i = 0
while (i < payloadSize) {
payload.add(i % 256)
i = i + 1
}
ws.sendBinary(payload)
var response = ws.receive()
if (response == null) {
result = "error: no response"
} else if (!response.isBinary) {
result = "error: not binary"
} else if (response.bytes.count != payloadSize) {
result = "error: size mismatch %(response.bytes.count) != %(payloadSize)"
} else {
var mismatch = false
i = 0
while (i < payloadSize) {
if (response.bytes[i] != (i % 256)) {
mismatch = true
break
}
i = i + 1
}
if (mismatch) {
result = "error: data mismatch at %(i)"
} else {
result = "ok"
}
}
ws.close()
}
Timer.sleep(30000)
System.print(result) // expect: ok