diff options
| author | William Boman <william@redwill.se> | 2022-12-20 00:02:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-20 00:02:41 +0100 |
| commit | ca77c845f71b669a3378c976d1f7cf729aee2614 (patch) | |
| tree | 50ac0a18db05fa9fcff3a155c94c52150053ab66 /tests/mason-core | |
| parent | feat(registry): add nil (#774) (diff) | |
| download | mason-ca77c845f71b669a3378c976d1f7cf729aee2614.tar mason-ca77c845f71b669a3378c976d1f7cf729aee2614.tar.gz mason-ca77c845f71b669a3378c976d1f7cf729aee2614.tar.bz2 mason-ca77c845f71b669a3378c976d1f7cf729aee2614.tar.lz mason-ca77c845f71b669a3378c976d1f7cf729aee2614.tar.xz mason-ca77c845f71b669a3378c976d1f7cf729aee2614.tar.zst mason-ca77c845f71b669a3378c976d1f7cf729aee2614.zip | |
feat: add expr module (#775)
This is (soon) to be used when installing package definitions from
https://github.com/mason-org/mason-registry/. See for example:
https://github.com/mason-org/mason-registry/blob/7df69dd2a73efc3a08520552ca64597d1db5f4fb/packages/go-debug-adapter/package.yaml#L16
Diffstat (limited to 'tests/mason-core')
| -rw-r--r-- | tests/mason-core/installer/registry/expr_spec.lua | 57 | ||||
| -rw-r--r-- | tests/mason-core/result_spec.lua | 16 |
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/mason-core/installer/registry/expr_spec.lua b/tests/mason-core/installer/registry/expr_spec.lua new file mode 100644 index 00000000..7e662b79 --- /dev/null +++ b/tests/mason-core/installer/registry/expr_spec.lua @@ -0,0 +1,57 @@ +local match = require "luassert.match" +local expr = require "mason-core.installer.registry.expr" +local Result = require "mason-core.result" + +describe("registry expressions", function() + it("should eval simple expressions", function() + assert.same(Result.success "Hello, world!", expr.eval "Hello, world!") + + assert.same( + Result.success "Hello, John Doe!", + expr.eval("Hello, {{firstname}} {{ lastname }}!", { + firstname = "John", + lastname = "Doe", + }) + ) + end) + + it("should eval expressions with filters", function() + assert.same( + Result.success "Hello, MR. John!", + expr.eval("Hello, {{prefix|to_upper}} {{ name | trim }}!", { + prefix = "Mr.", + name = " John ", + }) + ) + + assert.same( + Result.success "Hello, Sir MR. John!", + expr.eval("Hello, {{prefix|to_upper | format 'Sir %s'}} {{ name | trim }}!", { + prefix = "Mr.", + name = " John ", + }) + ) + end) + + it("should reject invalid values", function() + assert.is_true( + match.matches [[^.*Unable to interpolate value: "non_existent"%.$]]( + expr.eval("Hello, {{non_existent}}", {}):err_or_nil() + ) + ) + end) + + it("should reject invalid filters", function() + assert.is_true( + match.matches [[^.*Invalid filter expression: "whut"%.$]]( + expr.eval("Hello, {{ value | whut }}", { value = "value" }):err_or_nil() + ) + ) + + assert.is_true( + match.matches [[^.*Failed to parse filter: "wh%-!uut"%.$]]( + expr.eval("Hello, {{ value | wh-!uut }}", { value = "value" }):err_or_nil() + ) + ) + end) +end) diff --git a/tests/mason-core/result_spec.lua b/tests/mason-core/result_spec.lua index 1d8f36fb..d7d629f5 100644 --- a/tests/mason-core/result_spec.lua +++ b/tests/mason-core/result_spec.lua @@ -195,4 +195,20 @@ describe("result", function() assert.equals("Error", failure:get_or_nil()) assert.spy(chain).was_not_called() end) + + it("should pcall", function() + assert.same( + Result.success "Great success!", + Result.pcall(function() + return "Great success!" + end) + ) + + assert.same( + Result.failure "Task failed successfully!", + Result.pcall(function() + error("Task failed successfully!", 0) + end) + ) + end) end) |
