-
Notifications
You must be signed in to change notification settings - Fork 7
/
vars.lua
79 lines (56 loc) · 1.97 KB
/
vars.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
--[[
This file demonstrates how `Var`s work in Modes and Prompts.
]]
--- WARN: do not import this in your code! it is not part of the public API.
local Var = require 'libmodal.utils.Var'
--- Check the value of the local var
--- @param var any
--- @param val unknown the value to check is equal to
local function assert_local_eq(var, val)
assert(var:get_local() == val, 'assertion: the global value equals ' .. vim.inspect(val))
end
--- Check the value of the global var
--- @param var any
--- @param val unknown the value to check is equal to
local function assert_global_eq(var, val)
assert(var:get_global() == val, 'assertion: the global value equals ' .. vim.inspect(val))
end
--- Check the value of the scoped var
--- @param var any
--- @param val unknown the value to check is equal to
--- @param scope 'global'|'local'
local function assert_eq(var, val, scope)
assert(var:get() == val, 'assertion: the value equals ' .. vim.inspect(val))
local fn = scope == 'local' and assert_local_eq or assert_global_eq
fn(var, val)
end
--- check the value of all vars
--- @param var any
--- @param val unknown the value to check is equal to
local function assert_all_eq(var, val)
assert_eq(var, val, 'local')
assert_global_eq(var, val)
end
local mode_name = 'Foo'
local var_name = 'Bar'
--- WARN: do not use this function in your code! It is not part of the public API.
local foo = Var.new(mode_name, var_name)
-- 1. baseline
assert_all_eq(foo, nil)
-- 2. without local value, `:get` and `:set` use globals
local global_value = true
foo:set(global_value)
assert_eq(foo, global_value, 'global')
assert_local_eq(foo, nil)
-- 3. set local value
foo:set_local(global_value)
assert_all_eq(foo, global_value)
-- 4. with local value, `:get` and `:set` use locals
local local_value = false
foo:set(local_value)
assert_eq(foo, local_value, 'local')
assert_global_eq(foo, global_value)
-- Finally, unset all so the test can be run again
foo:set_global(nil)
foo:set_local(nil)
assert_all_eq(foo, nil)