Skip to content

Commit 36043c3

Browse files
authored
(enh) Runtime.assertVersion (#15)
* (enh) Runtime.assertVersion * add tests for runtime
1 parent 103f3bb commit 36043c3

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

package.wren

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Package is WrenPackage {
77
version { "0.2.90" }
88
dependencies {
99
return [
10-
Dependency.new("wren-testie", "0.1.1", "https://github.com/joshgoebel/wren-testie.git"),
10+
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git"),
1111
Dependency.new("wren-assert", "HEAD", "https://github.com/RobLoach/wren-assert.git")
1212
]
1313
}

src/module/runtime.wren

+20
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ class Runtime {
1111
static NAME { "wren-console" }
1212
static WREN_VERSION { "0.4.0" }
1313
static VERSION { "0.2.91" }
14+
// allows simple numeric comparison of semantic version strings
15+
// by turning them into large integers
16+
static versionToNumber_(v) {
17+
var segments = v.split(".").map { |x| Num.fromString(x) }.toList
18+
return segments[0] * 1000000 + segments[1] * 10000 + segments[2] * 100
19+
}
20+
// takes a semantic version string, ex "3.0.0" and aborts if the currently running
21+
// version of wren-console is less than the version specified
22+
//
23+
// If we running Wren Console 0.3:
24+
//
25+
// Runtime.assertVersion("1.0") // aborts with error about version mismatch
26+
// Runtime.assertVersion("0.1") // ok
27+
// Runtime.assertVersion("0.3") // ok
28+
static assertVersion(desiredMinimalVersion) {
29+
if (versionToNumber_(Runtime.VERSION) < versionToNumber_(desiredMinimalVersion)) {
30+
Fiber.abort("wren-console version %(desiredMinimalVersion) or higher required.")
31+
}
32+
}
1433
static details {
1534
return {
1635
"name": Runtime.NAME,
1736
"wrenVersion": Runtime.WREN_VERSION,
1837
"version": Runtime.VERSION,
1938
"capabilities": [
2039
Capability.new("essentials"),
40+
Capability.new("json"),
2141
Capability.new("mirror")
2242
]
2343
}

src/module/runtime.wren.inc

+20
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,33 @@ static const char* runtimeModuleSource =
1515
" static NAME { \"wren-console\" }\n"
1616
" static WREN_VERSION { \"0.4.0\" }\n"
1717
" static VERSION { \"0.2.91\" }\n"
18+
" // allows simple numeric comparison of semantic version strings\n"
19+
" // by turning them into large integers\n"
20+
" static versionToNumber_(v) {\n"
21+
" var segments = v.split(\".\").map { |x| Num.fromString(x) }.toList\n"
22+
" return segments[0] * 1000000 + segments[1] * 10000 + segments[2] * 100\n"
23+
" }\n"
24+
" // takes a semantic version string, ex \"3.0.0\" and aborts if the currently running\n"
25+
" // version of wren-console is less than the version specified\n"
26+
" //\n"
27+
" // If we running Wren Console 0.3:\n"
28+
" //\n"
29+
" // Runtime.assertVersion(\"1.0\") // aborts with error about version mismatch\n"
30+
" // Runtime.assertVersion(\"0.1\") // ok\n"
31+
" // Runtime.assertVersion(\"0.3\") // ok\n"
32+
" static assertVersion(desiredMinimalVersion) {\n"
33+
" if (versionToNumber_(Runtime.VERSION) < versionToNumber_(desiredMinimalVersion)) {\n"
34+
" Fiber.abort(\"wren-console version %(desiredMinimalVersion) or higher required.\")\n"
35+
" }\n"
36+
" }\n"
1837
" static details {\n"
1938
" return {\n"
2039
" \"name\": Runtime.NAME,\n"
2140
" \"wrenVersion\": Runtime.WREN_VERSION,\n"
2241
" \"version\": Runtime.VERSION,\n"
2342
" \"capabilities\": [\n"
2443
" Capability.new(\"essentials\"),\n"
44+
" Capability.new(\"json\"),\n"
2545
" Capability.new(\"mirror\")\n"
2646
" ]\n"
2747
" }\n"

test/unit/runtime_test.wren

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// nontest
2+
import "wren-testie/testie" for Testie, Expect
3+
import "../../src/cli/path" for Path
4+
import "runtime" for Runtime
5+
6+
Testie.test("Runtime") { |it, skip|
7+
it.should("have class level constants") {
8+
Expect.value(Runtime.NAME).toEqual("wren-console")
9+
Expect.value(Runtime.WREN_VERSION).toEqual("0.4.0")
10+
Expect.value(Runtime.VERSION).toEqual("0.2.91")
11+
}
12+
it.should("have details") {
13+
var details = Runtime.details
14+
Expect.value(details["name"]).toEqual("wren-console")
15+
Expect.value(details["wrenVersion"]).toEqual("0.4.0")
16+
Expect.value(details["version"]).toEqual("0.2.91")
17+
}
18+
it.should("assertVersion") {
19+
Runtime.assertVersion("0.1.0")
20+
Runtime.assertVersion("0.2.91")
21+
Expect.that { Runtime.assertVersion("12.0.0") }
22+
.toAbortWith("wren-console version 12.0.0 or higher required.")
23+
}
24+
it.should("have capabilities") {
25+
var details = Runtime.details
26+
Expect.value(details.where { |x| x.name == "essentials"}).toBeDefined()
27+
Expect.value(details.where { |x| x.name == "json"}).toBeDefined()
28+
}
29+
}

0 commit comments

Comments
 (0)