#[cfg(test)]
mod tests {
use crate::handlers::test_lib::{ProviderVirtualWorkspace, VirtualLocation, check};
use glua_code_analysis::EmmyrcLuaVersion;
use googletest::prelude::*;
use lsp_types::HoverContents;
fn make_lua51_emmyrc(ws: &ProviderVirtualWorkspace) -> glua_code_analysis::Emmyrc {
let mut emmyrc = ws.get_emmyrc();
emmyrc.runtime.version = EmmyrcLuaVersion::Lua51;
emmyrc
}
#[gtest]
fn test_legacy_module_bare_name_hover_does_not_render_brace_wrapped() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
ws.update_emmyrc(make_lua51_emmyrc(&ws));
ws.def_file(
"includes.lua",
r#"
module("includes", package.seeall)
---Include a file by path.
---@param path string The file path to include
---@return boolean success Whether the include succeeded
function File(path) end
"#,
);
let (content, position) = ProviderVirtualWorkspace::handle_file_content(
r#"
local mod = inclu<??>des
"#,
)?;
let file_id = ws.def_file("consumer_bare.lua", &content);
let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
.ok_or("expected hover result for bare module name")
.or_fail()?;
let HoverContents::Markup(markup) = hover.contents else {
return fail!("expected HoverContents::Markup, got {:?}", hover.contents);
};
assert!(
!markup.value.contains("{ includes }"),
"hover on bare legacy module name must not render as '{{ includes }}', got: {}",
markup.value
);
assert!(
!markup.value.starts_with("{ "),
"hover on bare legacy module name must not start with '{{ ', got: {}",
markup.value
);
Ok(())
}
#[gtest]
fn test_legacy_module_member_hover_shows_rich_signature() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
ws.update_emmyrc(make_lua51_emmyrc(&ws));
ws.def_file(
"includes.lua",
r#"
module("includes", package.seeall)
---Include a file by path.
---@param path string The file path to include
---@return boolean success Whether the include succeeded
function File(path) end
"#,
);
let (content, position) = ProviderVirtualWorkspace::handle_file_content(
r#"
includes.Fi<??>le("sv_init.lua")
"#,
)?;
let file_id = ws.def_file("consumer_hover.lua", &content);
let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
.ok_or("expected hover result for includes.File")
.or_fail()?;
let HoverContents::Markup(markup) = hover.contents else {
return fail!("expected HoverContents::Markup, got {:?}", hover.contents);
};
assert!(
markup.value.contains("File"),
"hover on includes.File must show the function name, got: {}",
markup.value
);
assert!(
markup.value.contains("path"),
"hover on includes.File must show the 'path' parameter, got: {}",
markup.value
);
assert!(
markup.value.contains("Include a file by path"),
"hover on includes.File must include the doc comment, got: {}",
markup.value
);
assert!(
markup.value.contains("```lua"),
"hover on includes.File must include a lua code block, got: {}",
markup.value
);
Ok(())
}
#[gtest]
fn test_legacy_module_method_member_hover_shows_rich_signature() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
ws.update_emmyrc(make_lua51_emmyrc(&ws));
ws.def_file(
"netstream.lua",
r#"
module("netstream", package.seeall)
---Send a net message to a target.
---@param name string The network message name
---@param payload table The data payload to send
---@return boolean success Whether the send succeeded
function Send(name, payload) end
"#,
);
let (content, position) = ProviderVirtualWorkspace::handle_file_content(
r#"
netstream.Se<??>nd("chat", {})
"#,
)?;
let file_id = ws.def_file("consumer_netstream.lua", &content);
let hover = crate::handlers::hover::hover(&ws.analysis, file_id, position, None)
.ok_or("expected hover result for netstream.Send")
.or_fail()?;
let HoverContents::Markup(markup) = hover.contents else {
return fail!("expected HoverContents::Markup, got {:?}", hover.contents);
};
assert!(
markup.value.contains("Send"),
"hover on netstream.Send must show the function name, got: {}",
markup.value
);
assert!(
markup.value.contains("name"),
"hover on netstream.Send must show the 'name' parameter, got: {}",
markup.value
);
assert!(
markup.value.contains("Send a net message"),
"hover on netstream.Send must include the doc comment, got: {}",
markup.value
);
Ok(())
}
#[gtest]
fn test_legacy_module_member_goto_definition_resolves_to_definition_file() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
ws.update_emmyrc(make_lua51_emmyrc(&ws));
ws.def_file(
"includes.lua",
r#"
module("includes", package.seeall)
---Include a file by path.
---@param path string The file path to include
---@return boolean success Whether the include succeeded
function File(path) end
"#,
);
check!(ws.check_definition(
r#"
includes.Fi<??>le("sv_init.lua")
"#,
vec![VirtualLocation {
file: "includes.lua".to_string(),
line: 6,
}]
));
Ok(())
}
#[gtest]
fn test_legacy_module_method_goto_definition_resolves_to_definition_file() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
ws.update_emmyrc(make_lua51_emmyrc(&ws));
ws.def_file(
"netstream.lua",
r#"
module("netstream", package.seeall)
---Send a net message to a target.
---@param name string The network message name
---@param payload table The data payload to send
---@return boolean success Whether the send succeeded
function Send(name, payload) end
"#,
);
check!(ws.check_definition(
r#"
netstream.Se<??>nd("chat", {})
"#,
vec![VirtualLocation {
file: "netstream.lua".to_string(),
line: 7,
}]
));
Ok(())
}
}