Skip to content

Commit

Permalink
initial testing of fsm
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam committed Apr 13, 2023
1 parent 9b38efd commit c005257
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/analyzer-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ serde = "1.0.143"
serde_json = "1.0.83"
thiserror = "1.0.37"
tracing-subscriber = "0.3.16"

[dev-dependencies]
async-io = "1.13.0"
63 changes: 63 additions & 0 deletions crates/analyzer-host/src/fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,66 @@ impl LspProtocolMachine {
)
}
}

#[cfg(test)]
mod tests {
use json_rpc::message::Request;
use json_rpc::message::Notification;
use serde_json::Value;

use crate::{fs::LspEnumerableFileSystem, json_rpc};
use super::*;
#[test]
fn test_is_active() {
let rm = RequestManager::new(async_channel::unbounded::<Message>());
let mut lsp = LspProtocolMachine::new(None, rm.clone(),
Arc::new(Box::new(LspEnumerableFileSystem::new(rm.clone()))));
assert_eq!(lsp.is_active(), true);

lsp.current_state = LspServerState::Stopped;
assert_eq!(lsp.is_active(), false);
}
#[test]
fn test_process_message() {
let rm = RequestManager::new(async_channel::unbounded::<Message>());
let mut lsp = LspProtocolMachine::new(None, rm.clone(),
Arc::new(Box::new(LspEnumerableFileSystem::new(rm.clone()))));
assert_eq!(lsp.current_state, LspServerState::ActiveUninitialized);

let params = serde_json::json!(analyzer_abstractions::lsp_types::InitializeParams{ ..Default::default() });
let message = Message::Request(Request{
id: 0.into(),
method: String::from("initialize"),
params: params,
});
let future = async_io::block_on(lsp.process_message(Arc::new(message)));
assert_eq!(future.is_ok(), true);
assert_eq!(lsp.current_state, LspServerState::Initializing);

let params = serde_json::json!(analyzer_abstractions::lsp_types::InitializedParams{});
let message = Message::Notification(Notification {
method: String::from("initialized"),
params: params,
});
let future = async_io::block_on(lsp.process_message(Arc::new(message)));
assert_eq!(future.is_ok(), true);
assert_eq!(lsp.current_state, LspServerState::ActiveInitialized);

let message = Message::Request(Request{
id: 0.into(),
method: String::from("shutdown"),
params: Value::Null,
});
let future = async_io::block_on(lsp.process_message(Arc::new(message)));
assert_eq!(future.is_ok(), true);
assert_eq!(lsp.current_state, LspServerState::ShuttingDown);

let message = Message::Notification(Notification{
method: String::from("exit"),
params: Value::Null,
});
let future = async_io::block_on(lsp.process_message(Arc::new(message)));
assert_eq!(future.is_ok(), true);
assert_eq!(lsp.current_state, LspServerState::Stopped);
}
}

0 comments on commit c005257

Please sign in to comment.