Skip to content

Commit

Permalink
Merge pull request #2 from Xenira/basic-tests
Browse files Browse the repository at this point in the history
test: add basic tests
  • Loading branch information
Xenira authored Oct 28, 2023
2 parents 828c8e7 + 3c32ba4 commit a97364e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,23 @@ prost = "0.12.1"
serde = { version = "1.0.190", features = ["derive"] }
tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] }
tonic = "0.10.2"

[dev-dependencies]
pretty_assertions = "1.4.0"

[profile.dev]
opt-level = 0
debug = true
panic = "abort"

[profile.test]
opt-level = 0
debug = true

[profile.release]
opt-level = 3
debug = false
panic = "unwind"
lto = true
codegen-units = 1
strip = true
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use oxi::conversion::{self, FromObject};
use oxi::serde::Deserializer;
use serde::{Deserialize, Serialize};

/// Config is used to store the configuration of the plugin
#[derive(Serialize, Deserialize)]
pub(crate) struct Config {
/// Port of the RPC server to connect to
#[serde(default = "default_rpc_port")]
pub(crate) rpc_port: u16,
}

/// Default RPC port
fn default_rpc_port() -> u16 {
50051
}
Expand Down Expand Up @@ -36,3 +39,15 @@ impl lua::Poppable for Config {
Self::from_object(obj).map_err(lua::Error::pop_error_from_err::<Self, _>)
}
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_config_default() {
let config = Config::default();
assert_eq!(config.rpc_port, 50051);
}
}
66 changes: 66 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! Neovim plugin for daktilo

#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]

use config::Config;
use daktilo_server::client_proto::{daktilo_client::DaktiloClient, ReportCursorMovementRequest};
use nvim_oxi as oxi;
Expand All @@ -8,16 +13,25 @@ use oxi::{
};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};

/// Configuration of the plugin
mod config;

/// BufInfo is used to store information about the buffer
struct BufInfo {
/// Column number. 0 indexed
col: u64,
/// Line number. 0 indexed
line: u64,
/// Name of the buffer
name: Option<String>,
}

/// MessageEvent is used to send messages to the user
/// This is needed because the gRPC client runs on a separate thread
struct MessageEvent {
/// Whether the message is an error message
err: bool,
/// Message to be printed to the user
message: String,
}

Expand All @@ -34,12 +48,14 @@ impl From<&str> for MessageEvent {
}

impl MessageEvent {
/// Constructor for MessageEvent
fn new(err: bool, message: String) -> Self {
Self { err, message }
}
}

impl From<BufInfo> for ReportCursorMovementRequest {
/// Converts BufInfo into ReportCursorMovementRequest to be sent to the server
fn from(buf_info: BufInfo) -> Self {
Self {
column_number: buf_info.col,
Expand All @@ -50,6 +66,7 @@ impl From<BufInfo> for ReportCursorMovementRequest {
}

impl BufInfo {
/// Constructor for BufInfo
fn new(name: Option<String>, cursor: (usize, usize)) -> Self {
Self {
col: cursor.1 as u64,
Expand All @@ -59,6 +76,8 @@ impl BufInfo {
}
}

/// Start function for the neovim plugin
/// This sets up the event listeners and starts the gRPC client
fn start(config: Config) -> oxi::Result<()> {
let (sender, receiver) = unbounded_channel::<BufInfo>();
let (message_sender, mut message_receiver) = unbounded_channel::<MessageEvent>();
Expand Down Expand Up @@ -102,6 +121,7 @@ fn start(config: Config) -> oxi::Result<()> {
Ok(())
}

/// Starts the gRPC client
#[tokio::main]
async fn start_grpc_client(
port: u16,
Expand Down Expand Up @@ -136,10 +156,56 @@ async fn start_grpc_client(
message_handle.send().unwrap();
}

/// Daktilo neovim plugin entrypoint
#[oxi::module]
fn daktilo_nvim() -> oxi::Result<Dictionary> {
Ok(Dictionary::from_iter(vec![(
"start",
Function::from_fn(start),
)]))
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_buff_info_constructor() {
let buf_info = BufInfo::new(Some("test".to_string()), (1, 2));
assert_eq!(buf_info.col, 2);
assert_eq!(buf_info.line, 0);
assert_eq!(buf_info.name, Some("test".to_string()));
}

#[test]
fn test_buff_info_into_report_cursor_movement_request() {
let buf_info = BufInfo::new(Some("test".to_string()), (1, 2));
let request: ReportCursorMovementRequest = buf_info.into();
assert_eq!(request.column_number, 2);
assert_eq!(request.line_number, Some(0));
assert_eq!(request.file_path, Some("test".to_string()));
}

#[test]
fn test_message_event_from_anyhow_error() {
let err = anyhow::anyhow!("test");
let message_event = MessageEvent::from(err);
assert_eq!(message_event.err, true);
assert_eq!(message_event.message, "test".to_string());
}

#[test]
fn test_message_event_from_str() {
let message_event = MessageEvent::from("test");
assert_eq!(message_event.err, false);
assert_eq!(message_event.message, "test".to_string());
}

#[test]
fn test_message_event_new() {
let message_event = MessageEvent::new(true, "test".to_string());
assert_eq!(message_event.err, true);
assert_eq!(message_event.message, "test".to_string());
}
}

0 comments on commit a97364e

Please sign in to comment.