Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod api;
mod r#macro;
mod mlua;

// Libuv bindings don't work on Windows.
#[cfg(not(any(target_os = "windows", target_env = "msvc")))]
Expand Down
66 changes: 66 additions & 0 deletions tests/src/mlua.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use nvim_oxi::{
api,
mlua::{self, lua},
};

#[nvim_oxi::test]
fn get_variable() {
api::set_var("a_varialbe", "Hello!").unwrap();

let lua = lua();

let a_variable = lua.load("vim.g.a_varialbe").eval::<String>().unwrap();
let not_a_variable =
lua.load("vim.g.not_a_varialbe").eval::<mlua::Value>().unwrap();

assert!(not_a_variable.is_nil());
assert_eq!(a_variable, "Hello!");
}

#[nvim_oxi::test]
fn set_variable() {
lua().load("vim.g.a_varialbe = 'Hello!'").exec().unwrap();

let var = api::get_var::<String>("a_varialbe").unwrap();
assert_eq!(var, "Hello!");
}

#[nvim_oxi::test]
fn nvim_api() {
let hello = "Hello!";

let hello1 = lua()
.load(format!(
"
buf = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_lines(buf, 0, 1, true, {{ '{hello}' }})
return vim.api.nvim_buf_get_lines(buf, 0, 1, true)[1]
"
))
.eval::<String>()
.unwrap();

assert_eq!(hello1, hello);
}

#[nvim_oxi::test]
fn rust_callback() {
let hello: &'static str = "Hello!";
let lua = lua();
let func = lua
.create_function(|_, ()| {
api::set_var("a_varialbe", "Hello!").unwrap();
Ok(())
})
.unwrap();

lua.globals().set("rust_callback", func).unwrap();
let hello1 = lua
.load("rust_callback(); return vim.g.a_varialbe")
.eval::<String>()
.unwrap();
let hello2 = api::get_var::<String>("a_varialbe").unwrap();

assert_eq!(hello1, hello);
assert_eq!(hello2, hello);
}
Loading