-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathmodule_test.rs
More file actions
173 lines (149 loc) · 4.64 KB
/
Copy pathmodule_test.rs
File metadata and controls
173 lines (149 loc) · 4.64 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#[cfg(test)]
mod test {
use crate::{DiagnosticCode, ModuleVisibility, VirtualWorkspace};
#[test]
fn test_module_annotation() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
ws.def_files(vec![(
"a.lua",
r#"
local a = {
}
return a
"#,
)]);
ws.def(
r#"
---@module "a"
aaa = {}
"#,
);
let aaa_ty = ws.expr_ty("aaa");
assert!(aaa_ty.is_module_ref());
}
#[test]
fn test_module_no_require() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
// ---@meta no-require 的优先级最高
let file_id = ws.def_file(
"a.lua",
r#"
---@meta no-require
---@public
A = {
}
return A
"#,
);
let module_index = ws.analysis.compilation.get_db().get_module_index();
let module = module_index.get_module(file_id);
assert!(module.is_some());
assert!(module.unwrap().visible.is_hidden());
}
#[test]
fn test_module_default_visibility() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
let file_id = ws.def_file(
"a.lua",
r#"
A = {
}
return A
"#,
);
let module_index = ws.analysis.compilation.get_db().get_module_index();
let module = module_index.get_module(file_id);
assert!(module.is_some());
assert!(module.unwrap().visible == ModuleVisibility::Default);
}
#[test]
fn test_module_internal() {
let mut ws = VirtualWorkspace::new();
{
let file_id = ws.def_file(
"a.lua",
r#"
---@internal
A = {
}
return A
"#,
);
let module_index = ws.analysis.compilation.get_db().get_module_index();
let module = module_index.get_module(file_id);
assert!(module.is_some());
assert!(module.unwrap().visible == ModuleVisibility::Internal);
}
{
// 可见性必须附加在定义语句上
let file_id = ws.def_file(
"b.lua",
r#"
B = {
}
---@internal
return B
"#,
);
let module_index = ws.analysis.compilation.get_db().get_module_index();
let module = module_index.get_module(file_id);
assert!(module.is_some());
assert!(module.unwrap().visible == ModuleVisibility::Default);
}
{
// 当 return 返回匿名结构时, 允许为其附加可见性
let file_id = ws.def_file(
"c.lua",
r#"
---@internal
return {
}
"#,
);
let module_index = ws.analysis.compilation.get_db().get_module_index();
let module = module_index.get_module(file_id);
assert!(module.is_some());
assert!(module.unwrap().visible == ModuleVisibility::Internal);
}
}
#[test]
fn test_module_return_from_truthy_while_block() {
let mut ws = VirtualWorkspace::new_with_init_std_lib();
ws.def(
r#"
while {} do
return 1
end
"#,
);
// `def()` creates `virtual_0.lua`, so the block is requireable as `virtual_0`.
let ty = ws.expr_ty(r#"require("virtual_0")"#);
let integer = ws.ty("integer");
let nil = ws.ty("nil");
assert!(ws.check_type(&integer, &ty));
assert!(!ws.check_type(&nil, &ty));
}
#[test]
fn test_module_multiple_return_paths_preserve_export_metadata_block() {
let mut ws = VirtualWorkspace::new();
ws.def(
r#"
---@class (partial) ModuleExport
---@field private hidden integer
local export = {}
if flag then
return export
end
return export
"#,
);
// `AccessInvisible` only fires if the export still points at `export`.
assert!(!ws.has_no_diagnostic(
DiagnosticCode::AccessInvisible,
r#"
local export = require("virtual_0")
export.hidden = 1
"#,
));
}
}